An open source design system that's fully customizable and agent ready
TypeScript
fix(docsite): add column widths to token tables (#2053)
Cindy Zhangc0a645410a0f
diff --git a/apps/docsite/src/components/docs/TokensDocView.tsx b/apps/docsite/src/components/docs/TokensDocView.tsxindex c82fd7e937..f2dae60db1 100644--- a/apps/docsite/src/components/docs/TokensDocView.tsx+++ b/apps/docsite/src/components/docs/TokensDocView.tsx@@ -6,6 +6,7 @@ import {useXDSTheme} from '@xds/core/theme';import {ColorTokenTable,SpacingTokenTable,+ SizeTokenTable,RadiusTokenTable,BorderTokenTable,ElevationTokenTable,@@ -35,7 +36,7 @@ const TOPIC_SECTION_OVERRIDES: Record<tokens: {'Color Tokens': ColorTokenTable,'Spacing Tokens': SpacingTokenTable,- 'Size Tokens': SpacingTokenTable,+ 'Size Tokens': SizeTokenTable,'Border Tokens': BorderTokenTable,'Radius Tokens': RadiusTokenTable,'Shadow Tokens': ElevationTokenTable,@@ -83,10 +84,11 @@ function TokenSection({Table: TableComponent;theme: TokenTableProps['theme'];}) {+ const prose = section.content.filter(block => block.type !== 'table');return (<XDSVStack gap={4}><XDSText type="display-3">{section.title}</XDSText>- {section.content.map((block, i) => (+ {prose.map((block, i) => (<ContentBlockRenderer key={i} block={block} />))}<Table theme={theme} />diff --git a/apps/docsite/src/components/tokens/ColorTokenTable.tsx b/apps/docsite/src/components/tokens/ColorTokenTable.tsxindex d65a861056..cbe284f9aa 100644--- a/apps/docsite/src/components/tokens/ColorTokenTable.tsx+++ b/apps/docsite/src/components/tokens/ColorTokenTable.tsx@@ -3,45 +3,38 @@import * as stylex from '@stylexjs/stylex';import {XDSHStack} from '@xds/core/Layout';import {XDSText} from '@xds/core/Text';-import {XDSTable} from '@xds/core/Table';+import {XDSTable, pixel} from '@xds/core/Table';+import {useMediaQuery} from '@xds/core/hooks';import type {TokenTableProps} from './types';-import {resolveTokenForMode, hasDualMode, getTokensByPrefix} from './helpers';+import {+ useResolveTokenForMode,+ hasDualMode,+ getTokensByPrefix,+} from './helpers';const styles = stylex.create({- swatch: {- width: 28,- height: 28,- borderRadius: 'var(--radius-element)',- flexShrink: 0,- border: '1px solid var(--color-border)',- },- contextLight: {+ surface: {width: 28,height: 28,borderRadius: 'var(--radius-element)',- backgroundColor: '#FFFFFF',display: 'flex',alignItems: 'center',justifyContent: 'center',flexShrink: 0,border: '1px solid var(--color-border)',},- contextDark: {+ swatchInner: {+ width: 20,+ height: 20,+ borderRadius: 'var(--radius-inner)',+ },+ swatch: {width: 28,height: 28,borderRadius: 'var(--radius-element)',- backgroundColor: '#1C1C1E',- display: 'flex',- alignItems: 'center',- justifyContent: 'center',flexShrink: 0,border: '1px solid var(--color-border)',},- swatchInner: {- width: 20,- height: 20,- borderRadius: 'var(--radius-inner)',- },});function ContextSwatch({@@ -53,9 +46,8 @@ function ContextSwatch({}) {return (<div- {...stylex.props(- surface === 'light' ? styles.contextLight : styles.contextDark,- )}>+ style={{backgroundColor: surface === 'light' ? '#FFFFFF' : '#1C1C1E'}}+ {...stylex.props(styles.surface)}><div{...stylex.props(styles.swatchInner)}style={{backgroundColor: value}}@@ -73,11 +65,13 @@ function Swatch({value}: {value: string}) {export function ColorTokenTable({theme}: TokenTableProps) {const tokens = getTokensByPrefix(theme, '--color-');const isDual = hasDualMode(theme);+ const resolveForMode = useResolveTokenForMode();+ const isMobile = useMediaQuery('(max-width: 768px)');const data = tokens.map(name => ({tokenName: name,- light: resolveTokenForMode(theme, name, 'light'),- dark: resolveTokenForMode(theme, name, 'dark'),+ light: resolveForMode(name, 'light'),+ dark: resolveForMode(name, 'dark'),}));if (isDual) {@@ -85,30 +79,27 @@ export function ColorTokenTable({theme}: TokenTableProps) {<XDSTabledata={data as Record<string, unknown>[]}columns={[- {key: 'tokenName', header: 'Token'},- {- key: 'light',- header: 'Light',- renderCell: (item: Record<string, unknown>) => (- <XDSHStack gap={2} vAlign="center">- <ContextSwatch value={item.light as string} surface="light" />- <XDSText type="code" color="secondary">- {item.light as string}- </XDSText>- </XDSHStack>- ),- },+ {key: 'tokenName', header: 'Token', width: pixel(260)},{- key: 'dark',- header: 'Dark',- renderCell: (item: Record<string, unknown>) => (- <XDSHStack gap={2} vAlign="center">- <ContextSwatch value={item.dark as string} surface="dark" />- <XDSText type="code" color="secondary">- {item.dark as string}- </XDSText>- </XDSHStack>- ),+ key: 'value',+ header: 'Value',+ renderCell: (item: Record<string, unknown>) => {+ const light = item.light as string;+ const dark = item.dark as string;+ const isSame = light === dark;++ return (+ <XDSHStack gap={2} vAlign="center">+ <ContextSwatch value={light} surface="light" />+ <ContextSwatch value={dark} surface="dark" />+ {!isMobile && (+ <XDSText type="code" color="secondary">+ {isSame ? light : `${light} / ${dark}`}+ </XDSText>+ )}+ </XDSHStack>+ );+ },},]}density="spacious"@@ -122,16 +113,18 @@ export function ColorTokenTable({theme}: TokenTableProps) {<XDSTabledata={data as Record<string, unknown>[]}columns={[- {key: 'tokenName', header: 'Token'},+ {key: 'tokenName', header: 'Token', width: pixel(260)},{key: 'light',header: 'Value',renderCell: (item: Record<string, unknown>) => (<XDSHStack gap={2} vAlign="center"><Swatch value={item.light as string} />- <XDSText type="code" color="secondary">- {item.light as string}- </XDSText>+ {!isMobile && (+ <XDSText type="code" color="secondary">+ {item.light as string}+ </XDSText>+ )}</XDSHStack>),},diff --git a/apps/docsite/src/components/tokens/ElevationTokenTable.tsx b/apps/docsite/src/components/tokens/ElevationTokenTable.tsxindex c33bd9d9b6..5b26540cec 100644--- a/apps/docsite/src/components/tokens/ElevationTokenTable.tsx+++ b/apps/docsite/src/components/tokens/ElevationTokenTable.tsx@@ -1,16 +1,14 @@'use client';import * as stylex from '@stylexjs/stylex';-import {XDSHStack} from '@xds/core/Layout';-import {XDSText} from '@xds/core/Text';-import {XDSTable} from '@xds/core/Table';+import {XDSTable, pixel} from '@xds/core/Table';import type {TokenTableProps} from './types';import {resolveToken, getTokensByPrefix} from './helpers';const styles = stylex.create({shadowBox: {- width: 32,- height: 24,+ width: 48,+ height: 32,borderRadius: 'var(--radius-element)',backgroundColor: 'var(--color-background-surface)',flexShrink: 0,@@ -29,20 +27,16 @@ export function ElevationTokenTable({theme}: TokenTableProps) {<XDSTabledata={data as Record<string, unknown>[]}columns={[- {key: 'tokenName', header: 'Token'},+ {key: 'tokenName', header: 'Token', width: pixel(220)},{- key: 'value',- header: 'Value',+ key: 'preview',+ header: 'Preview',+ width: pixel(80),renderCell: (item: Record<string, unknown>) => (- <XDSHStack gap={2} vAlign="center">- <div- {...stylex.props(styles.shadowBox)}- style={{boxShadow: item.value as string}}- />- <XDSText type="code" color="secondary">- {item.value as string}- </XDSText>- </XDSHStack>+ <div+ {...stylex.props(styles.shadowBox)}+ style={{boxShadow: item.value as string}}+ />),},]}diff --git a/apps/docsite/src/components/tokens/FontTokenTables.tsx b/apps/docsite/src/components/tokens/FontTokenTables.tsxindex 05766df050..5b0bbd50f8 100644--- a/apps/docsite/src/components/tokens/FontTokenTables.tsx+++ b/apps/docsite/src/components/tokens/FontTokenTables.tsx@@ -3,7 +3,7 @@import * as stylex from '@stylexjs/stylex';import {XDSHStack} from '@xds/core/Layout';import {XDSText} from '@xds/core/Text';-import {XDSTable} from '@xds/core/Table';+import {XDSTable, pixel, proportional} from '@xds/core/Table';import type {TokenTableProps} from './types';import {resolveToken, getTokensByPrefix} from './helpers';@@ -39,10 +39,11 @@ export function FontFamilyTokenTable({theme}: TokenTableProps) {<XDSTabledata={data as Record<string, unknown>[]}columns={[- {key: 'tokenName', header: 'Token'},+ {key: 'tokenName', header: 'Token', width: pixel(200)},{key: 'value',header: 'Value',+ width: proportional(1, {minWidth: 300}),renderCell: (item: Record<string, unknown>) => (<XDSHStack gap={3} vAlign="center"><XDSText type="code" color="secondary">@@ -79,10 +80,11 @@ export function FontWeightTokenTable({theme}: TokenTableProps) {<XDSTabledata={data as Record<string, unknown>[]}columns={[- {key: 'tokenName', header: 'Token'},+ {key: 'tokenName', header: 'Token', width: pixel(200)},{key: 'value',header: 'Value',+ width: proportional(1, {minWidth: 300}),renderCell: (item: Record<string, unknown>) => (<XDSHStack gap={3} vAlign="center"><XDSText type="code" color="secondary">@@ -115,10 +117,11 @@ export function FontSizeTokenTable({theme}: TokenTableProps) {<XDSTabledata={data as Record<string, unknown>[]}columns={[- {key: 'tokenName', header: 'Token'},+ {key: 'tokenName', header: 'Token', width: pixel(200)},{key: 'value',header: 'Value',+ width: proportional(1, {minWidth: 300}),renderCell: (item: Record<string, unknown>) => (<XDSHStack gap={3} vAlign="center"><XDSText type="code" color="secondary">diff --git a/apps/docsite/src/components/tokens/MotionTokenTable.tsx b/apps/docsite/src/components/tokens/MotionTokenTable.tsxindex e66ccaf34b..2df56070e1 100644--- a/apps/docsite/src/components/tokens/MotionTokenTable.tsx+++ b/apps/docsite/src/components/tokens/MotionTokenTable.tsx@@ -4,7 +4,7 @@ import {useState, useEffect, useRef} from 'react';import * as stylex from '@stylexjs/stylex';import {XDSVStack, XDSHStack} from '@xds/core/Layout';import {XDSText, XDSHeading} from '@xds/core/Text';-import {XDSTable} from '@xds/core/Table';+import {XDSTable, pixel} from '@xds/core/Table';import type {TokenTableProps} from './types';import {resolveToken, getTokensByPrefix} from './helpers';@@ -173,7 +173,7 @@ export function DurationTokenTable({theme}: TokenTableProps) {<XDSTabledata={data as Record<string, unknown>[]}columns={[- {key: 'tokenName', header: 'Token'},+ {key: 'tokenName', header: 'Token', width: pixel(200)},{key: 'value',header: 'Value',@@ -205,7 +205,7 @@ export function EasingTokenTable({theme}: TokenTableProps) {<XDSTabledata={data as Record<string, unknown>[]}columns={[- {key: 'tokenName', header: 'Token'},+ {key: 'tokenName', header: 'Token', width: pixel(200)},{key: 'value',header: 'Value',diff --git a/apps/docsite/src/components/tokens/ShapeTokenTable.tsx b/apps/docsite/src/components/tokens/ShapeTokenTable.tsxindex 2ec6ee2beb..e206e5d84a 100644--- a/apps/docsite/src/components/tokens/ShapeTokenTable.tsx+++ b/apps/docsite/src/components/tokens/ShapeTokenTable.tsx@@ -3,7 +3,7 @@import * as stylex from '@stylexjs/stylex';import {XDSVStack, XDSHStack} from '@xds/core/Layout';import {XDSText, XDSHeading} from '@xds/core/Text';-import {XDSTable} from '@xds/core/Table';+import {XDSTable, pixel} from '@xds/core/Table';import type {TokenTableProps} from './types';import {resolveToken, getTokensByPrefix} from './helpers';@@ -35,7 +35,7 @@ export function RadiusTokenTable({theme}: TokenTableProps) {<XDSTabledata={data as Record<string, unknown>[]}columns={[- {key: 'tokenName', header: 'Token'},+ {key: 'tokenName', header: 'Token', width: pixel(200)},{key: 'value',header: 'Value',@@ -72,7 +72,7 @@ export function BorderTokenTable({theme}: TokenTableProps) {<XDSTabledata={data as Record<string, unknown>[]}columns={[- {key: 'tokenName', header: 'Token'},+ {key: 'tokenName', header: 'Token', width: pixel(200)},{key: 'value',header: 'Value',diff --git a/apps/docsite/src/components/tokens/SizeTokenTable.tsx b/apps/docsite/src/components/tokens/SizeTokenTable.tsxnew file mode 100644index 0000000000..1d30902481--- /dev/null+++ b/apps/docsite/src/components/tokens/SizeTokenTable.tsx@@ -0,0 +1,55 @@+'use client';++import * as stylex from '@stylexjs/stylex';+import {XDSHStack} from '@xds/core/Layout';+import {XDSText} from '@xds/core/Text';+import {XDSTable, pixel} from '@xds/core/Table';+import type {TokenTableProps} from './types';+import {resolveToken, getTokensByPrefix} from './helpers';++const styles = stylex.create({+ box: {+ borderRadius: 'var(--radius-element)',+ backgroundColor: 'var(--color-accent)',+ opacity: 0.6,+ flexShrink: 0,+ },+});++export function SizeTokenTable({theme}: TokenTableProps) {+ const tokens = getTokensByPrefix(theme, '--size-');+ const data = tokens.map(name => ({+ tokenName: name,+ value: resolveToken(theme, name),+ }));++ return (+ <XDSTable+ data={data as Record<string, unknown>[]}+ columns={[+ {key: 'tokenName', header: 'Token', width: pixel(200)},+ {+ key: 'value',+ header: 'Value',+ renderCell: (item: Record<string, unknown>) => (+ <XDSHStack gap={2} vAlign="center">+ <div+ {...stylex.props(styles.box)}+ style={{+ width: item.value as string,+ height: item.value as string,+ }}+ />+ <XDSText type="code" color="secondary">+ {item.value as string}+ </XDSText>+ </XDSHStack>+ ),+ },+ ]}+ density="spacious"+ dividers="rows"+ hasHover+ />+ );+}diff --git a/apps/docsite/src/components/tokens/SpacingTokenTable.tsx b/apps/docsite/src/components/tokens/SpacingTokenTable.tsxindex 290da9ffd5..dc884839ef 100644--- a/apps/docsite/src/components/tokens/SpacingTokenTable.tsx+++ b/apps/docsite/src/components/tokens/SpacingTokenTable.tsx@@ -3,7 +3,7 @@import * as stylex from '@stylexjs/stylex';import {XDSHStack} from '@xds/core/Layout';import {XDSText} from '@xds/core/Text';-import {XDSTable} from '@xds/core/Table';+import {XDSTable, pixel} from '@xds/core/Table';import type {TokenTableProps} from './types';import {resolveToken, getTokensByPrefix} from './helpers';@@ -30,7 +30,7 @@ export function SpacingTokenTable({theme}: TokenTableProps) {<XDSTabledata={data as Record<string, unknown>[]}columns={[- {key: 'tokenName', header: 'Token'},+ {key: 'tokenName', header: 'Token', width: pixel(200)},{key: 'value',header: 'Value',diff --git a/apps/docsite/src/components/tokens/TypographyTokenTable.tsx b/apps/docsite/src/components/tokens/TypographyTokenTable.tsxindex 2f624186a1..8a4f2dcb3d 100644--- a/apps/docsite/src/components/tokens/TypographyTokenTable.tsx+++ b/apps/docsite/src/components/tokens/TypographyTokenTable.tsx@@ -3,7 +3,7 @@import * as stylex from '@stylexjs/stylex';import {XDSVStack} from '@xds/core/Layout';import {XDSText} from '@xds/core/Text';-import {XDSTable, proportional} from '@xds/core/Table';+import {XDSTable, proportional, pixel} from '@xds/core/Table';import type {XDSTextType} from '@xds/core';import type {XDSHeadingLevel} from '@xds/core/Text';import type {TokenTableProps} from './types';@@ -121,7 +121,7 @@ export function TypographyTokenTable({theme}: TokenTableProps) {{key: 'label',header: 'Sample',- width: proportional(2),+ width: pixel(200),renderCell: (item: Record<string, unknown>) => (<span{...stylex.props(styles.sample)}@@ -138,6 +138,7 @@ export function TypographyTokenTable({theme}: TokenTableProps) {{key: 'tokens',header: 'Tokens',+ width: proportional(1, {minWidth: 280}),renderCell: (item: Record<string, unknown>) => (<XDSVStack gap={1}><XDSText type="code" color="secondary">diff --git a/apps/docsite/src/components/tokens/helpers.ts b/apps/docsite/src/components/tokens/helpers.tsindex 5dbb817a17..5e7300d0bb 100644--- a/apps/docsite/src/components/tokens/helpers.ts+++ b/apps/docsite/src/components/tokens/helpers.ts@@ -1,5 +1,11 @@+import {useContext, useMemo} from 'react';import type {UseXDSThemeReturn} from '@xds/core/theme';-import {borderDefaults} from '@xds/core/theme';+import {+ borderDefaults,+ XDSThemeContext,+ xdsTokenDefaults,+} from '@xds/core/theme';+import type {XDSTokenValue} from '@xds/core/theme';const extraDefaults: Record<string, string> = {...borderDefaults,@@ -9,12 +15,72 @@ export function resolveToken(theme: UseXDSThemeReturn, name: string): string {return theme.token(name) || extraDefaults[name] || '';}-export function resolveTokenForMode(- theme: UseXDSThemeReturn,+function resolveValue(value: XDSTokenValue, mode: 'light' | 'dark'): string {+ if (Array.isArray(value)) {+ return mode === 'dark' ? value[1] : value[0];+ }+ const prefix = 'light-dark(';+ if (value.startsWith(prefix) && value.endsWith(')')) {+ const inner = value.slice(prefix.length, -1);+ const commaIdx = inner.indexOf(',');+ if (commaIdx !== -1) {+ const light = inner.slice(0, commaIdx).trim();+ const dark = inner.slice(commaIdx + 1).trim();+ return mode === 'dark' ? dark : light;+ }+ }+ return value;+}++function buildTokenMap(+ rawTheme: {+ tokens: Record<string, string>;+ __inputTokens?: Partial<Record<string, XDSTokenValue>>;+ } | null,+ mode: 'light' | 'dark',+): Record<string, string> {+ const resolved: Record<string, string> = {};++ for (const [key, value] of Object.entries(xdsTokenDefaults)) {+ resolved[key] = resolveValue(value, mode);+ }+ for (const [key, value] of Object.entries(extraDefaults)) {+ resolved[key] = resolveValue(value, mode);+ }++ if (rawTheme?.__inputTokens) {+ for (const [key, value] of Object.entries(rawTheme.__inputTokens)) {+ if (value !== undefined) {+ resolved[key] = resolveValue(value, mode);+ }+ }+ } else if (rawTheme) {+ for (const [key, value] of Object.entries(rawTheme.tokens)) {+ resolved[key] = resolveValue(value, mode);+ }+ }++ return resolved;+}++export function useResolveTokenForMode(): (name: string,- _mode: 'light' | 'dark',-): string {- return theme.token(name) || extraDefaults[name] || '';+ mode: 'light' | 'dark',+) => string {+ const ctx = useContext(XDSThemeContext);+ const rawTheme = ctx?.theme ?? null;++ const lightTokens = useMemo(+ () => buildTokenMap(rawTheme, 'light'),+ [rawTheme],+ );+ const darkTokens = useMemo(() => buildTokenMap(rawTheme, 'dark'), [rawTheme]);++ return (name: string, mode: 'light' | 'dark'): string => {+ return mode === 'dark'+ ? (darkTokens[name] ?? '')+ : (lightTokens[name] ?? '');+ };}export function hasDualMode(_theme: UseXDSThemeReturn): boolean {@@ -27,6 +93,7 @@ export function getTokensByPrefix(): string[] {const allKeys = new Set([...Object.keys(theme.tokens),+ ...Object.keys(xdsTokenDefaults),...Object.keys(extraDefaults),]);return [...allKeys].filter(k => k.startsWith(prefix));diff --git a/apps/docsite/src/components/tokens/index.ts b/apps/docsite/src/components/tokens/index.tsindex dc393a68bf..5d93a69f98 100644--- a/apps/docsite/src/components/tokens/index.ts+++ b/apps/docsite/src/components/tokens/index.ts@@ -1,5 +1,6 @@export {ColorTokenTable} from './ColorTokenTable';export {SpacingTokenTable} from './SpacingTokenTable';+export {SizeTokenTable} from './SizeTokenTable';export {ShapeTokenTable,RadiusTokenTable,