{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "toggletip",
  "type": "registry:ui",
  "title": "<ui-toggletip>",
  "description": "Carbon-style toggletip: a click-to-open bubble that may hold links or buttons (use ui-tooltip for short hover hints instead). 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/core-popup",
    "@kanok-ui/shared-align",
    "@kanok-ui/shared-element",
    "@kanok-ui/shared-events",
    "@kanok-ui/shared-field",
    "@kanok-ui/shared-localize",
    "@kanok-ui/shared-position",
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/toggletip/toggletip.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/toggletip/toggletip.ts",
      "content": "import { LitElement, html, css } from 'lit';\nimport { customElement, property, query } from 'lit/decorators.js';\nimport { uid } from '../shared/field.ts';\nimport { caretStyles, placeWithCaret, type Placement } from '../shared/position.ts';\nimport { LocalizeController } from '../shared/localize.ts';\nimport type { OpenDetail } from '../shared/events.ts';\nimport { WithAlign } from '../shared/align.ts';\nimport { UiElement } from '../shared/element.ts';\nimport { trackAnchor } from '../../core/popup.ts';\n\n/**\n * Carbon-style toggletip: a click-to-open bubble that may hold links or buttons\n * (use ui-tooltip for short hover hints instead).\n *\n * - The trigger is a real button with `aria-expanded` / `aria-controls`, wired as the native\n *   `popovertarget` so the browser toggles it (and doesn't light-dismiss on the trigger itself).\n * - Popover API (`popover=\"auto\"`): Esc and outside clicks close it; it lives in the top layer.\n * - Content follows the trigger in tab order; when focus leaves both, it closes.\n * - Closing while focus is inside returns focus to the trigger.\n *\n * @slot - Bubble content (text, links, buttons)\n * @slot trigger - Custom trigger icon (default: an \"i\" information icon)\n * @fires ui-toggle - detail: { open }, after it opens or closes\n * @csspart trigger - The info button\n * @csspart bubble - The popover content box\n * @csspart caret - The arrow pointing at the trigger\n * @attr align - Carbon's name for `placement` (same values)\n * @cssprop --ui-menu-border / --ui-menu-radius - Border and corners of the bubble (the menu settings)\n * @cssprop --ui-icon-control-radius - Corners of its small icon buttons, their hover fill and ring (default 0; Kanok: round)\n * @cssprop --ui-focus-offset - `outline-offset` of its focus ring (default -2px: inside; Kanok: 2px outside)\n */\n@customElement('ui-toggletip')\nexport class UiToggletip 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 = [caretStyles, css`\n        :host {\n            display: inline-block;\n            vertical-align: middle;\n        }\n\n        .trigger {\n            display: grid;\n            place-items: center;\n            width: 24px;\n            height: 24px;\n            padding: 0;\n            border: none;\n            background: transparent;\n            color: var(--ui-ink-muted);\n            cursor: pointer;\n            border-radius: var(--ui-icon-control-radius, 0); /* round in a design that rounds icon controls (Kanok) */\n        }\n\n        .trigger:hover,\n        .trigger[aria-expanded='true'] {\n            color: var(--ui-ink);\n        }\n\n        .trigger:focus-visible {\n            outline: var(--ui-focus-width, 2px) solid var(--ui-focus);\n            outline-offset: var(--ui-focus-offset, -2px);\n        }\n\n        .trigger svg,\n        ::slotted(svg) {\n            width: 16px;\n            height: 16px;\n            fill: currentColor;\n        }\n\n        /* Carbon toggletip: the default (light) popover — layer surface, drop shadow, 12×6 caret, 16px padding */\n        .bubble {\n            position: absolute; /* document coordinates in the top layer: scrolls with the page, see position.ts */\n            inset: auto;\n            margin: 0;\n            box-sizing: border-box;\n            width: max-content; /* Carbon popover-content; a shrink-to-fit width would depend on where it lands */\n            max-width: 288px;\n            padding: var(--ui-space-md);\n            /* the menu settings (shared/listbox.ts): a design rounds and outlines every floating surface alike */\n            border: var(--ui-menu-border, none);\n            border-radius: var(--ui-menu-radius, var(--ui-radius-xs));\n            background-color: var(--ui-overlay-background, var(--ui-surface-1)); /* a design can raise floating surfaces (--ui-overlay-background) */\n            color: var(--ui-ink);\n            font: var(--ui-font-body-sm);\n            line-height: 20px; /* body-01 */\n            letter-spacing: var(--ui-tracking-body);\n            filter: drop-shadow(0 2px 2px rgb(0 0 0 / 0.2));\n            overflow: visible;\n        }\n\n        /* Carbon link-primary: readable on the bubble in every theme (the button blue is too dark on g90 / g100) */\n        ::slotted(a) {\n            color: var(--ui-link);\n        }\n\n        /* no fade: Carbon's toggletip shows and hides at once (its CSS has no transition or animation) */\n\n        @media (forced-colors: active) {\n            .bubble {\n                border: 1px solid CanvasText;\n            }\n        }\n    `];\n\n    /** Accessible name of the trigger, e.g. \"More about retention\". */\n    @property() label = '';\n\n    private readonly localize = new LocalizeController(this);\n\n    @property({ reflect: true }) placement: Placement = 'bottom';\n\n    @property({ type: Boolean, reflect: true }) open = false;\n\n    @query('.trigger') private readonly trigger!: HTMLButtonElement;\n\n    @query('.bubble') private readonly bubble!: HTMLElement;\n\n    private readonly bubbleId = uid('ui-toggletip');\n    /** Whether focus was inside when closing started, so it can go back to the trigger. */\n    private returnFocus = false;\n    /** True while `open` is being synced from the browser's own toggle (see `updated`). */\n    private browserToggling = false;\n\n    constructor() {\n        super();\n        this.addEventListener('keydown', this.onKeydown);\n    }\n\n    override disconnectedCallback() {\n        super.disconnectedCallback();\n        this.stopTracking();\n    }\n\n    /**\n     * Esc closes the toggletip only: taken here (preventDefault) rather than left to the popover's light dismiss, which\n     * the page's Esc listener can't see, so a side panel or modal around it stays open (core/overlay.ts).\n     */\n    private readonly onKeydown = (event: KeyboardEvent) => {\n        if (event.key !== 'Escape' || event.defaultPrevented || !this.open) return;\n        event.preventDefault();\n        this.returnFocus = this.hasFocus();\n        this.open = false;\n    };\n\n    show() {\n        this.open = true;\n    }\n\n    hide() {\n        this.open = false;\n    }\n\n    /**\n     * `open` is the source of truth for our own show()/hide(); the browser's own toggles\n     * (trigger click, Esc, outside click) sync back through `beforetoggle`, which fires\n     * synchronously every time — unlike `toggle`, which the browser may coalesce.\n     */\n    protected override updated(changed: Map<string, unknown>) {\n        if (!changed.has('open')) return;\n        if (this.browserToggling) {\n            // `beforetoggle` set `open` while the browser is still mid show/hide: calling\n            // showPopover()/hidePopover() now throws InvalidStateError, and the browser\n            // finishes the job itself.\n            this.browserToggling = false;\n        } else {\n            const showing = this.bubble.matches(':popover-open');\n            if (this.open && !showing) this.bubble.showPopover();\n            if (!this.open && showing) this.bubble.hidePopover();\n        }\n        if (this.open) {\n            // After the browser's show operation has laid the bubble out.\n            requestAnimationFrame(this.reposition);\n            // on its button while open (scrolling ancestors in any shadow root, layout shifts)\n            this.untrack ??= trackAnchor(this.trigger, this.bubble, this.reposition);\n        } else {\n            this.stopTracking();\n            if (this.returnFocus) this.trigger.focus();\n            this.returnFocus = false;\n        }\n        if (changed.get('open') !== undefined) {\n            this.dispatchEvent(new CustomEvent('ui-toggle', { detail: { open: this.open }, bubbles: true, composed: true }));\n        }\n    }\n\n    private readonly reposition = () => placeWithCaret(this.trigger, this.bubble, this.placement, 10); // Carbon popover--caret offset\n\n    private untrack?: () => void;\n\n    private stopTracking() {\n        this.untrack?.();\n        this.untrack = undefined;\n    }\n\n    /** Focus is on the trigger, inside the bubble, or on slotted content. */\n    private hasFocus() {\n        const root = this.getRootNode() as Document | ShadowRoot;\n        return this.contains(root.activeElement) || !!this.shadowRoot?.activeElement;\n    }\n\n    private onBeforeToggle(event: ToggleEvent) {\n        const opening = event.newState === 'open';\n        // Capture now: once hidden, the browser drops focus from the bubble's content.\n        if (!opening) this.returnFocus = this.hasFocus();\n        if (opening !== this.open) this.browserToggling = true;\n        this.open = opening;\n    }\n\n    /** Close once focus has left both the trigger and the bubble. */\n    private onFocusOut(event: FocusEvent) {\n        const next = event.relatedTarget as Node | null;\n        if (next && !this.contains(next) && !this.shadowRoot?.contains(next)) this.open = false;\n    }\n\n    override render() {\n        return html`\n            <button\n                class=\"trigger\"\n                part=\"trigger\"\n                type=\"button\"\n                aria-label=${this.label || this.localize.t.common.moreInfo}\n                aria-expanded=${this.open ? 'true' : 'false'}\n                aria-controls=${this.bubbleId}\n                popovertarget=${this.bubbleId}\n                @focusout=${this.onFocusOut}\n            >\n                <slot name=\"trigger\">\n                    <svg viewBox=\"0 0 16 16\" aria-hidden=\"true\">\n                        <path\n                            d=\"M8.5 11V6.5h-2v1h1V11H6v1h4v-1h-1.5ZM8 3.5a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Z\"\n                        />\n                        <path d=\"M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14ZM8 2a6 6 0 1 0 0 12A6 6 0 0 0 8 2Z\" />\n                    </svg>\n                </slot>\n            </button>\n            <div\n                class=\"bubble\"\n                part=\"bubble\"\n                id=${this.bubbleId}\n                popover=\"auto\"\n                role=\"group\"\n                aria-label=${this.label || this.localize.t.common.moreInfo}\n                @beforetoggle=${this.onBeforeToggle}\n                @focusout=${this.onFocusOut}\n            >\n                <slot></slot>\n                <span class=\"caret\" part=\"caret\" aria-hidden=\"true\"></span>\n            </div>\n        `;\n    }\n}\n\nexport interface UiToggletipEventMap extends HTMLElementEventMap {\n    'ui-toggle': CustomEvent<OpenDetail>;\n}\n\n/** Typed listeners for UiToggletip's events (declaration merging with the class). */\nexport interface UiToggletip {\n    addEventListener<K extends keyof UiToggletipEventMap>(type: K, listener: (this: UiToggletip, event: UiToggletipEventMap[K]) => unknown, options?: boolean | AddEventListenerOptions): void;\n    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;\n    removeEventListener<K extends keyof UiToggletipEventMap>(type: K, listener: (this: UiToggletip, event: UiToggletipEventMap[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-toggletip': UiToggletip;\n    }\n}\n"
    }
  ]
}
