{"version":3,"file":"use-indicator.mjs","names":[],"sources":["../../../src/spotlight/use-indicator.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  type ElementRects,\n  offset,\n  useFloating,\n} from '@floating-ui/react';\nimport classNames from 'classnames';\nimport { useMemo } from 'react';\n\nexport type IndicatorVariant = 'border' | 'point';\n\ninterface IndicatorOptions {\n  isOpen: boolean;\n  indicatorVariant?: IndicatorVariant;\n  indicatorPlacement?: IndicatorPlacement;\n  targetRef: React.RefObject<HTMLElement | undefined | null>;\n  hasPulse?: boolean;\n  hasAnchorPortal?: boolean;\n}\n\ninterface UseIndicatorReturn extends ReturnType<typeof useFloating> {\n  style: React.CSSProperties;\n  indicatorClasses?: string;\n  hasAnchorPortal?: boolean;\n}\n\nexport type IndicatorPlacement =\n  | 'top-left'\n  | 'top-middle'\n  | 'top-right'\n  | 'bottom-left'\n  | 'bottom-middle'\n  | 'bottom-right'\n  | 'middle-left'\n  | 'middle-middle'\n  | 'middle-right';\n\nconst crossAxisMult: Record<string, number> = {\n  left: -1,\n  middle: 0,\n  right: 1,\n};\n\nconst calcOffset = (\n  variant: IndicatorVariant,\n  placement: IndicatorPlacement,\n  rects: ElementRects,\n) => {\n  if (variant === 'border') {\n    // Border variant is always centered around the middle point\n    return {\n      crossAxis: 0,\n      mainAxis: -rects.reference.height / 2 - rects.floating.height / 2,\n    };\n  }\n\n  const [mainAxisPlacement, crossAxisPlacement] = placement.split('-');\n  /** Note for future reference:\n   * Main axis (vertical) is zero at the bottom border of the target.\n   * Cross axis (horizontal) is zero at the middle of the target\n   * */\n\n  let mainAxis = 0;\n  if (mainAxisPlacement === 'bottom') {\n    // put the indicator one height of the point above the bottom border of the target\n    mainAxis = -2 * rects.floating.height;\n  } else if (mainAxisPlacement === 'middle') {\n    // put the indicator in the middle of the target\n    mainAxis = -rects.reference.height / 2 - rects.floating.height / 2;\n  } else if (mainAxisPlacement === 'top') {\n    // put the indicator one height of the point below the top border of the target\n    mainAxis = -rects.reference.height + rects.floating.height;\n  }\n\n  return {\n    crossAxis:\n      crossAxisMult[crossAxisPlacement] *\n      (rects.reference.width / 2 - rects.floating.width * 1.5),\n    mainAxis,\n  };\n};\n\nconst getComputedBorderRadius = (\n  element: HTMLElement | null | undefined,\n): string | undefined => {\n  if (!element) {\n    return undefined;\n  }\n\n  // First check if there's an inline style\n  if (element.style.borderRadius) {\n    return element.style.borderRadius;\n  }\n\n  // If no inline style, get the computed style (includes CSS classes)\n  const computedStyle = window.getComputedStyle(element);\n  const borderRadius = computedStyle.borderRadius;\n\n  // Return undefined if border-radius is '0px' or empty\n  return borderRadius && borderRadius !== '0px' ? borderRadius : undefined;\n};\n\nexport function useIndicator({\n  isOpen,\n  indicatorVariant = 'border',\n  indicatorPlacement = 'top-right',\n  targetRef,\n  hasPulse = true,\n}: IndicatorOptions): UseIndicatorReturn {\n  const data = useFloating({\n    elements: {\n      reference: targetRef?.current,\n    },\n    middleware: [\n      offset(({ rects }) =>\n        calcOffset(indicatorVariant, indicatorPlacement, rects),\n      ),\n    ],\n    open: isOpen,\n    placement: 'bottom',\n    whileElementsMounted: autoUpdate,\n  });\n\n  return useMemo(\n    () => ({\n      ...data,\n      indicatorClasses: classNames('ndl-spotlight-target-indicator', {\n        'ndl-spotlight-target-indicator-border': indicatorVariant === 'border',\n        'ndl-spotlight-target-indicator-point': indicatorVariant === 'point',\n        'ndl-spotlight-target-pulse': hasPulse,\n      }),\n      style: {\n        borderRadius:\n          indicatorVariant === 'border'\n            ? getComputedBorderRadius(targetRef?.current)\n            : '50%',\n        height:\n          indicatorVariant === 'border'\n            ? `${targetRef?.current?.clientHeight ?? 0}px`\n            : '8px',\n        width:\n          indicatorVariant === 'border'\n            ? `${targetRef?.current?.clientWidth ?? 0}px`\n            : '8px',\n      },\n    }),\n    [data, indicatorVariant, targetRef, hasPulse],\n  );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,MAAM,gBAAwC;CAC5C,MAAM;CACN,QAAQ;CACR,OAAO;AACT;AAEA,MAAM,cACJ,SACA,WACA,UACG;CACH,IAAI,YAAY,UAEd,OAAO;EACL,WAAW;EACX,UAAU,CAAC,MAAM,UAAU,SAAS,IAAI,MAAM,SAAS,SAAS;CAClE;CAGF,MAAM,CAAC,mBAAmB,sBAAsB,UAAU,MAAM,GAAG;;;;;CAMnE,IAAI,WAAW;CACf,IAAI,sBAAsB,UAExB,WAAW,KAAK,MAAM,SAAS;MAC1B,IAAI,sBAAsB,UAE/B,WAAW,CAAC,MAAM,UAAU,SAAS,IAAI,MAAM,SAAS,SAAS;MAC5D,IAAI,sBAAsB,OAE/B,WAAW,CAAC,MAAM,UAAU,SAAS,MAAM,SAAS;CAGtD,OAAO;EACL,WACE,cAAc,uBACb,MAAM,UAAU,QAAQ,IAAI,MAAM,SAAS,QAAQ;EACtD;CACF;AACF;AAEA,MAAM,2BACJ,YACuB;CACvB,IAAI,CAAC,SACH;CAIF,IAAI,QAAQ,MAAM,cAChB,OAAO,QAAQ,MAAM;CAKvB,MAAM,eADgB,OAAO,iBAAiB,OACb,CAAC,CAAC;CAGnC,OAAO,gBAAgB,iBAAiB,QAAQ,eAAe,KAAA;AACjE;AAEA,SAAgB,aAAa,EAC3B,QACA,mBAAmB,UACnB,qBAAqB,aACrB,WACA,WAAW,QAC4B;CACvC,MAAM,OAAO,YAAY;EACvB,UAAU,EACR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