|
| 1 | +import { |
| 2 | + type ElementType, |
| 3 | + type ForwardRefExoticComponent, |
| 4 | + type PropsWithoutRef, |
| 5 | + type RefAttributes, |
| 6 | + createContext, |
| 7 | + forwardRef, |
| 8 | + useContext, |
| 9 | +} from "react"; |
| 10 | +import { cx } from "styled-system/css"; |
| 11 | +import { type StyledComponent, isCssProperty, styled } from "styled-system/jsx"; |
| 12 | + |
| 13 | +type Props = Record<string, unknown>; |
| 14 | +type Recipe = { |
| 15 | + (props?: Props): Props; |
| 16 | + splitVariantProps: (props: Props) => [Props, Props]; |
| 17 | +}; |
| 18 | +type Slot<R extends Recipe> = keyof ReturnType<R>; |
| 19 | +type Options = { forwardProps?: string[] }; |
| 20 | + |
| 21 | +const shouldForwardProp = ( |
| 22 | + prop: string, |
| 23 | + variantKeys: string[], |
| 24 | + options: Options = {}, |
| 25 | +) => |
| 26 | + options.forwardProps?.includes(prop) || |
| 27 | + (!variantKeys.includes(prop) && !isCssProperty(prop)); |
| 28 | + |
| 29 | +export const createStyleContext = <R extends Recipe>(recipe: R) => { |
| 30 | + const StyleContext = createContext<Record<Slot<R>, string> | null>(null); |
| 31 | + |
| 32 | + const withRootProvider = <P extends {}>(Component: ElementType) => { |
| 33 | + const StyledComponent = (props: P) => { |
| 34 | + const [variantProps, otherProps] = recipe.splitVariantProps(props); |
| 35 | + const slotStyles = recipe(variantProps) as Record<Slot<R>, string>; |
| 36 | + |
| 37 | + return ( |
| 38 | + <StyleContext.Provider value={slotStyles}> |
| 39 | + <Component {...otherProps} /> |
| 40 | + </StyleContext.Provider> |
| 41 | + ); |
| 42 | + }; |
| 43 | + return StyledComponent; |
| 44 | + }; |
| 45 | + |
| 46 | + const withProvider = <T, P extends { className?: string | undefined }>( |
| 47 | + Component: ElementType, |
| 48 | + slot: Slot<R>, |
| 49 | + options?: Options, |
| 50 | + ): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>> => { |
| 51 | + const StyledComponent = styled( |
| 52 | + Component, |
| 53 | + {}, |
| 54 | + { |
| 55 | + shouldForwardProp: (prop, variantKeys) => |
| 56 | + shouldForwardProp(prop, variantKeys, options), |
| 57 | + }, |
| 58 | + ) as StyledComponent<ElementType>; |
| 59 | + const StyledSlotProvider = forwardRef<T, P>((props, ref) => { |
| 60 | + const [variantProps, otherProps] = recipe.splitVariantProps(props); |
| 61 | + const slotStyles = recipe(variantProps) as Record<Slot<R>, string>; |
| 62 | + |
| 63 | + return ( |
| 64 | + <StyleContext.Provider value={slotStyles}> |
| 65 | + <StyledComponent |
| 66 | + {...otherProps} |
| 67 | + ref={ref} |
| 68 | + className={cx(slotStyles?.[slot], props.className)} |
| 69 | + /> |
| 70 | + </StyleContext.Provider> |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + StyledSlotProvider.displayName = Component.displayName || Component.name; |
| 75 | + |
| 76 | + return StyledSlotProvider; |
| 77 | + }; |
| 78 | + |
| 79 | + const withContext = <T, P extends { className?: string | undefined }>( |
| 80 | + Component: ElementType, |
| 81 | + slot: Slot<R>, |
| 82 | + ): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>> => { |
| 83 | + const StyledComponent = styled(Component); |
| 84 | + const StyledSlotComponent = forwardRef<T, P>((props, ref) => { |
| 85 | + const slotStyles = useContext(StyleContext); |
| 86 | + return ( |
| 87 | + <StyledComponent |
| 88 | + {...props} |
| 89 | + ref={ref} |
| 90 | + className={cx(slotStyles?.[slot], props.className)} |
| 91 | + /> |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + StyledSlotComponent.displayName = Component.displayName || Component.name; |
| 96 | + |
| 97 | + return StyledSlotComponent; |
| 98 | + }; |
| 99 | + |
| 100 | + return { |
| 101 | + withRootProvider, |
| 102 | + withProvider, |
| 103 | + withContext, |
| 104 | + }; |
| 105 | +}; |
0 commit comments