|
| 1 | +import type { ArgTypes } from "@storybook/react" |
| 2 | +import type { ThemingProps } from "@chakra-ui/react" |
| 3 | + |
| 4 | +// Type declarations below pulled directly from `@chakra-ui/storybook-addon` |
| 5 | +// with some alteration |
| 6 | +// (Subject to deprecation and removal upon release of Chakra v3) |
| 7 | + |
| 8 | +/** |
| 9 | + * `keyof` alternative which omits non-string keys |
| 10 | + */ |
| 11 | +type KeyOf<T> = [T] extends [never] |
| 12 | + ? never |
| 13 | + : T extends object |
| 14 | + ? Extract<keyof T, string> |
| 15 | + : never |
| 16 | + |
| 17 | +export type ThemingArgTypeKey = "variant" | "size" |
| 18 | + |
| 19 | +/** |
| 20 | + * Create Storybook controls based on a Chakra UI theme component. |
| 21 | + * |
| 22 | + * @example |
| 23 | + * export default { |
| 24 | + * title: "Components / Forms / Button", |
| 25 | + * argTypes: getThemingArgTypes(theme, "Button"), |
| 26 | + * } |
| 27 | + * |
| 28 | + * @example full example |
| 29 | + * import { Meta, StoryFn } from "@storybook/react" |
| 30 | + * import { getThemingArgTypes } from "@chakra-ui/storybook-addon" |
| 31 | + * import { theme } from "<your-theme>" |
| 32 | + * |
| 33 | + * export default { |
| 34 | + * title: "Components / Forms / Button", |
| 35 | + * argTypes: { |
| 36 | + * ...getThemingArgTypes(theme, "Button"), |
| 37 | + * children: "string" |
| 38 | + * }, |
| 39 | + * args: { children: "Button" }, |
| 40 | + * } as Meta |
| 41 | + * |
| 42 | + * interface StoryProps extends ThemingProps<"Button"> { |
| 43 | + * children?: React.ReactNode |
| 44 | + * } |
| 45 | + * |
| 46 | + * export const Basic: StoryFn<StoryProps> = (props) => <Button {...props} /> |
| 47 | + * |
| 48 | + * @param theme same Chakra UI theme used in .storybook/preview.tsx |
| 49 | + * @param componentName component name to create the ArgTypes for |
| 50 | + */ |
| 51 | +export function getThemingArgTypes< |
| 52 | + Theme extends Record<string, any>, |
| 53 | + ComponentName extends KeyOf<Theme["components"]> |
| 54 | +>(theme: Theme, componentName: ComponentName) { |
| 55 | + const component = theme.components[componentName] |
| 56 | + if (!component) { |
| 57 | + return undefined |
| 58 | + } |
| 59 | + |
| 60 | + const argTypes: ArgTypes< |
| 61 | + Partial<Pick<ThemingProps<ComponentName>, ThemingArgTypeKey>> |
| 62 | + > = {} |
| 63 | + |
| 64 | + const variantOptions = Object.keys(component.variants || {}) |
| 65 | + if (variantOptions.length) { |
| 66 | + argTypes.variant = { |
| 67 | + type: { name: "enum", value: variantOptions }, |
| 68 | + defaultValue: component.defaultProps?.variant, |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + const sizeOptions = Object.keys(component.sizes || {}) |
| 73 | + if (sizeOptions.length) { |
| 74 | + argTypes.size = { |
| 75 | + type: { name: "enum", value: sizeOptions }, |
| 76 | + defaultValue: component.defaultProps?.size, |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return argTypes |
| 81 | +} |
0 commit comments