{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-overlay",
  "type": "registry:lib",
  "title": "core/overlay",
  "description": "Framework-free behaviour shared by the ui-* components (overlay); no DOM, no lit. 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": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "src/core/overlay.ts",
      "type": "registry:lib",
      "target": "components/ui/core/overlay.ts",
      "content": "/**\n * The page's open overlays (modal, tearsheet, side panel — Lit and React alike share this module): which one is on\n * top, so one Esc closes one layer; and one scroll lock for all of them, so closing them in any order lets the page\n * scroll again only when the last one is gone. The renderers pass in what they touch (the window, the root element):\n * this module needs no DOM itself.\n */\n\n/** An open overlay, as the stack knows it. */\nexport interface OverlayLayer {\n    /** Esc reached it as the topmost layer: ask to close (the app may veto). */\n    readonly escape: () => void;\n    /** A native modal `<dialog>` (top layer, the page behind inert): what opens over it must be one too to be usable. */\n    readonly modal?: boolean;\n}\n\n/** What the stack reads of a keydown (a DOM `KeyboardEvent` fits). */\nexport interface EscapeEventLike {\n    readonly key: string;\n    readonly defaultPrevented: boolean;\n    readonly isComposing?: boolean;\n    preventDefault(): void;\n}\n\n/** Where the Esc listener goes (the window). */\nexport interface KeydownTarget {\n    addEventListener(type: 'keydown', listener: (event: EscapeEventLike) => void): void;\n    removeEventListener(type: 'keydown', listener: (event: EscapeEventLike) => void): void;\n}\n\n/** What the scroll lock holds still (the root element: `document.documentElement`). */\nexport interface ScrollRoot {\n    readonly style: { overflow: string };\n}\n\n/** Open layers, oldest first. */\nconst layers: OverlayLayer[] = [];\n\n/** The topmost open layer, if any. */\nexport const topOverlay = (): OverlayLayer | undefined => layers.at(-1);\n\n/** Whether `layer` is the topmost open layer. */\nexport const isTopOverlay = (layer: OverlayLayer) => layers.at(-1) === layer;\n\n/** Whether a native modal dialog is open (a layer opened now goes over it). */\nexport const modalOverlayOpen = () => layers.some((layer) => layer.modal);\n\nlet target: KeydownTarget | null = null;\n\n/**\n * One Esc listener for the page (bubbling on the window, so the controls inside — a dropdown, a date picker, a\n * tooltip — handle their own Esc first and mark it with preventDefault). The topmost layer gets it, and the key is\n * marked handled: the browser's own close request (a modal `<dialog>`'s cancel, or its close watcher after a vetoed\n * Esc) doesn't follow, so a veto holds however often Esc is pressed.\n */\nfunction onKeydown(event: EscapeEventLike) {\n    if (event.key !== 'Escape' || event.defaultPrevented || event.isComposing) return;\n    const top = layers.at(-1);\n    if (!top) return;\n    event.preventDefault();\n    top.escape();\n}\n\n/** Puts `layer` on top of the stack (again, if it was open); `on` is the window, for the Esc listener. */\nexport function openOverlay(layer: OverlayLayer, on: KeydownTarget) {\n    const at = layers.indexOf(layer);\n    if (at >= 0) layers.splice(at, 1);\n    layers.push(layer);\n    if (!target) {\n        target = on;\n        target.addEventListener('keydown', onKeydown);\n    }\n}\n\n/** Takes `layer` off the stack, wherever it is (layers may close out of order). */\nexport function closeOverlay(layer: OverlayLayer) {\n    const at = layers.indexOf(layer);\n    if (at >= 0) layers.splice(at, 1);\n    if (layers.length === 0 && target) {\n        target.removeEventListener('keydown', onKeydown);\n        target = null;\n    }\n}\n\n/** Who holds the page still, and the root's overflow from before the first of them. */\nconst lockers = new Set<object>();\nlet savedOverflow = '';\n\n/** The page stops scrolling while any owner holds the lock (ref-counted: each owner once). */\nexport function lockScroll(owner: object, root: ScrollRoot) {\n    if (lockers.has(owner)) return;\n    if (lockers.size === 0) {\n        savedOverflow = root.style.overflow;\n        root.style.overflow = 'hidden';\n    }\n    lockers.add(owner);\n}\n\n/** Lets go of `owner`'s hold; the page scrolls again once the last owner has let go. */\nexport function unlockScroll(owner: object, root: ScrollRoot) {\n    if (!lockers.delete(owner) || lockers.size > 0) return;\n    root.style.overflow = savedOverflow;\n    savedOverflow = '';\n}\n"
    }
  ]
}
