{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-tile",
  "type": "registry:ui",
  "title": "react-tw/tile.tsx",
  "description": "Carbon tiles as React + Tailwind components, the look of `<ui-tile>`, `<ui-expandable-tile>`, 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-checkbox",
    "@kanok-ui/tw-field",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-radio-group",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/tile.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/tile.tsx",
      "content": "import { useId, useState, type ChangeEvent, type ComponentProps, type ReactNode, type HTMLAttributes, type Ref } from 'react';\nimport { Checkbox as CheckboxPrimitive, RadioGroup as RadioGroupPrimitive } from '@purpurabog/ui/headless';\nimport { describedByOf, useCheckboxGroup } from './checkbox.tsx';\nimport { FieldMessageLine, fieldMessage } from './field.tsx';\nimport { useRadioGroup, type RadioLabelPosition } from './radio-group.tsx';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\n\n/**\n * Carbon tiles as React + Tailwind components, the look of `<ui-tile>`, `<ui-expandable-tile>`,\n * `<ui-selectable-tile>` and `<ui-radio-tile>`: a `$layer` box (16px padding, no border, no shadow),\n * so a tile follows the `Layer` it sits on.\n */\n\nexport interface TileProps extends Omit<ComponentProps<'a'>, 'children' | 'ref'> {\n    /** The tile: its `<a>` with `href`, else the box. */\n    ref?: Ref<HTMLElement>;\n    /** Makes the whole tile one link (a single tab stop): no other controls inside it. */\n    href?: string;\n    target?: '_self' | '_blank';\n    /** A linked tile's icon (e.g. an arrow), 20px, 12px from the bottom-end corner; as Carbon, none by default. */\n    icon?: ReactNode;\n    /** A linked tile that can't be followed right now (Carbon `ClickableTile disabled`): plain text, no link. */\n    disabled?: boolean;\n    lang?: string;\n    children?: ReactNode;\n}\n\n/** Carbon .cds--tile: body-compact-01, 16px padding, layer background, 64px tall and 128px wide at least. */\n// Carbon: square and borderless (the layer colour sets it apart); the card settings (--ui-card-*) let a design round and outline it\nconst tileBox = 'relative box-border block h-full min-h-16 min-w-32 rounded-[var(--ui-card-radius,0px)] ui-card-edge p-4 bg-[var(--ui-card-background,var(--ui-surface-1))] text-ink type-body-sm leading-[18px]! no-underline outline-[length:var(--ui-focus-width,2px)] [outline-offset:var(--ui-focus-offset,-2px)] outline-solid outline-transparent forced-colors:border forced-colors:border-solid forced-colors:border-[CanvasText]';\nconst iconBox = 'absolute end-3 bottom-3 size-5 [&_svg]:block [&_svg]:size-5 [&_svg]:fill-icon-interactive';\n\n/** Carbon `Tile` / `ClickableTile`: with `href` the whole tile is a link. */\nexport function Tile({ href, target = '_self', icon, disabled = false, lang, className, children, ref, ...rest }: Readonly<TileProps>) {\n    const t = useTranslation(lang).common;\n    if (!href)\n        return (\n            <div className={cn('block', className)}>\n                <div {...(rest as HTMLAttributes<HTMLDivElement>)} ref={ref as Ref<HTMLDivElement> | undefined} className={tileBox}>\n                    {children}\n                </div>\n            </div>\n        );\n    if (disabled)\n        return (\n            <div className={cn('block', className)}>\n                <div {...(rest as HTMLAttributes<HTMLDivElement>)} ref={ref as Ref<HTMLDivElement> | undefined} aria-disabled=\"true\" className={cn(tileBox, 'cursor-not-allowed text-ink-disabled')}>\n                    {children}\n                </div>\n            </div>\n        );\n    const external = target === '_blank';\n    return (\n        <div className={cn('block', className)}>\n            {/* tabindex as Carbon's clickable tile: without it Safari neither focuses it on click nor stops on it with Tab */}\n            <a\n                {...rest}\n                ref={ref as Ref<HTMLAnchorElement> | undefined}\n                href={href}\n                target={target}\n                rel={external ? 'noopener noreferrer' : undefined}\n                tabIndex={0}\n                className={cn(tileBox, 'cursor-pointer transition-all duration-(--ui-duration-moderate-01) ease-carbon hover:bg-ghost-hover focus:outline-focus focus:not-focus-visible:outline-[color:var(--ui-pointer-focus,var(--ui-focus))]')}\n            >\n                {children}\n                {external ? <span className=\"sr-only\">{t.opensInNewTab}</span> : null}\n                {icon ? (\n                    <span aria-hidden=\"true\" className={iconBox}>\n                        {icon}\n                    </span>\n                ) : null}\n            </a>\n        </div>\n    );\n}\n\nexport interface ExpandableTileProps extends Omit<ComponentProps<'div'>, 'summary' | 'expanded' | 'defaultExpanded' | 'onBeforeToggle' | 'onToggle' | 'children' | 'className'> {\n    /** Always visible, inside the toggle button: text only. */\n    summary: ReactNode;\n    expanded?: boolean;\n    defaultExpanded?: boolean;\n    /** About to open / close: return false to keep the current state. */\n    onBeforeToggle?: (expanded: boolean) => boolean | void;\n    onToggle?: (expanded: boolean) => void;\n    /** Revealed content (interactive content goes here, never in the summary). */\n    children?: ReactNode;\n    className?: string;\n}\n\n/** Carbon `ExpandableTile` as a WAI-ARIA disclosure: the summary is a button with `aria-expanded` / `aria-controls`. */\nexport function ExpandableTile({ summary, expanded: expandedProp, defaultExpanded = false, onBeforeToggle, onToggle, children, className, ...rest }: Readonly<ExpandableTileProps>) {\n    const contentId = `${useId()}content`;\n    const [own, setOwn] = useState(defaultExpanded);\n    const expanded = expandedProp ?? own;\n    const toggle = () => {\n        const next = !expanded;\n        if (onBeforeToggle?.(next) === false) return;\n        setOwn(next);\n        onToggle?.(next);\n    };\n    return (\n        <div {...rest} className={cn('block text-ink type-body-sm leading-[18px]!', className)}>\n            {/* Carbon .cds--tile--expandable: a layer tile, 16px padding, no border (its hover fill changes at once); the ring goes round the whole tile on :focus */}\n            <div\n                className={cn(\n                    // the card settings: Carbon no border and square, Kanok a hairline and 12px (ui-card-edge in theme.css)\n                    'relative box-border min-h-16 rounded-[var(--ui-card-radius,0px)] ui-card-edge bg-[var(--ui-card-background,var(--ui-surface-1))] p-4 outline-[length:var(--ui-focus-width,2px)] [outline-offset:var(--ui-focus-offset,-2px)] outline-solid outline-transparent hover:bg-ghost-hover',\n                    'has-[button:focus]:outline-focus forced-colors:border forced-colors:border-solid forced-colors:border-[CanvasText]',\n                )}\n            >\n                <button\n                    type=\"button\"\n                    aria-expanded={expanded}\n                    aria-controls={contentId}\n                    onClick={toggle}\n                    className=\"m-0 block min-h-8 w-full cursor-pointer border-0 bg-transparent p-0 text-start text-inherit outline-none [font:inherit] [letter-spacing:inherit]\"\n                >\n                    {summary}\n                    {/* Carbon .cds--tile__chevron: a 48px square in the tile's bottom-end corner, the 16px icon centred */}\n                    <svg\n                        viewBox=\"0 0 16 16\"\n                        aria-hidden=\"true\"\n                        className={cn(\n                            'absolute end-0 bottom-0 box-border size-12 fill-icon-primary p-4 forced-colors:fill-[CanvasText]',\n                            'motion-safe:transition-transform motion-safe:duration-(--ui-duration-moderate) motion-safe:ease-carbon',\n                            expanded && '[transform:rotate(180deg)]',\n                        )}\n                    >\n                        <path d=\"M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z\" />\n                    </svg>\n                </button>\n                {/* Carbon .cds--tile-content__below-the-fold: fades in for 110ms as it opens (closing, the tile shuts at once) */}\n                <div id={contentId} hidden={!expanded} className=\"transition-[opacity,visibility] duration-(--ui-duration-moderate) ease-carbon starting:invisible starting:opacity-0\">\n                    {children}\n                </div>\n            </div>\n        </div>\n    );\n}\n\n/** Carbon .cds--tile--selectable: a layer tile, a transparent 1px border, 16px padding with 48px at the end for the mark (`--ui-tile-mark-space`, beyond Carbon), 64px tall. */\n// Carbon: no visible border until chosen, square; a design can outline and round the tile (--ui-tile-border-color, --ui-card-radius)\nconst selectionTile = 'relative box-border block min-h-16 rounded-[var(--ui-card-radius,0px)] border border-solid border-[var(--ui-tile-border-color,transparent)] bg-[var(--ui-card-background,var(--ui-surface-1))] py-4 ps-4 pe-[var(--ui-tile-mark-space,calc(var(--spacing)*12))] text-ink type-body-sm leading-[18px]! cursor-pointer outline-[length:var(--ui-focus-width,2px)] [outline-offset:var(--ui-focus-offset,-2px)] outline-solid outline-transparent transition-all duration-(--ui-duration-moderate-01) ease-carbon';\n/** The 16px mark in the top-end corner (Carbon .cds--tile__checkmark), above content that runs under it with a small `--ui-tile-mark-space`. */\nconst mark = 'pointer-events-none absolute end-4 top-4 z-1 size-4 transition-all duration-(--ui-duration-moderate) ease-carbon forced-colors:fill-[CanvasText]';\n\nexport interface SelectableTileProps extends Omit<ComponentProps<'input'>, 'type' | 'children'> {\n    /** Lit `hide-label`: the content stays for screen readers only. */\n    hideLabel?: boolean;\n    /** Lit `indeterminate`: the checkbox's mixed state (screen readers hear \"mixed\"); the mark stays the empty box, as on the web component. */\n    indeterminate?: boolean;\n    invalid?: boolean;\n    invalidText?: ReactNode;\n    warn?: boolean;\n    warnText?: ReactNode;\n    helperText?: ReactNode;\n    /** Tile content (title, description…); also the checkbox's name. */\n    children: ReactNode;\n}\n\n/** Carbon `SelectableTile`: a checkbox drawn as a tile (a real checkbox inside, so Space toggles and forms submit it); a box of a `CheckboxGroup` too. */\nexport function SelectableTile({ hideLabel, indeterminate = false, invalid, invalidText, warn, warnText, helperText, disabled: ownDisabled, readOnly: ownReadOnly, id, className, children, onChange, checked, defaultChecked, ...rest }: Readonly<SelectableTileProps>) {\n    const group = useCheckboxGroup();\n    const disabled = !!ownDisabled || group.disabled;\n    const readOnly = !!ownReadOnly || group.readOnly;\n    const autoId = useId();\n    const inputId = id ?? autoId;\n    const [own, setOwn] = useState(!!defaultChecked);\n    const isChecked = checked ?? own;\n    const interactive = !disabled && !readOnly;\n    const { message, content } = fieldMessage({ interactive, invalid, invalidText, warn, warnText, helperText });\n    // its own error, or its group's (Lit data-group-invalid)\n    const showError = message?.kind === 'error' || group.invalid;\n    return (\n        <div className={cn('block font-sans', className)}>\n            <label\n                className={cn(\n                    selectionTile,\n                    'w-full',\n                    interactive && !isChecked && 'hover:bg-ghost-hover',\n                    isChecked && 'border-[var(--ui-tile-selected-border,var(--ui-layer-selected-inverse))] bg-[var(--ui-tile-selected-background,var(--ui-highlight))]',\n                    showError && 'border-2 border-error',\n                    disabled && 'cursor-not-allowed text-ink-disabled bg-[var(--ui-card-background,var(--ui-surface-1))]',\n                    disabled && isChecked && 'border-border-disabled bg-[var(--ui-card-background,var(--ui-surface-1))]',\n                    readOnly && !disabled && 'cursor-default',\n                    // ring the whole tile on :focus (a click too, as Carbon), not the small mark\n                    'has-[input:focus]:outline-focus has-[input:focus:not(:focus-visible)]:outline-[color:var(--ui-pointer-focus,var(--ui-focus))]',\n                )}\n            >\n                {/* tabindex as Carbon's selectable tile, so Safari focuses it on click and Tab reaches it; read-only blocks the toggle */}\n                <CheckboxPrimitive.Root\n                    {...rest}\n                    id={inputId}\n                    tabIndex={0}\n                    checked={isChecked}\n                    indeterminate={indeterminate}\n                    disabled={disabled}\n                    readOnly={readOnly}\n                    invalid={showError}\n                    aria-describedby={describedByOf(message ? `${inputId}-message` : undefined, group)}\n                    className=\"peer absolute end-4 top-4 z-1 m-0 size-4 cursor-[inherit] opacity-0\"\n                    onChange={(event: ChangeEvent<HTMLInputElement>) => {\n                        setOwn(event.target.checked);\n                        onChange?.(event);\n                    }}\n                />\n                {/* Carbon's Checkbox / CheckboxCheckedFilled icons (32 grid at 16px) */}\n                <svg viewBox=\"0 0 32 32\" aria-hidden=\"true\" className={cn(mark, isChecked ? 'fill-[var(--ui-tile-selected-mark,var(--ui-icon-primary))]' : 'fill-icon-secondary', disabled && 'fill-icon-disabled')}>\n                    {isChecked ? <path d=\"M26 4H6a2 2 0 0 0-2 2v20a2 2 0 0 0 2 2h20a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2zM14 21.5l-5-4.957L10.59 15 14 18.346 21.409 11 23 12.577z\" /> : <path d=\"M26 4H6a2 2 0 0 0-2 2v20a2 2 0 0 0 2 2h20a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2zM6 26V6h20v20z\" />}\n                </svg>\n                <span className={hideLabel ? 'sr-only' : undefined}>{children}</span>\n            </label>\n            <FieldMessageLine id={`${inputId}-message`} message={message} content={content} disabled={disabled} withIcon />\n        </div>\n    );\n}\n\nexport interface RadioTileProps extends Omit<ComponentProps<'input'>, 'value' | 'disabled' | 'children' | 'className'> {\n    value: string;\n    disabled?: boolean;\n    /**\n     * Lit `label-position` on `<ui-radio-tile>`; unset, the group's. A tile draws no circle, so it moves nothing on the\n     * tile (as on the web component); it shows as `data-label-position` to style on.\n     */\n    labelPosition?: RadioLabelPosition;\n    /** Tile content (title, description…); also the option's name. */\n    children: ReactNode;\n    className?: string;\n}\n\n/** Carbon `RadioTile`: a radio option drawn as a tile; goes inside a `RadioGroup` (arrows, forms, read-only, errors). */\nexport function RadioTile({ value, disabled: ownDisabled, labelPosition, children, className, ...rest }: Readonly<RadioTileProps>) {\n    const group = useRadioGroup('RadioTile');\n    const disabled = group.disabled || !!ownDisabled;\n    const checked = group.value === value;\n    const { readOnly } = group;\n    return (\n        <label\n            data-label-position={labelPosition ?? group.labelPosition}\n            className={cn(\n                selectionTile,\n                !disabled && !readOnly && !checked && 'hover:bg-ghost-hover',\n                checked && 'border-[var(--ui-tile-selected-border,var(--ui-layer-selected-inverse))] bg-[var(--ui-tile-selected-background,var(--ui-highlight))]',\n                group.invalid && 'border-2 border-error',\n                disabled && 'cursor-not-allowed text-ink-disabled bg-[var(--ui-card-background,var(--ui-surface-1))]',\n                disabled && checked && 'border-border-disabled bg-[var(--ui-card-background,var(--ui-surface-1))]',\n                readOnly && !disabled && 'cursor-default',\n                'has-[input:focus]:outline-focus has-[input:focus:not(:focus-visible)]:outline-[color:var(--ui-pointer-focus,var(--ui-focus))]',\n                'forced-colors:has-[input:checked]:border-2 forced-colors:has-[input:checked]:border-[Highlight]',\n                className,\n            )}\n        >\n            <RadioGroupPrimitive.Item {...rest}\n                value={value}\n                disabled={ownDisabled}\n                required={group.required || undefined}\n                aria-describedby={[rest['aria-describedby'], group.describedBy].filter(Boolean).join(' ') || undefined}\n                className=\"absolute end-4 top-4 z-1 m-0 size-4 cursor-[inherit] opacity-0\"\n            />\n            {/* Carbon's CheckmarkFilled, shown only on the chosen tile */}\n            <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className={cn(mark, 'fill-[var(--ui-tile-selected-mark,var(--ui-icon-primary))]', !checked && 'opacity-0', disabled && 'fill-icon-disabled')}>\n                <path d=\"M8 1C4.1 1 1 4.1 1 8s3.1 7 7 7 7-3.1 7-7-3.1-7-7-7zM7 11 4.3 8.3l.9-.8L7 9.3l4-3.9.9.8L7 11z\" />\n            </svg>\n            {children}\n        </label>\n    );\n}\n"
    }
  ]
}
