Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ShadCN]: Move Color Stories to Tailwind #14915

Merged
merged 6 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import type { StorybookConfig } from "@storybook/nextjs"
const config: StorybookConfig = {
stories: [
"../src/components/**/*.stories.{ts,tsx}",
"../src/@chakra-ui/stories/*.stories.tsx",
"../src/layouts/stories/*.stories.tsx",
"../src/styles/*.stories.tsx",
],
addons: [
"@storybook/addon-links",
Expand Down
187 changes: 0 additions & 187 deletions src/@chakra-ui/stories/Colors.stories.tsx

This file was deleted.

149 changes: 149 additions & 0 deletions src/styles/colors.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { type ReactNode } from "react"
import capitalize from "lodash/capitalize"
import type { Meta, StoryObj } from "@storybook/react"

import { HStack, Stack, VStack } from "@/components/ui/flex"

import { cn } from "@/lib/utils/cn"

const meta = {
title: "Design System / Colors",
parameters: {
// Do not create snapshots for any stories in the file.
chromatic: { disableSnapshot: true },
},
} satisfies Meta

export default meta

/**
* Get all CSS Variables in the document.
*
* Used to search CSS Variables and retrieve their values.
*
* NOTE: Function created with AI assistance
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆 nice!

*/
const getCSSCustomPropIndex = () => {
const rootStyles = document.styleSheets
const variables = {}

for (const sheet of rootStyles) {
for (const rule of sheet.cssRules) {
// Check for CSSStyleRule type as `selectorText` might not always be available
if (rule instanceof CSSStyleRule && rule.selectorText === ":root") {
for (const style of rule.style) {
if (style.startsWith("--")) {
variables[style] = rule.style.getPropertyValue(style).trim()
}
}
}
}
}
return variables as Record<string, string>
}

const cssVarsEntries = Object.entries(getCSSCustomPropIndex())

const primitiveColorKeys = ["gray", "purple"] as const
export const Primitives: StoryObj = {
render: () => {
return (
<Stack className="gap-16">
{primitiveColorKeys.map((color) => (
<ColorGroupWrapper key={color} color={color}>
<HStack className="justify-between gap-4">
{cssVarsEntries
.filter(([key]) => key.startsWith(`--${color}`))
.map(([tokenKey, value]) => (
<VStack key={`${tokenKey}-${value}`}>
<div>
<div
className="size-20"
style={{ background: `hsla(var(${tokenKey}))` }}
/>
</div>
<Stack>
<span>{value}</span>
<span>{tokenKey}</span>
</Stack>
</VStack>
))}
</HStack>
</ColorGroupWrapper>
))}
</Stack>
)
},
}

const ColorGroupWrapper = ({
color,
children,
}: {
color: (typeof primitiveColorKeys)[number]
children: ReactNode
}) => (
<div
key={color}
className="bg-gradient-to-t from-[#1b1b1b] from-65% to-white to-35% p-8 text-white"
>
{children}
</div>
)

export const SemanticScheme: StoryObj = {
render: () => {
const tokenNames = [
"primary",
"body",
"background",
"disabled",
"success",
"warning",
"error",
] as const

return (
<Stack className="gap-16">
{tokenNames.map((tokenName) => {
const variableObj = cssVarsEntries.filter(([key]) =>
key.startsWith(`--${tokenName}`)
)

return (
<Stack key={tokenName} className="gap-4">
<h2>{capitalize(tokenName)}</h2>
<HStack className="gap-8">
{variableObj.map((variable) => (
<SemanticColorBlock
key={JSON.stringify(variable)}
variable={variable}
/>
))}
</HStack>
</Stack>
)
})}
</Stack>
)
},
}

const SemanticColorBlock = ({
variable: [varName, varValue],
}: {
variable: [string, string]
}) => (
<VStack key={`${varName}-${varValue}`} className="mb-auto">
<div
className={cn(
"size-20",
varName === "--background" || varName === "--body-inverse"
? "border"
: undefined
)}
style={{ background: `hsla(var(${varName}))` }}
/>
<span>{varName}</span>
</VStack>
)