|
| 1 | +import { type ReactNode } from "react" |
| 2 | +import type { Meta, StoryObj } from "@storybook/react/" |
| 3 | + |
| 4 | +import { HStack, Stack, VStack } from "@/components/ui/flex" |
| 5 | + |
| 6 | +const meta = { |
| 7 | + title: "Design System/ShadCN Colors", |
| 8 | + parameters: { |
| 9 | + // Do not create snapshots for any stories in the file. |
| 10 | + chromatic: { disableSnapshot: true }, |
| 11 | + }, |
| 12 | +} satisfies Meta |
| 13 | + |
| 14 | +export default meta |
| 15 | + |
| 16 | +/** |
| 17 | + * Get all CSS Variables in the document. |
| 18 | + * |
| 19 | + * Used to search CSS Variables and retrieve their values. |
| 20 | + * |
| 21 | + * NOTE: Function created with AI assistance |
| 22 | + */ |
| 23 | +const getCSSCustomPropIndex = () => { |
| 24 | + const rootStyles = document.styleSheets |
| 25 | + const variables = {} |
| 26 | + |
| 27 | + for (const sheet of rootStyles) { |
| 28 | + for (const rule of sheet.cssRules) { |
| 29 | + // Check for CSSStyleRule type as `selectorText` might not always be available |
| 30 | + if (rule instanceof CSSStyleRule && rule.selectorText === ":root") { |
| 31 | + for (const style of rule.style) { |
| 32 | + if (style.startsWith("--")) { |
| 33 | + variables[style] = rule.style.getPropertyValue(style).trim() |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return variables as Record<string, string> |
| 40 | +} |
| 41 | +const primitiveColorKeys = ["gray", "purple"] as const |
| 42 | +export const Primitives: StoryObj = { |
| 43 | + render: () => { |
| 44 | + const cssVarsEntries = Object.entries(getCSSCustomPropIndex()) |
| 45 | + |
| 46 | + return ( |
| 47 | + <Stack className="gap-16"> |
| 48 | + {primitiveColorKeys.map((color) => ( |
| 49 | + <ColorGroupWrapper key={color} color={color}> |
| 50 | + <HStack className="justify-between gap-4"> |
| 51 | + {cssVarsEntries |
| 52 | + .filter(([key]) => key.startsWith(`--${color}`)) |
| 53 | + .map(([tokenKey, value]) => ( |
| 54 | + <VStack key={`${tokenKey}-${value}`}> |
| 55 | + <div> |
| 56 | + <div |
| 57 | + className="size-20" |
| 58 | + style={{ background: `hsla(var(${tokenKey}))` }} |
| 59 | + /> |
| 60 | + </div> |
| 61 | + <Stack> |
| 62 | + <span>{value}</span> |
| 63 | + <span>{tokenKey}</span> |
| 64 | + </Stack> |
| 65 | + </VStack> |
| 66 | + ))} |
| 67 | + </HStack> |
| 68 | + </ColorGroupWrapper> |
| 69 | + ))} |
| 70 | + </Stack> |
| 71 | + ) |
| 72 | + }, |
| 73 | +} |
| 74 | + |
| 75 | +const ColorGroupWrapper = ({ |
| 76 | + color, |
| 77 | + children, |
| 78 | +}: { |
| 79 | + color: (typeof primitiveColorKeys)[number] |
| 80 | + children: ReactNode |
| 81 | +}) => ( |
| 82 | + <div |
| 83 | + key={color} |
| 84 | + className="bg-gradient-to-t from-[#1b1b1b] from-65% to-white to-35% p-8 text-white" |
| 85 | + > |
| 86 | + {children} |
| 87 | + </div> |
| 88 | +) |
| 89 | + |
| 90 | +// bg-linear-[180deg,_#1b1b1b_35%,_#fff_35%] |
0 commit comments