diff --git a/src/styled.tsx b/src/styled.tsx index 07c48c0..b294820 100644 --- a/src/styled.tsx +++ b/src/styled.tsx @@ -5,6 +5,7 @@ import { useInteraction } from "./use-interaction"; import { withStyledChildren } from "./with-styled-children"; import { withStyledProps } from "./with-styled-props"; import { useTailwind } from "./use-tailwind"; +import { withClassNames } from "./with-class-names"; export interface StyledOptions

{ props?: Array; @@ -12,7 +13,7 @@ export interface StyledOptions

{ cssProps?: Array; } -/* +/** * Normal usage */ export function styled( @@ -40,21 +41,29 @@ export function styled( children: componentChildren, ...componentProps }: StyledProps) { - const classes = twClassName ?? className ?? ""; - const isComponent = classes.split(/\s+/).includes("component"); + const { classes, allClasses, isComponent } = withClassNames({ + className, + twClassName, + componentProps, + propsToTransform, + spreadProps, + cssProps, + }); const { hover, focus, active, ...handlers } = useInteraction({ - className, + className: allClasses, isComponent, ...componentProps, }); + const tw = useTailwind({ + hover, + focus, + active, + }); + const { childStyles, ...styledProps } = withStyledProps({ - tw: useTailwind({ - hover, - focus, - active, - }), + tw, classes, styleProp, propsToTransform, diff --git a/src/with-class-names.ts b/src/with-class-names.ts new file mode 100644 index 0000000..59797b6 --- /dev/null +++ b/src/with-class-names.ts @@ -0,0 +1,33 @@ +export interface WithClassNames { + className?: string; + twClassName?: string; + propsToTransform?: string[]; + componentProps: Record; + spreadProps?: string[]; + cssProps?: string[]; +} +export function withClassNames({ + className, + twClassName, + componentProps, + propsToTransform = [], + spreadProps = [], + cssProps = [], +}: WithClassNames) { + const classes = twClassName ?? className ?? ""; + const isComponent = classes.split(/\s+/).includes("component"); + + const allClasses = []; + + for (const prop of [...propsToTransform, ...spreadProps, ...cssProps]) { + if (typeof componentProps[prop] === "string") { + allClasses.push(componentProps[prop]); + } + } + + return { + classes, + allClasses: allClasses.join(" "), + isComponent, + }; +}