{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-structured-list",
  "type": "registry:ui",
  "title": "react-tw/structured-list.tsx",
  "description": "Carbon `StructuredList` as React + Tailwind components, the look and rules of Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-refs",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/structured-list.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/structured-list.tsx",
      "content": "import { Children, createContext, isValidElement, useContext, useLayoutEffect, useMemo, useRef, useState, type ReactElement, type ReactNode, type ComponentProps } from 'react';\nimport { StructuredList as StructuredListPrimitive } from '@purpurabog/ui/headless';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\nimport { useMergedRefs } from './lib/refs.ts';\n\n/**\n * Carbon `StructuredList` as React + Tailwind components, the look and rules of\n * `<ui-structured-list>`: a read-only `<table>` of `<StructuredListRow>`s (`head` rows go in its\n * `<thead>`) and `<StructuredListCell>`s with Carbon's row rules, header type and cell\n * paddings. `selection` makes the body rows a single choice — a hidden radio with Carbon's radio\n * icon in a leading cell; click, Space or the arrow keys select (`value` / `onChange`; the headless\n * StructuredList). `condensed` halves the paddings; `flush` drops the leading one (not with `selection`).\n */\n/** What the styled rows and cells need to look right; the selection is the primitive's. */\ninterface ListLookValue {\n    selection: boolean;\n    condensed: boolean;\n    flush: boolean;\n    selectionLabel: string;\n}\n\nconst ListContext = createContext<ListLookValue | null>(null);\n\ninterface RowState {\n    head: boolean;\n    selection: boolean;\n    condensed: boolean;\n    flush: boolean;\n    selected: boolean;\n}\n\nconst RowContext = createContext<RowState | null>(null);\nconst FirstCell = createContext(false);\n\nexport interface StructuredListProps extends Omit<ComponentProps<'table'>, 'selection' | 'condensed' | 'flush' | 'value' | 'defaultValue' | 'onChange' | 'label' | 'selectionLabel' | 'lang' | 'className' | 'children'> {\n    selection?: boolean;\n    condensed?: boolean;\n    flush?: boolean;\n    /** The selected row's `value` (selection lists, controlled). */\n    value?: string;\n    defaultValue?: string;\n    onChange?: (value: string) => void;\n    /** Accessible name of the table (default: the translation's \"Structured list section\"). */\n    label?: string;\n    /** Screen-reader name of the radio column (default: \"Select\"). */\n    selectionLabel?: string;\n    lang?: string;\n    className?: string;\n    /** `<StructuredListRow>`s. */\n    children: ReactNode;\n}\n\ntype RowElement = ReactElement<StructuredListRowProps>;\n\nexport function StructuredList({ selection = false, condensed = false, flush = false, value, defaultValue = '', onChange, label, selectionLabel, lang, className, children, ...rest }: Readonly<StructuredListProps>) {\n    const t = useTranslation(lang).common;\n    const { headRows, bodyRows, body } = useMemo(() => {\n        const all = Children.toArray(children).filter(isValidElement) as RowElement[];\n        const bodyOnly = all.filter((row) => !row.props.head);\n        return { headRows: all.filter((row) => row.props.head), bodyRows: bodyOnly, body: bodyOnly.map((row) => row.props.value ?? '') };\n    }, [children]);\n    const radioLabel = selectionLabel || t.select;\n    const look = useMemo<ListLookValue>(() => ({ selection, condensed, flush: flush && !selection, selectionLabel: radioLabel }), [selection, condensed, flush, radioLabel]);\n    return (\n        <StructuredListPrimitive.Root\n            {...rest}\n            values={body}\n            value={value}\n            defaultValue={defaultValue}\n            onValueChange={onChange}\n            aria-label={label || t.structuredList}\n            className={cn('table w-full border-collapse border-spacing-0 font-sans', className)}\n        >\n            <ListContext.Provider value={look}>\n                {headRows.length > 0 ? <thead>{headRows}</thead> : null}\n                <tbody>{bodyRows}</tbody>\n            </ListContext.Provider>\n        </StructuredListPrimitive.Root>\n    );\n}\n\nexport interface StructuredListRowProps extends Omit<ComponentProps<'tr'>, 'head' | 'value' | 'aria-label' | 'className' | 'children'> {\n    /** The header row: its cells become column headers. */\n    head?: boolean;\n    /** Identifies the row in the list's `value` / `onChange`. */\n    value?: string;\n    /** Name of the row's radio (default: the row's text). */\n    'aria-label'?: string;\n    className?: string;\n    /** `<StructuredListCell>`s. */\n    children: ReactNode;\n}\n\n/** The row's rules, recoloured at once (Carbon moves only the row's fill). */\n/** Transparent borders that keep the room for the row's rules, on the part that pads a cell. */\nconst ruleBorders = '[border-block-start:var(--_rule-top,0px)_solid_transparent] [border-block-end:var(--_rule-bottom,0px)_solid_transparent]';\n/**\n * The row's rules, painted at the cell's own edges (the row's): a cell shorter than its row (one line beside a wrapped\n * one, a status indicator) would draw them out of line on its padding part. As the Lit cell.\n */\nconst ruleLines = cn(\n    \"relative before:pointer-events-none before:absolute before:inset-x-0 before:top-0 before:h-(--_rule-top,0px) before:bg-(--_rule,transparent) before:content-['']\",\n    \"after:pointer-events-none after:absolute after:inset-x-0 after:bottom-0 after:h-(--_rule-bottom,0px) after:bg-(--_rule,transparent) after:content-['']\",\n);\n\n/** A row: its rules (drawn by the cells from these variables), and the hover / selected fills of a selection list. */\nfunction rowClasses(head: boolean, choosable: boolean, selected: boolean, className?: string) {\n    return cn(\n        'group/row table-row transition-[background-color] duration-(--ui-duration-moderate) ease-carbon',\n        '[--_rule-bottom:0px] [--_rule-top:1px] [--_rule:var(--ui-hairline)]',\n        !head && 'last-of-type:[--_rule-bottom:1px]',\n        head && '[--_rule-top:0px]',\n        choosable && !selected && 'hover:cursor-pointer hover:bg-ghost-hover hover:[--_rule:var(--ui-ghost-hover)]',\n        selected && 'bg-[var(--ui-structured-list-selected,var(--ui-surface-2))] [--_rule:var(--ui-structured-list-selected,var(--ui-surface-2))] forced-colors:outline-1 forced-colors:outline-[Highlight] forced-colors:outline-solid',\n        'focus-within:outline-[length:var(--ui-focus-width,2px)] focus-within:-outline-offset-2 focus-within:outline-focus focus-within:outline-solid',\n        className,\n    );\n}\n\ninterface RadioCellProps {\n    head: boolean;\n    condensed: boolean;\n    selected: boolean;\n    /** The radio's name: the row's text. */\n    label: string;\n    selectionLabel: string;\n}\n\n/** The leading cell of a selection list: the column header's screen-reader name, or a body row's hidden radio and radio icon. */\nfunction RadioCell({ head, condensed, selected, label, selectionLabel }: Readonly<RadioCellProps>) {\n    const radioBase = cn('box-border pt-4 pe-0 pb-6 ps-4', ruleBorders, head && 'pt-4 pb-2', condensed && 'py-2 ps-2');\n    if (head) {\n        return (\n            <th scope=\"col\" className={cn('table-cell box-border w-8 p-0 align-top font-normal', ruleLines)}>\n                <div className={radioBase}>\n                    <span className=\"sr-only\">{selectionLabel}</span>\n                </div>\n            </th>\n        );\n    }\n    return (\n        <td className={cn('table-cell box-border w-8 p-0 align-top', ruleLines)}>\n            <div className={radioBase}>\n                <StructuredListPrimitive.Radio className=\"sr-only\" aria-label={label} />\n                <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className=\"mt-0.5 inline-block size-4 fill-icon-primary align-top text-icon-primary\">\n                    <circle cx=\"8\" cy=\"8\" r=\"6.5\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1\" />\n                    {selected ? <circle cx=\"8\" cy=\"8\" r=\"3\" /> : null}\n                </svg>\n            </div>\n        </td>\n    );\n}\n\nexport function StructuredListRow({ head = false, value = '', 'aria-label': ariaLabel, className, children, ref, ...rest }: Readonly<StructuredListRowProps>) {\n    const list = useContext(ListContext);\n    const row = useRef<HTMLTableRowElement>(null);\n    const rowRef = useMergedRefs(row, ref);\n    // the radio is named by the row's text (as the Lit row), read back after each render\n    const [text, setText] = useState('');\n    // the row's text, read back after every render; set only when it changed\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n    useLayoutEffect(() => {\n        const now = row.current?.textContent?.replaceAll(/\\s+/g, ' ').trim() ?? '';\n        if (now !== text) setText(now);\n    });\n    if (!list) throw new Error('<StructuredListRow> goes inside <StructuredList>');\n    return (\n        <StructuredListRowBody {...rest} rowRef={rowRef} head={head} value={value} label={ariaLabel ?? text} list={list} className={className}>\n            {children}\n        </StructuredListRowBody>\n    );\n}\n\n/** The row itself, on the primitive: a body row of a selection list is selectable. */\nfunction StructuredListRowBody({ head, value, label, list, rowRef, className, children, ...rest }: Readonly<Omit<StructuredListRowProps, 'aria-label'> & { head: boolean; value: string; label: string; list: ListLookValue; rowRef: ComponentProps<'tr'>['ref'] }>) {\n    const { selection, condensed, flush } = list;\n    const current = StructuredListPrimitive.useStructuredListContext().value;\n    const choosable = selection && !head;\n    const selected = choosable && current !== '' && current === value;\n    const state = useMemo<RowState>(() => ({ head, selection, condensed, flush, selected }), [head, selection, condensed, flush, selected]);\n    return (\n        <StructuredListPrimitive.Row {...rest} ref={rowRef} value={choosable ? value : undefined} className={rowClasses(head, choosable, selected, className)}>\n            {selection ? <RadioCell head={head} condensed={condensed} selected={selected} label={label} selectionLabel={list.selectionLabel} /> : null}\n            <RowContext.Provider value={state}>\n                {Children.toArray(children).map((cell, i) => (\n                    <FirstCell.Provider key={isValidElement(cell) ? (cell.key ?? i) : i} value={i === 0}>\n                        {cell}\n                    </FirstCell.Provider>\n                ))}\n            </RowContext.Provider>\n        </StructuredListPrimitive.Row>\n    );\n}\n\nexport interface StructuredListCellProps extends Omit<ComponentProps<'td'>, 'children' | 'className'> {\n    /** Keep the content on one line. */\n    nowrap?: boolean;\n    className?: string;\n    children?: ReactNode;\n}\n\nexport function StructuredListCell({ nowrap = false, className, children, ...rest }: Readonly<StructuredListCellProps>) {\n    const row = useContext(RowContext);\n    const first = useContext(FirstCell);\n    if (!row) throw new Error('<StructuredListCell> goes inside <StructuredListRow>');\n    const { head, selection, condensed, flush, selected } = row;\n    const Cell = head ? 'th' : 'td';\n    return (\n        <Cell\n            {...rest}\n            scope={head ? 'col' : rest.scope}\n            className={cn(\n                ruleLines,\n                'table-cell box-border max-w-[576px] p-0 text-start align-baseline text-ink-muted type-body-sm leading-5! transition-[color] duration-(--ui-duration-moderate) ease-carbon',\n                head && 'h-10 align-top font-semibold! leading-[18px]! text-ink',\n                selection && !head && 'group-hover/row:text-ink',\n                selected && 'text-ink',\n                nowrap && 'whitespace-nowrap',\n                className,\n            )}\n        >\n            {/* the padding and the row's rules are drawn here, as by the Lit cell's part */}\n            <div className={cn('box-border px-2 pt-4 pb-6', ruleBorders, head && 'pb-2', condensed && 'p-2', first && 'ps-4', selection && 'px-4', flush && 'ps-0 pe-4')}>{children}</div>\n        </Cell>\n    );\n}\n\n/** Carbon `StructuredListSkeleton`: a header row of three bars (75% of their cells, columns 8 : 30 : 15) and `rows` empty rows. Hidden from assistive tech. */\nexport function StructuredListSkeleton({ rows = 5, className, ...rest }: Readonly<ComponentProps<'div'> & { rows?: number }>) {\n    return (\n        <div {...rest} aria-hidden=\"true\" className={cn('block', className)}>\n            <div className=\"grid grid-cols-[8fr_30fr_15fr]\">\n                {[0, 1, 2].map((i) => (\n                    <div key={i} className={cn('px-2 pt-4 pb-2', i === 0 && 'ps-4')}>\n                        <span className=\"skeleton block h-4 w-3/4\" />\n                    </div>\n                ))}\n            </div>\n            {Array.from({ length: Math.max(0, rows) }, (_, i) => (\n                <div key={i} className=\"box-content h-10 border-0 border-t border-solid border-hairline last:border-b\" />\n            ))}\n        </div>\n    );\n}\n"
    }
  ]
}
