{"version":3,"file":"chunk-PopupScroll.mjs","names":["ScrollArea.Root","ScrollArea.Viewport"],"sources":["../../src/components/ScrollArea/PopupScroll.tsx"],"sourcesContent":["//\n// Copyright 2026 DXOS.org\n//\n\nimport { useEnvironmentContext } from '@ark-ui/react/environment';\nimport { type Popover as PopoverPrimitive } from '@ark-ui/react/popover';\nimport React, { type ReactElement, type ReactNode, useMemo } from 'react';\n\nimport { mx } from '@dxos/ui-theme';\n\nimport { recipes } from '../../recipes.ts';\nimport { type Size, SIZES } from '../../sizes.ts';\nimport * as ScrollArea from './ScrollArea.tsx';\n\ntype Positioning = NonNullable<PopoverPrimitive.RootProps['positioning']>;\n\n/**\n * zag's per-placement update functions (one each time a popup opens) that have run once; the first runs synchronously\n * so the popup opens in place and focus and dismissal wire up as before.\n */\nconst placed = new WeakSet<() => Promise<void>>();\n\n/**\n * Defers a popup's later position updates to an animation frame (DESIGN.md follow-up 53). floating-ui repositions from\n * its ResizeObserver on the trigger and applies `--reference-width` before that callback's frame ends, so a popup\n * widening with its trigger resized the (shallower) ScrollArea viewport mid-delivery and the thumbs' observation of it\n * was skipped: \"ResizeObserver loop completed with undelivered notifications\". A frame requested from a scroll event\n * still runs in that frame, so only a resize-driven update lands a frame later.\n */\nconst deferPosition: Positioning['updatePosition'] = ({ updatePosition }) => {\n  if (!placed.has(updatePosition)) {\n    placed.add(updatePosition);\n    return updatePosition();\n  }\n\n  return new Promise<void>((resolve, reject) => requestAnimationFrame(() => updatePosition().then(resolve, reject)));\n};\n\n/** A portalled popup's positioning: `gutter` px from its trigger, updated outside ResizeObserver callbacks. */\nexport const popupPositioning = (gutter: number, positioning?: Positioning): Positioning => ({\n  gutter,\n  updatePosition: deferPosition,\n  ...positioning,\n});\n\n/** The `data-size` of the nearest sized ancestor (or self) of the first of `ids` present under `root`. */\nconst readSize = (\n  root: ReturnType<ReturnType<typeof useEnvironmentContext>['getRootNode']>,\n  ids: string[],\n): Size | undefined => {\n  if (!('getElementById' in root)) {\n    return undefined;\n  }\n\n  for (const id of ids) {\n    const element = root.getElementById(id);\n    if (element) {\n      const value = element.closest('[data-size]')?.getAttribute('data-size');\n      return SIZES.find((size) => size === value);\n    }\n  }\n};\n\n/**\n * The size a portalled popup renders at (Phase 4 decision 2): its own `size` if given, else the `data-size` of the\n * nearest sized ancestor of its trigger or anchor (the first of `ids` in the popup's root node), else `fallback`. A DOM lookup\n * each time the popup opens rather than React context (decision 3); a popup with no trigger (a Dialog opened by state,\n * a virtual anchor) takes the fallback.\n */\nexport const usePopupSize = (\n  size: Size | undefined,\n  open: boolean,\n  ids: (string | undefined)[],\n  fallback?: Size,\n): Size | undefined => {\n  // The root zag resolves the popup in, so a trigger in a shadow root or another document is still found.\n  const { getRootNode } = useEnvironmentContext();\n  const key = ids.filter(Boolean).join(' ');\n  // `open` recomputes on every opening, when the trigger is certainly mounted.\n  return useMemo(\n    () => size ?? readSize(getRootNode(), key.split(' ').filter(Boolean)) ?? fallback,\n    [size, open, key, fallback, getRootNode],\n  );\n};\n\nexport type PopupScrollProps = {\n  size?: Size;\n  classNames?: string;\n  /** The Ark Content, which becomes the scrolling viewport: zag scrolls its highlighted item into view only there. */\n  children: ReactElement;\n  /** Parts drawn outside the viewport (an arrow), which its overflow would otherwise clip. */\n  outside?: ReactNode;\n};\n\n/**\n * The frame of a scrolling popup (Menu, Select, Combobox; DESIGN.md follow-up 49): a thin overlay ScrollArea that is\n * also the popup surface, sized and levelled like any `.dx-popup`, with the portalled Content as its viewport.\n */\nexport const PopupScroll = ({ size, classNames, children, outside }: PopupScrollProps) => (\n  <ScrollArea.Root data-surface='popup' size={size} width='thin' classNames={mx(recipes.popup(), classNames)}>\n    <ScrollArea.Viewport asChild>{children}</ScrollArea.Viewport>\n    {outside}\n  </ScrollArea.Root>\n);\n"],"mappings":";;;;;;;;;;;;AAoBA,IAAM,yBAAS,IAAI,QAA6B;;;;;;;;AAShD,IAAM,iBAAgD,EAAE,qBAAqB;CAC3E,IAAI,CAAC,OAAO,IAAI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