{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-search",
  "type": "registry:ui",
  "title": "react-tw/search.tsx",
  "description": "Carbon `Search` as a React + Tailwind component, the look of `<ui-search>`: a `role=\"search\"` 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-field",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-skeleton",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-tooltip",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/search.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/search.tsx",
      "content": "import { type ComponentProps, type CSSProperties, type ReactNode } from 'react';\nimport { Search as SearchPrimitive } from '@purpurabog/ui/headless';\nimport { fluidBoxClasses, fluidControlClasses, fluidLabelClasses, useFluid, useFieldDisabled, fieldBoxClasses, fieldDisabledState, fieldTextSize } from './field.tsx';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\nimport { FieldSkeleton, type FieldSkeletonProps } from './skeleton.tsx';\nimport { Tooltip } from './tooltip.tsx';\n\n/**\n * Carbon `Search` as a React + Tailwind component, the look of `<ui-search>`: a `role=\"search\"`\n * landmark with a visually hidden label, a magnifier, an `<input type=\"search\">` and a clear\n * button that appears once there is text (reachable with Tab). Esc clears while there is text and\n * keeps focus, as in Carbon. Heights sm 32 / md 40 (default) / lg 48; the input is padded by one\n * field height on each side and the clear button is a square of the field height at the end.\n * Clearing fires `onChange` with the emptied input (a real `input` event) and then `onClear`.\n * `fluid` (or a `FluidForm` around it) is Carbon `FluidSearch`: a 64px box with the label shown\n * inside, the magnifier at the end and the clear button before it. The behaviour is the headless Search.\n */\nexport interface SearchProps extends Omit<ComponentProps<'input'>, 'size' | 'type'> {\n    /** Accessible name (visually hidden, as in Carbon); default the translation's \"Search\". */\n    label?: ReactNode;\n    size?: 'sm' | 'md' | 'lg';\n    /** After the clear button or Esc emptied the field. */\n    onClear?: () => void;\n    /** Name of the clear button (default: the translation's \"Clear search input\"). */\n    clearLabel?: string;\n    /** Language of the built-in strings; defaults to `<html lang>`. */\n    lang?: string;\n    /** Classes for the `<input>` (the table toolbar paints it on the layer). */\n    inputClassName?: string;\n    /** Carbon `FluidSearch` (also set by `FluidForm`). */\n    fluid?: boolean;\n    /** Carbon `ExpandableSearch`: collapsed to its magnifier until opened (see `ExpandableSearch`). */\n    expandable?: boolean;\n    /** Whether an expandable search is open (controlled). */\n    expanded?: boolean;\n    defaultExpanded?: boolean;\n    onExpandedChange?: (expanded: boolean) => void;\n}\n\nconst HEIGHT = { sm: 'var(--ui-field-height-sm, 32px)', md: 'var(--ui-field-height-md, 40px)', lg: 'var(--ui-field-height-lg, 48px)' } as const;\n\nconst magnifierPath = <path d=\"M15 14.3 10.7 10a5.5 5.5 0 1 0-.7.7l4.3 4.3.7-.7ZM2 6.5a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0Z\" />;\n\nconst closeIcon = (\n    <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\">\n        <path d=\"m12 4.7-.7-.7L8 7.3 4.7 4l-.7.7L7.3 8 4 11.3l.7.7L8 8.7l3.3 3.3.7-.7L8.7 8z\" />\n    </svg>\n);\n\n/** The plain search's magnifier: decorative, at the start (fluid: at the end). */\n/**\n * Carbon's search-input motion: background and outline at fast-02 (110ms); an expandable one moves only its padding in\n * (fast-01) while it opens, and nothing while collapsed (Carbon: transform, which the box never changes).\n */\nfunction inputTransition(expandable: boolean | undefined, open: boolean) {\n    if (!expandable) return 'transition-[background-color,outline-color] duration-(--ui-duration-moderate) ease-carbon';\n    return open ? 'transition-[padding] duration-(--ui-duration-fast) ease-carbon' : 'transition-none';\n}\n\nfunction Magnifier({ fluid }: Readonly<{ fluid: boolean }>) {\n    return (\n        <svg\n            viewBox=\"0 0 16 16\"\n            aria-hidden=\"true\"\n            className={cn('pointer-events-none absolute start-[calc((var(--_h)-16px)/2)] z-1 size-4 fill-ink-muted peer-disabled:fill-ink-disabled', fluid && 'start-auto end-4 top-[35px]')}\n        >\n            {magnifierPath}\n        </svg>\n    );\n}\n\n/** An expandable search's magnifier: a button (Carbon draws a div with role=button) named by the label, with a tooltip while collapsed. */\nfunction ExpandMagnifier({ name }: Readonly<{ name: string }>) {\n    const { open, disabled } = SearchPrimitive.useSearchContext();\n    return (\n        <Tooltip text={name} asLabel placement=\"top\" className={cn('absolute start-0 top-0 z-1', (open || disabled) && 'pointer-events-none')}>\n            <SearchPrimitive.Trigger\n                className={cn(\n                    'm-0 box-border flex size-(--_h) cursor-pointer items-center justify-center rounded-[var(--ui-icon-control-radius,0px)] border-0 bg-transparent p-0 outline-[length:var(--ui-focus-width,2px)] -outline-offset-2 outline-solid outline-transparent hover:bg-canvas-hover focus:outline-focus focus:not-focus-visible:outline-[color:var(--ui-pointer-focus,var(--ui-focus))]',\n                    'disabled:cursor-not-allowed disabled:hover:bg-transparent',\n                )}\n            >\n                <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\" className={cn('size-4 fill-icon-primary', open && 'fill-icon-secondary', disabled && 'fill-icon-disabled')}>\n                    {magnifierPath}\n                </svg>\n            </SearchPrimitive.Trigger>\n        </Tooltip>\n    );\n}\n\nexport function Search({ id, value, defaultValue, disabled: ownDisabled, readOnly, onClear, expandable = false, expanded, defaultExpanded = false, onExpandedChange, fluid, ...rest }: Readonly<SearchProps>) {\n    const disabled = useFieldDisabled(ownDisabled);\n    const inFluid = useFluid(fluid);\n    return (\n        <SearchPrimitive.Root\n            inputId={id}\n            value={value}\n            defaultValue={defaultValue}\n            disabled={disabled}\n            readOnly={!!readOnly}\n            onClear={onClear}\n            expandable={expandable}\n            expanded={expanded}\n            defaultExpanded={defaultExpanded}\n            onExpandedChange={onExpandedChange}\n        >\n            <SearchField {...rest} fluid={inFluid} expandable={expandable} />\n        </SearchPrimitive.Root>\n    );\n}\n\ntype SearchFieldProps = Omit<SearchProps, 'id' | 'value' | 'defaultValue' | 'disabled' | 'readOnly' | 'onClear' | 'expanded' | 'defaultExpanded' | 'onExpandedChange'> & { fluid: boolean; expandable: boolean };\n\n/** The box, label, input, magnifier and clear button, inside the primitive (they follow its open and empty state). */\nfunction SearchField({ label, size = 'md', clearLabel, lang, className, inputClassName, placeholder, autoComplete = 'off', style, fluid, expandable, ...rest }: Readonly<SearchFieldProps>) {\n    const t = useTranslation(lang).field;\n    const { open, clearable, readOnly } = SearchPrimitive.useSearchContext();\n    // read-only: nothing to clear\n    const empty = !clearable;\n    const magnifierName = typeof label === 'string' ? label : t.search;\n\n    return (\n        <div\n            className={cn('block font-sans', fluid && fluidBoxClasses(undefined, readOnly), expandable && 'transition-[width] duration-(--ui-duration-fast) ease-carbon motion-reduce:transition-none', !open && 'w-(--_h)', className)}\n            style={{ '--_h': fluid ? '64px' : HEIGHT[size], ...style } as CSSProperties}\n        >\n            <SearchPrimitive.Landmark className=\"relative flex w-full items-center\">\n                <SearchPrimitive.Label className={fluid ? cn('mb-2 inline-block type-caption text-ink-muted', fluidLabelClasses) : 'sr-only'}>{label ?? t.search}</SearchPrimitive.Label>\n                <SearchPrimitive.Input\n                    {...rest}\n                    placeholder={placeholder || t.search}\n                    autoComplete={autoComplete}\n                    className={cn(\n                        'peer m-0 box-border h-(--_h) w-full appearance-none text-ellipsis px-(--_h) py-0',\n                        fieldBoxClasses,\n                        'text-ink type-body-sm',\n                        fieldTextSize,\n                        'outline-[length:var(--ui-field-focus-width,2px)] [outline-offset:calc(-1*var(--ui-field-focus-width,2px))] outline-transparent',\n                        inputTransition(expandable, open),\n                        'placeholder:text-ink-placeholder placeholder:opacity-100 focus:outline-focus focus:[box-shadow:var(--ui-field-focus-halo,none)]',\n                        '[&::-webkit-search-cancel-button]:hidden [&::-webkit-search-decoration]:hidden [&::-webkit-search-results-button]:hidden [&::-webkit-search-results-decoration]:hidden',\n                        'disabled:cursor-not-allowed disabled:text-ink-disabled disabled:placeholder:text-ink-disabled',\n                        fieldDisabledState,\n                        'forced-colors:border forced-colors:border-[FieldText] forced-colors:focus:outline-[Highlight]',\n                        // fluid: the label above the text, the magnifier and the clear button at the end\n                        fluid && cn('pt-8 pe-[88px] pb-[13px] ps-4', fluidControlClasses(undefined)),\n                        // expandable: no box while collapsed, the placeholder fades in when open\n                        expandable && 'placeholder:transition-opacity placeholder:duration-(--ui-duration-fast) placeholder:ease-carbon',\n                        !open && 'border-0 bg-transparent p-0 placeholder:opacity-0',\n                        inputClassName,\n                    )}\n                />\n                {expandable ? <ExpandMagnifier name={magnifierName} /> : <Magnifier fluid={fluid} />}\n                <SearchPrimitive.Clear\n                    aria-label={clearLabel ?? t.clearSearch}\n                    title={clearLabel ?? t.clearSearch}\n                    className={cn(\n                        'absolute inset-y-0 end-0 m-0 flex w-(--_h) cursor-pointer items-center justify-center rounded-[var(--ui-icon-control-radius,0px)] border border-solid border-transparent bg-transparent p-0 text-ink',\n                        // no transition: Carbon's search-close appears and tints at once\n                        'hover:bg-ghost-hover active:bg-ghost-active',\n                        // Carbon: the field focused (it keeps focus while × is pressed), a hovered or pressed × keeps the field's\n                        // 2px focus line — its fill drawn inside it, 2px in from the top, bottom and end, under the icon\n                        \"[&_svg]:relative [&_svg]:z-[1] after:pointer-events-none after:absolute after:inset-y-0.5 after:start-0 after:end-0.5 after:content-['']\",\n                        '[input:focus~&]:hover:bg-transparent [input:focus~&]:active:bg-transparent [input:focus~&]:hover:after:bg-ghost-hover [input:focus~&]:active:after:bg-ghost-active',\n                        'outline-none focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:outline-solid focus-visible:-outline-offset-2 focus-visible:outline-focus [&_svg]:size-4 [&_svg]:fill-current',\n                        'disabled:cursor-not-allowed disabled:bg-transparent disabled:text-ink-disabled',\n                        empty && 'invisible opacity-0',\n                        fluid && 'top-6 bottom-auto end-12 size-10',\n                    )}\n                >\n                    {closeIcon}\n                </SearchPrimitive.Clear>\n            </SearchPrimitive.Landmark>\n        </div>\n    );\n}\n\n/** Carbon `ExpandableSearch`: a `Search` collapsed to its magnifier until clicked or Enter / Space (`expandable`). */\nexport function ExpandableSearch(props: Readonly<Omit<SearchProps, 'expandable'>>) {\n    return <Search {...props} expandable />;\n}\n\n/** Carbon `SearchSkeleton`: one bar at the field's height (sm 32 / md 40 / lg 48px). Hidden from assistive tech. */\nexport function SearchSkeleton(props: Readonly<Omit<FieldSkeletonProps, 'hideLabel'>>) {\n    // fluid (Carbon FluidSearchSkeleton): the fluid box, which always has both bars\n    return <FieldSkeleton {...props} hideLabel />;\n}\n"
    }
  ]
}
