{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-tag",
  "type": "registry:ui",
  "title": "react-tw/tag.tsx",
  "description": "Carbon tag in its four forms, as React + Tailwind components with the look of `<ui-tag>`: 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-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/tag.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/tag.tsx",
      "content": "import { type CSSProperties, type HTMLAttributes, type MouseEvent, type ReactNode, type Ref, type ComponentProps } from 'react';\nimport { Tag as TagPrimitive } from '@purpurabog/ui/headless';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\n\n/**\n * Carbon tag in its four forms, as React + Tailwind components with the look of `<ui-tag>`:\n * read-only (default), `dismissible` (a round close button, `onDismiss`), `selectable` (a toggle\n * button, `selected`) and `operational` (a button with the colour's border). 12 colours (`type`),\n * sizes sm 18 / md 24 / lg 32px, an optional 16px icon before the text, 208px at most with an\n * ellipsis (the full text in `title`).\n */\nexport type TagType = 'red' | 'magenta' | 'purple' | 'blue' | 'cyan' | 'teal' | 'green' | 'gray' | 'cool-gray' | 'warm-gray' | 'high-contrast' | 'outline';\n\n/** The colour of each type: background, text, hover, border. */\nfunction colours(type: TagType): CSSProperties {\n    // --ui-tag-high-contrast-*: Carbon's inverse block; a design can make it an outlined ink tag\n    if (type === 'high-contrast') return { '--_bg': 'var(--ui-tag-high-contrast,var(--ui-inverse-canvas))', '--_fg': 'var(--ui-tag-high-contrast-ink,var(--ui-inverse-ink))', '--_hover': 'var(--ui-tag-high-contrast-hover,var(--ui-inverse-hover))', '--_border': 'var(--ui-tag-high-contrast-border,var(--ui-inverse-canvas))' } as CSSProperties;\n    if (type === 'outline') return { '--_bg': 'var(--ui-canvas)', '--_fg': 'var(--ui-ink)', '--_hover': 'var(--ui-ghost-hover)', '--_border': 'var(--ui-inverse-canvas)' } as CSSProperties;\n    return { '--_bg': `var(--ui-tag-background-${type})`, '--_fg': `var(--ui-tag-color-${type})`, '--_hover': `var(--ui-tag-hover-${type})`, '--_border': `var(--ui-tag-border-${type})` } as CSSProperties;\n}\n\nconst HEIGHT = { sm: 18, md: 24, lg: 32 } as const;\n\nexport interface TagProps extends Omit<HTMLAttributes<HTMLElement>, 'type' | 'size' | 'children' | 'icon' | 'title' | 'disabled' | 'dismissible' | 'onDismiss' | 'dismissLabel' | 'selectable' | 'selected' | 'defaultSelected' | 'onSelectedChange' | 'operational' | 'onClick' | 'lang' | 'className'> {\n    /** The `<span>`, or the `<button>` of a selectable / operational tag. */\n    ref?: Ref<HTMLElement>;\n    type?: TagType;\n    size?: 'sm' | 'md' | 'lg';\n    /** The tag's text. */\n    children: ReactNode;\n    /** 16px icon before the text. */\n    icon?: ReactNode;\n    /** Full text for the tooltip when it is cut (default: the text itself). */\n    title?: string;\n    disabled?: boolean;\n    /** Show a close button; the tag stays until you remove it in `onDismiss`. */\n    dismissible?: boolean;\n    onDismiss?: () => void;\n    /** Name of the close button; `{text}` is the tag's text (default: the translation's \"Dismiss {text}\"). */\n    dismissLabel?: string;\n    /** A toggle button (aria-pressed). */\n    selectable?: boolean;\n    selected?: boolean;\n    defaultSelected?: boolean;\n    onSelectedChange?: (selected: boolean) => void;\n    /** A button with the colour's border (opens details, filters …). */\n    operational?: boolean;\n    onClick?: (event: MouseEvent<HTMLButtonElement>) => void;\n    lang?: string;\n    className?: string;\n}\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\ninterface TagLook {\n    type: TagType;\n    size: NonNullable<TagProps['size']>;\n    icon: boolean;\n    dismissible: boolean;\n    /** Rendered as a button (selectable or operational). */\n    button: boolean;\n    selectable: boolean;\n    operational: boolean;\n    disabled: boolean;\n}\n\n/** The tag box: the colour's fill (or outline), sizes, and the button / disabled states. */\nfunction tagClasses(look: TagLook, className?: string) {\n    const { type, size, icon, dismissible, button, selectable, operational, disabled } = look;\n    return cn(\n        'm-0 box-border inline-flex min-h-(--_h) max-w-[208px] min-w-8 items-center justify-center rounded-[var(--ui-tag-radius,16px)] border-0 px-2 align-middle',\n        // --ui-tag-radius / -font (ui-tag-type in theme.css): Carbon's pill and caption type; a design can change them\n        'bg-(--_bg) text-(--_fg) ui-tag-type leading-4! cursor-default [overflow-wrap:anywhere]',\n        size === 'lg' && 'px-3',\n        type === 'outline' && 'shadow-[inset_0_0_0_1px_var(--_border)]',\n        type === 'high-contrast' && 'shadow-[var(--ui-tag-high-contrast-edge,none)]',\n        icon && 'ps-1',\n        dismissible && !button && 'pe-0',\n        // no transition: Carbon's selectable and operational tags change colour at once\n        button && 'cursor-pointer font-[inherit] focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:outline-offset-1 focus-visible:outline-focus',\n        // --ui-tag-selectable-border / --ui-tag-selected / -ink: Carbon's inverse outline and fill; a design can use its brand\n        selectable && [\n            'border border-solid border-[var(--ui-tag-selectable-border,var(--ui-border-inverse))] bg-surface-1 text-ink hover:bg-ghost-hover',\n            'aria-pressed:border-[var(--ui-tag-selected,var(--ui-border-inverse))] aria-pressed:bg-[var(--ui-tag-selected,var(--ui-layer-selected-inverse))] aria-pressed:text-[var(--ui-tag-selected-ink,var(--ui-inverse-ink))]',\n            'aria-pressed:hover:bg-[var(--ui-tag-selected,var(--ui-layer-selected-inverse))]',\n            // the chosen tag is a fill, which forced colours drop: the system highlight shows it instead (as <ui-tag>)\n            'forced-colors:aria-pressed:[forced-color-adjust:none] forced-colors:aria-pressed:border-[Highlight] forced-colors:aria-pressed:bg-[Highlight] forced-colors:aria-pressed:text-[HighlightText]',\n        ],\n        operational && 'border border-solid border-(--_border) hover:bg-(--_hover)',\n        disabled && 'cursor-not-allowed bg-surface-1 text-ink-disabled shadow-none hover:bg-surface-1',\n        disabled && button && 'border-ink-disabled',\n        'forced-colors:border forced-colors:border-solid forced-colors:border-[CanvasText]',\n        className,\n    );\n}\n\n/** The 16px × close button of a dismissible tag. */\nfunction DismissButton({ label, highContrast, disabled, onDismiss }: Readonly<{ label: string; highContrast: boolean; disabled: boolean; onDismiss?: () => void }>) {\n    return (\n        <TagPrimitive.Dismiss\n            aria-label={label}\n            disabled={disabled}\n            className={cn(\n                'm-0 ms-[2px] flex size-(--_h) shrink-0 cursor-pointer items-center justify-center rounded-[50%] border-0 bg-transparent p-0 text-current transition-[background-color] duration-(--ui-duration-fast) ease-carbon',\n                'hover:bg-(--_hover) focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:[outline-offset:var(--ui-focus-offset,-2px)] focus-visible:outline-focus [&_svg]:size-4 [&_svg]:fill-current',\n                highContrast && 'focus-visible:outline-[var(--ui-tag-high-contrast-focus,var(--ui-focus-inverse))]',\n                disabled && 'cursor-not-allowed hover:bg-transparent',\n            )}\n            onDismiss={onDismiss}\n        >\n            {closeIcon}\n        </TagPrimitive.Dismiss>\n    );\n}\n\nexport function Tag({ type = 'gray', size = 'md', children, icon, title, disabled, dismissible, onDismiss, dismissLabel, selectable, selected, defaultSelected = false, onSelectedChange, operational, onClick, lang, className, ref, ...rest }: Readonly<TagProps>) {\n    const t = useTranslation(lang).common;\n    const text = typeof children === 'string' ? children : '';\n    const button = !!(selectable || operational);\n    const classes = tagClasses({ type, size, icon: !!icon, dismissible: !!dismissible, button, selectable: !!selectable, operational: !!operational, disabled: !!disabled }, className);\n    const style = { ...colours(type), '--_h': `${HEIGHT[size]}px` } as CSSProperties;\n    const content = (\n        <>\n            {icon ? <span className=\"me-1 inline-flex shrink-0 [&_svg]:size-4 [&_svg]:fill-current\">{icon}</span> : null}\n            <span className=\"block truncate\" title={title ?? (text || undefined)}>\n                {children}\n            </span>\n        </>\n    );\n    if (selectable) {\n        return (\n            <TagPrimitive.Toggle\n                {...rest}\n                ref={ref as Ref<HTMLButtonElement> | undefined}\n                className={classes}\n                style={{ ...rest.style, ...style }}\n                selected={selected}\n                defaultSelected={defaultSelected}\n                disabled={disabled}\n                onSelectedChange={(next) => onSelectedChange?.(next)}\n            >\n                {content}\n            </TagPrimitive.Toggle>\n        );\n    }\n    if (operational) {\n        return (\n            <button {...rest} ref={ref as Ref<HTMLButtonElement> | undefined} type=\"button\" className={classes} style={{ ...rest.style, ...style }} disabled={disabled} onClick={onClick}>\n                {content}\n            </button>\n        );\n    }\n    return (\n        <TagPrimitive.Root {...rest} ref={ref} disabled={disabled} className={classes} style={{ ...rest.style, ...style }}>\n            {content}\n            {dismissible ? <DismissButton label={(dismissLabel ?? t.dismiss).replace('{text}', text)} highContrast={type === 'high-contrast'} disabled={!!disabled} onDismiss={onDismiss} /> : null}\n        </TagPrimitive.Root>\n    );\n}\n\n/** Carbon `TagSkeleton`: a 60px pill at the tag's height (sm 18 / md 24 / lg 32px). Hidden from assistive tech. */\nexport function TagSkeleton({ size = 'md', className, ...rest }: Readonly<ComponentProps<'span'> & { size?: 'sm' | 'md' | 'lg' }>) {\n    return <span {...rest} aria-hidden=\"true\" className={cn('skeleton inline-block w-[60px] rounded-[var(--ui-tag-radius,16px)] align-middle', { sm: 'h-[18px]', md: 'h-6', lg: 'h-8' }[size], className)} />;\n}\n"
    }
  ]
}
