{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-dropdown",
  "type": "registry:ui",
  "title": "react-tw/dropdown.tsx",
  "description": "Carbon `Dropdown` as a React + Tailwind component: the look of `<ui-dropdown>` on the headless 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/core-field",
    "@kanok-ui/tw-field",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-list",
    "@kanok-ui/tw-skeleton",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/dropdown.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/dropdown.tsx",
      "content": "import { useId, type CSSProperties, type ReactNode, type ComponentProps } from 'react';\nimport { Dropdown as DropdownPrimitive, isGroup } from '@purpurabog/ui/headless';\nimport { FieldMessageLine, StatusIcon, fluidBoxClasses, fluidControlClasses, fluidLabelClasses, useFluid, useFieldDisabled, fieldMessage, fieldBoxClasses, fieldDisabledEdge, fieldDisabledFill, fieldReadOnlyEdge, fieldInlineEdge, fieldLabelFont, fieldLabelInk, useRequiredError, fieldInvalidRing } from './field.tsx';\nimport type { FieldMessage } from '../core/field.ts';\nimport { cn } from './lib/utils.ts';\nimport { checkMark, EmptyRow, groupLabelClasses, ListStatus, listMenuClasses, listRowClasses, OptionText, type ListItem, type ListOption } from './list.tsx';\nimport { useTranslation } from './lib/i18n.ts';\nimport { FieldSkeleton, type FieldSkeletonProps } from './skeleton.tsx';\n\n/**\n * Carbon `Dropdown` as a React + Tailwind component: the look of `<ui-dropdown>` on the headless\n * `Dropdown` (keyboard traced key by key on Carbon React; restyle it from there): a button with `role=\"combobox\"` opens a\n * listbox in the top layer as wide as the field; the highlight is `aria-activedescendant`, so focus\n * stays on the button. A click opens on the chosen option (or none), ↓ / Enter / Space on the\n * chosen or first, letters jump (typeahead), Tab closes without picking. `inline` puts label and\n * field in one row. With `name`, the value is submitted with the form. `loading` (no Carbon\n * counterpart) shows one \"Loading…\" row and the keys pick nothing; `onMenuChange` (Carbon React\n * MultiSelect's) lets an app load the options when the list first opens.\n */\nexport interface DropdownProps extends Omit<ComponentProps<'button'>, 'label' | 'hideLabel' | 'items' | 'value' | 'defaultValue' | 'onChange' | 'placeholder' | 'size' | 'direction' | 'helperText' | 'invalid' | 'invalidText' | 'warn' | 'warnText' | 'disabled' | 'readOnly' | 'required' | 'inline' | 'fluid' | 'name' | 'id' | 'loading' | 'onMenuChange' | 'lang' | 'className'> {\n    label: ReactNode;\n    hideLabel?: boolean;\n    /** Options, and groups of options (`{ label, options }`). */\n    items: ListItem[];\n    value?: string;\n    defaultValue?: string;\n    onChange?: (value: string) => void;\n    placeholder?: string;\n    size?: 'sm' | 'md' | 'lg';\n    /** Open the list above the field. */\n    direction?: 'bottom' | 'top';\n    helperText?: ReactNode;\n    invalid?: boolean;\n    invalidText?: ReactNode;\n    warn?: boolean;\n    warnText?: ReactNode;\n    disabled?: boolean;\n    readOnly?: boolean;\n    required?: boolean;\n    /** Label, field and message in one row (Carbon inline dropdown). */\n    inline?: boolean;\n    /** Carbon `FluidDropdown`: the label inside a 64px box (also set by `FluidForm`); the open list keeps its row height. */\n    fluid?: boolean;\n    name?: string;\n    id?: string;\n    /** The options are on their way: one \"Loading…\" row, the keys pick nothing (no Carbon counterpart). */\n    loading?: boolean;\n    /** After the list opens or closes (Carbon React `onMenuChange`), e.g. to load the options on first open. */\n    onMenuChange?: (open: boolean) => void;\n    /** Language of the built-in strings; defaults to `<html lang>`. */\n    lang?: string;\n    className?: string;\n}\n\nconst ROW = { sm: 'var(--ui-field-height-sm)', md: 'var(--ui-field-height-md)', lg: 'var(--ui-field-height-lg)' } as const;\n\nfunction dropdownLabelClasses({ hidden, inline, disabled, fluid }: { hidden: boolean; inline: boolean; disabled: boolean; fluid: boolean }) {\n    if (hidden) return 'sr-only';\n    return cn('mb-2 inline-block', inline ? 'mb-0 type-body-sm' : fieldLabelFont, fieldLabelInk, disabled && 'text-ink-disabled', fluid && fluidLabelClasses);\n}\n\ninterface TriggerLook {\n    showError: boolean;\n    status: FieldMessage['kind'] | undefined;\n    disabled: boolean;\n    readOnly: boolean;\n    inline: boolean;\n    fluid: boolean;\n}\n\n/** The field box that opens the list; fluid: 64px with the value under the label, the box carrying colour, focus and status. */\nfunction triggerClasses({ showError, status, disabled, readOnly, inline, fluid }: TriggerLook) {\n    return cn(\n        'm-0 box-border flex h-(--list-row) w-full cursor-pointer items-center ps-4 pe-12 text-start',\n        fieldBoxClasses,\n        'text-ink type-body-sm',\n        // open, the field keeps its fill under the pointer (--ui-field-open-background; Carbon: the hover fill)\n        'outline-[length:var(--ui-field-focus-width,2px)] [outline-offset:calc(-1*var(--ui-field-focus-width,2px))] outline-transparent hover:bg-ghost-hover aria-expanded:hover:bg-[var(--ui-field-open-background,var(--ui-ghost-hover))] focus:outline-focus focus:[box-shadow:var(--ui-field-focus-halo,none)] aria-expanded:outline-focus aria-expanded:[box-shadow:var(--ui-field-focus-halo,none)]',\n        // Carbon list-box: all at fast-01; the hover fill is what changes\n        'transition-[background-color] duration-(--ui-duration-fast) ease-carbon',\n        showError && fieldInvalidRing,\n        status && 'pe-16',\n        disabled && cn('cursor-not-allowed text-ink-disabled', fieldDisabledEdge, fieldDisabledFill),\n        readOnly && cn('cursor-default bg-[var(--ui-field-readonly-background,transparent)]', fieldReadOnlyEdge),\n        inline && cn('w-auto min-w-0 bg-transparent hover:bg-ghost-hover', fieldInlineEdge),\n        'forced-colors:border forced-colors:border-solid forced-colors:border-[FieldText] forced-colors:focus:outline-[Highlight]',\n        fluid && cn('h-16 pt-[33px] pe-12 pb-[13px] ps-4 hover:bg-transparent', fluidControlClasses(status, readOnly)),\n    );\n}\n\n/** The chevron (turned while open) and the status icon before it; fluid: the chevron 34px down, the status icon in the message row. */\nfunction Chevron({ kind, fluid, readOnly, disabled }: Readonly<{ kind: FieldMessage['kind'] | undefined; fluid: boolean; readOnly: boolean; disabled: boolean }>) {\n    const { open } = DropdownPrimitive.useDropdownContext();\n    return (\n        <span className={cn('pointer-events-none absolute inset-y-0 end-4 flex items-center gap-2', fluid && 'items-start pt-[34px]')} aria-hidden=\"true\">\n            <StatusIcon kind={kind} className={fluid ? 'absolute end-0 top-[calc(100%+8px)]' : undefined} />\n            <svg\n                className={cn(\n                    'size-4 fill-ink transition-transform duration-(--ui-duration-fast) ease-carbon forced-colors:fill-[FieldText]',\n                    open && '[transform:rotate(180deg)]',\n                    readOnly && 'fill-ink-subtle',\n                    disabled && 'fill-ink-disabled',\n                )}\n                viewBox=\"0 0 16 16\"\n            >\n                <path d=\"M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z\" />\n            </svg>\n        </span>\n    );\n}\n\nexport function Dropdown({ label, hideLabel, items, value, defaultValue = '', onChange, placeholder = '', size = 'md', direction = 'bottom', helperText, invalid, invalidText, warn, warnText, disabled: ownDisabled, readOnly, required, inline, fluid: fluidProp, name, id, loading = false, onMenuChange, lang, className, ref, ...rest }: Readonly<DropdownProps>) {\n    const t = useTranslation(lang).list;\n    const disabled = useFieldDisabled(ownDisabled);\n    const autoId = useId();\n    const fieldId = id ?? autoId;\n    const interactive = !disabled && !readOnly;\n    // nothing chosen on a submit attempt: the required message, until a choice is made\n    const requiredError = useRequiredError(onChange);\n    const { message, content } = fieldMessage({ interactive, invalid: invalid || requiredError.missing, invalidText: invalid ? invalidText : undefined, fallbackError: requiredError.missing ? t.required : '', warn, warnText, helperText });\n    const showError = message?.kind === 'error';\n    const fluid = useFluid(fluidProp) && !inline;\n    const status = message?.kind === 'helper' ? undefined : message?.kind;\n    return (\n        <DropdownPrimitive.Root items={items} value={value} defaultValue={defaultValue} onValueChange={requiredError.onValueChange} onInvalid={requiredError.onInvalid} onOpenChange={onMenuChange} disabled={disabled} readOnly={readOnly} required={required} invalid={showError} loading={loading} name={name} id={fieldId}>\n            <div\n                style={{ '--list-row': ROW[size] } as CSSProperties}\n                className={cn('block font-sans', inline && 'inline-grid grid-cols-[auto_auto_auto] items-center justify-start gap-x-2', fluid && fluidBoxClasses(status, readOnly), className)}\n            >\n                <DropdownPrimitive.Label className={dropdownLabelClasses({ hidden: !!hideLabel, inline: !!inline, disabled, fluid })}>{label}</DropdownPrimitive.Label>\n                <div className=\"relative\">\n                    <DropdownPrimitive.Trigger {...rest} ref={ref} aria-describedby={message ? `${fieldId}-message` : undefined} className={triggerClasses({ showError, status, disabled, readOnly: !!readOnly, inline: !!inline, fluid })}>\n                        <DropdownPrimitive.Value placeholder={placeholder} className=\"truncate data-placeholder:text-ink-muted\" />\n                    </DropdownPrimitive.Trigger>\n                    <Chevron kind={message?.kind} fluid={fluid} readOnly={!!readOnly} disabled={disabled} />\n                </div>\n                {inline && message?.kind === 'helper' ? null : (\n                    <FieldMessageLine id={`${fieldId}-message`} message={message} content={content} disabled={disabled} fluid={fluid} className={inline ? 'm-0' : undefined} />\n                )}\n                <DropdownPrimitive.Popup side={direction} className={listMenuClasses}>\n                    {loading ? <EmptyRow>{t.loadingOptions}</EmptyRow> : <DropdownRows items={items} />}\n                </DropdownPrimitive.Popup>\n                <LoadingStatus text={t.loadingOptions} />\n            </div>\n        </DropdownPrimitive.Root>\n    );\n}\n\n/** \"Loading…\" for screen readers while the list is open and its options are on their way. */\nfunction LoadingStatus({ text }: Readonly<{ text: string }>) {\n    const { loading, open } = DropdownPrimitive.useDropdownContext();\n    return <ListStatus>{loading && open ? text : ''}</ListStatus>;\n}\n\n/** One option with Carbon's divider, highlight and the check on the chosen one (the look of ListRows). */\nfunction DropdownRow({ option }: Readonly<{ option: ListOption }>) {\n    const { value } = DropdownPrimitive.useDropdownContext();\n    return (\n        <DropdownPrimitive.Option value={option.value} data-option=\"\" className={listRowClasses}>\n            <OptionText option={option} />\n            {option.value === value ? checkMark : null}\n        </DropdownPrimitive.Option>\n    );\n}\n\n/** The options group by group; labelled groups under a caption. */\nfunction DropdownRows({ items }: Readonly<{ items: ListItem[] }>) {\n    return (\n        <>\n            {items.map((item) =>\n                isGroup(item) ? (\n                    <DropdownPrimitive.Group key={`group-${item.label}`}>\n                        <DropdownPrimitive.GroupLabel className={groupLabelClasses}>{item.label}</DropdownPrimitive.GroupLabel>\n                        <DropdownPrimitive.GroupOptions className=\"m-0 list-none p-0\">\n                            {item.options.map((o) => (\n                                <DropdownRow key={o.value} option={o} />\n                            ))}\n                        </DropdownPrimitive.GroupOptions>\n                    </DropdownPrimitive.Group>\n                ) : (\n                    <DropdownRow key={item.value} option={item} />\n                ),\n            )}\n        </>\n    );\n}\n\n/** Carbon `DropdownSkeleton`: the shape of the field while its data loads (label bar, 8px, the field's bar). Hidden from assistive tech. */\nexport function DropdownSkeleton(props: Readonly<FieldSkeletonProps>) {\n    return <FieldSkeleton {...props} />;\n}\n"
    }
  ]
}
