{"version":3,"file":"SpotlightTarget.mjs","names":[],"sources":["../../../src/spotlight/SpotlightTarget.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 { FloatingPortal, useMergeRefs } from '@floating-ui/react';\nimport classNames from 'classnames';\nimport React, { useEffect } from 'react';\n\nimport { type PolymorphicCommonProps } from '../_common/types';\nimport { ConditionalWrap } from '../conditional-wrap';\nimport { useSpotlightContext } from './SpotlightProvider';\nimport {\n  type IndicatorPlacement,\n  type IndicatorVariant,\n  useIndicator,\n} from './use-indicator';\n\ntype SpotlightTargetProps = {\n  /** The id of the wrapper. This should match a `target` prop in a `Spotlight`, to indicate that this is where it should be rendered when open */\n  id: string;\n  /** A boolean that controls if the pulse animation should be used or not */\n  hasPulse?: boolean;\n  /** A string that controls the style of the indicator. It can be either \"point\" or \"border\" */\n  indicatorVariant?: IndicatorVariant;\n  /** A string that controls the placement of the indicator. A combination of \"middle\", \"top\", \"right\", \"bottom\" and \"left\" separated by a dash, e.g. \"top-right\" */\n  indicatorPlacement?: IndicatorPlacement;\n  /** An inline style CSS property to control the border radius of the wrapper */\n  borderRadius?: React.CSSProperties['borderRadius'];\n  /** The content to wrap */\n  children?: React.ReactNode;\n  /** Fit the target size to the children. */\n  shouldFitToChildren?: boolean;\n  /**\n   * When true, merges the spotlight target props directly with the child element instead of wrapping it in a div.\n   *\n   * This is useful for applying spotlight functionality to existing interactive elements (buttons, links, etc.)\n   * while preserving their original behavior, styling, and accessibility attributes.\n   *\n   * @remarks\n   * - **Warning:** Only use with interactive elements that can receive focus and handle click events.\n   * - **Warning:** The child element's `id` will be overridden with the target's `id`.\n   *\n   * @example\n   * ```tsx\n   * <SpotlightTarget id=\"my-button\" shouldRenderAsChild>\n   *   <FilledButton>Click me</FilledButton>\n   * </SpotlightTarget>\n   * ```\n   */\n  shouldRenderAsChild?: boolean;\n};\n\nexport const SpotlightTarget = ({\n  id,\n  hasPulse = true,\n  indicatorVariant = 'border',\n  indicatorPlacement = 'top-right',\n  borderRadius,\n  shouldFitToChildren = false,\n  children,\n  className,\n  htmlAttributes,\n  style,\n  as,\n  ref,\n  shouldRenderAsChild,\n  ...restProps\n}: PolymorphicCommonProps<'div', SpotlightTargetProps>) => {\n  const Component = as ?? 'div';\n\n  const {\n    isActiveSpotlight,\n    isOpen,\n    setIsOpen,\n    registerTarget,\n    unregisterTarget,\n    setActiveSpotlight,\n  } = useSpotlightContext();\n\n  const internalRef = React.useRef<HTMLElement>(null);\n  const childRef =\n    shouldRenderAsChild && React.isValidElement(children)\n      ? (\n          children as React.ReactElement<unknown> & {\n            ref?: React.Ref<HTMLElement>;\n          }\n        ).ref\n      : undefined;\n  const mergedRefs = useMergeRefs<HTMLElement>([\n    ref as React.RefObject<HTMLElement>,\n    internalRef as React.RefObject<HTMLElement>,\n    childRef as React.Ref<HTMLElement> | undefined,\n  ]);\n  useEffect(() => {\n    if (internalRef.current !== null) {\n      registerTarget(id, internalRef as React.RefObject<HTMLElement>);\n    }\n    return () => unregisterTarget(id);\n  }, [id, registerTarget, unregisterTarget]);\n\n  const isActive = isActiveSpotlight(id);\n  const indicator = useIndicator({\n    hasPulse,\n    indicatorPlacement,\n    indicatorVariant,\n    isOpen: isActive,\n    targetRef: internalRef,\n  });\n\n  if (shouldRenderAsChild === true && React.isValidElement(children)) {\n    const child = children as React.ReactElement<{\n      className?: string;\n      style?: React.CSSProperties;\n      onClick?: (event: React.MouseEvent<HTMLElement>) => void;\n      onKeyDown?: (event: React.KeyboardEvent<HTMLElement>) => void;\n    }>;\n\n    const onClick = (event: React.MouseEvent<HTMLElement>) => {\n      if (isActive) {\n        // When spotlight is active, always prevent the child's click handler\n        event.preventDefault();\n\n        if (!isOpen) {\n          setIsOpen(true);\n        }\n        // Don't call child.props?.onClick when spotlight is active\n        return;\n      }\n\n      // Only call the child's click handler when spotlight is not active\n      child.props?.onClick?.(event);\n    };\n\n    const onKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {\n      if (\n        isActive &&\n        !isOpen &&\n        (event.code === 'Enter' || event.code === 'Space')\n      ) {\n        event.preventDefault();\n        setIsOpen(true);\n        return;\n      }\n\n      if (isActive && event.code === 'Escape') {\n        event.preventDefault();\n        setActiveSpotlight(null);\n        return;\n      }\n\n      child.props?.onKeyDown?.(event);\n    };\n\n    // IMPORTANT: do NOT force role/tabIndex here; preserves Composite semantics for the SideNavigation\n    const mergedProps = {\n      id,\n      onClick,\n      onKeyDown,\n      ref: mergedRefs,\n      style: {\n        ...style,\n        ...child.props.style,\n      },\n      // Avoid overriding the child's own role/tabIndex with any passed htmlAttributes\n      ...(htmlAttributes\n        ? { ...htmlAttributes, role: undefined, tabIndex: undefined }\n        : {}),\n    };\n\n    return (\n      <>\n        {React.cloneElement(child, mergedProps as Record<string, unknown>)}\n        <ConditionalWrap\n          shouldWrap={isActive && isOpen}\n          wrap={(wrapChildren) => (\n            <FloatingPortal>{wrapChildren}</FloatingPortal>\n          )}\n        >\n          {indicator.context.open && (\n            <div\n              className={indicator.indicatorClasses}\n              ref={indicator.context.refs.setFloating}\n              style={{ ...indicator.floatingStyles, ...indicator.style }}\n            />\n          )}\n        </ConditionalWrap>\n      </>\n    );\n  }\n\n  const classes = classNames(\n    'ndl-spotlight-target',\n    {\n      'ndl-spotlight-target-fit-to-children': shouldFitToChildren,\n      'ndl-spotlight-target-open': isOpen && isActive,\n    },\n    className,\n  );\n\n  const inertClasses = classNames(\n    'ndl-spotlight-target-inert',\n    {\n      'ndl-spotlight-target-inert-fit-to-children': shouldFitToChildren,\n    },\n    className,\n  );\n\n  return (\n    <>\n      <Component\n        role={isActive ? 'button' : undefined}\n        id={id}\n        ref={mergedRefs}\n        className={classes}\n        onClick={(event) => {\n          if (isActive) {\n            event.preventDefault();\n            event.stopPropagation();\n\n            if (!isOpen) {\n              setIsOpen(true);\n            }\n          }\n        }}\n        style={{ borderRadius, ...style }}\n        onKeyDown={(event) => {\n          if (\n            isActive &&\n            !isOpen &&\n            (event.code === 'Enter' || event.code === 'Space')\n          ) {\n            setIsOpen(true);\n            event.preventDefault();\n          } else if (event.code === 'Escape' && isActive) {\n            setActiveSpotlight(null);\n          }\n        }}\n        tabIndex={isActive ? 0 : undefined}\n        {...(isActive && { 'aria-label': 'Open spotlight' })}\n        {...restProps}\n        {...htmlAttributes}\n      >\n        <ConditionalWrap\n          shouldWrap={isActive}\n          wrap={(children) => (\n            <div inert aria-disabled className={inertClasses}>\n              {children}\n            </div>\n          )}\n        >\n          {children}\n        </ConditionalWrap>\n      </Component>\n      <ConditionalWrap\n        shouldWrap={isActive && isOpen}\n        wrap={(wrapChildren) => <FloatingPortal>{wrapChildren}</FloatingPortal>}\n      >\n        {indicator.context.open && (\n          <div\n            className={indicator.indicatorClasses}\n            ref={indicator.context.refs.setFloating}\n            style={{ ...indicator.floatingStyles, ...indicator.style }}\n          />\n        )}\n      </ConditionalWrap>\n    </>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqEE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AAbF,MAAa,mBAAmB,SAe2B;CAf3B,IAAA,EAC9B,IACA,WAAW,MACX,mBAAmB,UACnB,qBAAqB,aACrB,cACA,sBAAsB,OACtB,UACA,WACA,gBACA,OACA,IACA,KACA,wBAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,YAAY,OAAA,QAAA,OAAA,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