Skip to content

Commit

Permalink
Merge pull request #577 from kendallgassner/Add_ARRAY_To_STYLED
Browse files Browse the repository at this point in the history
allowing more then one Component to be passed in to styled
  • Loading branch information
hipstersmoothie authored Oct 16, 2020
2 parents 88f654e + 649d485 commit 06dcdae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ all the same props as the element prop.

**Parameters:**

- element (`T`) - The html dom element to create a Component for
- element (`T | [T, ...ReactNode[]]`) - The html dom element to create a Component for
- options (`string | WrappedComponent`) - The class an metadata to attach to the Component

**returns:** DocGen & Slotted & WithRef
Expand Down
20 changes: 15 additions & 5 deletions packages/utils/src/utils/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ interface WrappedComponent {
* }
*/
export function styled<T extends keyof JSX.IntrinsicElements>(
element: T,
element: T | [T, ...React.ReactNode[]],
options: string | WrappedComponent
) {
const defaultDescription = `This component accepts all HTML attributes for a "${element}" element.`;
Expand All @@ -67,24 +67,34 @@ export function styled<T extends keyof JSX.IntrinsicElements>(
React.PropsWithoutRef<Props> & React.RefAttributes<HTMLElement>
>;

const elements = Array.isArray(element) ? element : [element];

/** The result "styled" component. */
const Wrapped = React.forwardRef<HTMLElement, Props>((props, ref) => {
const { as, ...rest } = props;
const Component = (as || element) as any;
const { as, ...rest } = props;

const [component, ...innerElements] = elements as any;
const Component = as || component;

return (
<Component
ref={ref}
{...rest}
className={makeClass(className, (props as any).className)}
>
{props.children}
{innerElements.length
? innerElements
.reverse()
.reduce((Children: React.ReactChildren, Parent: any) => {
return <Parent> {Children} </Parent>;
}, props.children)
: props.children}
</Component>
);
}) as DocGen & Slotted & WithRef;

// eslint-disable-next-line no-underscore-dangle
Wrapped._SLOT_ = slot || Symbol(element);
Wrapped._SLOT_ = slot || Symbol(elements[0]);
Wrapped.displayName = name;
// eslint-disable-next-line no-underscore-dangle
Wrapped.__docgenInfo = {
Expand Down

0 comments on commit 06dcdae

Please sign in to comment.