{"version":3,"file":"Breadcrumbs.mjs","names":[],"sources":["../../../src/breadcrumbs/Breadcrumbs.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 */\n\nimport classNames from 'classnames';\nimport type React from 'react';\nimport { createContext, useContext, useRef, useState } from 'react';\n\nimport {\n  type CommonProps,\n  type HtmlAttributes,\n  type PolymorphicCommonProps,\n} from '../_common/types';\nimport { ChevronDownIconOutline } from '../icons/generated/heroIcons/ChevronDownIconOutline';\nimport { Tooltip } from '../tooltip';\n\ntype BreadcrumbsProps = {\n  /** Content to be rendered. Should only be `Breadcrumbs.Item`. */\n  children?: React.ReactNode;\n  /** Additional classnames. */\n  shouldWrap?: boolean;\n};\n\nconst BreadcrumbsComponent = ({\n  children,\n  className,\n  htmlAttributes,\n  style,\n  ref,\n  shouldWrap = true,\n  ...restProps\n}: CommonProps<'nav', BreadcrumbsProps>) => {\n  const classes = classNames('ndl-breadcrumbs', className);\n  const listClasses = classNames('ndl-breadcrumbs-list', {\n    'ndl-breadcrumbs-wrap': shouldWrap,\n  });\n  return (\n    <nav\n      aria-label=\"Breadcrumb\"\n      ref={ref}\n      className={classes}\n      style={style}\n      {...restProps}\n      {...htmlAttributes}\n    >\n      <ol className={listClasses}>{children}</ol>\n    </nav>\n  );\n};\n\ntype BreadcrumbsItemContextType = {\n  isInBreadcrumbsItem: boolean;\n};\n\nconst BreadcrumbsItemContext = createContext<BreadcrumbsItemContextType | null>(\n  null,\n);\n\nexport const useBreadcrumbsItemContext = (): BreadcrumbsItemContextType => {\n  const context = useContext(BreadcrumbsItemContext);\n  if (!context) {\n    throw new Error('This component must be used within a <Breadcrumbs.Item>');\n  }\n\n  return context;\n};\n\ntype BreadcrumbsItemProps = {\n  /** Content to be rendered. Should be either `Breadcrumbs.Link`, `Breadcrumbs.Button` or `Breadcrumbs.SelectButton`. */\n  children?: React.ReactNode;\n};\n\nconst BreadcrumbsItem = ({\n  children,\n  className,\n  style,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: CommonProps<'li', BreadcrumbsItemProps>) => {\n  const classes = classNames('ndl-breadcrumbs-item', className);\n\n  return (\n    <BreadcrumbsItemContext.Provider value={{ isInBreadcrumbsItem: true }}>\n      <li\n        ref={ref}\n        className={classes}\n        style={style}\n        {...htmlAttributes}\n        {...restProps}\n      >\n        <div className=\"ndl-breadcrumbs-item-inner\">{children}</div>\n      </li>\n    </BreadcrumbsItemContext.Provider>\n  );\n};\n\ntype InteractiveBreadcrumbProps = {\n  /** Content to be rendered. */\n  children?: React.ReactNode;\n} & (\n  | {\n      as: 'button';\n      /**\n       * Event handler for when the button is clicked. Only applicable if `as` is `'button'`.\n       */\n      onClick?: (\n        event: React.MouseEvent<HTMLButtonElement, MouseEvent>,\n      ) => void;\n    }\n  | {\n      as: 'a';\n      /**\n       * The href of the link. Only applicable if `as` is `'a'`.\n       */\n      href: HtmlAttributes<'a'>['href'];\n      /**\n       * The target of the link. Only applicable if `as` is `'a'`.\n       */\n      target?: HtmlAttributes<'a'>['target'];\n    }\n  | {\n      /**\n       * What HTML element to render the root element as.\n       */\n      as?: React.ElementType;\n    }\n);\n\ntype BreadcrumbsButtonProps = InteractiveBreadcrumbProps & {\n  /** Leading element to be rendered. */\n  leadingElement?: React.ReactNode;\n  /** Trailing element to be rendered. */\n  trailingElement?: React.ReactNode;\n};\n\nconst BreadcrumbsButton = <T extends React.ElementType = 'button'>({\n  children,\n  as,\n  className,\n  style,\n  htmlAttributes,\n  ref,\n  leadingElement,\n  trailingElement,\n  ...restProps\n}: PolymorphicCommonProps<T, BreadcrumbsButtonProps>) => {\n  useBreadcrumbsItemContext();\n\n  const [isTooltipOpen, setIsTooltipOpen] = useState(false);\n  const contentRef = useRef<HTMLDivElement>(null);\n  const Component: React.ElementType = as ?? 'button';\n\n  const classes = classNames(\n    'ndl-breadcrumbs-button ndl-breadcrumbs-sibling-hover',\n    className,\n  );\n\n  return (\n    <Tooltip\n      type=\"simple\"\n      isOpen={isTooltipOpen}\n      onOpenChange={(isOpen) => {\n        if (isOpen) {\n          const isTruncated =\n            contentRef.current?.scrollWidth &&\n            contentRef.current?.clientWidth &&\n            contentRef.current.scrollWidth > contentRef.current.clientWidth;\n          const isVisible =\n            contentRef.current?.offsetWidth &&\n            contentRef.current?.offsetHeight &&\n            contentRef.current.offsetWidth > 0 &&\n            contentRef.current.offsetHeight > 0;\n          setIsTooltipOpen(isTruncated || !isVisible);\n        } else {\n          setIsTooltipOpen(false);\n        }\n      }}\n    >\n      <Tooltip.Trigger hasButtonWrapper>\n        <Component\n          ref={ref}\n          className={classes}\n          style={style}\n          {...htmlAttributes}\n          {...restProps}\n        >\n          {Boolean(leadingElement) && (\n            <div className=\"ndl-breadcrumbs-button-leading-element\">\n              {leadingElement}\n            </div>\n          )}\n          <div className=\"ndl-breadcrumbs-button-content\" ref={contentRef}>\n            {children}\n          </div>\n          {Boolean(trailingElement) && (\n            <div className=\"ndl-breadcrumbs-button-trailing-element\">\n              {trailingElement}\n            </div>\n          )}\n        </Component>\n      </Tooltip.Trigger>\n      <Tooltip.Content>{children}</Tooltip.Content>\n    </Tooltip>\n  );\n};\n\ntype BreadcrumbsSelectButtonProps = {\n  /** Event handler for when the button is clicked. Should be used to open a `Menu`. */\n  onClick?: (event: React.MouseEvent<HTMLButtonElement>) => void;\n  /** Aria label needed for accessibility. */\n  ariaLabel: string;\n  /** Content to be rendered. */\n  children?: React.ReactNode;\n  /** Active - used when a connected menu is open */\n  isActive?: boolean;\n};\n\nconst BreadcrumbsSelectButton = ({\n  onClick,\n  children,\n  isActive,\n  className,\n  style,\n  htmlAttributes,\n  ariaLabel,\n  ref,\n  ...restProps\n}: CommonProps<'button', BreadcrumbsSelectButtonProps>) => {\n  useBreadcrumbsItemContext();\n\n  const classes = classNames(\n    'ndl-breadcrumbs-button ndl-breadcrumbs-sibling-hover ndl-breadcrumbs-select',\n    className,\n    {\n      'ndl-active': Boolean(isActive),\n    },\n  );\n\n  return (\n    <button\n      type=\"button\"\n      ref={ref}\n      className={classes}\n      style={style}\n      onClick={onClick}\n      aria-label={ariaLabel}\n      aria-pressed={isActive}\n      {...htmlAttributes}\n      {...restProps}\n    >\n      <ChevronDownIconOutline className=\"ndl-breadcrumbs-chevron\" />\n      <div className=\"ndl-breadcrumbs-button-content\">{children}</div>\n    </button>\n  );\n};\n\ntype BreadcrumbsLinkProps = InteractiveBreadcrumbProps & {\n  /**\n   * Whether the link is the current item. Use `'page'` if it indicates the current page and `'step'` for steps (in a multi-step wizard for example).\n   * @see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-current\n   */\n  ariaCurrent?: React.AriaAttributes['aria-current'];\n};\n\nconst BreadcrumbsLink = <T extends React.ElementType = 'a'>({\n  children,\n  as,\n  className,\n  style,\n  htmlAttributes,\n  ariaCurrent = false,\n  ref,\n  ...restProps\n}: PolymorphicCommonProps<T, BreadcrumbsLinkProps>) => {\n  useBreadcrumbsItemContext();\n\n  const Component: React.ElementType = as ?? 'a';\n\n  const classes = classNames(\n    'ndl-breadcrumbs-link',\n    {\n      'ndl-breadcrumbs-link-current': Boolean(ariaCurrent),\n    },\n    className,\n  );\n\n  return (\n    <Component\n      ref={ref}\n      className={classes}\n      style={style}\n      aria-current={ariaCurrent}\n      {...restProps}\n      {...htmlAttributes}\n    >\n      {children}\n    </Component>\n  );\n};\n\nconst Breadcrumbs = Object.assign(BreadcrumbsComponent, {\n  Button: BreadcrumbsButton,\n  Item: BreadcrumbsItem,\n  Link: BreadcrumbsLink,\n  SelectButton: BreadcrumbsSelectButton,\n});\n\nexport { Breadcrumbs };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAyCE;CACA;CACA;CACA;CACA;CACA;;;CA4CA;CACA;CACA;CACA;CACA;;;CA4DA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;CA2EA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;CAwCA;CACA;CACA;CACA;CACA;CACA;CACA;;AAzPF,MAAM,wBAAwB,SAQc;CARd,IAAA,EAC5B,UACA,WACA,gBACA,OACA,KACA,aAAa,SAAA,MACV,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,UAAU,WAAW,mBAAmB,SAAS;CACvD,MAAM,cAAc,WAAW,wBAAwB,EACrD,wBAAwB,WAC1B,CAAC;CACD,OACE,oBAAC,OAAA,eAAA,eAAA,eAAA;EACC,cAAW;EACN;EACL,WAAW;EACJ;CACH,GAAA,SAAA,GACA,cAAA,GAAA,CAAA,GAAA,EAAA,UAEJ,oBAAC,MAAD;EAAI,WAAW;EAAc;CAAa,CAAA,EAAA,CACvC,CAAA;AAET;AAMA,MAAM,yBAAyB,cAC7B,IACF;AAEA,MAAa,kCAA8D;CACzE,MAAM,UAAU,WAAW,sBAAsB;CACjD,IAAI,CAAC,SACH,MAAM,IAAI,MAAM,yDAAyD;CAG3E,OAAO;AACT;AAOA,MAAM,mBAAmB,UAOsB;CAPtB,IAAA,EACvB,UACA,WACA,OACA,gBACA,QAAA,OACG,YAAA,yBAAA,OAAA,UAAA;CAEH,MAAM,UAAU,WAAW,wBAAwB,SAAS;CAE5D,OACE,oBAAC,uBAAuB,UAAxB;EAAiC,OAAO,EAAE,qBAAqB,KAAK;EAClE,UAAA,oBAAC,MAAA,eAAA,eAAA,eAAA;GACM;GACL,WAAW;GACJ;EACH,GAAA,cAAA,GACA,SAAA,GAAA,CAAA,GAAA,EAAA,UAEJ,oBAAC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