|
| 1 | +import { useStore } from '@nanostores/react' |
| 2 | +import type { TokenDataTypes } from '@pandacss/types' |
| 3 | +import React, { useEffect, useState } from 'react' |
| 4 | +import { css } from '../../styled-system/css' |
| 5 | +import { Center } from '../../styled-system/jsx' |
| 6 | +import { availableThemes } from '../lib/panda-context' |
| 7 | +import { currentThemeStore } from '../lib/theme-store' |
| 8 | +import { useThemeTokens } from '../lib/use-theme-tokens' |
| 9 | +import { Colors } from './colors' |
| 10 | +import { FontFamily } from './font-family' |
| 11 | +import FontTokens from './font-tokens' |
| 12 | +import { Radii } from './radii' |
| 13 | +import Sizes from './sizes' |
| 14 | + |
| 15 | +type TokenCategory = keyof TokenDataTypes |
| 16 | + |
| 17 | +const hasThemes = availableThemes.length > 0 |
| 18 | + |
| 19 | +const loadingStyles = css({ |
| 20 | + width: '40px', |
| 21 | + height: '40px', |
| 22 | + border: '2px solid #FFF', |
| 23 | + borderColor: 'yellow.400', |
| 24 | + borderBottomColor: 'transparent', |
| 25 | + borderRadius: '50%', |
| 26 | + display: 'inline-block', |
| 27 | + boxSizing: 'border-box', |
| 28 | + animation: 'spin 0.6s linear infinite', |
| 29 | +}) |
| 30 | + |
| 31 | +function Loader() { |
| 32 | + return ( |
| 33 | + <Center height="400px"> |
| 34 | + <span className={loadingStyles} /> |
| 35 | + </Center> |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +function ThemeLoading({ children }: { children: React.ReactNode }) { |
| 40 | + const [isHydrated, setIsHydrated] = useState(false) |
| 41 | + useStore(currentThemeStore) |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + setIsHydrated(true) |
| 45 | + }, []) |
| 46 | + |
| 47 | + if (!isHydrated) { |
| 48 | + return <Loader /> |
| 49 | + } |
| 50 | + |
| 51 | + return <>{children}</> |
| 52 | +} |
| 53 | + |
| 54 | +function createTokenPage<T extends TokenCategory>( |
| 55 | + tokenType: T, |
| 56 | + render: (tokens: ReturnType<typeof useThemeTokens>) => React.ReactNode, |
| 57 | +) { |
| 58 | + return function TokenPage() { |
| 59 | + const tokens = useThemeTokens(tokenType) |
| 60 | + return hasThemes ? <ThemeLoading>{render(tokens)}</ThemeLoading> : <>{render(tokens)}</> |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export const SizesPage = createTokenPage('sizes', (tokens) => <Sizes sizes={tokens} name="sizes" />) |
| 65 | + |
| 66 | +export const SpacingPage = createTokenPage('spacing', (tokens) => <Sizes sizes={tokens} name="spacing" />) |
| 67 | + |
| 68 | +export const FontSizesPage = createTokenPage('fontSizes', (tokens) => ( |
| 69 | + <FontTokens fontTokens={tokens} token="fontSize" /> |
| 70 | +)) |
| 71 | + |
| 72 | +export const FontWeightsPage = createTokenPage('fontWeights', (tokens) => ( |
| 73 | + <FontTokens fontTokens={tokens} token="fontWeight" /> |
| 74 | +)) |
| 75 | + |
| 76 | +export const LetterSpacingsPage = createTokenPage('letterSpacings', (tokens) => ( |
| 77 | + <FontTokens fontTokens={tokens} token="letterSpacing" text="The quick brown fox jumps over the lazy dog." /> |
| 78 | +)) |
| 79 | + |
| 80 | +export const LineHeightsPage = createTokenPage('lineHeights', (tokens) => ( |
| 81 | + <FontTokens |
| 82 | + fontTokens={tokens} |
| 83 | + token="lineHeight" |
| 84 | + largeText |
| 85 | + text="Panda design system lineHeight specifies the vertical distance between two lines of text. You can preview this visually here." |
| 86 | + /> |
| 87 | +)) |
| 88 | + |
| 89 | +export const RadiiPage = createTokenPage('radii', (tokens) => <Radii radii={tokens} />) |
| 90 | + |
| 91 | +export const FontsPage = createTokenPage('fonts', (tokens) => <FontFamily fonts={tokens} />) |
| 92 | + |
| 93 | +export function ColorsPage() { |
| 94 | + const theme = hasThemes ? useStore(currentThemeStore) : undefined |
| 95 | + return hasThemes ? ( |
| 96 | + <ThemeLoading> |
| 97 | + <Colors theme={theme} /> |
| 98 | + </ThemeLoading> |
| 99 | + ) : ( |
| 100 | + <Colors /> |
| 101 | + ) |
| 102 | +} |
0 commit comments