{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "copy-button",
  "type": "registry:ui",
  "title": "<ui-copy-button>",
  "description": "Carbon `CopyButton`: a 40px (`size=\"sm\"`: 32px) layer-coloured icon button, named by an icon tooltip, that copies `text` and shows `feedback` (\"Copied!\") in that tooltip for `feedback-timeout` ms. Fires `ui-copy` (cancelable: `preventDefault()` to copy yourself). 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": [
    "lit@^3.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/shared-align",
    "@kanok-ui/shared-element",
    "@kanok-ui/shared-events",
    "@kanok-ui/shared-field",
    "@kanok-ui/shared-localize",
    "@kanok-ui/tooltip",
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/copy-button/copy-button.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/copy-button/copy-button.ts",
      "content": "import { LitElement, html, css } from 'lit';\nimport { customElement, property, query } from 'lit/decorators.js';\nimport type { TooltipPlacement, UiTooltip } from '../tooltip/tooltip.ts';\nimport '../tooltip/tooltip.ts';\nimport { LocalizeController } from '../shared/localize.ts';\nimport { srOnlyStyles } from '../shared/field.ts';\nimport type { CopyDetail } from '../shared/events.ts';\nimport { WithAlign } from '../shared/align.ts';\nimport { UiElement } from '../shared/element.ts';\n\nexport const copyIcon = html`<svg viewBox=\"0 0 16 16\" aria-hidden=\"true\"><path d=\"M14 5v9H5V5h9m0-1H5a1 1 0 0 0-1 1v9a1 1 0 0 0 1 1h9a1 1 0 0 0 1-1V5a1 1 0 0 0-1-1Z\" /><path d=\"M2 9H1V2a1 1 0 0 1 1-1h7v1H2Z\" /></svg>`;\n\n/** Clipboard write with the `execCommand` fallback for insecure contexts; resolves to whether it worked. */\nexport async function copyToClipboard(text: string): Promise<boolean> {\n    try {\n        await navigator.clipboard.writeText(text);\n        return true;\n    } catch {\n        const area = document.createElement('textarea');\n        area.value = text;\n        area.setAttribute('readonly', '');\n        area.style.position = 'fixed';\n        area.style.opacity = '0';\n        document.body.append(area);\n        area.select();\n        let ok = false;\n        try {\n            ok = document.execCommand('copy'); // NOSONAR S1874: fallback where navigator.clipboard is unavailable (plain-http intranet)\n        } catch {\n            ok = false;\n        }\n        area.remove();\n        return ok;\n    }\n}\n\n/**\n * Carbon `CopyButton`: a 40px (`size=\"sm\"`: 32px) layer-coloured icon button, named by an\n * icon tooltip, that copies `text` and shows `feedback` (\"Copied!\") in that tooltip for\n * `feedback-timeout` ms. Fires `ui-copy` (cancelable: `preventDefault()` to copy yourself).\n *\n * @fires ui-copy - detail: { text }; cancelable\n * @csspart button - The native button\n * @cssprop --ui-copy-button-background - Its fill at rest (default `--ui-surface-1`; Kanok: transparent); `--ui-copy-button-radius` its corners (default 0; Kanok: 6px)\n * @attr align - Carbon's name for `placement` (same values)\n * @cssprop --ui-focus-offset - `outline-offset` of its focus ring (default -2px: inside; Kanok: 2px outside)\n */\n@customElement('ui-copy-button')\nexport class UiCopyButton extends WithAlign(UiElement) {\n    /** `el.focus()` lands on the control inside. */\n    static override readonly shadowRootOptions: ShadowRootInit = { ...LitElement.shadowRootOptions, delegatesFocus: true };\n\n    static override readonly styles = [srOnlyStyles, css`\n        :host {\n            display: inline-block;\n        }\n\n        .copy {\n            display: flex;\n            align-items: center;\n            justify-content: center;\n            inline-size: var(--_s, 40px);\n            block-size: var(--_s, 40px);\n            padding: 0;\n            border: none;\n            /* Carbon: a layer-coloured square; a design can round it and leave it clear until hovered */\n            border-radius: var(--ui-copy-button-radius, 0);\n            background-color: var(--ui-copy-button-background, var(--ui-surface-1));\n            color: var(--ui-icon-primary);\n            cursor: pointer;\n            outline: none;\n            /* Carbon .cds--btn: the fill and the ring change in fast-01 with the entrance easing */\n            transition:\n                background-color var(--ui-duration-fast) var(--ui-easing-entrance),\n                outline var(--ui-duration-fast) var(--ui-easing-entrance);\n        }\n\n        :host([size='sm']) .copy {\n            --_s: 32px;\n        }\n\n        .copy:hover {\n            background-color: var(--ui-ghost-hover);\n        }\n\n        .copy:active {\n            background-color: var(--ui-ghost-active);\n        }\n\n        .copy:focus-visible {\n            outline: var(--ui-focus-width, 2px) solid var(--ui-focus);\n            outline-offset: var(--ui-focus-offset, -2px);\n        }\n\n        .copy:disabled {\n            background-color: var(--ui-copy-button-background, var(--ui-surface-1));\n            color: var(--ui-icon-disabled);\n            cursor: not-allowed;\n        }\n\n        svg {\n            width: 16px;\n            height: 16px;\n            fill: currentColor;\n            transition: fill var(--ui-duration-fast) var(--ui-easing);\n        }\n\n        @media (forced-colors: active) {\n            .copy {\n                border: 1px solid CanvasText;\n                color: CanvasText;\n            }\n\n            .copy:disabled {\n                color: GrayText;\n            }\n        }\n\n    `];\n\n    /** What gets copied. */\n    @property() text = '';\n\n    private readonly localize = new LocalizeController(this);\n\n    /** Accessible name / tooltip at rest. */\n    @property() label = '';\n\n    /** Tooltip text after copying. */\n    @property() feedback = '';\n\n    @property({ type: Number, attribute: 'feedback-timeout' }) feedbackTimeout = 2000;\n\n    @property() placement: TooltipPlacement = 'bottom';\n\n    @property({ reflect: true }) size: 'sm' | 'md' = 'md';\n\n    @property({ type: Boolean, reflect: true }) disabled = false;\n\n    /** True while the feedback is showing. */\n    @property({ type: Boolean, reflect: true, attribute: 'data-copied' }) copied = false;\n\n    @query('ui-tooltip') private readonly tooltip!: UiTooltip;\n\n    private timer?: number;\n\n    override disconnectedCallback() {\n        super.disconnectedCallback();\n        clearTimeout(this.timer);\n    }\n\n    private async onClick() {\n        const event = new CustomEvent('ui-copy', { detail: { text: this.text }, bubbles: true, composed: true, cancelable: true });\n        // a page that handles `ui-copy` itself gets the feedback; our own copy only says \"Copied!\" when it worked\n        if (!this.dispatchEvent(event) || (await copyToClipboard(this.text))) this.showFeedback();\n    }\n\n    /** Show the \"Copied!\" tooltip for `feedback-timeout` ms (Carbon `Copy` fade-in → fade-out). */\n    showFeedback() {\n        clearTimeout(this.timer);\n        this.copied = true;\n        this.tooltip.pinned = true;\n        this.tooltip.open = true;\n        this.timer = globalThis.setTimeout(() => {\n            this.copied = false;\n            this.tooltip.pinned = false;\n            this.tooltip.open = false;\n        }, this.feedbackTimeout);\n    }\n\n    override render() {\n        return html`\n            <ui-tooltip as-label text=${this.copied ? (this.feedback || this.localize.t.copy.copied) : (this.label || this.localize.t.copy.copy)} placement=${this.placement}>\n                <button class=\"copy\" part=\"button\" type=\"button\" tabindex=\"0\" ?disabled=${this.disabled} @click=${this.onClick}>${copyIcon}</button>\n            </ui-tooltip>\n            <span class=\"sr-only\" role=\"status\">${this.copied ? (this.feedback || this.localize.t.copy.copied) : ''}</span>\n        `;\n    }\n}\n\nexport interface UiCopyButtonEventMap extends HTMLElementEventMap {\n    'ui-copy': CustomEvent<CopyDetail>;\n}\n\n/** Typed listeners for UiCopyButton's events (declaration merging with the class). */\nexport interface UiCopyButton {\n    addEventListener<K extends keyof UiCopyButtonEventMap>(type: K, listener: (this: UiCopyButton, event: UiCopyButtonEventMap[K]) => unknown, options?: boolean | AddEventListenerOptions): void;\n    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;\n    removeEventListener<K extends keyof UiCopyButtonEventMap>(type: K, listener: (this: UiCopyButton, event: UiCopyButtonEventMap[K]) => unknown, options?: boolean | EventListenerOptions): void;\n    removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-copy-button': UiCopyButton;\n    }\n}\n"
    }
  ]
}
