{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-position",
  "type": "registry:lib",
  "title": "core/position",
  "description": "Framework-free behaviour shared by the ui-* components (position); 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/position.ts",
      "type": "registry:lib",
      "target": "components/ui/core/position.ts",
      "content": "/**\n * Where a floating box (list, menu, calendar, tooltip, toggletip) goes beside its anchor: Floating UI's middleware, as\n * pure geometry in viewport coordinates (the renderer measures and writes the styles, see popup.ts `placePopup`):\n * - **flip**: the side asked for, or the opposite one when it lacks room and the other has it (the side with more room\n *   when neither has); while open, the side it is already on is kept as long as it still fits, so a popup doesn't jump\n *   to the other side in the middle of a scroll;\n * - **shift**: along the anchor's edge it is kept inside the viewport, but never further than the anchor's own extent\n *   (Floating UI's `limitShift`): it doesn't stay pinned to the screen edge once its anchor has scrolled away;\n * - **size**: the room on the side it went to (`availableHeight` / `availableWidth`), for lists that should scroll\n *   rather than run past the screen;\n * - **hide**: `anchorHidden` when the anchor is out of the visible area (the viewport, and the boxes that clip it).\n */\n\nexport type Side = 'top' | 'bottom' | 'left' | 'right';\nexport type Align = 'start' | 'center' | 'end';\n/** `top` centres on the anchor; `top-start` / `top-end` line up with its start / end edge. */\nexport type Placement = Side | `${Side}-start` | `${Side}-end`;\n\n/** A box in viewport coordinates (a DOMRect fits). */\nexport interface Box {\n    top: number;\n    left: number;\n    width: number;\n    height: number;\n}\n\n/**\n * The visible area: the layout viewport's size, or the visual viewport (`top` / `left`: its offset in the layout\n * viewport), which an on-screen keyboard or a pinch zoom makes smaller.\n */\nexport interface Viewport {\n    width: number;\n    height: number;\n    top?: number;\n    left?: number;\n}\n\nexport interface PositionOptions {\n    /** Distance from the anchor (default 8). */\n    gap?: number;\n    /** Right-to-left text: start / end are mirrored on the top and bottom sides. */\n    rtl?: boolean;\n    /** The side the popup is on now (while it is open): kept while it still fits, so it doesn't flip mid-scroll. */\n    current?: Side;\n    /** The part of the viewport its scrolling ancestors leave visible: the anchor counts as hidden outside it. */\n    clip?: Box;\n}\n\nexport interface Position {\n    top: number;\n    left: number;\n    side: Side;\n    /** The room for the popup on the side it went to, between the anchor's gap and the viewport edge (Floating UI `size`). */\n    availableHeight: number;\n    availableWidth: number;\n    /** The anchor is outside the visible area: the popup should hide (Floating UI `hide`). */\n    anchorHidden: boolean;\n}\n\n/** Room kept between a floating box and the viewport edge. */\nexport const VIEWPORT_EDGE = 8;\n\nconst OPPOSITE: Record<Side, Side> = { top: 'bottom', bottom: 'top', left: 'right', right: 'left' };\n\nexport function parsePlacement(placement: Placement): [Side, Align] {\n    const [side, align] = placement.split('-') as [Side, Align | undefined];\n    return [side, align ?? 'center'];\n}\n\n/** Offset along the anchor's edge for start / center / end alignment. */\nfunction alignTo(start: number, size: number, floatSize: number, align: Align) {\n    if (align === 'start') return start;\n    if (align === 'end') return start + size - floatSize;\n    return start + size / 2 - floatSize / 2;\n}\n\n/**\n * The viewport position of a `floating` box of the given size beside `anchor` (see the module comment for the rules),\n * with the room on that side and whether the anchor is out of view.\n */\nexport function computePosition(anchor: Box, floating: { width: number; height: number }, viewport: Viewport, placement: Placement, options: PositionOptions = {}): Position {\n    const gap = options.gap ?? 8;\n    // read the fields: a DOMRect's are getters, which a spread would drop\n    const a = { top: anchor.top, left: anchor.left, width: anchor.width, height: anchor.height };\n    const f = { width: floating.width, height: floating.height };\n    const vTop = viewport.top ?? 0;\n    const vLeft = viewport.left ?? 0;\n    const vBottom = vTop + viewport.height;\n    const vRight = vLeft + viewport.width;\n    const room = (side: Side) => {\n        switch (side) {\n            case 'top':\n                return a.top - gap - (vTop + VIEWPORT_EDGE);\n            case 'bottom':\n                return vBottom - VIEWPORT_EDGE - (a.top + a.height + gap);\n            case 'left':\n                return a.left - gap - (vLeft + VIEWPORT_EDGE);\n            default:\n                return vRight - VIEWPORT_EDGE - (a.left + a.width + gap);\n        }\n    };\n    const need = (side: Side) => (side === 'top' || side === 'bottom' ? f.height : f.width);\n\n    const [asked, askedAlign] = parsePlacement(placement);\n    // flip, keeping the side it is on while that still fits (the asked one only on opening)\n    const first = options.current && (options.current === asked || options.current === OPPOSITE[asked]) ? options.current : asked;\n    let side = first;\n    if (room(first) < need(first)) {\n        const other = OPPOSITE[first];\n        if (room(other) >= need(other) || room(other) > room(first)) side = other;\n    }\n\n    const vertical = side === 'top' || side === 'bottom';\n    let align = askedAlign;\n    if (vertical && align !== 'center' && options.rtl) align = align === 'start' ? 'end' : 'start';\n\n    let top: number;\n    let left: number;\n    if (vertical) {\n        top = side === 'top' ? a.top - f.height - gap : a.top + a.height + gap;\n        left = shift(alignTo(a.left, a.width, f.width, align), f.width, vLeft, vRight, a.left, a.width);\n    } else {\n        top = shift(alignTo(a.top, a.height, f.height, align), f.height, vTop, vBottom, a.top, a.height);\n        left = side === 'left' ? a.left - f.width - gap : a.left + a.width + gap;\n    }\n\n    const across = vertical ? viewport.width : viewport.height;\n    const availableMain = Math.max(0, room(side));\n    const availableCross = Math.max(0, across - 2 * VIEWPORT_EDGE);\n    const visible = intersect({ top: vTop, left: vLeft, width: viewport.width, height: viewport.height }, options.clip);\n    const anchorHidden = !visible || a.top + a.height <= visible.top || a.top >= visible.top + visible.height || a.left + a.width <= visible.left || a.left >= visible.left + visible.width;\n    if (anchorHidden) {\n        // not shown (placePopup hides it): kept inside the viewport, so it keeps its own size for when its anchor returns\n        // (past the edge an auto-width box would shrink to fit, and show at that size for a frame)\n        top = within(top, f.height, vTop, vBottom);\n        left = within(left, f.width, vLeft, vRight);\n    }\n    return {\n        top,\n        left,\n        side,\n        availableHeight: vertical ? availableMain : availableCross,\n        availableWidth: vertical ? availableCross : availableMain,\n        anchorHidden,\n    };\n}\n\n/**\n * Along the anchor's edge: inside the viewport (`VIEWPORT_EDGE` in), but no further than to touch the anchor's far\n * edge, so the box stays beside its anchor (Floating UI `shift` with `limitShift`).\n */\nfunction shift(at: number, size: number, viewStart: number, viewEnd: number, anchorStart: number, anchorSize: number) {\n    return Math.min(Math.max(within(at, size, viewStart, viewEnd), anchorStart - size), anchorStart + anchorSize);\n}\n\n/** `at` moved `VIEWPORT_EDGE` inside the viewport (from its start edge when it doesn't fit). */\nfunction within(at: number, size: number, viewStart: number, viewEnd: number) {\n    return Math.min(Math.max(at, viewStart + VIEWPORT_EDGE), Math.max(viewStart + VIEWPORT_EDGE, viewEnd - size - VIEWPORT_EDGE));\n}\n\n/** The overlap of two boxes (`b` absent: `a`), or null when they don't overlap. */\nfunction intersect(a: Box, b?: Box): Box | null {\n    if (!b) return a;\n    const top = Math.max(a.top, b.top);\n    const left = Math.max(a.left, b.left);\n    const bottom = Math.min(a.top + a.height, b.top + b.height);\n    const right = Math.min(a.left + a.width, b.left + b.width);\n    return bottom > top && right > left ? { top, left, width: right - left, height: bottom - top } : null;\n}\n\n/** Keeps a caret off the rounded corners of the floating box. */\nexport const CARET_INSET = 8;\n\n/**\n * Where a caret goes along the floating box's edge facing the anchor: the distance to the\n * anchor's centre, kept `CARET_INSET` from the corners (Carbon popover caret).\n */\nexport function caretOffset(anchor: Box, floating: Box, side: Side): number {\n    const vertical = side === 'top' || side === 'bottom';\n    const centre = vertical ? anchor.left + anchor.width / 2 - floating.left : anchor.top + anchor.height / 2 - floating.top;\n    const size = vertical ? floating.width : floating.height;\n    return Math.min(Math.max(centre, CARET_INSET), size - CARET_INSET);\n}\n"
    }
  ]
}
