{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "components-tag-input",
  "type": "registry:component",
  "title": "Tag input",
  "description": "Keywords entered through a field, as removable chips",
  "author": "sudo-kw <https://github.com/sudo-kw>",
  "dependencies": [
    "@gpuix/native@0.11.0",
    "@gpuix/react@0.11.0",
    "effect@4.1.2",
    "react@^19.2.4"
  ],
  "registryDependencies": [
    "https://gpuixui.sudokw.com/gpuix-icons.json",
    "https://gpuixui.sudokw.com/gpuix-primitives.json",
    "https://gpuixui.sudokw.com/gpuix-tokens.json",
    "https://gpuixui.sudokw.com/gpuix-tw.json"
  ],
  "files": [
    {
      "path": "packages/components/src/internal/key.ts",
      "target": "@ui/gpuixui/components/internal/key.ts",
      "type": "registry:lib",
      "content": "import type { EventPayload, KeyEvent, Props } from \"@gpuix/react\";\nimport * as Option from \"effect/Option\";\n\nexport type KeyPhase = \"down\" | \"up\";\n\nexport const isActivation = (event: EventPayload, phase: KeyPhase): boolean => {\n  if (event.isHeld === true) return false;\n  if (event.key === \"enter\") return phase === \"down\";\n  return event.key === \"space\" && phase === \"up\";\n};\n\nexport const swallowsDefault = (\n  event: EventPayload,\n  phase: KeyPhase,\n): boolean => event.key === \"space\" && phase === \"down\";\n\nexport const activationHandlers = (\n  activate: (event: KeyEvent) => void,\n  observe: Pick<Props, \"onKeyDown\" | \"onKeyUp\"> = {},\n): Pick<Props, \"onKeyDown\" | \"onKeyUp\"> => ({\n  onKeyDown: (event: KeyEvent) => {\n    if (swallowsDefault(event, \"down\")) event.preventDefault();\n    if (isActivation(event, \"down\")) activate(event);\n    observe.onKeyDown?.(event);\n  },\n  onKeyUp: (event: KeyEvent) => {\n    if (isActivation(event, \"up\")) activate(event);\n    observe.onKeyUp?.(event);\n  },\n});\n\nexport const isEnter = (event: EventPayload): boolean => event.key === \"enter\";\n\nexport const isDown = (event: EventPayload): boolean =>\n  event.key === \"down\" || (event.key === \"n\" && event.modifiers?.ctrl === true);\n\nexport const isUp = (event: EventPayload): boolean =>\n  event.key === \"up\" || (event.key === \"p\" && event.modifiers?.ctrl === true);\n\nexport const isRight = (event: EventPayload): boolean => event.key === \"right\";\n\nexport const isLeft = (event: EventPayload): boolean => event.key === \"left\";\n\nexport const isFirst = (event: EventPayload): boolean => event.key === \"home\";\n\nexport const isLast = (event: EventPayload): boolean => event.key === \"end\";\n\nexport const isPageUp = (event: EventPayload): boolean =>\n  event.key === \"pageup\";\n\nexport const isPageDown = (event: EventPayload): boolean =>\n  event.key === \"pagedown\";\n\nexport const isDismiss = (event: EventPayload): boolean =>\n  event.key === \"escape\";\n\nexport const isTab = (event: EventPayload): boolean => event.key === \"tab\";\n\nexport const isSelectAll = (event: EventPayload): boolean =>\n  event.key === \"a\" &&\n  (event.modifiers?.cmd === true || event.modifiers?.ctrl === true);\n\nexport const isMenuKey = (event: EventPayload): boolean =>\n  (event.key === \"f10\" && event.modifiers?.shift === true) ||\n  event.key === \"contextmenu\";\n\nexport const axisDirection = (\n  event: EventPayload,\n  orientation: \"horizontal\" | \"vertical\",\n): Option.Option<1 | -1> => {\n  if (event.key === \"right\") return Option.some(1);\n  if (event.key === \"left\") return Option.some(-1);\n  if (orientation === \"vertical\") {\n    if (isDown(event)) return Option.some(1);\n    if (isUp(event)) return Option.some(-1);\n  }\n  return Option.none();\n};\n"
    },
    {
      "path": "packages/components/src/internal/parts.ts",
      "target": "@ui/gpuixui/components/internal/parts.ts",
      "type": "registry:lib",
      "content": "import * as Arr from \"effect/Array\";\nimport * as P from \"effect/Predicate\";\nimport {\n  Children,\n  Fragment,\n  isValidElement,\n  type ReactElement,\n  type ReactNode,\n} from \"react\";\n\nexport type PartComponent = (props: never) => ReactNode;\n\nexport type PartChildren =\n  | ReactElement<unknown>\n  | boolean\n  | null\n  | undefined\n  | ReadonlyArray<PartChildren>;\n\nexport interface PartsContract {\n  readonly parent: string;\n\n  readonly family: ReadonlyArray<PartComponent>;\n\n  readonly accepts: ReadonlyArray<PartComponent>;\n\n  readonly hint?: string;\n}\n\nconst componentName = (type: unknown): string =>\n  P.isString(type)\n    ? type\n    : P.isFunction(type)\n      ? type.name || \"anonymous\"\n      : \"element\";\n\nconst fragmentChildren = (element: ReactElement<unknown>): ReactNode =>\n  hasChildren(element.props) ? element.props.children : undefined;\n\nconst hasChildren = (\n  value: unknown,\n): value is { readonly children?: ReactNode } =>\n  P.isObject(value) && \"children\" in value;\n\nconst leaves = (children: ReactNode): ReadonlyArray<unknown> =>\n  Arr.flatMap(Children.toArray(children), (child) =>\n    isValidElement<unknown>(child) && child.type === Fragment\n      ? leaves(fragmentChildren(child))\n      : [child],\n  );\n\nexport const assertParts = (\n  contract: PartsContract,\n  children: ReactNode,\n): void => {\n  Arr.forEach(leaves(children), (leaf) => {\n    if (!isValidElement(leaf)) return;\n    if (Arr.some(contract.accepts, (part) => part === leaf.type)) return;\n    if (!Arr.some(contract.family, (part) => part === leaf.type)) return;\n\n    throw new Error(\n      `${componentName(leaf.type)} cannot be used inside ${contract.parent}.` +\n        (contract.hint === undefined ? \"\" : ` ${contract.hint}`),\n    );\n  });\n};\n"
    },
    {
      "path": "packages/components/src/internal/recipe.ts",
      "target": "@ui/gpuixui/components/internal/recipe.ts",
      "type": "registry:lib",
      "content": "import type { StyleDesc } from \"@gpuix/react\";\nimport { createRecipeCache } from \"../../tw/index.js\";\nimport type { Theme } from \"./theme.js\";\n\nexport const recipeStyle: (recipe: string, theme: Theme) => StyleDesc =\n  createRecipeCache(512);\n"
    },
    {
      "path": "packages/components/src/internal/theme.ts",
      "target": "@ui/gpuixui/components/internal/theme.ts",
      "type": "registry:lib",
      "content": "import type { StyleDesc } from \"@gpuix/react\";\nimport * as Schema from \"effect/Schema\";\nimport {\n  alpha,\n  avatarPixels,\n  avatarSize,\n  brandTextOn,\n  controlFocusRing,\n  controlHeight,\n  controlPaddingX,\n  currentPlatform,\n  darkTheme,\n  disabledAction,\n  disabledOpacity,\n  elevationShadow,\n  filledHover,\n  filledPressed,\n  findHighlight,\n  focusRing,\n  keyboardFocusRing,\n  lightTheme,\n  nativeTextTheme,\n  pointerWash,\n  selectionOn,\n  statusTone,\n  syntaxColors,\n  targetSize,\n  textStyle,\n  type BoxShadow,\n  type ElevationLevel,\n  type FindHighlightSource,\n  type NativeTextFill,\n  type PlatformName,\n  type PointerWashSurface,\n  type StatusTone,\n  type SyntaxColors,\n  type SyntaxRole,\n  type TextRole,\n  type Theme,\n  type ThemeColors,\n} from \"../../tokens/index.js\";\n\nexport {\n  alpha,\n  avatarPixels,\n  avatarSize,\n  controlFocusRing,\n  controlHeight,\n  controlPaddingX,\n  currentPlatform,\n  darkTheme,\n  disabledAction,\n  disabledOpacity,\n  elevationShadow,\n  filledHover,\n  filledPressed,\n  findHighlight,\n  focusRing,\n  keyboardFocusRing,\n  lightTheme,\n  nativeTextTheme,\n  statusTone,\n  syntaxColors,\n  targetSize,\n  textStyle,\n};\nexport type {\n  BoxShadow,\n  ElevationLevel,\n  FindHighlightSource,\n  NativeTextFill,\n  PlatformName,\n  StatusTone,\n  SyntaxColors,\n  SyntaxRole,\n  TextRole,\n  Theme,\n};\nexport { ThemeProvider, useTheme } from \"../../tokens/react.js\";\n\nexport const keyboardRing = (theme: Theme, invalid = false): StyleDesc =>\n  controlFocusRing(theme, { invalid }) ?? {};\n\nexport interface ControlRingState {\n  readonly disabled?: boolean;\n\n  readonly invalid?: boolean;\n}\n\nexport const controlRing = (\n  theme: Theme,\n  state: ControlRingState = {},\n): StyleDesc => controlFocusRing(theme, state) ?? {};\n\nexport const pressWash = (theme: Theme, replaced: PointerWashSurface): string =>\n  pointerWash(theme, \"active\", replaced) ?? theme.colors.active;\n\nexport const selectionWash = (theme: Theme): StyleDesc => ({\n  selectionColor: theme.colors.selection,\n});\n\nexport const keepContent: StyleDesc = { keyboardFocusDim: false };\n\nexport const disabledFade = (\n  disabled: boolean,\n): { readonly opacity: number } | undefined =>\n  disabled ? { opacity: disabledOpacity } : undefined;\n\nconst Tones = Schema.Literals([\"info\", \"success\", \"warning\", \"danger\"]);\n\nexport type Tone = typeof Tones.Type;\n\nexport const toneOf = (value: unknown): Tone =>\n  Schema.is(Tones)(value) ? value : \"info\";\n\nexport interface ToneStyle {\n  readonly fg: string;\n  readonly bg: string;\n  readonly border: string;\n}\n\nconst TOKEN_TONE: Readonly<Record<Tone, StatusTone>> = {\n  info: \"info\",\n  success: \"success\",\n  warning: \"warning\",\n  danger: \"destructive\",\n};\n\nexport const tone = (theme: Theme, value: Tone): ToneStyle => {\n  const { background, border, foreground } = statusTone(\n    theme,\n    TOKEN_TONE[value],\n  );\n  return { bg: background, border, fg: foreground };\n};\n\nconst softRoles: Readonly<Record<Tone, keyof ThemeColors>> = {\n  info: \"infoSoft\",\n  success: \"successSoft\",\n  warning: \"warningSoft\",\n  danger: \"destructiveSoft\",\n};\n\nexport const messageLink = (theme: Theme, value: Tone): string =>\n  brandTextOn(theme, softRoles[value]);\n\nexport const messageSelection = (theme: Theme, value: Tone): string =>\n  selectionOn(theme, softRoles[value]);\n\nexport const toneSurface = (theme: Theme, value: Tone): StyleDesc => {\n  const { bg, border, fg } = tone(theme, value);\n  return {\n    backgroundColor: bg,\n    color: fg,\n    borderWidth: theme.borderWidth.thin,\n    borderColor: border,\n  };\n};\n"
    },
    {
      "path": "packages/components/src/tag-input.tsx",
      "target": "@ui/gpuixui/components/tag-input.tsx",
      "type": "registry:component",
      "content": "/** @jsxImportSource @gpuix/react */\nimport type { KeyEvent, InputProps, Props } from \"@gpuix/react\";\nimport {\n  mergeStyles,\n  resolveStyle,\n  type StateStyle,\n} from \"@gpuix/react/floating\";\nimport { Icon } from \"../icons/index.js\";\nimport {\n  cn,\n  TAG_INPUT_CAPTION,\n  TAG_INPUT_CHIP,\n  TAG_INPUT_CHIP_REMOVE,\n  TAG_INPUT_CHIP_TEXT,\n  TAG_INPUT_FIELD,\n  TAG_INPUT_HINT,\n  TAG_INPUT_ROOT,\n} from \"../tw/index.js\";\nimport * as Arr from \"effect/Array\";\nimport * as Option from \"effect/Option\";\nimport * as Str from \"effect/String\";\nimport { createContext, useContext, useState } from \"react\";\nimport {\n  fieldHelp,\n  fieldHint,\n  hintHost,\n  renderFieldHint,\n} from \"../primitives/components/hint.js\";\nimport { activationHandlers, isEnter } from \"./internal/key.js\";\nimport {\n  assertParts,\n  type PartChildren,\n  type PartComponent,\n  type PartsContract,\n} from \"./internal/parts.js\";\nimport {\n  addTokens,\n  removeToken,\n  splitDraft,\n  tokensOf,\n} from \"./tag-input/logic.js\";\nimport { recipeStyle } from \"./internal/recipe.js\";\nimport {\n  controlHeight,\n  controlRing,\n  disabledFade,\n  keepContent,\n  nativeTextTheme,\n  selectionWash,\n  targetSize,\n  useTheme,\n} from \"./internal/theme.js\";\n\ninterface TagInputContextValue {\n  readonly tokens: ReadonlyArray<string>;\n  readonly draft: string;\n  readonly disabled: boolean;\n  readonly editDraft: (text: string) => void;\n  readonly addDraft: () => void;\n  readonly remove: (value: string) => void;\n}\n\nconst TagInputContext = createContext<TagInputContextValue | undefined>(\n  undefined,\n);\n\nconst useTagInput = (): TagInputContextValue => {\n  const value = useContext(TagInputContext);\n  if (value === undefined) {\n    throw new Error(\"TagInput parts must be used inside TagInput\");\n  }\n  return value;\n};\n\nexport interface TagInputProps extends Omit<Props, \"children\"> {\n  readonly value: ReadonlyArray<string>;\n\n  readonly onValueChange: (value: ReadonlyArray<string>) => void;\n\n  readonly label: string;\n\n  readonly max?: number;\n\n  readonly allowDuplicates?: boolean;\n  readonly disabled?: boolean;\n\n  readonly hint?: string;\n\n  readonly caption?: string;\n\n  readonly className?: string;\n  readonly children?: PartChildren;\n}\n\nexport const TagInput = ({\n  value,\n  onValueChange,\n  label,\n  max,\n  allowDuplicates = false,\n  disabled = false,\n  hint,\n  caption,\n  className,\n  children,\n  style,\n  ...rest\n}: TagInputProps) => {\n  const theme = useTheme();\n  const [draft, setDraft] = useState(\"\");\n  assertParts(tagInputRoot, children);\n\n  const tokens = tokensOf(value);\n\n  const add = (incoming: ReadonlyArray<string>): void => {\n    onValueChange(addTokens(tokens, incoming, { allowDuplicates, max }));\n  };\n\n  const commit = (): void => {\n    if (disabled) return;\n    add(splitDraft(draft));\n    setDraft(\"\");\n  };\n\n  const editDraft = (text: string): void => {\n    if (disabled) return;\n    const segments = Str.split(\",\")(text);\n    if (Arr.length(segments) < 2) {\n      setDraft(text);\n      return;\n    }\n    const last = Option.getOrElse(Arr.last(segments), () => \"\");\n    add(\n      splitDraft(Arr.join(Arr.take(segments, Arr.length(segments) - 1), \",\")),\n    );\n    setDraft(last);\n  };\n\n  const context: TagInputContextValue = {\n    tokens,\n    draft,\n    disabled,\n    editDraft,\n    addDraft: commit,\n    remove: (token) => {\n      if (disabled) return;\n      onValueChange(removeToken(tokens, token));\n    },\n  };\n\n  const note = caption?.trim();\n  const quiet = hint?.trim();\n\n  return (\n    <TagInputContext.Provider value={context}>\n      <div\n        {...rest}\n        style={mergeStyles(\n          recipeStyle(cn(\"flex flex-col gap-1.5\", className), theme),\n          mergeStyles({ ...disabledFade(disabled) }, style),\n        )}\n      >\n        <div\n          role=\"group\"\n          aria-label={label}\n          style={mergeStyles(recipeStyle(TAG_INPUT_ROOT, theme), {\n            minHeight: controlHeight(theme, \"md\"),\n          })}\n        >\n          {children}\n          {quiet === undefined || quiet === \"\" ? null : (\n            <text style={recipeStyle(TAG_INPUT_HINT, theme)}>{quiet}</text>\n          )}\n        </div>\n        {note === undefined || note === \"\" ? null : (\n          <text style={recipeStyle(TAG_INPUT_CAPTION, theme)}>{note}</text>\n        )}\n      </div>\n    </TagInputContext.Provider>\n  );\n};\n\nexport interface TagInputFieldProps extends Omit<\n  InputProps,\n  \"value\" | \"onChange\" | \"onSubmit\" | \"style\"\n> {\n  readonly className?: string;\n  readonly style?: StateStyle<TagInputFieldState>;\n}\n\nexport interface TagInputFieldState {\n  readonly disabled: boolean;\n}\n\nexport const TagInputField = ({\n  className,\n  style,\n  placeholder,\n  onKeyDown,\n  onFocus,\n  onBlur,\n  ...rest\n}: TagInputFieldProps) => {\n  const theme = useTheme();\n  const { draft, disabled, editDraft, addDraft } = useTagInput();\n\n  const fieldStyle = mergeStyles(\n    mergeStyles(recipeStyle(cn(TAG_INPUT_FIELD, className), theme), {\n      height: controlHeight(theme, \"sm\"),\n      ...controlRing(theme, { disabled }),\n      ...selectionWash(theme),\n      ...keepContent,\n    }),\n    resolveStyle(style, { disabled }),\n  );\n  const hint = fieldHint({ placeholder, disabled }, theme, draft, {\n    origin: \"host\",\n    align: \"center\",\n    style: fieldStyle,\n  });\n\n  return (\n    <div style={hintHost({ flexGrow: 1, minWidth: 0 })}>\n      <input\n        {...rest}\n        theme={nativeTextTheme(theme, \"background\")}\n        readOnly={disabled}\n        tabIndex={disabled ? -1 : 0}\n        value={draft}\n        aria-description={fieldHelp(\n          { description: rest[\"aria-description\"], placeholder },\n          draft,\n        )}\n        onChange={(event) => editDraft(event.value ?? \"\")}\n        onKeyDown={(event: KeyEvent) => {\n          onKeyDown?.(event);\n          if (isEnter(event)) addDraft();\n        }}\n        onSubmit={() => addDraft()}\n        onFocus={(event) => onFocus?.(event)}\n        onBlur={(event) => onBlur?.(event)}\n        style={fieldStyle}\n      />\n      {Option.match(hint, {\n        onNone: () => null,\n        onSome: (painted) => renderFieldHint(painted, rest.testId),\n      })}\n    </div>\n  );\n};\n\nexport interface TagInputChipProps extends Omit<Props, \"children\" | \"style\"> {\n  readonly value: string;\n\n  readonly className?: string;\n  readonly style?: StateStyle<TagInputChipState>;\n}\n\nexport interface TagInputChipState {\n  readonly disabled: boolean;\n}\n\nexport const TagInputChip = ({\n  value,\n  className,\n  style,\n  testId,\n  ...rest\n}: TagInputChipProps) => {\n  const theme = useTheme();\n  const { disabled, remove } = useTagInput();\n\n  return (\n    <div\n      {...rest}\n      testId={testId}\n      style={mergeStyles(\n        recipeStyle(cn(TAG_INPUT_CHIP, className), theme),\n        mergeStyles(\n          disabled ? {} : { hover: { backgroundColor: theme.colors.accent } },\n\n          resolveStyle(style, { disabled }),\n        ),\n      )}\n    >\n      <text style={recipeStyle(TAG_INPUT_CHIP_TEXT, theme)}>{value}</text>\n      {disabled ? null : (\n        <div\n          role=\"button\"\n          aria-label={`Remove ${value}`}\n          testId={testId === undefined ? undefined : `${testId}-remove`}\n          tabIndex={0}\n          onClick={() => remove(value)}\n          {...activationHandlers(() => remove(value))}\n          style={mergeStyles(recipeStyle(TAG_INPUT_CHIP_REMOVE, theme), {\n            width: targetSize.minimum,\n            height: targetSize.minimum,\n            flexShrink: 0,\n            ...controlRing(theme),\n            hover: { backgroundColor: theme.colors.accent },\n          })}\n        >\n          <Icon\n            name=\"x\"\n            size=\"sm\"\n            color={\n              disabled\n                ? theme.colors.mutedForeground\n                : theme.colors.secondaryForeground\n            }\n          />\n        </div>\n      )}\n    </div>\n  );\n};\n\nconst tagInputParts: ReadonlyArray<PartComponent> = [\n  TagInput,\n  TagInputField,\n  TagInputChip,\n];\n\nconst tagInputRoot: PartsContract = {\n  parent: \"TagInput\",\n  family: tagInputParts,\n  accepts: [TagInputField, TagInputChip],\n  hint: \"Put TagInputChip and TagInputField directly inside TagInput.\",\n};\n\nexport const tagInputMeta = {\n  name: \"Tag / token input\",\n  tier: 2,\n  status: \"done\",\n  gpuixSupport: \"partial\",\n  limitations: [\n    \"controlled only: the caller owns the tokens and renders chips from that list\",\n    \"no aria-disabled in 0.11.0: disabled is a readOnly field plus dimming\",\n    \"a typed comma commits through onChange because the field reports the text it holds\",\n    \"Backspace on an empty field is not observable in 0.11.0 (no keyDown is emitted), so removal is the chip control\",\n  ],\n  docs: \"docs/tag-input.mdx\",\n  example: \"examples/tag-input.example.tsx\",\n  testFile: \"src/behaviour/tag-input.test.tsx\",\n} as const;\n"
    },
    {
      "path": "packages/components/src/tag-input/logic.ts",
      "target": "@ui/gpuixui/components/tag-input/logic.ts",
      "type": "registry:lib",
      "content": "import * as Arr from \"effect/Array\";\nimport * as Option from \"effect/Option\";\nimport * as P from \"effect/Predicate\";\nimport * as Result from \"effect/Result\";\nimport * as Str from \"effect/String\";\n\nexport interface AddRules {\n  readonly allowDuplicates?: boolean;\n\n  readonly max?: number;\n}\n\nexport const tokensOf = (value: unknown): ReadonlyArray<string> =>\n  Arr.isArray(value) ? Arr.filter(value, P.isString) : [];\n\nexport const splitDraft = (draft: string): ReadonlyArray<string> =>\n  Arr.filterMap(Str.split(\",\")(draft), (part) => {\n    const token = Str.trim(part);\n    return token === \"\" ? Result.failVoid : Result.succeed(token);\n  });\n\nconst capOf = (max: number | undefined): Option.Option<number> =>\n  Option.map(\n    Option.filter(Option.fromNullishOr(max), Number.isFinite),\n    (value) => Math.max(0, Math.floor(value)),\n  );\n\nexport const addTokens = (\n  current: ReadonlyArray<string>,\n  incoming: ReadonlyArray<string>,\n  rules: AddRules = {},\n): ReadonlyArray<string> => {\n  const cap = capOf(rules.max);\n  return Arr.reduce(incoming, current, (tokens, token) => {\n    if (Option.exists(cap, (limit) => Arr.length(tokens) >= limit)) {\n      return tokens;\n    }\n    if (\n      rules.allowDuplicates !== true &&\n      Arr.some(tokens, (existing) => existing === token)\n    ) {\n      return tokens;\n    }\n    return Arr.append(tokens, token);\n  });\n};\n\nexport const removeToken = (\n  current: ReadonlyArray<string>,\n  value: string,\n): ReadonlyArray<string> =>\n  Option.match(\n    Arr.findFirstIndex(current, (token) => token === value),\n    {\n      onNone: () => current,\n      onSome: (index) =>\n        Arr.appendAll(Arr.take(current, index), Arr.drop(current, index + 1)),\n    },\n  );\n"
    }
  ],
  "docs": "[Tag input](https://gpuixui.sudokw.com/components/tag-input) — These are GPUix native components: the files render through GPUix's reconciler against `@gpuix/react`, not the DOM, and need the per-platform `@gpuix/native` binding. The CLI installs the `@gpuixui/*` runtime the components import as registry items, so the copied tree resolves on its own; wrap it in `ThemeProvider` before it renders.",
  "meta": {
    "url": "/components/tag-input",
    "kind": "component",
    "runtime": "gpuix",
    "registry": "gpuixui"
  }
}