{"version":3,"file":"Popover.mjs","names":["hasAnchorPortal"],"sources":["../../../src/popover/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  FloatingFocusManager,\n  FloatingPortal,\n  type OffsetOptions,\n  type Placement,\n  useMergeRefs,\n} from '@floating-ui/react';\nimport classNames from 'classnames';\nimport React from 'react';\n\nimport {\n  type CommonProps,\n  type PolymorphicCommonProps,\n} from '../_common/types';\nimport { ConditionalWrap } from '../conditional-wrap';\nimport { useIsInsideDialog } from '../dialog/dialog-context';\nimport { useNeedleTheme } from '../theme/ThemeProvider';\nimport { type AnchorPosition, usePopover } from './use-popover';\nimport { usePatchFloatingFocusGuards } from './usePatchFloatingFocusGuards';\n\ntype PlacementOptions =\n  | 'bottom-end-top-end'\n  | 'bottom-start-top-start'\n  | 'bottom-middle-top-middle'\n  | 'top-end-bottom-end'\n  | 'top-start-bottom-start'\n  | 'top-middle-bottom-middle'\n  | 'bottom-start-bottom-end'\n  | 'top-start-top-end'\n  | 'middle-start-middle-end'\n  | 'bottom-end-bottom-start'\n  | 'top-end-top-start'\n  | 'middle-end-middle-start';\n\nexport const placementTranslation: Record<PlacementOptions, Placement> = {\n  'bottom-end-bottom-start': 'right-end',\n  'bottom-end-top-end': 'bottom-end',\n  'bottom-middle-top-middle': 'bottom',\n  'bottom-start-bottom-end': 'left-end',\n  'bottom-start-top-start': 'bottom-start',\n  'middle-end-middle-start': 'right',\n  'middle-start-middle-end': 'left',\n  'top-end-bottom-end': 'top-end',\n  'top-end-top-start': 'right-start',\n  'top-middle-bottom-middle': 'top',\n  'top-start-bottom-start': 'top-start',\n  'top-start-top-end': 'left-start',\n};\ntype PopoverProps = {\n  /** if the popover is open */\n  isOpen?: boolean;\n  /** The content of the popover */\n  children?: React.ReactNode;\n  /**\n   * Can be used if the anchor should be separated from the trigger.\n   * Note: to visually separate the anchor from the trigger, use the Popover.Anchor component instead.\n   */\n  anchorElement?: Element | null;\n  /** If the anchorPosition is provided the anchorElement will be ignored */\n  anchorPosition?: AnchorPosition;\n  /**\n   * The placement of the floating element is determined by two sets of words. The first set of words specifies the point on the anchor\n   * element where the floating element will be attached. The second set of coordinates specifies the point on the floating element that\n   * will attach to the anchor element.\n   * */\n  placement?: PlacementOptions;\n  /** can take either a number, an object or a function. Full documentation here:  https://floating-ui.com/docs/offset   */\n  offset?: OffsetOptions;\n  /**\n   * Called when an internal state change is triggered, when the floating element either opens or closes. It is not called when `isOpen`\n   * is updated by the consumer.\n   * */\n  onOpenChange?: (isOpen: boolean, event?: Event) => void;\n  /** Use anchorEl for portal's container. Should be used if the popover is portaled and is inside a Dialog for example. */\n  hasAnchorPortal?: boolean;\n  /** If the popover should constrain the focus inside the popover while open */\n  shouldCaptureFocus?: boolean;\n  /** Which element to initially focus. */\n  initialFocus?: number | React.RefObject<HTMLElement | null>;\n  /** The role of the popover. */\n  role?:\n    | 'tooltip'\n    | 'dialog'\n    | 'alertdialog'\n    | 'menu'\n    | 'listbox'\n    | 'grid'\n    | 'tree';\n  /** If the popover should close when the user clicks outside of it */\n  shouldCloseOnClickOutside?: boolean;\n  /**\n   * If the popover should close when focus moves outside of it, for example when tabbing away or focusing another element.\n   * Defaults to the value of `shouldCloseOnClickOutside`.\n   * Has no effect when `shouldCaptureFocus` is true, since focus cannot leave the popover.\n   */\n  shouldCloseOnFocusOut?: boolean;\n  /** The strategy of the popover. Fixed should be used when the popover is inside a Dialog. */\n  strategy?: 'fixed' | 'absolute';\n  /** Whether the content is rendered in a portal */\n  isPortaled?: boolean;\n  /**\n   * When true, limits the popover's max-height and max-width to the available viewport space.\n   * The consumer is responsible for adding overflow handling (e.g. overflow-y: auto) to the content.\n   * */\n  shouldConstrainSize?: boolean;\n};\n\ntype ContextType = ReturnType<typeof usePopover> | null;\n\nconst PopoverContext = React.createContext<ContextType>(null);\n\nexport const usePopoverContext = () => {\n  const context = React.useContext(PopoverContext);\n\n  if (context === null) {\n    throw new Error('Popover components must be wrapped in <Popover />');\n  }\n\n  return context;\n};\n\nconst PopoverComponent = ({\n  children,\n  anchorElement,\n  placement,\n  isOpen,\n  offset,\n  anchorPosition,\n  hasAnchorPortal: hasAnchorPortalProp,\n  shouldCaptureFocus = false,\n  initialFocus,\n  onOpenChange,\n  shouldCloseOnClickOutside = true,\n  shouldCloseOnFocusOut,\n  isPortaled: isPortaledProp,\n  strategy: strategyProp,\n  role,\n  shouldConstrainSize,\n}: PopoverProps) => {\n  const isInsideDialog = useIsInsideDialog();\n\n  const strategy = isInsideDialog ? 'fixed' : 'absolute';\n  const hasAnchorPortal = isInsideDialog;\n  const isPortaled = !isInsideDialog;\n\n  const popover = usePopover({\n    anchorElement,\n    anchorPosition,\n    hasAnchorElementAsPortalAnchor: hasAnchorPortalProp ?? hasAnchorPortal,\n    initialFocus,\n    isOpen: isOpen,\n    isPortaled: isPortaledProp ?? isPortaled,\n    offsetOption: offset,\n    onOpenChange: onOpenChange,\n    placement: placement ? placementTranslation[placement] : undefined,\n    role,\n    shouldCaptureFocus,\n    shouldCloseOnClickOutside,\n    shouldCloseOnFocusOut: shouldCloseOnFocusOut ?? shouldCloseOnClickOutside,\n    shouldConstrainSize,\n    strategy: strategyProp ?? strategy,\n  });\n\n  return (\n    <PopoverContext.Provider value={popover}>\n      {children}\n    </PopoverContext.Provider>\n  );\n};\n\ntype PopoverTriggerProps = {\n  /** The content of the trigger */\n  children: React.ReactNode;\n  /** Whether the trigger should render a button element, or pass its props to the child element */\n  hasButtonWrapper?: boolean;\n};\n\nconst PopoverTrigger = ({\n  children,\n  hasButtonWrapper = false,\n  ref,\n  ...props\n}: CommonProps<'button', PopoverTriggerProps>) => {\n  const context = usePopoverContext();\n  // Example from floating UI\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  const childrenProps = (children as any).props;\n  const mergedRefs = useMergeRefs([\n    context.refs.setReference,\n    ref,\n    childrenProps?.ref,\n  ]);\n\n  // `hasButtonWrapper` allows the user to pass any element as the anchor\n  if (hasButtonWrapper && React.isValidElement(children)) {\n    return React.cloneElement(\n      children,\n      context.getReferenceProps({\n        ...props,\n        ...childrenProps,\n        'data-state': context.isOpen ? 'open' : 'closed',\n        ref: mergedRefs,\n      }),\n    );\n  }\n  return (\n    <button\n      ref={context.refs.setReference}\n      type=\"button\"\n      data-state={context.isOpen ? 'open' : 'closed'}\n      {...context.getReferenceProps(props)}\n    >\n      {children}\n    </button>\n  );\n};\n\ntype PopoverAnchorProps = {\n  children?: React.ReactNode;\n};\n\n/**\n * Use this to separate the anchor visually from the trigger.\n */\nconst PopoverAnchor = ({\n  children,\n  ref,\n  ...props\n}: CommonProps<'div', PopoverAnchorProps>) => {\n  const context = usePopoverContext();\n\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  const childrenProps = (children as any)?.props;\n  const mergedRefs = useMergeRefs([\n    context.refs.setPositionReference,\n    ref,\n    childrenProps?.ref,\n  ]);\n\n  if (React.isValidElement(children)) {\n    return React.cloneElement(children, {\n      ...props,\n      ...childrenProps,\n      ref: mergedRefs,\n    });\n  }\n\n  return (\n    <div ref={mergedRefs} {...props}>\n      {children}\n    </div>\n  );\n};\n\ntype PopoverContentProps = {\n  children: React.ReactNode;\n  className?: string;\n  style?: React.CSSProperties;\n};\n\nconst PopoverContent = <T extends React.ElementType = 'div'>({\n  as,\n  className,\n  style,\n  children,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: PolymorphicCommonProps<T, PopoverContentProps>) => {\n  const { context: floatingContext, ...context } = usePopoverContext();\n\n  const refs = useMergeRefs([context.refs.setFloating, ref]);\n\n  const { themeClassName } = useNeedleTheme();\n\n  const classes = classNames('ndl-popover', themeClassName, className);\n  const Component: React.ElementType = as ?? 'div';\n\n  // Patch to not get axe errors for the focus guards\n  // To be noted is that axe gives us a false positive error here since the focus guards directly pass focus to other elements\n  // https://github.com/floating-ui/floating-ui/issues/2462\n  usePatchFloatingFocusGuards();\n\n  if (!floatingContext.open) {\n    return null;\n  }\n\n  return (\n    <ConditionalWrap\n      shouldWrap={context.isPortaled}\n      wrap={(wrapChildren) => (\n        <FloatingPortal\n          root={\n            (context.hasAnchorElementAsPortalAnchor ?? false)\n              ? (context.refs.reference.current as HTMLElement | null)\n              : undefined\n          }\n        >\n          {wrapChildren}\n        </FloatingPortal>\n      )}\n    >\n      <FloatingFocusManager\n        context={floatingContext}\n        modal={context.shouldCaptureFocus}\n        initialFocus={context.initialFocus}\n        closeOnFocusOut={context.shouldCloseOnFocusOut}\n      >\n        <Component\n          className={classes}\n          style={{\n            ...context.floatingStyles,\n            ...context.transitionStyles,\n            ...style,\n          }}\n          ref={refs}\n          aria-labelledby={context.labelId}\n          aria-describedby={context.descriptionId}\n          {...context.getFloatingProps({ ...htmlAttributes })}\n          {...restProps}\n        >\n          {children}\n        </Component>\n      </FloatingFocusManager>\n    </ConditionalWrap>\n  );\n};\n\nconst Popover = Object.assign(PopoverComponent, {\n  Anchor: PopoverAnchor,\n  Content: PopoverContent,\n  Trigger: PopoverTrigger,\n});\n\nexport { Popover };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAsME;CACA;CACA;;AA6CA,MAAA,aAAA,CAAA,YACA,KAAA;;CAmCA;CACA;CACA;CACA;CACA;CACA;;AAGQ,MAAA,aAAA,CAAA,SAAA;AA3OV,MAAa,uBAA4D;CACvE,2BAA2B;CAC3B,sBAAsB;CACtB,4BAA4B;CAC5B,2BAA2B;CAC3B,0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