-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
295bb32
commit 8283ab3
Showing
12 changed files
with
37 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[ | ||
{ | ||
"filename": "styled/utils/create-style-context.tsx", | ||
"content": "import {\n type ElementType,\n type ForwardRefExoticComponent,\n type PropsWithoutRef,\n type RefAttributes,\n createContext,\n forwardRef,\n useContext,\n} from 'react'\nimport { cx } from 'styled-system/css'\nimport { styled } from 'styled-system/jsx'\n\ntype Props = Record<string, unknown>\ntype Recipe = {\n (props?: Props): Props\n splitVariantProps: (props: Props) => [Props, Props]\n}\ntype Slot<R extends Recipe> = keyof ReturnType<R>\n\nexport const createStyleContext = <R extends Recipe>(recipe: R) => {\n const StyleContext = createContext<Record<Slot<R>, string> | null>(null)\n\n const withRootProvider = <P extends {}>(Component: ElementType) => {\n const StyledComponent = (props: P) => {\n const [variantProps, otherProps] = recipe.splitVariantProps(props)\n const slotStyles = recipe(variantProps) as Record<Slot<R>, string>\n\n return (\n <StyleContext.Provider value={slotStyles}>\n <Component {...otherProps} />\n </StyleContext.Provider>\n )\n }\n return StyledComponent\n }\n\n const withProvider = <T, P extends { className?: string | undefined }>(\n Component: ElementType,\n slot: Slot<R>,\n ): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>> => {\n const StyledComponent = styled(Component)\n const StyledSlotProvider = forwardRef<T, P>((props, ref) => {\n const [variantProps, otherProps] = recipe.splitVariantProps(props)\n const slotStyles = recipe(variantProps) as Record<Slot<R>, string>\n\n return (\n <StyleContext.Provider value={slotStyles}>\n <StyledComponent\n {...otherProps}\n ref={ref}\n className={cx(slotStyles?.[slot], props.className)}\n />\n </StyleContext.Provider>\n )\n })\n // @ts-expect-error\n StyledSlotProvider.displayName = Component.displayName || Component.name\n\n return StyledSlotProvider\n }\n\n const withContext = <T, P extends { className?: string | undefined }>(\n Component: ElementType,\n slot: Slot<R>,\n ): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>> => {\n const StyledComponent = styled(Component)\n const StyledSlotComponent = forwardRef<T, P>((props, ref) => {\n const slotStyles = useContext(StyleContext)\n return (\n <StyledComponent {...props} ref={ref} className={cx(slotStyles?.[slot], props.className)} />\n )\n })\n // @ts-expect-error\n StyledSlotComponent.displayName = Component.displayName || Component.name\n\n return StyledSlotComponent\n }\n\n return {\n withRootProvider,\n withProvider,\n withContext,\n }\n}\n" | ||
"content": "import {\n type ElementType,\n type ForwardRefExoticComponent,\n type PropsWithoutRef,\n type RefAttributes,\n createContext,\n forwardRef,\n useContext,\n} from 'react'\nimport { cx } from 'styled-system/css'\nimport { type StyledComponent, isCssProperty, styled } from 'styled-system/jsx'\n\ntype Props = Record<string, unknown>\ntype Recipe = {\n (props?: Props): Props\n splitVariantProps: (props: Props) => [Props, Props]\n}\ntype Slot<R extends Recipe> = keyof ReturnType<R>\ntype Options = { forwardProps?: string[] }\n\nconst shouldForwardProp = (prop: string, variantKeys: string[], options: Options = {}) =>\n options.forwardProps?.includes(prop) || (!variantKeys.includes(prop) && !isCssProperty(prop))\n\nexport const createStyleContext = <R extends Recipe>(recipe: R) => {\n const StyleContext = createContext<Record<Slot<R>, string> | null>(null)\n\n const withRootProvider = <P extends {}>(Component: ElementType) => {\n const StyledComponent = (props: P) => {\n const [variantProps, otherProps] = recipe.splitVariantProps(props)\n const slotStyles = recipe(variantProps) as Record<Slot<R>, string>\n\n return (\n <StyleContext.Provider value={slotStyles}>\n <Component {...otherProps} />\n </StyleContext.Provider>\n )\n }\n return StyledComponent\n }\n\n const withProvider = <T, P extends { className?: string | undefined }>(\n Component: ElementType,\n slot: Slot<R>,\n options?: Options,\n ): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>> => {\n const StyledComponent = styled(\n Component,\n {},\n {\n shouldForwardProp: (prop, variantKeys) => shouldForwardProp(prop, variantKeys, options),\n },\n ) as StyledComponent<ElementType>\n const StyledSlotProvider = forwardRef<T, P>((props, ref) => {\n const [variantProps, otherProps] = recipe.splitVariantProps(props)\n const slotStyles = recipe(variantProps) as Record<Slot<R>, string>\n\n return (\n <StyleContext.Provider value={slotStyles}>\n <StyledComponent\n {...otherProps}\n ref={ref}\n className={cx(slotStyles?.[slot], props.className)}\n />\n </StyleContext.Provider>\n )\n })\n // @ts-expect-error\n StyledSlotProvider.displayName = Component.displayName || Component.name\n\n return StyledSlotProvider\n }\n\n const withContext = <T, P extends { className?: string | undefined }>(\n Component: ElementType,\n slot: Slot<R>,\n ): ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<T>> => {\n const StyledComponent = styled(Component)\n const StyledSlotComponent = forwardRef<T, P>((props, ref) => {\n const slotStyles = useContext(StyleContext)\n return (\n <StyledComponent {...props} ref={ref} className={cx(slotStyles?.[slot], props.className)} />\n )\n })\n // @ts-expect-error\n StyledSlotComponent.displayName = Component.displayName || Component.name\n\n return StyledSlotComponent\n }\n\n return {\n withRootProvider,\n withProvider,\n withContext,\n }\n}\n" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.