{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-button",
  "type": "registry:ui",
  "title": "react-tw/button.tsx",
  "description": "Carbon `Button` as a React + Tailwind component (shadcn style: copy it, change the classes). 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": [
    "class-variance-authority@0.7.1"
  ],
  "registryDependencies": [
    "@kanok-ui/core-button",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/button.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/button.tsx",
      "content": "import { useId, type ComponentProps, type MouseEvent, type ReactNode, type Ref } from 'react';\nimport { cva, type VariantProps } from 'class-variance-authority';\nimport { busyPlace, type ButtonLoadingPosition } from '../core/button.ts';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\n\n/**\n * Carbon `Button` as a React + Tailwind component (shadcn style: copy it, change the classes).\n * Looks and behaves like `<ui-button>`: same kinds, sizes and states, Carbon's focus ring on\n * `:focus` (a click shows it too), 63px of room at the end, `href` renders a link. On xl (64px) and 2xl (80px)\n * the label and its icon stay at the top, as Carbon's padding stops growing at lg's 14px.\n */\nexport const buttonVariants = cva(\n    [\n        // the shape follows the button settings (--ui-button-*; Kanok sets them, these fallbacks are Carbon's; ui-button-layout in theme.css)\n        'box-border inline-flex ui-button-layout gap-[var(--ui-button-gap,32px)] align-middle',\n        // --_ui-button-pad: the padding of a size that has its own (xl / 2xl, when a design sets one)\n        'm-0 min-h-[var(--ui-button-height-lg,var(--ui-field-height-lg))] py-0 ps-[var(--_ui-button-pad,var(--ui-button-padding-start,15px))] pe-[var(--_ui-button-pad,var(--ui-button-padding-end,63px))]',\n        'rounded-[var(--ui-button-radius,0px)] border border-solid border-transparent',\n        'type-button text-start no-underline cursor-pointer',\n        // Carbon button-base: background, box-shadow, border-color, outline at fast-01, entrance easing (the text colour switches at once)\n        'transition-[background-color,box-shadow,border-color,outline] duration-(--ui-duration-fast) ease-(--ui-easing-entrance)',\n        // Carbon focus: 2px focus ring + 1px focus-inset line inside it (the inset starts inside the 1px border)\n        'focus:outline-[length:var(--ui-focus-width,2px)] focus:[outline-offset:var(--ui-focus-offset,-2px)] focus:outline-focus focus:not-focus-visible:outline-[color:var(--ui-pointer-focus,var(--ui-focus))] focus:shadow-[inset_0_0_0_var(--ui-focus-inset-width,2px)_var(--ui-focus-inset)]',\n        // an icon takes the text colour of the kind (Carbon btn__icon); not the spinner, whose circles are strokes\n        '[&_svg]:size-4 [&_svg]:shrink-0 [&_svg:not(.animate-spin)]:fill-current',\n        // a toggle button (aria-pressed) that is on: Carbon btn--selected, background-selected\n        'aria-pressed:bg-canvas-selected aria-pressed:text-ink',\n        // disabled (Carbon button mixin): button-disabled fill and border, text-on-color-disabled\n        'data-disabled:cursor-not-allowed data-disabled:border-button-disabled data-disabled:bg-button-disabled data-disabled:text-on-color-disabled data-disabled:shadow-none',\n        'forced-colors:border-[ButtonText] forced-colors:focus:outline-[Highlight]',\n    ],\n    {\n        variants: {\n            kind: {\n                primary: 'bg-primary text-on-primary hover:bg-blue-hover active:bg-blue-80',\n                secondary: [\n                    'border-[var(--ui-button-secondary-border,transparent)] bg-[var(--ui-button-secondary,var(--ui-secondary))] text-[var(--ui-on-button-secondary,var(--ui-on-primary))]',\n                    'hover:bg-[var(--ui-button-secondary-hover,var(--ui-secondary-hover))] active:bg-[var(--ui-button-secondary-active,var(--ui-secondary-active))]',\n                ],\n                tertiary: [\n                    'border-[var(--ui-button-tertiary-border,var(--ui-primary))] [border-width:var(--ui-button-tertiary-border-width,1px)] bg-transparent text-[var(--ui-button-tertiary-ink,var(--ui-primary))]',\n                    // --ui-button-tertiary-hover / -active / -focus (and -state-ink): a design can tint it instead of filling it as a primary\n                    'hover:bg-[var(--ui-button-tertiary-hover,var(--ui-blue-hover))] hover:text-[var(--ui-button-tertiary-state-ink,var(--ui-on-primary))] focus:bg-[var(--ui-button-tertiary-focus,var(--ui-primary))] focus:text-[var(--ui-button-tertiary-state-ink,var(--ui-on-primary))]',\n                    'active:border-[var(--ui-button-tertiary-active-border,var(--ui-blue-80))] active:bg-[var(--ui-button-tertiary-active,var(--ui-blue-80))] active:text-[var(--ui-button-tertiary-state-ink,var(--ui-on-primary))]',\n                    'data-disabled:bg-transparent data-disabled:text-ink-disabled',\n                ],\n                ghost: [\n                    'ui-button-ghost-layout gap-(--ui-space-xs) bg-transparent pe-[var(--_ui-button-pad,var(--ui-button-ghost-padding-end,15px))] text-link',\n                    'hover:bg-canvas-hover hover:text-blue-60 active:bg-canvas-active',\n                    'data-disabled:border-transparent data-disabled:bg-transparent data-disabled:text-ink-disabled',\n                ],\n                danger: 'bg-button-danger text-on-primary hover:bg-error-hover active:bg-error-active',\n                'danger-tertiary': [\n                    'border-error bg-transparent text-error',\n                    'hover:border-error-hover hover:bg-error-hover hover:text-on-primary',\n                    'focus:border-error-hover focus:bg-error-hover focus:text-on-primary',\n                    'active:border-error-active active:bg-error-active active:text-on-primary',\n                    'data-disabled:bg-transparent data-disabled:text-ink-disabled',\n                ],\n                'danger-ghost': [\n                    'ui-button-ghost-layout gap-(--ui-space-xs) bg-transparent pe-[var(--_ui-button-pad,var(--ui-button-ghost-padding-end,15px))] text-error',\n                    'hover:bg-error-hover hover:text-on-primary active:bg-error-active active:text-on-primary',\n                    'data-disabled:border-transparent data-disabled:bg-transparent data-disabled:text-ink-disabled',\n                ],\n            },\n            size: {\n                sm: 'min-h-field-sm',\n                md: 'min-h-field-md',\n                lg: 'min-h-[var(--ui-button-height-lg,var(--ui-field-height-lg))]',\n                // Carbon 64 / 80px; a design can change them (--ui-button-height-xl / -2xl) and their padding (--ui-button-padding-xl / -2xl)\n                xl: 'min-h-[var(--ui-button-height-xl,4rem)] [--_ui-button-pad:var(--ui-button-padding-xl)]',\n                '2xl': 'min-h-[var(--ui-button-height-2xl,5rem)] [--_ui-button-pad:var(--ui-button-padding-2xl)]',\n            },\n            /** A square the height of the button with the icon centred (Carbon icon-only; name it with `aria-label`). */\n            iconOnly: {\n                true: 'min-w-0 justify-center gap-0 p-0',\n                false: '',\n            },\n            fullWidth: {\n                true: 'flex w-full',\n                false: '',\n            },\n        },\n        compoundVariants: [\n            { iconOnly: true, size: 'sm', className: 'w-field-sm' },\n            { iconOnly: true, size: 'md', className: 'w-field-md' },\n            { iconOnly: true, size: 'lg', className: 'w-[var(--ui-button-height-lg,var(--ui-field-height-lg))]' },\n            { iconOnly: true, size: 'xl', className: 'w-[var(--ui-button-height-xl,4rem)]' },\n            { iconOnly: true, size: '2xl', className: 'w-[var(--ui-button-height-2xl,5rem)]' },\n            // Carbon xl / 2xl: padding-block stops at lg's 14px, so the content stays at the top; the icon (Carbon\n            // btn__icon: the same 14px + 1px margin) sits 1px lower. --ui-button-tall-* let a design centre it (Kanok)\n            {\n                iconOnly: false,\n                size: ['xl', '2xl'],\n                className: '[--_ui-button-align:var(--ui-button-tall-align,flex-start)] pt-[var(--ui-button-tall-padding-top,14px)] [&_svg]:mt-[var(--ui-button-tall-icon-offset,1px)]',\n            },\n            // Carbon btn--icon-only.btn--ghost: icon-primary, and the 2px ring only (no focus-inset line)\n            { iconOnly: true, kind: 'ghost', className: 'text-icon-primary focus:shadow-none' },\n        ],\n        defaultVariants: { kind: 'primary', size: 'lg', iconOnly: false, fullWidth: false },\n    },\n);\n\nexport type ButtonKind = NonNullable<VariantProps<typeof buttonVariants>['kind']>;\nexport type ButtonSize = NonNullable<VariantProps<typeof buttonVariants>['size']>;\n\nexport interface ButtonProps extends Omit<ComponentProps<'button'>, 'type' | 'ref' | 'prefix'> {\n    /** The `<button>`, or the `<a>` with `href`. */\n    ref?: Ref<HTMLElement>;\n    kind?: ButtonKind;\n    size?: ButtonSize;\n    /** Native button type; `button` by default (as Carbon), so a button in a form doesn't submit it by accident. */\n    type?: 'button' | 'submit' | 'reset';\n    /** Square, icon only (`icon` or the children); give it an `aria-label`. */\n    iconOnly?: boolean;\n    /** Stretch to the container width. */\n    fullWidth?: boolean;\n    /** Disabled but still focusable (aria-disabled, clicks ignored), so a tooltip can say why. */\n    disabledFocusable?: boolean;\n    /**\n     * Busy, as `<ui-button loading>`: looks disabled with Carbon's small spinner and never gets narrower; unlike Carbon\n     * it keeps focus (aria-disabled), ignores clicks and announces `loadingLabel` (else `loadingText`). Pass it from\n     * the start (`loading={busy}`) so the announcement's live region is there before it changes.\n     */\n    loading?: boolean;\n    /** What `loading` announces (default: `loadingText`, else the translation's \"Loading\"). */\n    loadingLabel?: string;\n    /**\n     * Where the spinner goes (MUI `loadingPosition`): `end` (default; Carbon for IBM Products ActionSet) where the\n     * trailing icon goes, the label stays; `start` in the label's place (Carbon Modal `loadingStatus`); `center` hides\n     * the content and centres it. Ghost kinds keep no room at the end, so `end` centres there; icon-only always centres.\n     */\n    loadingPosition?: ButtonLoadingPosition;\n    /** Shown in the label's place while loading, e.g. \"Saving…\" (Carbon Modal `loadingDescription`); the button grows only if it is longer. */\n    loadingText?: string;\n    /**\n     * Carbon `dangerDescription`: read after the name of a danger kind (danger, danger-tertiary, danger-ghost), e.g.\n     * \"Deletes the contract for good\". None by default, as Carbon v11.\n     */\n    dangerDescription?: string;\n    /** Trailing 16px icon (Carbon `renderIcon`); 16px from the end (ghost kinds keep their 15px), the label keeps its 32px gap. */\n    icon?: ReactNode;\n    /** Leading 16px icon before the label, the button's gap away (Lit slot `prefix`); not shown when `iconOnly`. */\n    prefix?: ReactNode;\n    /** Render a link that looks like the button (dropped while disabled, as `<ui-button href>`); `ref` then stays unset. */\n    href?: string;\n    target?: string;\n    rel?: string;\n}\n\n/** Ghost kinds keep their 15px; the others leave 16px after a trailing icon (the label keeps its 32px gap). */\nconst trailingIconPadding = (icon: ReactNode, iconOnly: boolean | undefined, kind: ButtonProps['kind']) => !!icon && !iconOnly && kind !== 'ghost' && kind !== 'danger-ghost';\n\n/** The small Loading spinner of Carbon's InlineLoading: a layer-accent track, an interactive arc, 690ms a turn. */\nfunction Spinner() {\n    return (\n        <svg viewBox=\"0 0 100 100\" className=\"size-4 animate-spin fill-transparent [animation-duration:690ms] motion-reduce:[animation-duration:2s]\">\n            <circle cx=\"50%\" cy=\"50%\" r=\"42\" className=\"stroke-layer-accent [stroke-dasharray:276.46_276.46] [stroke-dashoffset:-22] [stroke-linecap:butt] [stroke-width:16] forced-colors:stroke-[GrayText]\" />\n            <circle cx=\"50%\" cy=\"50%\" r=\"42\" className=\"stroke-interactive [stroke-dasharray:276.46_276.46] [stroke-dashoffset:143.76] [stroke-linecap:butt] [stroke-width:16] forced-colors:stroke-[CanvasText]\" />\n        </svg>\n    );\n}\n\ninterface BusyLook {\n    place: ButtonLoadingPosition;\n    /** xl / 2xl: the content at the top, 14px down (see the size variants). */\n    top?: boolean;\n    iconOnly?: boolean;\n    icon: ReactNode;\n    prefix: ReactNode;\n    text?: string;\n    children: ReactNode;\n}\n\n/** xl / 2xl: where the content sits (see the size variants), for the spinner's place and the label's cell. */\nconst tallLabel = { alignItems: 'var(--ui-button-tall-align, flex-start)' };\nconst tallMark = { ...tallLabel, paddingTop: 'var(--ui-button-tall-padding-top, 14px)' };\n\n/** Where the spinner on its own sits: the trailing icon's place (16px inside the padding box), the prefix icon's, or centred. */\nconst MARK_PLACE: Record<ButtonLoadingPosition, string> = { end: 'end-4', start: 'start-[var(--ui-button-padding-start,15px)]', center: 'inset-x-0 justify-center' };\n\n/** The spinner on its own, over the icon it replaces (or centred, with the loading text). */\nfunction BusyMark({ place, top, text }: Readonly<{ place: ButtonLoadingPosition; top?: boolean; text?: string }>) {\n    return (\n        <span aria-hidden=\"true\" className={cn('pointer-events-none absolute inset-y-0 box-border flex items-center', MARK_PLACE[place])} style={top ? tallMark : undefined}>\n            <span className=\"inline-flex items-center gap-2\">\n                <Spinner />\n                {place === 'center' && text ? <span>{text}</span> : null}\n            </span>\n        </span>\n    );\n}\n\n/** What a loading button hides but keeps the room of. */\nconst hidden = (node: ReactNode) => <span className=\"invisible contents\">{node}</span>;\n\n/** The prefix icon while loading: shown at `end`; elsewhere see-through, the spinner or the centred mark over it. */\nfunction busyPrefix(place: ButtonLoadingPosition, prefix: ReactNode) {\n    if (!prefix) return null;\n    return place === 'end' ? prefix : hidden(prefix);\n}\n\n/**\n * A loading button's content, as `<ui-button loading>`: the label and the loading text (with the spinner, at `start`\n * without a prefix icon) share one grid cell, so the button keeps the wider of the two; what is hidden is see-through\n * (the label stays the button's name); icons the spinner replaces keep their room (at `start` with a prefix icon, the\n * spinner takes that icon's place).\n */\nfunction BusyContent({ place, top, iconOnly, icon, prefix, text, children }: Readonly<BusyLook>) {\n    if (iconOnly)\n        return (\n            <>\n                {hidden(icon ?? children)}\n                <BusyMark place={place} top={top} text={text} />\n            </>\n        );\n    // start without a prefix icon: the spinner takes the label's place (Carbon Modal's InlineLoading)\n    const inline = place === 'start' && !prefix;\n    const withSpinner = inline || place === 'center';\n    const replaced = withSpinner || !!text;\n    return (\n        <>\n            {busyPrefix(place, prefix)}\n            <span className=\"inline-grid items-center [&>*]:[grid-area:1/1]\" style={top ? tallLabel : undefined}>\n                <span className={cn(replaced && 'opacity-0')}>{children}</span>\n                {replaced ? (\n                    // Carbon InlineLoading: 8px between the spinner and its text\n                    <span aria-hidden=\"true\" className={cn('inline-flex items-center gap-2', place === 'center' && 'opacity-0')}>\n                        {withSpinner ? <Spinner /> : null}\n                        {text ? <span>{text}</span> : null}\n                    </span>\n                ) : null}\n            </span>\n            {place === 'start' ? icon : hidden(icon)}\n            {inline ? null : <BusyMark place={place} top={top} text={text} />}\n        </>\n    );\n}\n\n/** The prefix icon, the label, then the icon; an icon-only button shows just the icon (`icon`, else the children). */\nfunction buttonContent(iconOnly: boolean | undefined, icon: ReactNode, prefix: ReactNode, children: ReactNode) {\n    if (iconOnly) return <>{icon ?? children}</>;\n    return (\n        <>\n            {prefix}\n            {children}\n            {icon}\n        </>\n    );\n}\n\ninterface LinkLook {\n    href: string;\n    target?: string;\n    rel?: string;\n    disabled?: boolean;\n    disabledFocusable?: boolean;\n    kind?: ButtonKind;\n    className: string;\n    onClick: (event: MouseEvent<HTMLElement>) => void;\n    content: ReactNode;\n    rest: ComponentProps<'a'>;\n    linkRef?: Ref<HTMLAnchorElement>;\n    describedBy?: string;\n}\n\n/** `<ui-button href>`: a link that looks like the button; disabled drops the link. */\nfunction ButtonLink({ href, target, rel, disabled, disabledFocusable, kind, className, onClick, content, rest, linkRef, describedBy }: Readonly<LinkLook>) {\n    const off = disabled || disabledFocusable;\n    const external = target === '_blank';\n    return (\n        <a\n            tabIndex={disabled && !disabledFocusable ? undefined : 0}\n            {...rest}\n            ref={linkRef}\n            className={className}\n            href={off ? undefined : href}\n            target={off ? undefined : target}\n            rel={rel ?? (external ? 'noopener noreferrer' : undefined)}\n            role={disabledFocusable ? 'link' : undefined}\n            aria-describedby={describedBy}\n            aria-disabled={off || undefined}\n            data-disabled={off ? '' : undefined}\n            data-kind={kind ?? 'primary'}\n            onClick={onClick}\n        >\n            {content}\n        </a>\n    );\n}\n\n/**\n * Carbon `dangerDescription`, read only for the danger kinds: its id goes first in `aria-describedby`, then any\n * description of yours (a tooltip's). The text sits beside the control, not inside it as Carbon's: inside, it would\n * also join the button's name.\n */\nfunction useDangerDescription(kind: ButtonKind | null | undefined, text: string | undefined, describedBy: string | undefined) {\n    const id = useId();\n    const danger = !!text && !!kind?.startsWith('danger');\n    return {\n        describedBy: [danger ? id : '', describedBy].filter(Boolean).join(' ') || undefined,\n        node: danger ? (\n            <span id={id} className=\"sr-only\">\n                {text}\n            </span>\n        ) : null,\n    };\n}\n\nexport function Button({ kind, size, type = 'button', iconOnly, fullWidth, disabled, disabledFocusable, loading, loadingLabel, loadingPosition = 'end', loadingText, dangerDescription, icon, prefix, href, target, rel, className, children, onClick, ref, ...rest }: Readonly<ButtonProps>) {\n    const t = useTranslation(rest.lang).common;\n    const danger = useDangerDescription(kind, dangerDescription, rest['aria-describedby']);\n    const off = disabled || disabledFocusable;\n    const place = busyPlace(loadingPosition, kind, iconOnly);\n    // the spinner at the end of a button with no trailing icon: room for it after the label (Carbon's 63px has it)\n    const busyEnd = loading && place === 'end' && !icon && 'pe-[max(var(--_ui-button-pad,var(--ui-button-padding-end,63px)),40px)]';\n    const classes = cn(buttonVariants({ kind, size, iconOnly, fullWidth }), trailingIconPadding(icon, iconOnly, kind) && 'pe-4', busyEnd, loading && 'relative', className);\n    const content = buttonContent(iconOnly, icon, prefix, children);\n    const guard = (event: MouseEvent<HTMLElement>) => {\n        if (disabledFocusable || loading) {\n            event.preventDefault();\n            return;\n        }\n        onClick?.(event as MouseEvent<HTMLButtonElement>);\n    };\n\n    // tabIndex 0 (yours wins) as Carbon's cds-button: without it Safari neither focuses the control on click\n    // (no :focus ring) nor stops on it with Tab\n    if (href !== undefined) {\n        return (\n            <>\n                <ButtonLink href={href} target={target} rel={rel} disabled={disabled} disabledFocusable={disabledFocusable} kind={kind} className={classes} onClick={guard} content={content} rest={rest as ComponentProps<'a'>} linkRef={ref as Ref<HTMLAnchorElement> | undefined} describedBy={danger.describedBy} />\n                {danger.node}\n            </>\n        );\n    }\n\n    const button = (\n        <button\n            tabIndex={0}\n            {...rest}\n            ref={ref as Ref<HTMLButtonElement> | undefined}\n            type={type}\n            className={classes}\n            disabled={disabled}\n            aria-describedby={danger.describedBy}\n            aria-disabled={disabledFocusable || loading || undefined}\n            data-disabled={off || loading ? '' : undefined}\n            data-kind={kind ?? 'primary'}\n            onClick={guard}\n        >\n            {loading ? (\n                <BusyContent place={place} top={!iconOnly && (size === 'xl' || size === '2xl')} iconOnly={iconOnly} icon={icon} prefix={prefix} text={loadingText}>\n                    {children}\n                </BusyContent>\n            ) : (\n                content\n            )}\n        </button>\n    );\n    // the announcement, beside the button as <ui-button>'s (inside it, it would join the button's name)\n    return (\n        <>\n            {button}\n            {danger.node}\n            {loading === undefined ? null : <output className=\"sr-only\">{loading ? loadingLabel || loadingText || t.loading : ''}</output>}\n        </>\n    );\n}\n\n/** Carbon `ButtonSkeleton`: a 150px bar at the button's height (sm 32 / md 40 / lg 48 / xl 64 / 2xl 80px). Hidden from assistive tech. */\nexport function ButtonSkeleton({ size = 'lg', className, ...rest }: Readonly<ComponentProps<'div'> & { size?: ButtonSize }>) {\n    return <div {...rest} aria-hidden=\"true\" className={cn('skeleton inline-block w-[150px] rounded-[var(--ui-button-radius,var(--ui-skeleton-radius))] align-middle', { sm: 'h-field-sm', md: 'h-field-md', lg: 'h-[var(--ui-button-height-lg,var(--ui-field-height-lg))]', xl: 'h-[var(--ui-button-height-xl,4rem)]', '2xl': 'h-[var(--ui-button-height-2xl,5rem)]' }[size], className)} />;\n}\n"
    }
  ]
}
