{"version":3,"file":"chunk-Tooltip.mjs","names":["TooltipPrimitive"],"sources":["../../src/components/Tooltip/Tooltip.tsx"],"sourcesContent":["//\n// Copyright 2026 DXOS.org\n//\n\n// @import-as-namespace\n\nimport { Portal } from '@ark-ui/react/portal';\nimport { Tooltip as TooltipPrimitive, useTooltipContext } from '@ark-ui/react/tooltip';\nimport React, {\n  type ComponentPropsWithoutRef,\n  type ReactNode,\n  type RefObject,\n  createContext,\n  forwardRef,\n  useContext,\n  useEffect,\n  useRef,\n  useState,\n} from 'react';\n\nimport { useComposedRefs } from '@dxos/react-hooks';\nimport { mx } from '@dxos/ui-theme';\nimport { type ThemedClassName } from '@dxos/ui-types';\n\nimport { recipes } from '../../recipes.ts';\nimport { type Size } from '../../sizes.ts';\nimport { usePopupSize } from '../ScrollArea/PopupScroll.tsx';\n\n/** Gap between trigger and popup, in px (positioning takes a number, not a CSS variable). */\nconst POPUP_GUTTER = 2;\n\n/** Short enough to feel responsive, long enough that sweeping the pointer across a toolbar shows nothing. */\nconst OPEN_DELAY = 600;\n\ntype TooltipSide = 'top' | 'right' | 'bottom' | 'left';\n\n/** The Root's `openDelay`, read by the Trigger, which runs the hover delay itself (DESIGN.md follow-up 33). */\nconst OpenDelayContext = createContext(OPEN_DELAY);\n\n//\n// Root\n//\n\ntype TooltipRootProps = TooltipPrimitive.RootProps;\n\n/** Ark tooltip; content mounts on open and unmounts on close unless the caller opts out. */\nconst TooltipRoot = ({\n  openDelay = OPEN_DELAY,\n  lazyMount = true,\n  unmountOnExit = true,\n  positioning,\n  ...props\n}: TooltipRootProps) => (\n  <OpenDelayContext.Provider value={openDelay}>\n    <TooltipPrimitive.Root\n      {...props}\n      openDelay={openDelay}\n      lazyMount={lazyMount}\n      unmountOnExit={unmountOnExit}\n      positioning={{ gutter: POPUP_GUTTER, ...positioning }}\n    />\n  </OpenDelayContext.Provider>\n);\n\nTooltipRoot.displayName = 'Tooltip.Root';\n\n//\n// Trigger\n//\n\ntype TooltipTriggerProps = Omit<TooltipPrimitive.TriggerProps, 'content'> & {\n  /** Shorthand, as on the current `Tooltip.Trigger`: the trigger brings its own Root and a Content showing this (any node, so it replaces the HTML `content` attribute). */\n  content?: ReactNode;\n  /** With `content`, the side the tooltip opens on; below by default. */\n  side?: TooltipSide;\n};\n\n/**\n * Use `asChild` to describe a `Button`. Opens on hover after the Root's delay and on keyboard\n * focus only; the delay runs here because zag skips it while any tooltip is marked open, so a click would flash one\n * (DESIGN.md follow-up 33). With `content` it needs no Root or Content around it.\n */\nconst TooltipTrigger = forwardRef<HTMLButtonElement, TooltipTriggerProps>(\n  ({ content, side, ...props }, forwardedRef) =>\n    content === undefined ? (\n      <TooltipTriggerImpl {...props} ref={forwardedRef} />\n    ) : (\n      <TooltipRoot positioning={side ? { placement: side } : undefined}>\n        <TooltipTriggerImpl {...props} ref={forwardedRef} />\n        <TooltipContent>{content}</TooltipContent>\n      </TooltipRoot>\n    ),\n);\n\nTooltipTrigger.displayName = 'Tooltip.Trigger';\n\nconst TooltipTriggerImpl = forwardRef<HTMLButtonElement, TooltipPrimitive.TriggerProps>(\n  (\n    {\n      onBlur,\n      onFocus,\n      onKeyDown,\n      onPointerMove,\n      onPointerOver,\n      onPointerEnter,\n      onPointerLeave,\n      onPointerDown,\n      ...props\n    },\n    forwardedRef,\n  ) => {\n    const tooltip = useTooltipContext();\n    const openDelay = useContext(OpenDelayContext);\n    const timer = useRef<ReturnType<typeof setTimeout>>(undefined);\n    const closeTimer = useRef<ReturnType<typeof setTimeout>>(undefined);\n    const pressed = useRef(false);\n    const cancel = () => clearTimeout(timer.current);\n    useEffect(\n      () => () => {\n        cancel();\n        clearTimeout(closeTimer.current);\n      },\n      [],\n    );\n\n    return (\n      <TooltipPrimitive.Trigger\n        {...props}\n        onPointerMove={(event) => {\n          onPointerMove?.(event);\n          event.preventDefault();\n        }}\n        onPointerOver={(event) => {\n          onPointerOver?.(event);\n          event.preventDefault();\n        }}\n        onPointerEnter={(event) => {\n          onPointerEnter?.(event);\n          // An enter is a fresh hover: a press can only be stale here (its leave was never seen).\n          pressed.current = false;\n          if (event.defaultPrevented || event.pointerType === 'touch') {\n            return;\n          }\n          cancel();\n          timer.current = setTimeout(() => {\n            if (!pressed.current) {\n              tooltip.setOpen(true);\n            }\n          }, openDelay);\n        }}\n        onPointerLeave={(event) => {\n          onPointerLeave?.(event);\n          pressed.current = false;\n          cancel();\n        }}\n        onPointerDown={(event) => {\n          onPointerDown?.(event);\n          pressed.current = true;\n          cancel();\n          tooltip.setOpen(false);\n        }}\n        onKeyDown={(event) => {\n          onKeyDown?.(event);\n          // Keyboard use ends the press, so the next keyboard focus shows the label again.\n          pressed.current = false;\n        }}\n        onFocus={(event) => {\n          onFocus?.(event);\n          if (pressed.current || !event.currentTarget.matches(':focus-visible')) {\n            event.preventDefault();\n          }\n        }}\n        onBlur={(event) => {\n          onBlur?.(event);\n          // The press that focused the trigger is over once focus leaves; a later focus is judged on its own.\n          pressed.current = false;\n          if (event.defaultPrevented) {\n            return;\n          }\n          // Deferred so a tooltip opened by the same focus move claims zag's shared store first (DESIGN.md follow-up 20).\n          event.preventDefault();\n          // A pending hover would otherwise reopen the tooltip on a trigger that no longer has focus.\n          cancel();\n          closeTimer.current = setTimeout(() => tooltip.setOpen(false));\n        }}\n        ref={forwardedRef}\n      />\n    );\n  },\n);\n\n//\n// Content\n//\n\ntype TooltipContentProps = ThemedClassName<TooltipPrimitive.ContentProps> & {\n  /** Overrides the size inherited from the trigger's nearest sized ancestor (Phase 4 decision 2); `sm` without one. */\n  size?: Size;\n  /** Point at the trigger with an arrow in the tooltip's inverted surface colour. */\n  arrow?: boolean;\n  /** Portals into this element instead of the body (e.g. a sized scope, AUDIT 2.2). */\n  container?: RefObject<HTMLElement | null>;\n};\n\n/**\n * Portalled text on the inverted surface (as the current Tooltip; no `data-surface`, whose popup fill it would fight),\n * capped at 20rem wide, with an arrow unless `arrow={false}`.\n */\nconst TooltipContent = forwardRef<HTMLDivElement, TooltipContentProps>(\n  ({ classNames, size, arrow = true, container, children, ...props }, forwardedRef) => {\n    const tooltip = useTooltipContext();\n    const popupSize = usePopupSize(size, tooltip.open, [tooltip.getTriggerProps().id], 'sm');\n    return (\n      <Portal container={container}>\n        <TooltipPrimitive.Positioner>\n          <TooltipPrimitive.Content\n            {...props}\n            data-size={popupSize}\n            className={mx(recipes.popup(), recipes.tooltipContent(), classNames)}\n            ref={forwardedRef}\n          >\n            {children}\n            {arrow && (\n              <TooltipPrimitive.Arrow className={recipes.arrow()}>\n                <TooltipPrimitive.ArrowTip className={recipes.arrowTip()} />\n              </TooltipPrimitive.Arrow>\n            )}\n          </TooltipPrimitive.Content>\n        </TooltipPrimitive.Positioner>\n      </Portal>\n    );\n  },\n);\n\nTooltipContent.displayName = 'Tooltip.Content';\n//\n// TextTooltip\n//\n\ntype TextTooltipProps = ThemedClassName<Omit<ComponentPropsWithoutRef<'span'>, 'children'>> & {\n  /** Rendered truncated to one line; the tooltip shows it in full, and only while it is cut off. */\n  text: string;\n  side?: TooltipSide;\n};\n\n/** One line of text that ellipsizes, with a tooltip of the full text on hover only when it is truncated. */\nexport const TextTooltip = forwardRef<HTMLSpanElement, TextTooltipProps>(\n  ({ classNames, text, side, ...props }, forwardedRef) => {\n    const [open, setOpen] = useState(false);\n    const localRef = useRef<HTMLSpanElement>(null);\n    const ref = useComposedRefs(forwardedRef, localRef);\n    return (\n      <TooltipRoot\n        open={open}\n        // Measured when the tooltip would open, so a resize that truncates the text needs no observer.\n        onOpenChange={({ open }) => {\n          const element = localRef.current;\n          setOpen(open && !!element && element.scrollWidth > element.clientWidth);\n        }}\n        positioning={side ? { placement: side } : undefined}\n      >\n        <TooltipTriggerImpl asChild>\n          <span\n            {...props}\n            data-scope='text-tooltip'\n            data-part='root'\n            className={mx(recipes.textTooltip(), classNames)}\n            ref={ref}\n          >\n            {text}\n          </span>\n        </TooltipTriggerImpl>\n        <TooltipContent>{text}</TooltipContent>\n      </TooltipRoot>\n    );\n  },\n);\n\nTextTooltip.displayName = 'TextTooltip';\n\nexport type {\n  TooltipContentProps as ContentProps,\n  TooltipRootProps as RootProps,\n  TextTooltipProps,\n  TooltipTriggerProps as TriggerProps,\n};\n\nexport { TooltipContent as Content, TooltipRoot as Root, TooltipTrigger as Trigger };\nexport type { TooltipSide as Side };\n"],"mappings":";;;;;;;;;;;;;;;;;AA6BA,IAAM,eAAe;;AAGrB,IAAM,aAAa;;AAKnB,IAAM,mBAAmB,cAAc,UAAU;;AASjD,IAAM,eAAe,EACnB,YAAY,YACZ,YAAY,MACZ,gBAAgB,MAChB,aACA,GAAG,YAEH,oBAAC,iBAAiB,UAAlB;CAA2B,OAAO;CAChC,UAAA,oBAACA,QAAiB,MAAlB;EACE,GAAI;EACO;EACA;EACI;EACf,aAAa;GAAE,QAAQ;GAAc,GAAG;EAAY;CACrD,CAAA;AACwB,CAAA;AAG7B,YAAY,cAAc;;;;;;AAkB1B,IAAM,iBAAiB,YACpB,EAAE,SAAS,MAAM,GAAG,SAAS,iBAC5B,YAAY,KAAA,IACV,oBAAC,oBAAD;CAAoB,GAAI;CAAO,KAAK;AAAe,CAAA,IAEnD,qBAAC,aAAD;CAAa,aAAa,OAAO,EAAE,WAAW,KAAK,IAAI,KAAA;CAAvD,UAAA,CACE,oBAAC,oBAAD;EAAoB,GAAI;EAAO,KAAK;CAAe,CAAA,GACnD,oBAAC,gBAAD,EAAA,UAAiB,QAAwB,CAAA,CAC9B;AAEnB,CAAA,CAAA;AAEA,eAAe,cAAc;AAE7B,IAAM,qBAAqB,YAEvB,EACE,QACA,SACA,WACA,eACA,eACA,gBACA,gBACA,eACA,GAAG,SAEL,iBACG;CACH,MAAM,UAAU,kBAAkB;CAClC,MAAM,YAAY,WAAW,gBAAgB;CAC7C,MAAM,QAAQ,OAAsC,KAAA,CAAS;CAC7D,MAAM,aAAa,OAAsC,KAAA,CAAS;CAClE,MAAM,UAAU,OAAO,KAAK;CAC5B,MAAM,eAAe,aAAa,MAAM,OAAO;CAC/C,sBACc;EACV,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