{"version":3,"file":"use-popover.mjs","names":[],"sources":["../../../src/popover/use-popover.tsx"],"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  flip,\n  type FloatingFocusManagerProps,\n  offset,\n  type OffsetOptions,\n  type Placement,\n  shift,\n  size,\n  useClick,\n  useClientPoint,\n  useDismiss,\n  useFloating,\n  useInteractions,\n  type UseInteractionsReturn,\n  useRole,\n  useTransitionStyles,\n} from '@floating-ui/react';\nimport { tokens } from '@neo4j-ndl/base';\nimport { useMemo, useState } from 'react';\n\n/**\n * Client coordinates used to anchor a floating element at a fixed point\n * (e.g. the cursor position for a context menu), instead of at an anchor element.\n */\nexport type AnchorPosition = {\n  // oxlint-disable-next-line id-length\n  x: number;\n  // oxlint-disable-next-line id-length\n  y: number;\n};\n\nexport interface PopoverOptions {\n  isInitialOpen?: boolean;\n  placement?: Placement;\n  isOpen?: boolean;\n  onOpenChange?: (open: boolean, event?: Event) => void;\n  offsetOption?: OffsetOptions;\n  anchorElement?: Element | null;\n  anchorPosition?: AnchorPosition;\n  hasAnchorElementAsPortalAnchor?: boolean;\n  shouldCaptureFocus?: boolean;\n  initialFocus?: number | React.RefObject<HTMLElement | null>;\n  role?:\n    | 'tooltip'\n    | 'dialog'\n    | 'alertdialog'\n    | 'menu'\n    | 'listbox'\n    | 'grid'\n    | 'tree';\n  shouldCloseOnClickOutside?: boolean;\n  shouldCloseOnReferencePress?: boolean;\n  shouldCloseOnFocusOut?: boolean;\n  returnFocus?: FloatingFocusManagerProps['returnFocus'];\n  strategy?: 'fixed' | 'absolute';\n  isPortaled?: boolean;\n  shouldConstrainSize?: boolean;\n}\n\nexport interface UsePopoverReturn\n  extends UseInteractionsReturn, ReturnType<typeof useFloating> {\n  isOpen: boolean;\n  labelId: string | undefined;\n  descriptionId: string | undefined;\n  transitionStyles: React.CSSProperties;\n  setLabelId: (labelId: string | undefined) => void;\n  setDescriptionId: (descriptionId: string | undefined) => void;\n  hasAnchorElementAsPortalAnchor: boolean | undefined;\n  shouldCaptureFocus: boolean | undefined;\n  shouldCloseOnFocusOut: boolean | undefined;\n  initialFocus: number | React.RefObject<HTMLElement | null> | undefined;\n  isPortaled: boolean;\n  returnFocus: FloatingFocusManagerProps['returnFocus'];\n}\n\nexport function usePopover({\n  isInitialOpen = false,\n  placement = 'bottom',\n  isOpen: controlledOpen,\n  onOpenChange: setControlledOpen,\n  offsetOption = 10,\n  anchorElement,\n  anchorPosition,\n  hasAnchorElementAsPortalAnchor,\n  shouldCaptureFocus,\n  initialFocus,\n  role: roleProp,\n  shouldCloseOnClickOutside,\n  shouldCloseOnReferencePress,\n  shouldCloseOnFocusOut,\n  returnFocus,\n  strategy = 'absolute',\n  isPortaled = true,\n  shouldConstrainSize = false,\n}: PopoverOptions = {}): UsePopoverReturn {\n  // Internal state for uncontrolled mode (no `isOpen` prop provided).\n  const [isOpenUncontrolled, setIsOpenUncontrolled] = useState(isInitialOpen);\n  const [labelId, setLabelId] = useState<string | undefined>();\n  const [descriptionId, setDescriptionId] = useState<string | undefined>();\n\n  // Controlled/uncontrolled pattern: prefer the consumer-provided `isOpen`,\n  // falling back to internal state when not provided.\n  const isOpen = controlledOpen ?? isOpenUncontrolled;\n\n  const data = useFloating({\n    elements: {\n      reference: anchorElement,\n    },\n    middleware: [\n      offset(offsetOption),\n      flip({\n        crossAxis: placement.includes('-'),\n        fallbackAxisSideDirection: 'end',\n        padding: 5,\n      }),\n      shift(),\n      ...(shouldConstrainSize\n        ? [\n            size({\n              apply({ availableHeight, availableWidth, elements }) {\n                Object.assign(elements.floating.style, {\n                  maxHeight: `${availableHeight}px`,\n                  maxWidth: `${availableWidth}px`,\n                });\n              },\n              padding: 5,\n            }),\n          ]\n        : []),\n    ],\n    // Floating UI calls this when interactions (useClick, useDismiss, etc.)\n    // trigger an open/close. We always sync internal state so uncontrolled\n    // mode works, and notify the consumer callback exactly once if provided.\n    onOpenChange: (open, event) => {\n      setIsOpenUncontrolled(open);\n      setControlledOpen?.(open, event);\n    },\n    open: isOpen,\n    placement: placement,\n    strategy,\n    whileElementsMounted: autoUpdate,\n  });\n  const context = data.context;\n\n  // Only enable built-in click-to-toggle in uncontrolled mode. In controlled\n  // mode the consumer owns the open state and wires up their own trigger.\n  const click = useClick(context, {\n    enabled: controlledOpen === undefined,\n  });\n\n  const dismiss = useDismiss(context, {\n    outsidePress: shouldCloseOnClickOutside,\n    referencePress: shouldCloseOnReferencePress,\n  });\n  const role = useRole(context, { role: roleProp });\n\n  const clientPoint = useClientPoint(context, {\n    enabled: anchorPosition !== undefined,\n    // oxlint-disable-next-line id-length\n    x: anchorPosition?.x,\n    // oxlint-disable-next-line id-length\n    y: anchorPosition?.y,\n  });\n\n  const interactions = useInteractions([click, dismiss, role, clientPoint]);\n\n  const { styles: transitionStyles } = useTransitionStyles(context, {\n    duration: Number.parseInt(tokens.motion.duration.quick) ?? 0,\n  });\n\n  return useMemo(\n    () => ({\n      ...data,\n      ...interactions,\n      descriptionId,\n      hasAnchorElementAsPortalAnchor,\n      initialFocus,\n      isOpen,\n      isPortaled,\n      labelId,\n      returnFocus,\n      setDescriptionId,\n      setLabelId,\n      shouldCaptureFocus,\n      shouldCloseOnFocusOut,\n      transitionStyles,\n    }),\n    [\n      isOpen,\n      interactions,\n      data,\n      transitionStyles,\n      labelId,\n      descriptionId,\n      hasAnchorElementAsPortalAnchor,\n      shouldCaptureFocus,\n      shouldC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