{"version":3,"file":"TruncatedTextWithTooltip.mjs","names":[],"sources":["../../../src/_common/TruncatedTextWithTooltip.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 classNames from 'classnames';\nimport { type RefObject, useEffect, useRef, useState } from 'react';\n\nimport { Tooltip } from '../tooltip';\n\nconst useIsTruncated = (\n  text: string,\n): [RefObject<HTMLSpanElement | null>, boolean] => {\n  const ref = useRef<HTMLSpanElement>(null);\n  const [isTruncated, setIsTruncated] = useState(false);\n\n  useEffect(() => {\n    const element = ref.current;\n    if (element === null) {\n      return;\n    }\n\n    // Reads inside the ResizeObserver callback are layout-synced and cheap;\n    // setState with an unchanged boolean does not trigger a re-render.\n    const check = () =>\n      setIsTruncated(element.scrollWidth > element.clientWidth);\n\n    check();\n    const observer = new ResizeObserver(check);\n    observer.observe(element);\n    return () => observer.disconnect();\n  }, [text]);\n\n  return [ref, isTruncated];\n};\n\ntype TruncatedTextWithTooltipProps = {\n  /** The text to display. Truncated with an ellipsis when space is limited. */\n  text: string;\n  /** Class name applied to the text element. Typography is inherited from the parent. */\n  className?: string;\n  /** Placement of the tooltip showing the full text. */\n  tooltipPlacement?: React.ComponentProps<typeof Tooltip>['placement'];\n};\n\n/**\n * Internal component that renders single-line truncating text and, only while\n * the text is actually clipped, shows a tooltip with the full text.\n *\n * Accessibility: the text element receives `tabIndex={0}` only while truncated,\n * so sighted keyboard users can reveal the full text on focus without adding a\n * dead tab stop otherwise. No button semantics are used since there is no\n * action to perform. Screen readers always receive the full text from the\n * element's own content, as CSS truncation is visual-only. The tooltip is\n * dismissible with Escape and hoverable (WCAG 1.4.13).\n *\n * @internal - Not part of the public API.\n */\nexport const TruncatedTextWithTooltip = ({\n  text,\n  className,\n  tooltipPlacement = 'top-start',\n}: TruncatedTextWithTooltipProps) => {\n  const [textRef, isTruncated] = useIsTruncated(text);\n\n  return (\n    <Tooltip\n      type=\"simple\"\n      placement={tooltipPlacement}\n      isDisabled={!isTruncated}\n    >\n      <Tooltip.Trigger hasButtonWrapper>\n        <span\n          ref={textRef}\n          className={classNames('ndl-truncated-text', className)}\n          {...(isTruncated && { tabIndex: 0 })}\n        >\n          {text}\n        </span>\n      </Tooltip.Trigger>\n      <Tooltip.Content>{text}</Tooltip.Content>\n    </Tooltip>\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,MAAM,kBACJ,SACiD;CACjD,MAAM,MAAM,OAAwB,IAAI;CACxC,MAAM,CAAC,aAAa,kBAAkB,SAAS,KAAK;CAEpD,gBAAgB;EACd,MAAM,UAAU,IAAI;EACpB,IAAI,YAAY,MACd;EAKF,MAAM,cACJ,eAAe,QAAQ,cAAc,QAAQ,WAAW;EAE1D,MAAM;EACN,MAAM,WAAW,IAAI,eAAe,KAAK;EACzC,SAAS,QAAQ,OAAO;EACxB,aAAa,SAAS,WAAW;CACnC,GAAG,CAAC,IAAI,CAAC;CAET,OAAO,CAAC,KAAK,WAAW;AAC1B;;;;;;;;;;;;;;AAwBA,MAAa,4BAA4B,EACvC,MACA,WACA,mBAAmB,kBACgB;CACnC,MAAM,CAAC,SAAS,eAAe,eAAe,IAAI;CAElD,OACE,qBAAC,SAAD;EACE,MAAK;EACL,WAAW;EACX,YAAY,CAAC;EAHf,UAAA,CAKE,oBAAC,QAAQ,SAAT;GAAiB,kBAAA;GACf,UAAA,oBAAC,QAAA,eAAA,eAAA;IACC,KAAK;IACL,WAAW,WAAW,sBAAsB,SAAS;GAChD,GAAA,eAAe,EAAE,UAAU,EAAE,CAAA,GAAA,CAAA,GAAA,EAAA,UAEjC,KAAA,CACG,CAAA;EACS,CAAA,GACjB,oBAAC,QAAQ,SAAT,EAAA,UAAkB,KAAsB,CAAA,CACjC;;AAEb"}