{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "loading",
  "type": "registry:ui",
  "title": "<ui-loading>",
  "description": "Carbon `Loading`: the 88px spinner (`small`: 16px with a grey track). With `overlay` it covers the page (fixed, Carbon `overlay` colour), traps focus while `active` and gives focus back afterwards — use it while a whole view is unavailable; prefer `<ui-inline-loading>` next to a button. Set `active` to false to stop and hide it (the element stays in the DOM). 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-spin-delay",
    "@kanok-ui/shared-element",
    "@kanok-ui/shared-field",
    "@kanok-ui/shared-localize",
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/loading/loading.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/loading/loading.ts",
      "content": "import { html, css, nothing, svg } from 'lit';\nimport { customElement, property, query, state } from 'lit/decorators.js';\nimport { ifDefined } from 'lit/directives/if-defined.js';\nimport { LocalizeController } from '../shared/localize.ts';\nimport { deepActiveElement, defaultTrue } from '../shared/field.ts';\nimport { UiElement } from '../shared/element.ts';\nimport { SpinDelay } from '../../core/spin-delay.ts';\n\n/**\n * Carbon `Loading`: the 88px spinner (`small`: 16px with a grey track). With `overlay` it covers\n * the page (fixed, Carbon `overlay` colour), traps focus while `active` and gives focus back\n * afterwards — use it while a whole view is unavailable; prefer `<ui-inline-loading>` next to\n * a button. Set `active` to false to stop and hide it (the element stays in the DOM).\n *\n * Beyond Carbon: `contained` wraps an area (a card, a table) instead of the page — while active the\n * overlay covers just that box and its content is inert and `aria-busy`; the rest of the page stays\n * usable, and focus that was inside waits on the spinner, then goes back. `delay` (ms) shows it only\n * if the work is still going after that long and `min-duration` (ms) keeps it up at least that long\n * once shown, so quick work never flashes a spinner.\n *\n * State on the host for styling: `data-shown` while the spinner actually shows (`active`, after `delay`, held for\n * `min-duration`), where `[active]` is only the request.\n *\n * @cssstate shown - While the spinner actually shows (after `delay`, held for `min-duration`) (also `[data-shown]`)\n * @slot - The area `contained` covers\n * @csspart base - The spinner wrapper (`aria-live`)\n * @csspart overlay - The page overlay (`overlay`), or the area's (`contained`)\n * @csspart area - The covered content (`contained`)\n * @csspart spinner - The `<svg>`\n */\n@customElement('ui-loading')\nexport class UiLoading extends UiElement {\n    static override hostStates: readonly string[] = ['shown'];\n\n    static override readonly styles = css`\n        :host {\n            display: inline-block;\n        }\n\n        :host([overlay]) {\n            display: contents;\n        }\n\n        :host([contained]) {\n            position: relative;\n            display: block;\n        }\n\n        /* the area's own overlay: over just this box, under anything floating above the page */\n        .overlay.contained {\n            position: absolute;\n            z-index: 1;\n        }\n\n        :host([hidden]) {\n            display: none;\n        }\n\n        .overlay {\n            position: fixed;\n            inset: 0;\n            z-index: 6000; /* Carbon z('overlay') */\n            display: flex;\n            align-items: center;\n            justify-content: center;\n            background-color: var(--ui-overlay);\n            /* Carbon .cds--loading-overlay: $duration-slow-02 with the expressive standard easing (no token for either) */\n            transition: background-color 700ms cubic-bezier(0.4, 0.14, 0.3, 1);\n        }\n\n        .overlay.stop,\n        :host(:not([overlay])) .base.stop {\n            display: none;\n        }\n\n        .focus {\n            outline: none;\n        }\n\n        .base {\n            inline-size: 88px;\n            block-size: 88px;\n            animation: rotate 690ms linear infinite;\n        }\n\n        :host([small]) .base {\n            inline-size: 16px;\n            block-size: 16px;\n            line-height: 1rem;\n        }\n\n        svg {\n            fill: transparent;\n            inline-size: 100%;\n            block-size: 100%;\n        }\n\n        circle {\n            stroke-dasharray: 276.46 276.46;\n            stroke-linecap: butt;\n            stroke-width: 10;\n        }\n\n        :host([small]) circle {\n            stroke-width: 16;\n        }\n\n        .stroke {\n            stroke: var(--ui-interactive, var(--ui-primary));\n            stroke-dashoffset: 52.53; /* 81 % of the circle */\n        }\n\n        :host([small]) .stroke {\n            stroke-dashoffset: 143.76; /* 48 % */\n        }\n\n        .background {\n            stroke: var(--ui-layer-accent);\n            stroke-dashoffset: -22;\n        }\n\n        @keyframes rotate {\n            to {\n                transform: rotate(360deg);\n            }\n        }\n\n        @media (prefers-reduced-motion: reduce) {\n            .base {\n                animation-duration: 2s;\n            }\n\n            .overlay {\n                transition: none;\n            }\n        }\n\n        @media (forced-colors: active) {\n            .stroke {\n                stroke: CanvasText;\n            }\n\n            .background {\n                stroke: GrayText;\n            }\n\n            .overlay {\n                background-color: Canvas;\n                opacity: 0.8;\n            }\n        }\n    `;\n\n    /** On by default; `active=\"false\"` stops it. */\n    @property({ reflect: true, converter: defaultTrue }) active = true;\n\n    @property({ type: Boolean, reflect: true }) small = false;\n\n    /** Cover the page and trap focus while active. */\n    @property({ type: Boolean, reflect: true }) overlay = false;\n\n    /** Accessible description; defaults to the translation (\"Loading\"). */\n    @property() description = '';\n\n    /** Cover just this element's content (the slot) instead of the page; the content is inert while active (no Carbon counterpart). */\n    @property({ type: Boolean, reflect: true }) contained = false;\n\n    /** Milliseconds `active` must last before the spinner shows (default 0, as Carbon). */\n    @property({ type: Number }) delay = 0;\n\n    /** Once shown, the least milliseconds it stays (default 0). */\n    @property({ type: Number, attribute: 'min-duration' }) minDuration = 0;\n\n    /** Showing now: `active`, after `delay`, held for `min-duration`. */\n    @state() private shown = false;\n\n    private spin?: SpinDelay;\n\n    @query('.focus') private readonly focusTarget!: HTMLElement | null;\n\n    private readonly localize = new LocalizeController(this);\n    private previouslyFocused: HTMLElement | null = null;\n    private trapping = false;\n    /** Where focus moves are watched: the document, and the shadow root this element sits in (a focus move inside one never reaches the document). */\n    private scopes: (Document | ShadowRoot)[] = [];\n\n    override connectedCallback() {\n        super.connectedCallback();\n        // moved: a wait cut short by the disconnect starts again\n        if (this.hasUpdated) this.spin?.update(this.active, this.delay, this.minDuration);\n    }\n\n    override disconnectedCallback() {\n        super.disconnectedCallback();\n        this.spin?.dispose();\n        this.stopTrap();\n        this.releaseArea();\n    }\n\n    protected override willUpdate(changed: Map<string, unknown>) {\n        if (!changed.has('active') && !changed.has('delay') && !changed.has('minDuration')) return;\n        this.spin ??= new SpinDelay((shown) => (this.shown = shown));\n        this.spin.update(this.active, this.delay, this.minDuration);\n    }\n\n    protected override updated(changed: Map<string, unknown>) {\n        this.toggleAttribute('data-shown', this.shown);\n        if (!changed.has('shown') && !changed.has('overlay') && !changed.has('contained')) return;\n        if (this.overlay && !this.contained && this.shown) this.startTrap();\n        else this.stopTrap();\n        if (this.contained && this.shown) this.holdArea();\n        else this.releaseArea();\n    }\n\n    /** `contained`: focus that was in the area waits on the spinner (the area is inert), then goes back. */\n    private areaFocus: HTMLElement | null = null;\n\n    /** In the covered content (light DOM, across its shadow roots), not in this element's own parts. */\n    private inArea(node: Node | null) {\n        for (let n = node; n; n = n.parentNode ?? (n as ShadowRoot).host ?? null) {\n            if (n === this.shadowRoot) return false;\n            if (n === this) return true;\n        }\n        return false;\n    }\n\n    private holdArea() {\n        const active = deepActiveElement();\n        if (!this.inArea(active)) return;\n        this.areaFocus = active;\n        this.focusTarget?.focus();\n    }\n\n    private releaseArea() {\n        const focus = this.areaFocus;\n        this.areaFocus = null;\n        if (!focus?.isConnected) return;\n        const now = deepActiveElement();\n        if (!now || now === document.body || now === this.focusTarget) focus.focus();\n    }\n\n    private get text() {\n        return this.description || this.localize.t.common.loading;\n    }\n\n    /** Carbon: focus stays on the overlay (Tab, programmatic focus) until it goes away; modals layered above are left alone. */\n    private startTrap() {\n        if (this.trapping) return;\n        this.trapping = true;\n        this.previouslyFocused = document.activeElement instanceof HTMLElement && document.activeElement !== document.body ? document.activeElement : null;\n        this.focusTarget?.focus();\n        const root = this.getRootNode();\n        this.scopes = root instanceof ShadowRoot ? [document, root] : [document];\n        for (const scope of this.scopes) scope.addEventListener('focusin', this.onFocusIn as EventListener, true);\n        document.addEventListener('keydown', this.onKeydown, true);\n    }\n\n    private stopTrap() {\n        if (!this.trapping) return;\n        this.trapping = false;\n        for (const scope of this.scopes) scope.removeEventListener('focusin', this.onFocusIn as EventListener, true);\n        this.scopes = [];\n        document.removeEventListener('keydown', this.onKeydown, true);\n        const active = document.activeElement;\n        const ownsFocus = !active || active === document.body || this.shadowRoot?.contains(active) || active === this;\n        if (ownsFocus && this.previouslyFocused?.isConnected) this.previouslyFocused.focus();\n        this.previouslyFocused = null;\n    }\n\n    private isLayeredOnTop(target: EventTarget | null) {\n        if (!(target instanceof Element)) return false;\n        const dialog = target.closest('dialog[open], [aria-modal=\"true\"]');\n        return !!dialog && !dialog.contains(this);\n    }\n\n    private readonly onFocusIn = (event: FocusEvent) => {\n        const path = event.composedPath();\n        if (path.includes(this) || this.isLayeredOnTop(path[0] ?? null)) return;\n        this.focusTarget?.focus();\n    };\n\n    private readonly onKeydown = (event: KeyboardEvent) => {\n        if (event.key !== 'Tab' || this.isLayeredOnTop(event.composedPath()[0] ?? null)) return;\n        event.preventDefault();\n        this.focusTarget?.focus();\n    };\n\n    private renderSpinner() {\n        return html`<div class=\"base ${this.shown ? '' : 'stop'}\" part=\"base\" aria-atomic=\"true\" aria-live=${this.shown ? 'assertive' : 'off'}>\n            <svg part=\"spinner\" viewBox=\"0 0 100 100\" role=\"img\" aria-label=${this.text}>\n                <title .textContent=${this.text}></title>\n                ${this.small ? svg`<circle class=\"background\" cx=\"50%\" cy=\"50%\" r=\"42\" />` : nothing}\n                <circle class=\"stroke\" cx=\"50%\" cy=\"50%\" r=${this.small ? 42 : 44} />\n            </svg>\n        </div>`;\n    }\n\n    override render() {\n        if (this.contained) return this.renderContained();\n        if (!this.overlay) return this.renderSpinner();\n        const on = this.shown;\n        return html`<div class=\"overlay ${on ? '' : 'stop'}\" part=\"overlay\" role=\"presentation\">\n            <div class=\"focus\" role=${ifDefined(on ? 'dialog' : undefined)} aria-modal=${ifDefined(on ? 'true' : undefined)} aria-label=${ifDefined(on ? this.text : undefined)} tabindex=${ifDefined(on ? -1 : undefined)}>${this.renderSpinner()}</div>\n        </div>`;\n    }\n\n    /** The area, inert and busy while shown, under its own overlay. */\n    private renderContained() {\n        const on = this.shown;\n        return html`<div class=\"area\" part=\"area\" aria-busy=${ifDefined(on ? 'true' : undefined)} ?inert=${on}><slot></slot></div>\n            <div class=\"overlay contained ${on ? '' : 'stop'}\" part=\"overlay\">\n                <div class=\"focus\" role=${ifDefined(on ? 'group' : undefined)} aria-label=${ifDefined(on ? this.text : undefined)} tabindex=${ifDefined(on ? -1 : undefined)}>${this.renderSpinner()}</div>\n            </div>`;\n    }\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-loading': UiLoading;\n    }\n}\n"
    }
  ]
}
