|
| 1 | +import { type ReactNode } from "react" |
| 2 | +import capitalize from "lodash/capitalize" |
| 3 | +import type { Meta, StoryObj } from "@storybook/react" |
| 4 | + |
| 5 | +import { HStack, Stack, VStack } from "@/components/ui/flex" |
| 6 | + |
| 7 | +import { cn } from "@/lib/utils/cn" |
| 8 | + |
| 9 | +const meta = { |
| 10 | + title: "Design System / Colors", |
| 11 | + parameters: { |
| 12 | + // Do not create snapshots for any stories in the file. |
| 13 | + chromatic: { disableSnapshot: true }, |
| 14 | + }, |
| 15 | +} satisfies Meta |
| 16 | + |
| 17 | +export default meta |
| 18 | + |
| 19 | +/** |
| 20 | + * Get all CSS Variables in the document. |
| 21 | + * |
| 22 | + * Used to search CSS Variables and retrieve their values. |
| 23 | + * |
| 24 | + * NOTE: Function created with AI assistance |
| 25 | + */ |
| 26 | +const getCSSCustomPropIndex = () => { |
| 27 | + const rootStyles = document.styleSheets |
| 28 | + const variables = {} |
| 29 | + |
| 30 | + for (const sheet of rootStyles) { |
| 31 | + for (const rule of sheet.cssRules) { |
| 32 | + // Check for CSSStyleRule type as `selectorText` might not always be available |
| 33 | + if (rule instanceof CSSStyleRule && rule.selectorText === ":root") { |
| 34 | + for (const style of rule.style) { |
| 35 | + if (style.startsWith("--")) { |
| 36 | + variables[style] = rule.style.getPropertyValue(style).trim() |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + return variables as Record<string, string> |
| 43 | +} |
| 44 | + |
| 45 | +const cssVarsEntries = Object.entries(getCSSCustomPropIndex()) |
| 46 | + |
| 47 | +const primitiveColorKeys = ["gray", "purple"] as const |
| 48 | +export const Primitives: StoryObj = { |
| 49 | + render: () => { |
| 50 | + return ( |
| 51 | + <Stack className="gap-16"> |
| 52 | + {primitiveColorKeys.map((color) => ( |
| 53 | + <ColorGroupWrapper key={color} color={color}> |
| 54 | + <HStack className="justify-between gap-4"> |
| 55 | + {cssVarsEntries |
| 56 | + .filter(([key]) => key.startsWith(`--${color}`)) |
| 57 | + .map(([tokenKey, value]) => ( |
| 58 | + <VStack key={`${tokenKey}-${value}`}> |
| 59 | + <div> |
| 60 | + <div |
| 61 | + className="size-20" |
| 62 | + style={{ background: `hsla(var(${tokenKey}))` }} |
| 63 | + /> |
| 64 | + </div> |
| 65 | + <Stack> |
| 66 | + <span>{value}</span> |
| 67 | + <span>{tokenKey}</span> |
| 68 | + </Stack> |
| 69 | + </VStack> |
| 70 | + ))} |
| 71 | + </HStack> |
| 72 | + </ColorGroupWrapper> |
| 73 | + ))} |
| 74 | + </Stack> |
| 75 | + ) |
| 76 | + }, |
| 77 | +} |
| 78 | + |
| 79 | +const ColorGroupWrapper = ({ |
| 80 | + color, |
| 81 | + children, |
| 82 | +}: { |
| 83 | + color: (typeof primitiveColorKeys)[number] |
| 84 | + children: ReactNode |
| 85 | +}) => ( |
| 86 | + <div |
| 87 | + key={color} |
| 88 | + className="bg-gradient-to-t from-[#1b1b1b] from-65% to-white to-35% p-8 text-white" |
| 89 | + > |
| 90 | + {children} |
| 91 | + </div> |
| 92 | +) |
| 93 | + |
| 94 | +export const SemanticScheme: StoryObj = { |
| 95 | + render: () => { |
| 96 | + const tokenNames = [ |
| 97 | + "primary", |
| 98 | + "body", |
| 99 | + "background", |
| 100 | + "disabled", |
| 101 | + "success", |
| 102 | + "warning", |
| 103 | + "error", |
| 104 | + ] as const |
| 105 | + |
| 106 | + return ( |
| 107 | + <Stack className="gap-16"> |
| 108 | + {tokenNames.map((tokenName) => { |
| 109 | + const variableObj = cssVarsEntries.filter(([key]) => |
| 110 | + key.startsWith(`--${tokenName}`) |
| 111 | + ) |
| 112 | + |
| 113 | + return ( |
| 114 | + <Stack key={tokenName} className="gap-4"> |
| 115 | + <h2>{capitalize(tokenName)}</h2> |
| 116 | + <HStack className="gap-8"> |
| 117 | + {variableObj.map((variable) => ( |
| 118 | + <SemanticColorBlock |
| 119 | + key={JSON.stringify(variable)} |
| 120 | + variable={variable} |
| 121 | + /> |
| 122 | + ))} |
| 123 | + </HStack> |
| 124 | + </Stack> |
| 125 | + ) |
| 126 | + })} |
| 127 | + </Stack> |
| 128 | + ) |
| 129 | + }, |
| 130 | +} |
| 131 | + |
| 132 | +const SemanticColorBlock = ({ |
| 133 | + variable: [varName, varValue], |
| 134 | +}: { |
| 135 | + variable: [string, string] |
| 136 | +}) => ( |
| 137 | + <VStack key={`${varName}-${varValue}`} className="mb-auto"> |
| 138 | + <div |
| 139 | + className={cn( |
| 140 | + "size-20", |
| 141 | + varName === "--background" || varName === "--body-inverse" |
| 142 | + ? "border" |
| 143 | + : undefined |
| 144 | + )} |
| 145 | + style={{ background: `hsla(var(${varName}))` }} |
| 146 | + /> |
| 147 | + <span>{varName}</span> |
| 148 | + </VStack> |
| 149 | +) |
0 commit comments