{"version":3,"file":"use-tooltip.mjs","names":[],"sources":["../../../src/tooltip/use-tooltip.ts"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport {\n  autoUpdate,\n  type AutoUpdateOptions,\n  type ElementProps,\n  flip,\n  offset,\n  type Placement,\n  type ReferenceType,\n  safePolygon,\n  shift,\n  useClick,\n  useClientPoint,\n  useDismiss,\n  useFloating,\n  type UseFloatingOptions,\n  type UseFloatingReturn,\n  useFocus,\n  useHover,\n  useInteractions,\n  type UseInteractionsReturn,\n  useRole,\n} from '@floating-ui/react';\nimport {\n  createContext,\n  type PointerEvent as ReactPointerEvent,\n  useContext,\n  useId,\n  useMemo,\n  useRef,\n  useState,\n} from 'react';\n\nexport interface TooltipOptions {\n  isInitialOpen?: boolean;\n  placement?: Placement;\n  isOpen?: boolean;\n  onOpenChange?: UseFloatingOptions['onOpenChange'];\n  type?: 'simple' | 'rich';\n  isPortaled?: boolean;\n  strategy?: 'fixed' | 'absolute';\n  hoverDelay?: {\n    open: number;\n    close: number;\n  };\n  shouldCloseOnReferenceClick?: boolean;\n  autoUpdateOptions?: AutoUpdateOptions;\n  isDisabled?: boolean;\n  /**\n   * Whether the tooltip should be positioned at the mouse cursor. Only applies to 'simple' tooltips with mouse hover.\n   * `true` follows the cursor continuously along one axis derived from `placement` (horizontal for top/bottom, vertical for left/right).\n   * `'initial'` opens the tooltip at the cursor's position (both axes) and keeps it there, like the native `title` attribute; `placement` controls which side of that point the tooltip appears on.\n   */\n  followCursor?: boolean | 'initial';\n}\n\nexport function useTooltip({\n  isInitialOpen = false,\n  placement = 'top',\n  isOpen: controlledOpen,\n  onOpenChange: setControlledOpen,\n  type = 'simple',\n  isPortaled = true,\n  strategy = 'absolute',\n  hoverDelay = undefined,\n  shouldCloseOnReferenceClick = false,\n  autoUpdateOptions,\n  isDisabled = false,\n  followCursor = false,\n}: TooltipOptions = {}): {\n  headerId?: string;\n  setHeaderId?: (id: string | undefined) => void;\n  isOpen: boolean;\n  setOpen: (open: boolean) => void;\n  type: 'simple' | 'rich';\n  isPortaled: boolean;\n} & UseInteractionsReturn &\n  UseFloatingReturn<ReferenceType> {\n  const id = useId();\n  const [headerId, setHeaderId] = useState<string | undefined>(id);\n  const [isUncontrolledOpen, setIsUncontrolledOpen] = useState(isInitialOpen);\n  const isOpen = controlledOpen ?? isUncontrolledOpen;\n  const setOpen: NonNullable<UseFloatingOptions['onOpenChange']> =\n    setControlledOpen ?? setIsUncontrolledOpen;\n\n  // For followCursor='initial': the latest pointer position over the trigger,\n  // and a snapshot of it frozen at the moment the tooltip opens.\n  const pointerPositionRef = useRef<{\n    // oxlint-disable-next-line id-length\n    x: number;\n    // oxlint-disable-next-line id-length\n    y: number;\n  } | null>(null);\n  const [initialCursorPoint, setInitialCursorPoint] = useState<{\n    // oxlint-disable-next-line id-length\n    x: number;\n    // oxlint-disable-next-line id-length\n    y: number;\n  } | null>(null);\n\n  const data = useFloating({\n    middleware: [\n      offset(5),\n      flip({\n        crossAxis: placement.includes('-'),\n        fallbackAxisSideDirection: 'start',\n        padding: 5,\n      }),\n      shift({ padding: 5 }),\n    ],\n    onOpenChange(open, event, reason) {\n      if (followCursor === 'initial') {\n        // Only anchor to the cursor for pointer-driven opens; keyboard focus\n        // opens fall back to positioning relative to the trigger element.\n        const isPointerDrivenOpen = event !== undefined && 'clientX' in event;\n        setInitialCursorPoint(\n          open && isPointerDrivenOpen ? pointerPositionRef.current : null,\n        );\n      }\n      setOpen(open, event, reason);\n    },\n    open: isOpen,\n    placement,\n    strategy,\n    whileElementsMounted(referenceEl, floatingEl, update) {\n      const cleanup = autoUpdate(referenceEl, floatingEl, update, {\n        ...autoUpdateOptions,\n      });\n      return cleanup;\n    },\n  });\n\n  const context = data.context;\n\n  const hover = useHover(context, {\n    delay: hoverDelay,\n    enabled: type === 'simple' && !isDisabled,\n    handleClose: safePolygon(),\n    move: false,\n  });\n  const click = useClick(context, {\n    enabled: type === 'rich' && !isDisabled,\n  });\n  const focus = useFocus(context, {\n    enabled: type === 'simple' && !isDisabled,\n    visibleOnly: true,\n  });\n  const dismiss = useDismiss(context, {\n    escapeKey: true,\n    outsidePress: true,\n    referencePress: shouldCloseOnReferenceClick,\n  });\n  const role = useRole(context, {\n    role: type === 'simple' ? 'tooltip' : 'dialog',\n  });\n  const followAxis =\n    placement.startsWith('left') || placement.startsWith('right') ? 'y' : 'x';\n  const isInitialFollowCursor = followCursor === 'initial';\n  const clientPoint = useClientPoint(context, {\n    // 'initial' anchors to the exact cursor point (both axes); `true` follows\n    // continuously along the axis derived from `placement`.\n    axis: isInitialFollowCursor ? 'both' : followAxis,\n    enabled: followCursor !== false && type === 'simple' && !isDisabled,\n    // Explicit coordinates lock the position: useClientPoint stops tracking\n    // pointer movement when they are provided.\n    ...(isInitialFollowCursor && {\n      // oxlint-disable-next-line id-length\n      x: initialCursorPoint?.x ?? null,\n      // oxlint-disable-next-line id-length\n      y: initialCursorPoint?.y ?? null,\n    }),\n  });\n\n  const initialCursorTracking = useMemo<ElementProps>(() => {\n    if (!isInitialFollowCursor) {\n      return {};\n    }\n    const capturePointerPosition = (event: ReactPointerEvent) => {\n      pointerPositionRef.current = {\n        // oxlint-disable-next-line id-length\n        x: event.clientX,\n        // oxlint-disable-next-line id-length\n        y: event.clientY,\n      };\n    };\n    return {\n      reference: {\n        onPointerEnter: capturePointerPosition,\n        onPointerMove: capturePointerPosition,\n      },\n    };\n  }, [isInitialFollowCursor]);\n\n  const interactions = useInteractions([\n    hover,\n    focus,\n    dismiss,\n    role,\n    click,\n    clientPoint,\n    initialCursorTracking,\n  ]);\n\n  return useMemo(\n    () => ({\n      headerId: type === 'rich' ? headerId : undefined,\n      isOpen,\n      isPortaled,\n      setHeaderId: type === 'rich' ? setHeaderId : undefined,\n      setOpen,\n      type,\n      ...interactions,\n      ...data,\n    }),\n    [headerId, isOpen, isPortaled, setOpen, type, interactions, data],\n  );\n}\n\ntype ContextType = ReturnType<typeof useTooltip> | null;\n\nexport const TooltipContext = createContext<ContextType>(null);\n\nexport const useTooltipContext = (): ReturnType<typeof useTooltip> => {\n  const context = useContext(TooltipContext);\n\n  if (context === null) {\n    throw new Error('Tooltip components must be wrapped in <Tooltip />');\n  }\n\n  return context;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA2EA,SAAgB,WAAW,EACzB,gBAAgB,OAChB,YAAY,OACZ,QAAQ,gBACR,cAAc,mBACd,OAAO,UACP,aAAa,MACb,WAAW,YACX,aAAa,KAAA,GACb,8BAA8B,OAC9B,mBACA,aAAa,OACb,eAAe,UACG,CAAC,GAQc;;CACjC,MAAM,KAAK,MAAM;CACjB,MAAM,CAAC,UAAU,eAAe,SAA6B,EAAE;CAC/D,MAAM,CAAC,oBAAoB,yBAAyB,SAAS,aAAa;CAC1E,MAAM,SAAS,mBAAA,QAAA,mBAAA,KAAA,IAAA,iBAAkB;CACjC,MAAM,UACJ,sBAAA,QAAA,sBAAA,KAAA,IAAA,oBAAqB;CAIvB,MAAM,qBAAqB,OAKjB,IAAI;CACd,MAAM,CAAC,oBAAoB,yBAAyB,SAK1C,IAAI;CAEd,MAAM,OAAO,YAAY;EACvB,YAAY;GACV,OAAO,CAAC;GACR,KAAK;IACH,WAAW,UAAU,SAAS,GAAG;IACjC,2BAA2B;IAC3B,SAAS;GACX,CAAC;GACD,MAAM,EAAE,SAAS,EAAE,CAAC;EACtB;EACA,aAAa,MAAM,OAAO,QA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