{"version":3,"file":"Tooltip.mjs","names":[],"sources":["../../../src/tooltip/Tooltip.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  type AutoUpdateOptions,\n  FloatingFocusManager,\n  FloatingPortal,\n  type Placement,\n  type UseFloatingOptions,\n  useMergeRefs,\n} from '@floating-ui/react';\nimport classNames from 'classnames';\nimport React, { useEffect } from 'react';\n\nimport { type CommonProps } from '../_common/types';\nimport { ConditionalWrap } from '../conditional-wrap';\nimport { useIsInsideDialog } from '../dialog/dialog-context';\nimport { useNeedleTheme } from '../theme';\nimport { Typography, type TypographyVariants } from '../typography';\nimport { TooltipContext, useTooltip, useTooltipContext } from './use-tooltip';\n\nexport type TooltipProps = {\n  /** Content displayed inside the tooltip. Wraps `Tooltip.Trigger`, `Tooltip.Content`, and optional parts */\n  children: React.ReactNode;\n  /** Whether the tooltip is disabled */\n  isDisabled?: boolean;\n  /** Whether the tooltip should be open on first render  */\n  isInitialOpen?: boolean;\n  /** Placement of the tooltip content relative to the trigger */\n  placement?: Placement;\n  /** Controls the open state . If omitted, the tooltip manages its own state */\n  isOpen?: boolean;\n  /**\n   *  Visual style of the tooltip content. `simple` renders plain text, `rich` renders a composable panel\n   */\n  type: 'simple' | 'rich';\n  /** Whether the content is rendered in a portal.  */\n  isPortaled?: boolean;\n  /** Callback fired when the open state changes, if omitted, the tooltip manages its own state using internal hooks */\n  onOpenChange?: UseFloatingOptions['onOpenChange'];\n  /** Positioning strategy of the tooltip. Defaults to `fixed` when inside a dialog, otherwise `absolute` */\n  floatingStrategy?: 'absolute' | 'fixed';\n  /** Hover open/close delays in milliseconds */\n  hoverDelay?: {\n    open: number;\n    close: number;\n  };\n  /** Whether the tooltip should close when the reference element is clicked. Useful when the trigger moves after click. */\n  shouldCloseOnReferenceClick?: boolean;\n  autoUpdateOptions?: AutoUpdateOptions;\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\nconst TooltipComponent = ({\n  children,\n  isDisabled = false,\n  type,\n  isInitialOpen,\n  placement,\n  isOpen,\n  onOpenChange,\n  isPortaled: isPortaledProp,\n  floatingStrategy: strategyProp,\n  hoverDelay,\n  shouldCloseOnReferenceClick,\n  autoUpdateOptions,\n  followCursor,\n}: TooltipProps) => {\n  const isInsideDialog = useIsInsideDialog();\n  const strategy = isInsideDialog ? 'fixed' : 'absolute';\n  const isPortaled = !isInsideDialog;\n\n  const tooltip = useTooltip({\n    autoUpdateOptions,\n    followCursor,\n    hoverDelay,\n    isDisabled,\n    isInitialOpen,\n    // if isDisabled is true tooltip will not open otherwise either controlled or uncontrolled depending on if isOpen is passed\n    isOpen: isDisabled === true ? false : isOpen,\n    isPortaled: isPortaledProp ?? isPortaled,\n    onOpenChange,\n    placement,\n    shouldCloseOnReferenceClick,\n    strategy: strategyProp ?? strategy,\n    type,\n  });\n\n  return (\n    <TooltipContext.Provider value={tooltip}>\n      {children}\n    </TooltipContext.Provider>\n  );\n};\n\nTooltipComponent.displayName = 'Tooltip';\n\ntype TooltipTriggerProps = {\n  /** Content displayed inside the trigger element */\n  children: React.ReactNode;\n  /** Whether the trigger element can be any element without an extra button wrapper. The child element will be cloned with the necessary trigger props and merged refs */\n  hasButtonWrapper?: boolean;\n};\n\nconst TooltipTrigger = ({\n  children,\n  hasButtonWrapper = false,\n  htmlAttributes,\n  className,\n  style,\n  ref,\n  ...restProps\n}: CommonProps<'button', TooltipTriggerProps>) => {\n  const context = useTooltipContext();\n\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  const triggerClasses = classNames(\n    {\n      'ndl-closed': !context.isOpen,\n      'ndl-open': context.isOpen,\n    },\n    'ndl-tooltip-trigger',\n    className,\n  );\n\n  // `hasButtonWrapper=true` allows the user to pass any element as the anchor\n  if (hasButtonWrapper && React.isValidElement(children)) {\n    const props: Record<string, unknown> = {\n      className: triggerClasses,\n      ...htmlAttributes,\n      ...childrenProps,\n      ref: mergedRefs,\n    };\n\n    return React.cloneElement(children, context.getReferenceProps(props));\n  }\n\n  return (\n    <button\n      type=\"button\"\n      className={triggerClasses}\n      style={style}\n      ref={mergedRefs}\n      {...context.getReferenceProps(htmlAttributes)}\n      {...restProps}\n    >\n      {children}\n    </button>\n  );\n};\n\ntype TooltipContentProps = {\n  /** Content displayed inside the tooltip */\n  children: React.ReactNode;\n};\n\nconst TooltipContent = ({\n  children,\n  style,\n  htmlAttributes,\n  className,\n  ref,\n  ...restProps\n}: CommonProps<'div', TooltipContentProps>) => {\n  const context = useTooltipContext();\n  const mergedRef = useMergeRefs([context.refs.setFloating, ref]);\n  const { themeClassName } = useNeedleTheme();\n\n  const hasHeaderChild = React.useMemo(() => {\n    if (context.type !== 'rich') {\n      return false;\n    }\n    const childrenArray = React.Children.toArray(children);\n    return childrenArray.some(\n      (child) => React.isValidElement(child) && child.type === TooltipHeader,\n    );\n  }, [children, context.type]);\n\n  if (!context.isOpen) {\n    return null;\n  }\n\n  const classes = classNames('ndl-tooltip-content', themeClassName, className, {\n    'ndl-tooltip-content-rich': context.type === 'rich',\n    'ndl-tooltip-content-simple': context.type === 'simple',\n  });\n\n  if (context.type === 'simple') {\n    return (\n      <ConditionalWrap\n        shouldWrap={context.isPortaled}\n        wrap={(wrapChildren) => <FloatingPortal>{wrapChildren}</FloatingPortal>}\n      >\n        <div\n          ref={mergedRef}\n          className={classes}\n          style={{\n            ...context.floatingStyles,\n            ...style,\n          }}\n          {...restProps}\n          {...context.getFloatingProps(htmlAttributes)}\n        >\n          <Typography variant=\"body-medium\">{children}</Typography>\n        </div>\n      </ConditionalWrap>\n    );\n  }\n\n  const ariaLabelledBy =\n    htmlAttributes?.['aria-labelledby'] ??\n    (hasHeaderChild ? context.headerId : undefined);\n\n  if (htmlAttributes?.['aria-label'] === undefined && !ariaLabelledBy) {\n    console.warn(\n      'The rich Tooltip is missing aria-label and Header. Please add one of them for accessibility.',\n    );\n  }\n\n  return (\n    <ConditionalWrap\n      shouldWrap={context.isPortaled}\n      wrap={(wrapChildren) => <FloatingPortal>{wrapChildren}</FloatingPortal>}\n    >\n      <FloatingFocusManager\n        context={context.context}\n        returnFocus={true}\n        modal={false}\n        initialFocus={0}\n        closeOnFocusOut\n      >\n        <div\n          ref={mergedRef}\n          className={classes}\n          style={{\n            ...context.floatingStyles,\n            ...style,\n          }}\n          {...restProps}\n          {...context.getFloatingProps({\n            ...htmlAttributes,\n            'aria-labelledby': ariaLabelledBy,\n          })}\n        >\n          {children}\n        </div>\n      </FloatingFocusManager>\n    </ConditionalWrap>\n  );\n};\n\ntype TooltipHeaderProps = {\n  /** Content displayed inside the header */\n  children: React.ReactNode;\n  /** @deprecated Use `typographyVariant` instead */\n  passThroughProps?: Partial<React.ComponentPropsWithoutRef<typeof Typography>>;\n  typographyVariant?: TypographyVariants;\n};\n\nconst TooltipHeader = ({\n  children,\n  passThroughProps,\n  typographyVariant = 'subheading-medium',\n  className,\n  style,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: CommonProps<'span', TooltipHeaderProps>) => {\n  const context = useTooltipContext();\n\n  const classes = classNames('ndl-tooltip-header', className);\n\n  useEffect(() => {\n    if (Boolean(htmlAttributes?.id) === true) {\n      context.setHeaderId?.(htmlAttributes?.id);\n    }\n  }, [context, htmlAttributes?.id]);\n\n  if (!context.isOpen) {\n    return null;\n  }\n\n  return (\n    <Typography\n      ref={ref}\n      variant={typographyVariant}\n      className={classes}\n      style={style}\n      htmlAttributes={{ ...htmlAttributes, id: context.headerId }}\n      {...passThroughProps}\n      {...restProps}\n    >\n      {children}\n    </Typography>\n  );\n};\n\nexport type TooltipBodyProps = {\n  /** Content displayed inside the body */\n  children: React.ReactNode;\n  /** Additional props to pass to the Typography component */\n  passThroughProps?: Partial<React.ComponentPropsWithoutRef<typeof Typography>>;\n};\n\nconst TooltipBody = ({\n  children,\n  className,\n  style,\n  htmlAttributes,\n  passThroughProps,\n  ref,\n  ...restProps\n}: CommonProps<'span', TooltipBodyProps>) => {\n  const context = useTooltipContext();\n\n  const classes = classNames('ndl-tooltip-body', className);\n  if (!context.isOpen) {\n    return null;\n  }\n\n  return (\n    <Typography\n      ref={ref}\n      variant=\"body-medium\"\n      className={classes}\n      style={style}\n      htmlAttributes={htmlAttributes}\n      {...passThroughProps}\n      {...restProps}\n    >\n      {children}\n    </Typography>\n  );\n};\n\ntype ToolTipActionsProps = {\n  /** Content displayed inside the actions container */\n  children: React.ReactNode;\n};\n\nconst Actions = ({\n  children,\n  className,\n  style,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: CommonProps<'div', ToolTipActionsProps>) => {\n  const context = useTooltipContext();\n  const mergedRefs = useMergeRefs([context.refs.setFloating, ref]);\n\n  if (!context.isOpen) {\n    return null;\n  }\n\n  const classes = classNames('ndl-tooltip-actions', className);\n\n  return (\n    <div\n      className={classes}\n      ref={mergedRefs}\n      style={style}\n      {...restProps}\n      {...htmlAttributes}\n    >\n      {children}\n    </div>\n  );\n};\n\nActions.displayName = 'Tooltip.Actions';\n\nconst Tooltip = Object.assign(TooltipComponent, {\n  Actions: Actions,\n  Body: TooltipBody,\n  Content: TooltipContent,\n  Header: TooltipHeader,\n  Trigger: TooltipTrigger,\n});\n\nexport { Tooltip };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+HE;CACA;CACA;CACA;CACA;CACA;;;CAuDA;CACA;CACA;CACA;CACA;;;CAmGA;CACA;CACA;CACA;CACA;CACA;CACA;;;CAwCA;CACA;CACA;CACA;CACA;CACA;;;CA+BA;CACA;CACA;CACA;CACA;;AA7SF,MAAM,oBAAoB,EACxB,UACA,aAAa,OACb,MACA,eACA,WACA,QACA,cACA,YAAY,gBACZ,kBAAkB,cAClB,YACA,6BACA,mBACA,mBACkB;CAClB,MAAM,iBAAiB,kBAAkB;CAIzC,MAAM,UAAU,WAAW;EACzB;EACA;EACA;EACA;EACA;EAEA,QAAQ,eAAe,OAAO,QAAQ;EACtC,YAAY,mBAAA,QAAA,mBAAA,KAAA,IAAA,iBAAkB,CAVZ;EAWlB;EACA;EACA;EACA,UAAU,iBAAA,QAAA,iBAAA,KAAA,IAAA,eAfK,iBAAiB,UAAU;EAgB1C;CACF,CAAC;CAED,OACE,oBAAC,eAAe,UAAhB;EAAyB,OAAO;EAC7B;CACsB,CAAA;AAE7B;AAEA,iBAAiB,cAAc;AAS/B,MAAM,kBAAkB,SAQ0B;CAR1B,IAAA,EACtB,UACA,mBAAmB,OACnB,gBACA,WACA,OACA,QAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,UAAU,kBAAkB;CAIlC,MAAM,gBAAiB,SAAiB;CACxC,MAAM,aAAa,aAAa;EAC9B,QAAQ,KAAK;EACb;EACA,kBAAA,QAAA,kBAAA,KAAA,IAAA,KAAA,IAAA,cAAe;CACjB,CAAC;CAED,MAAM,iBAAiB,WACrB;EACE,cAAc,CAAC,QAAQ;EACvB,YAAY,QAAQ;CACtB,GACA,uBACA,SACF;CAGA,IAAI,oBAAoB,MAAM,eAA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