{"version":3,"file":"chunk-Hooks.mjs","names":[],"sources":["../../src/hooks/theme.ts","../../src/util/height-memory.ts","../../src/hooks/useSafeCollisionPadding.ts","../../src/hooks/usePositioning.ts","../../src/hooks/useVisualViewport.ts","../../src/hooks/Hooks.ts"],"sourcesContent":["//\n// Copyright 2026 DXOS.org\n//\n\nimport { type ThemeMode } from '@dxos/ui-types';\n\nimport { useThemeContext } from './useThemeContext.ts';\n\n// Next styles through `.dx-*` CSS, so these read the existing ThemeProvider's values and never expose its `tx`.\n\n/** The ThemeProvider's light or dark mode. */\nexport const useThemeMode = (): ThemeMode => useThemeContext().themeMode;\n\n/** The ThemeProvider's platform, if the app set one. */\nexport const usePlatform = (): 'mobile' | 'desktop' | undefined => useThemeContext().platform;\n\n/** Whether the device shows an iOS on-screen keyboard (measured once by the ThemeProvider). */\nexport const useIosKeyboard = (): boolean => useThemeContext().hasIosKeyboard;\n","//\n// Copyright 2026 DXOS.org\n//\n\nimport { type RefObject, useLayoutEffect } from 'react';\n\n/** The height things last rendered at this session, so their next first render can reserve the space. */\nexport type HeightMemory = {\n  get: (key: string) => number | undefined;\n  set: (key: string, height: number) => void;\n};\n\n/** Entries kept at most; the least recently used go first. */\nconst MAX_HEIGHTS = 1_000;\n\n/** A height memory for the session, holding up to {@link MAX_HEIGHTS} entries. */\nexport const createHeightMemory = (): HeightMemory => {\n  const heights = new Map<string, number>();\n  return {\n    // Reading keeps an entry: what is still opened is not what goes first.\n    get: (key) => {\n      const height = heights.get(key);\n      if (height !== undefined) {\n        heights.delete(key);\n        heights.set(key, height);\n      }\n      return height;\n    },\n    set: (key, height) => {\n      heights.delete(key);\n      heights.set(key, height);\n      if (heights.size > MAX_HEIGHTS) {\n        const [oldest] = heights.keys();\n        if (oldest !== undefined) {\n          heights.delete(oldest);\n        }\n      }\n    },\n  };\n};\n\n/** How long an element keeps the height reserved for it while its content arrives. */\nconst RESERVE_MS = 1_000;\n\n/** Heights are remembered per width, in steps this wide: the same content is taller in a narrower column. */\nconst WIDTH_STEP = 32;\n\n/**\n * Reserves the height `key` last rendered at, at this width if known, as `ref`'s minimum height while its content arrives, then\n * records the heights it renders at. A section whose content loads a moment after it mounts otherwise grows into it and\n * pushes everything below it down. No `key`, no reservation.\n */\nexport const useReservedHeight = (\n  memory: HeightMemory,\n  key: string | undefined,\n  ref: RefObject<HTMLElement | null>,\n) => {\n  useLayoutEffect(() => {\n    const element = ref.current;\n    if (!element || key === undefined) {\n      return;\n    }\n    // At mount, the height it last rendered at whatever the width; observations then refine it by width. A column can\n    // lay out at no width first, or settle its width while the content arrives (a companion opening beside the plank).\n    let reserved = memory.get(key);\n    let reservedFor: string | undefined;\n    let released = false;\n    const reserve = (height: number | undefined) => {\n      reserved = height;\n      // A minimum, so content already taller than it is unaffected.\n      element.style.minHeight = height !== undefined ? `${height}px` : '';\n    };\n    reserve(reserved);\n    const release = setTimeout(() => {\n      released = true;\n      element.style.minHeight = '';\n    }, RESERVE_MS);\n    // Observations arrive after layout and before paint, so the width costs no forced layout per element.\n    const observer = new ResizeObserver(([entry]) => {\n      const box = entry.borderBoxSize[0];\n      if (!box || box.inlineSize === 0) {\n        return;\n      }\n      const slot = `${key}@${Math.round(box.inlineSize / WIDTH_STEP)}`;\n      // The reservation this box was laid out under, which a new width's reservation replaces only for the next one.\n      const laidOutUnder = reserved;\n      if (!released && slot !== reservedFor) {\n        reservedFor = slot;\n        reserve(memory.get(slot) ?? reserved);\n      }\n      // While reserved, only a height past the reservation is the content's own.\n      if (box.blockSize > 0 && (released || box.blockSize > Math.max(laidOutUnder ?? 0, reserved ?? 0))) {\n        const height = Math.round(box.blockSize);\n        memory.set(slot, height);\n        memory.set(key, height);\n      }\n    });\n    observer.observe(element);\n    return () => {\n      clearTimeout(release);\n      observer.disconnect();\n      element.style.minHeight = '';\n    };\n  }, [memory, key, ref]);\n};\n","//\n// Copyright 2025 DXOS.org\n//\n\nimport { useMemo } from 'react';\n\nimport { type SafeAreaPadding } from './useSafeArea.ts';\nimport { useThemeContext } from './useThemeContext.ts';\n\n/** A uniform inset, or one per side; Zag's `overflowPadding` takes one number, so the widest side wins. */\nexport type CollisionPadding = number | Partial<Record<'top' | 'right' | 'bottom' | 'left', number>>;\n\ntype PaddingProp = CollisionPadding | undefined;\ntype PaddingRecord = Exclude<PaddingProp, number | undefined>;\n\nconst propIsNumber = (prop: PaddingProp): prop is number => Number.isFinite(prop);\nconst propsIsRecord = (prop: PaddingProp): prop is PaddingRecord => !!(prop && typeof prop === 'object');\n\n// The safe area is optional on the theme context, and absent entirely when a consumer resolves the\n// fallback context — which any portalled surface does under the dev server's dual-module split. Its\n// insets are also `NaN` until the first viewport measurement, so every read here is already written\n// to fall back to zero; an absent object is the same case, not a different one.\nconst safePadding = (propsPadding: PaddingProp, safeArea: SafeAreaPadding | undefined, side: keyof SafeAreaPadding) => {\n  const inset = safeArea?.[side];\n  return (\n    (propIsNumber(inset) ? inset : 0) +\n    (propIsNumber(propsPadding) ? propsPadding : propsIsRecord(propsPadding) ? (propsPadding[side] ?? 0) : 0)\n  );\n};\n\nexport const useSafeCollisionPadding = (collisionPadding?: PaddingProp) => {\n  const { safeAreaPadding } = useThemeContext();\n  return useMemo(\n    () => ({\n      top: safePadding(collisionPadding, safeAreaPadding, 'top'),\n      right: safePadding(collisionPadding, safeAreaPadding, 'right'),\n      bottom: safePadding(collisionPadding, safeAreaPadding, 'bottom'),\n      left: safePadding(collisionPadding, safeAreaPadding, 'left'),\n    }),\n    [collisionPadding, safeAreaPadding],\n  );\n};\n","//\n// Copyright 2026 DXOS.org\n//\n\nimport { type PopoverRootProps } from '@ark-ui/react/popover';\nimport { type RefObject, useCallback, useMemo, useState } from 'react';\n\nimport { type CollisionPadding, useSafeCollisionPadding } from './useSafeCollisionPadding.ts';\n\nexport type PositionSide = 'top' | 'right' | 'bottom' | 'left';\nexport type PositionAlign = 'start' | 'center' | 'end';\n\n/** How floating content wants to be placed; the machine, which the root owns, does the placing. */\nexport type PlacementOptions = {\n  side?: PositionSide;\n  align?: PositionAlign;\n  sideOffset?: number;\n  alignOffset?: number;\n  collisionPadding?: CollisionPadding;\n  collisionBoundary?: Element | null | Array<Element | null>;\n  avoidCollisions?: boolean;\n  hideWhenDetached?: boolean;\n};\n\n/** The `positioning` every Zag floating machine takes; the popover's is the shared shape. */\nexport type Positioning = NonNullable<PopoverRootProps['positioning']>;\n\n/** What a Zag machine accepts as the element its floating content is positioned against. */\nexport type AnchorElement = HTMLElement | { getBoundingClientRect: () => DOMRect; contextElement?: Element };\n\n/**\n * Hands a virtual anchor to the machine as its anchor element rather than as a bare rect: the\n * element is what the machine's scroll and resize observers attach to, so the content follows the\n * anchor when an ancestor scrolls.\n */\nexport const toAnchorElement = (element: Element | null): AnchorElement | null => {\n  if (element instanceof HTMLElement) {\n    return element;\n  }\n  return element ? { getBoundingClientRect: () => element.getBoundingClientRect(), contextElement: element } : null;\n};\n\nexport const toPlacement = (side: PositionSide = 'bottom', align: PositionAlign = 'center') =>\n  align === 'center' ? side : (`${side}-${align}` as const);\n\n/** Consumers hand the machine a per-side padding; it takes one number, so the widest side wins. */\nexport const toOverflowPadding = (padding: { top: number; right: number; bottom: number; left: number }) =>\n  Math.max(padding.top, padding.right, padding.bottom, padding.left);\n\nexport type UsePositioningOptions = {\n  open: boolean;\n  /** The element the content is placed at and bounded by, unless a virtual anchor stands in. */\n  triggerRef: RefObject<HTMLElement | null>;\n  placement: PlacementOptions;\n  defaultSide?: PositionSide;\n  defaultAlign?: PositionAlign;\n};\n\n/**\n * Radix-shaped placement props as one Zag `positioning` object, plus a virtual anchor the machine\n * positions at, and observes, in place of its trigger. Shared by every floating root so the\n * collision, offset and anchor semantics are one implementation.\n */\nexport type UsePositioningResult = {\n  positioning: Positioning;\n  /** Registers the element the content is positioned at instead of the trigger; returns the unregister. */\n  setVirtualAnchor: (ref: RefObject<Element | null>) => () => void;\n};\n\nexport const usePositioning = ({\n  open,\n  triggerRef,\n  placement: options,\n  defaultSide = 'bottom',\n  defaultAlign = 'center',\n}: UsePositioningOptions): UsePositioningResult => {\n  const [virtualAnchor, setVirtualAnchorState] = useState<RefObject<Element | null> | null>(null);\n  const setVirtualAnchor = useCallback((ref: RefObject<Element | null>) => {\n    setVirtualAnchorState(ref);\n    return () => setVirtualAnchorState((current) => (current === ref ? null : current));\n  }, []);\n\n  const {\n    side = defaultSide,\n    align = defaultAlign,\n    sideOffset = 0,\n    alignOffset,\n    collisionPadding = 8,\n    collisionBoundary,\n    avoidCollisions = true,\n    hideWhenDetached,\n  } = options;\n  const safeCollisionPadding = useSafeCollisionPadding(collisionPadding);\n  const overflowPadding = toOverflowPadding(safeCollisionPadding);\n\n  // The closest annotated ancestor bounds the content.\n  const boundary = useMemo(() => {\n    const closest = triggerRef.current?.closest<HTMLElement>('[data-popover-collision-boundary]') ?? null;\n    const given = Array.isArray(collisionBoundary) ? collisionBoundary : collisionBoundary ? [collisionBoundary] : [];\n    const elements = [closest, ...given].filter((element): element is Element => !!element);\n    return elements.length ? () => elements : undefined;\n    // The trigger is read when the content opens, which is when the boundary matters.\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [open, collisionBoundary]);\n\n  const positioning = useMemo<Positioning>(\n    () => ({\n      strategy: 'fixed',\n      placement: toPlacement(side, align),\n      gutter: sideOffset,\n      ...(alignOffset !== undefined && { offset: { mainAxis: sideOffset, crossAxis: alignOffset } }),\n      overflowPadding,\n      // Keeps the arrow off the rounded corners, where its fill would paint over the curve.\n      arrowPadding: 12,\n      flip: avoidCollisions,\n      hideWhenDetached,\n      boundary,\n      ...(virtualAnchor && { getAnchorElement: () => toAnchorElement(virtualAnchor.current) }),\n    }),\n    [side, align, sideOffset, alignOffset, overflowPadding, avoidCollisions, hideWhenDetached, boundary, virtualAnchor],\n  );\n\n  return { positioning, setVirtualAnchor };\n};\n","//\n// Copyright 2023 DXOS.org\n//\n\nimport { useCallback, useState } from 'react';\n\nimport { useViewportResize } from '@dxos/react-hooks';\n\nexport const useVisualViewport = (deps?: Parameters<typeof useViewportResize>[1]) => {\n  const [width, setWidth] = useState<number | null>(null);\n  const [height, setHeight] = useState<number | null>(null);\n\n  const handleResize = useCallback(() => {\n    if (window.visualViewport) {\n      setWidth(window.visualViewport.width);\n      setHeight(window.visualViewport.height);\n    }\n  }, []);\n\n  useViewportResize(handleResize, deps);\n\n  return { width, height };\n};\n","//\n// Copyright 2026 DXOS.org\n//\n\n// @import-as-namespace\n\n// General-purpose hooks. A hook tied to one component family lives in that family's namespace.\n\n// Named rather than `export *`, so a release of `@dxos/react-hooks` cannot change this package's API.\nexport {\n  type AtomState,\n  type PossibleRef,\n  type UseControllableStateParams,\n  type UseMediaQueryOptions,\n  composeEventHandlers,\n  composeRefs,\n  createContext,\n  makeId,\n  mergeRefs,\n  randomString,\n  setRef,\n  useAsyncEffect,\n  useAsyncState,\n  useAtomState,\n  useComposedRefs,\n  useControllableState,\n  useControlledState,\n  useDebugDeps,\n  useDefaults,\n  useDefaultValue,\n  useDynamicRef,\n  useFileDownload,\n  useForwardedRef,\n  useId,\n  useInterval,\n  useIsFocused,\n  useMediaQuery,\n  useMergeRefs,\n  useMulticastObservable,\n  useOnTransition,\n  useRefCallback,\n  useScroller,\n  useSize,\n  useStable,\n  useStateWithRef,\n  useTimeout,\n  useViewportResize,\n} from '@dxos/react-hooks';\n\nexport { useTranslation } from '../providers/ThemeProvider/TranslationsContext.ts';\nexport * from './theme.ts';\nexport * from '../util/animation.ts';\nexport * from '../util/height-memory.ts';\nexport * from '../util/usePx.ts';\nexport * from './usePositioning.ts';\nexport * from './useSafeArea.ts';\nexport * from './useSafeCollisionPadding.ts';\nexport * from './useVisualViewport.ts';\n"],"mappings":";;;;;;;;;AAWA,IAAa,qBAAgC,gBAAgB,CAAC,CAAC;;AAG/D,IAAa,oBAAsD,gBAAgB,CAAC,CAAC;;AAGrF,IAAa,uBAAgC,gBAAgB,CAAC,CAAC;;;;ACJ/D,IAAM,cAAc;;AAGpB,IAAa,2BAAyC;CACpD,MAAM,0BAAU,IAAI,IAAo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