All files / packages/design-system-2-site/src/app/(frontend)/styling-api/styling-hook-index HookCells.tsx

98.7% Statements 76/77
89.47% Branches 51/57
100% Functions 57/57
100% Lines 65/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 2791x                                           10038x 10038x           2x                                                   11721x       11721x                   32x 32x                                             1584x                           1764x                     323x               913x             1x   1x 1x 1x 1x 1x 1535x 4145x 78x 993x 993x         152x 152x         3537x         4343x     962x 1219x   1083x     11721x   962x   32x   6195x 5558x 962x 573x             57x 836x   748x     19x       114x               19x     76x   76x   19x     304x       19x     152x   152x 152x   20x       267x             267x   252x 19x 323x     63x       756x                 756x                 756x   779x   962x       962x 10038x        
'use client';
 
import { Check, Copy } from 'lucide-react';
import type { CSSProperties } from 'react';
import { Badge } from '@/components/ui/Badge';
import type { DataTableColumn } from '@/components/ui/DataTable';
import type { HookDisplay } from '@/lib/styling-hook-groups';
import type { HookGroup, HookRow, ModeCell } from '@/lib/styling-hooks';
 
/**
 * Cells for the Styling Hook Index tables. Previews use the resolved values
 * inline (never `var()`): the site stylesheet only loads one theme, and the
 * Index shows either. Previews are decorative; the values are written out.
 */
 
export interface CopyState {
  /** The hook name last copied, while its confirmation shows. */
  copied: string;
  onCopy: (name: string) => void;
}
 
export function HookName({ row, copied, onCopy }: { row: HookRow } & CopyState) {
  const isCopied = copied === row.name;
  return (
    <div className="hook-name">
      <button
        type="button"
        className="code-chip hook-name__copy"
        aria-label={`Copy ${row.name}`}
        onClick={() => onCopy(row.name)}
      >
        <span>{row.name}</span>
        {isCopied ? (
          <Check className="hook-name__icon hook-name__icon--done" aria-hidden="true" />
        ) : (
          <Copy className="hook-name__icon" aria-hidden="true" />
        )}
        {isCopied && (
          <span className="hook-name__copied" aria-hidden="true">
            Copied
          </span>
        )}
      </button>
      {row.deprecated && (
        // TODO design review: deprecated treatment isn't in Figma yet.
        // TODO: link to the replacement hooks once the page lists them.
        <span className="hook-name__deprecated">
          <Badge tone="warning">Deprecated</Badge>
        </span>
      )}
    </div>
  );
}
 
function Swatch({ color }: { color: string }) {
  return <span className="hook-swatch" style={{ background: color }} aria-hidden="true" />;
}
 
function ColorValue({ cell }: { cell: ModeCell }) {
  return (
    <div className="hook-value">
      <Swatch color={cell.value} />
      {/* The palette name, or the raw value for colors outside the palette. */}
      {cell.paletteName ? <span>{cell.paletteName}</span> : <code>{cell.value}</code>}
    </div>
  );
}
 
function Reference({ cell }: { cell: ModeCell }) {
  Iif (!cell.references && !cell.resolves) return <span className="hook-muted">None</span>;
  return (
    <dl className="hook-reference">
      {cell.references && (
        <div>
          <dt>References:</dt>
          <dd>
            <code>{cell.references}</code>
          </dd>
        </div>
      )}
      {cell.resolves && (
        <div>
          <dt>Resolves:</dt>
          <dd>
            <code>{cell.resolves}</code>
          </dd>
        </div>
      )}
    </dl>
  );
}
 
function ShadowValue({ cell, dark }: { cell: ModeCell; dark?: boolean }) {
  return (
    <div className="hook-shadow">
      <span
        className={dark ? 'hook-shadow__preview hook-shadow__preview--dark' : 'hook-shadow__preview'}
        style={{ boxShadow: cell.value }}
        aria-hidden="true"
      />
      <code>{cell.value}</code>
    </div>
  );
}
 
/** Spacing: the gap between two boxes. */
function Gap({ size }: { size: string }) {
  return (
    <span className="hook-gap" aria-hidden="true">
      <span className="hook-gap__box" />
      <span className="hook-gap__space" style={{ inlineSize: size }} />
      <span className="hook-gap__box" />
    </span>
  );
}
 
/** Sizing: an outlined box as wide as the value. */
function Size({ size }: { size: string }) {
  return (
    <span className="hook-size" aria-hidden="true">
      <span className="hook-size__box" style={{ inlineSize: size }} />
    </span>
  );
}
 
function Preview({ style, className, text }: { style: CSSProperties; className: string; text?: string }) {
  return (
    <span className={className} style={style} aria-hidden="true">
      {text}
    </span>
  );
}
 
const SAMPLE = 'Aa Bb Cc 123';
// Figma column widths: hook name, color values, plain values (narrower in Sizing, for its wide previews).
const HOOK_WIDTH = '18rem';
const COLOR_WIDTH = '11rem';
const VALUE_WIDTH = '8.75rem';
const SIZING_VALUE_WIDTH = '8.75rem';
const code = (text: string | undefined) =>
  text ? <code>{text}</code> : <span className="hook-muted">—</span>;
const plain = (text: string | undefined) => <span className="hook-muted">{text || '—'}</span>;
const rawColumns = (width = VALUE_WIDTH): DataTableColumn<HookRow>[] => [
  { header: 'Raw Value', width, cell: (r) => plain(r.px ?? r.light.value) },
  { header: 'Calculated Value', width, cell: (r) => plain(r.rem) },
];
 
/** "Regular | 400." → "Regular": the weight's name, from its description. */
function weightName(row: HookRow): string | undefined {
  const name = row.description?.split('|')[0].trim();
  return name && !/^font weight/i.test(name) ? name : undefined;
}
 
/** Whether a group's rows have References / Resolves to show behind the switch. */
export function hasReferences(group: HookGroup): boolean {
  return group.display === 'color' && group.rows.some((r) => r.light.references || r.light.resolves);
}
 
/** The columns for a group's table, after the Styling Hook column. */
function valueColumns(group: HookGroup, showRefs: boolean): DataTableColumn<HookRow>[] {
  const withDark = group.rows.some((r) => r.dark);
  // Figma drops the Description column for System and Global Palettes.
  const description: DataTableColumn<HookRow>[] =
    group.chip === 'semantic' && group.rows.some((r) => r.description)
      ? [{ header: 'Description', cell: (r) => <span className="hook-description">{r.description}</span> }]
      : [];
  const value = (header: string, mode: (r: HookRow) => ModeCell): DataTableColumn<HookRow> => ({
    header,
    width: COLOR_WIDTH,
    cell: (r) => <ColorValue cell={mode(r)} />,
  });
  const reference = (header: string, mode: (r: HookRow) => ModeCell): DataTableColumn<HookRow> => ({
    header,
    cell: (r) => <Reference cell={mode(r)} />,
  });
  const light = (r: HookRow) => r.light;
  const dark = (r: HookRow) => r.dark ?? r.light;
  const columnsByDisplay: Record<HookDisplay, () => DataTableColumn<HookRow>[]> = {
    color: () => [
      ...description,
      value('Light Mode Value', light),
      ...(showRefs ? [reference('Light Mode Reference', light)] : []),
      ...(withDark ? [value('Dark Mode Value', dark)] : []),
      ...(withDark && showRefs ? [reference('Dark Mode Reference', dark)] : []),
    ],
    shadow: () => [
      { header: 'Light Mode', cell: (r) => <ShadowValue cell={r.light} /> },
      ...(withDark
        ? [{ header: 'Dark Mode', cell: (r: HookRow) => <ShadowValue cell={r.dark ?? r.light} dark /> }]
        : []),
    ],
    radius: () => [
      {
        header: 'Example',
        cell: (r) => (
          <Preview
            className="hook-radius"
            style={{ borderStartStartRadius: r.light.value, borderEndStartRadius: r.light.value }}
          />
        ),
      },
      ...rawColumns(),
    ],
    'border-width': () => [
      {
        header: 'Example',
        cell: (r) => <Preview className="hook-border" style={{ borderWidth: r.light.value }} />,
      },
      { header: 'Value', width: VALUE_WIDTH, cell: (r) => plain(r.light.value) },
    ],
    'font-scale': () => [
      {
        header: 'Example',
        cell: (r) => <Preview className="hook-text" style={{ fontSize: r.light.value }} text={SAMPLE} />,
      },
      ...rawColumns(),
    ],
    'font-weight': () => [
      {
        header: 'Example',
        cell: (r) => <Preview className="hook-text" style={{ fontWeight: r.light.value }} text={SAMPLE} />,
      },
      { header: 'Raw Value', width: VALUE_WIDTH, cell: (r) => plain(weightName(r)) },
      { header: 'Calculated Value', width: VALUE_WIDTH, cell: (r) => plain(r.light.value) },
    ],
    'line-height': () => [
      {
        header: 'Example',
        cell: (r) => (
          <Preview
            className="hook-text hook-text--lines"
            style={{ lineHeight: r.light.value }}
            text="Line height sets the space between lines of text in a paragraph."
          />
        ),
      },
      { header: 'Multiplier', width: VALUE_WIDTH, cell: (r) => plain(r.light.value) },
    ],
    spacing: () => [{ header: 'Example', cell: (r) => <Gap size={r.light.value} /> }, ...rawColumns()],
    dimension: () => [
      { header: 'Example', cell: (r) => <Size size={r.light.value} /> },
      ...rawColumns(SIZING_VALUE_WIDTH),
    ],
    density: () => [
      {
        header: 'Comfy',
        cell: (r) => (
          <div className="hook-density">
            <Gap size={r.density?.comfy ?? r.light.value} />
            {plain(r.density?.comfy ?? r.light.value)}
          </div>
        ),
      },
      {
        header: 'Compact',
        cell: (r) =>
          r.density ? (
            <div className="hook-density">
              <Gap size={r.density.compact} />
              {plain(r.density.compact)}
            </div>
          ) : (
            plain(undefined)
          ),
      },
      { header: 'Reference Hook', cell: (r) => code(r.density?.reference) },
    ],
    value: () => [...description, { header: 'Value', cell: (r) => code(r.light.value) }],
  };
  return columnsByDisplay[group.display]();
}
 
export function columnsFor(group: HookGroup, showRefs: boolean, copy: CopyState): DataTableColumn<HookRow>[] {
  return [
    { header: 'Styling Hook', width: HOOK_WIDTH, cell: (r) => <HookName row={r} {...copy} /> },
    ...valueColumns(group, showRefs),
  ];
}