{"version":3,"file":"Breadcrumbs.mjs","sources":["../../../src/components/Breadcrumbs/Breadcrumbs.tsx"],"sourcesContent":["import clsx from 'clsx';\nimport debounce from 'lodash/debounce.js';\nimport React, { createContext, useContext, type ReactNode } from 'react';\nimport {\n  assertNoRemovedIconProp,\n  type WithRemovedIconProps,\n} from '../../util/logging';\nimport { hasSlotContent, IconSlot, useSemanticIcon } from '../Icon';\nimport Menu from '../Menu';\nimport Text from '../Text';\n\nimport styles from './Breadcrumbs.module.css';\n\ntype Separators = '|' | '>' | '/';\n\ntype ReactChildArray = ReturnType<typeof React.Children.toArray>;\n\nexport function flattenReactChildren(children: ReactNode): ReactChildArray {\n  const childrenArray = React.Children.toArray(children);\n  return childrenArray.reduce((flatChildren: ReactChildArray, child) => {\n    if ((child as React.ReactElement<any>).type === React.Fragment) {\n      return flatChildren.concat(\n        flattenReactChildren((child as React.ReactElement<any>).props.children),\n      );\n    }\n    flatChildren.push(child);\n    return flatChildren;\n  }, []);\n}\n\ntype Props = {\n  /**\n   * aria-label for `nav` element to describe Breadcrumbs navigation to screen readers\n   */\n  'aria-label'?: string;\n  /**\n   * Child node(s) that can be nested inside component\n   */\n  children?: ReactNode;\n  /**\n   * CSS class names that can be appended to the component\n   */\n  className?: string;\n  /**\n   * HTML id for the component\n   */\n  id?: string;\n  /**\n   * Custom string separator between individual breadcrumbs\n   *\n   * **Defaults to `\"/\"`**.\n   */\n  separator?: Separators;\n};\n\ntype Context = {\n  separator?: Separators;\n};\n\nconst BreadcrumbsContext = createContext<Context>({});\n\n/**\n * ## Usage\n *\n * Navigation component used to show hierarchical position and depth of content in a webpage or\n * application. Can show all available links in the hierarchy, or collapse the links into a menu\n * as needed.\n *\n * | Type/Use | Description | Example |\n * |----------|-------------|---------|\n * | Full trail | Every ancestor is shown as its own link, separated by `separator`. | Shallow hierarchies that fit the available width. |\n * | Collapsed | Middle items move into a `Menu` behind an ellipsis when the trail would overflow. | Deep hierarchies. Narrow containers. |\n * | Back | The parent page is shown as a single back arrow at small breakpoints. | Mobile and other narrow viewports. |\n *\n * ### Best Practices\n *\n * * Use brief text for breadcrumb item text descriptions.\n *\n * ## Interaction\n *\n * Each item in `Breadcrumbs` represents one anchor which leads to a parent page in the hierarchy.\n * Adjacent pages in the information architecture will have the same exact ancestors.\n *\n * ## Content & Accessibility\n *\n * ### Do's\n *\n * * Use `Breadcrumbs` when pages have a tree structure resulting in 1-3 topmost parent pages, and many children per parent.\n *\n * ### Don'ts\n *\n * * Avoid having breadcrumbs with hierarchies greater than 10 ancestors. Consider a custom sub-navigation, more top-level links in `AppHeader`, or other grouping to avoid such deep hierarchies.\n * * Don't go beyond 1-3 words for the text of each breadcrumb.\n *\n * ## Resources\n *\n * * https://www.nngroup.com/articles/breadcrumbs/\n */\nexport const Breadcrumbs = ({\n  'aria-label': ariaLabel = 'breadcrumbs links',\n  className,\n  children,\n  id,\n  separator,\n  ...other\n}: Props) => {\n  const [shouldTruncate, setShouldTruncate] = React.useState(false);\n\n  const ref = React.useRef<HTMLUListElement>(null);\n\n  React.useEffect(() => {\n    const updateShouldTruncate = () => {\n      const willOverflow = ref.current\n        ? ref.current.clientWidth < ref.current.scrollWidth\n        : false;\n\n      setShouldTruncate(willOverflow);\n    };\n\n    const debouncedUpdateShouldTruncate = debounce(updateShouldTruncate, 200);\n\n    updateShouldTruncate();\n    window.addEventListener('resize', debouncedUpdateShouldTruncate);\n    return () => {\n      window.removeEventListener('resize', debouncedUpdateShouldTruncate);\n    };\n  }, []);\n\n  /**\n   * Flattens breadcrumb items that may be wrapped in React.Fragment into an array so they can be manipulated easily.\n   */\n  const breadcrumbsItems = flattenBreadcrumbsItems(children);\n\n  /**\n   * Finds the second to last breadcrumb item for mobile Breadcrumbs back icon.\n   */\n  const backBreadCrumb =\n    breadcrumbsItems.length > 1\n      ? React.cloneElement(\n          breadcrumbsItems[\n            breadcrumbsItems.length - 2\n          ] as React.ReactElement<any>,\n          {\n            variant: 'back',\n          },\n        )\n      : null;\n\n  /**\n   * Finds all the breadcrumb items between the first and last breadcrumb items so they can be placed in the Menu.\n   */\n  const menuItems = breadcrumbsItems\n    .slice(1, breadcrumbsItems.length - 1)\n    .map((breadcrumbItem, index) => {\n      const menuItem = breadcrumbItem as React.JSX.Element;\n      return (\n        <Menu.Item\n          href={menuItem.props.href}\n          // FIXME\n          // eslint-disable-next-line react/no-array-index-key\n          key={`breadcrumb-menu-item-${index}`}\n          leadingContent=\"link\"\n        >\n          {menuItem.props.text}\n        </Menu.Item>\n      );\n    });\n\n  return (\n    <BreadcrumbsContext.Provider value={{ separator }}>\n      <nav aria-label={ariaLabel} className={className} id={id} {...other}>\n        <ul className={styles['breadcrumbs__list']} ref={ref}>\n          {/**\n           * Back icon breadcrumb always exists, just hidden via css depending on breakpoint to increase performance\n           */}\n          {backBreadCrumb}\n          {/**\n           * The ellipsis breadcrumb with Menu only exists if there would be overflow and there are 3 or more breadcrumb items.\n           */}\n          {shouldTruncate && breadcrumbsItems.length > 2 ? (\n            <>\n              {breadcrumbsItems[0]}\n              <BreadcrumbsItem\n                href={null}\n                menuItems={menuItems}\n                separator={separator}\n                variant=\"collapsed\"\n              />\n              {breadcrumbsItems[breadcrumbsItems.length - 1]}\n            </>\n          ) : (\n            /**\n             * If the above conditions aren't met, display all breadcrumbs.\n             */\n            breadcrumbsItems\n          )}\n        </ul>\n      </nav>\n    </BreadcrumbsContext.Provider>\n  );\n};\n\nconst flattenBreadcrumbsItems = (children: ReactNode) => {\n  const flattenedChildren = flattenReactChildren(children);\n  /**\n   * Throws error if children are not BreadcrumbsItems\n   */\n  const shouldThrowError = (flattenedChildren as React.JSX.Element[]).some(\n    (child) => {\n      if (child.type === BreadcrumbsItem || child.type === Breadcrumbs.Item) {\n        return false;\n      }\n      return true;\n    },\n  );\n  if (process.env.NODE_ENV !== 'production' && shouldThrowError) {\n    throw new Error(\n      'Only <Breadcrumbs.Item> or React.Fragment of aforementioned components allowed',\n    );\n  }\n  return flattenedChildren;\n};\n\nconst CustomSeparatorBreadcrumbsItem = (\n  props: React.ComponentProps<typeof BreadcrumbsItem>,\n) => {\n  const { separator } = useContext(BreadcrumbsContext);\n  return <BreadcrumbsItem separator={separator} {...props} />;\n};\n\ntype BreadcrumbItemProps = {\n  /**\n   * CSS class names that can be appended to the component.\n   */\n  className?: string;\n  /**\n   * URL for the breadcrumbs item.\n   * Required since breadcrumbs should reroute user.\n   * Null case is used for the collapsed variant, which uses Menu Items which has hrefs.\n   */\n  href: string | null;\n  /**\n   * URLs for the collapsed breadcrumbs variant.\n   * Should be <Menu.Item href={href}>{text}</Menu.Item>.\n   */\n  menuItems?: ReactNode[];\n  /**\n   * Custom string separator after current breadcrumb item.\n   * Defaults to '/'\n   */\n  separator?: Separators;\n  /**\n   * Breadcrumbs item text.\n   */\n  text?: string;\n  /**\n   * Behavior variations for the breadcrumbs item.\n   * - **back** - results in a left facing icon, usually denoting the second last breadcrumb item in a mobile breakpoint.\n   * - **collapsed** - results in an ellipsis, where interaction spawns a Menu containing more links.\n   */\n  variant?: 'back' | 'collapsed';\n};\n\n/**\n * A single breadcrumb subcomponent, to be used in the Breadcrumbs component.\n */\nexport const BreadcrumbsItem = (props: BreadcrumbItemProps) => {\n  const {\n    className,\n    href,\n    menuItems,\n    separator = '/',\n    text,\n    variant,\n    // TODO(next-major): remove, with the assert below.\n    icon: removedIcon,\n    ...other\n  } = props as WithRemovedIconProps<BreadcrumbItemProps, 'icon'>;\n\n  // TODO(next-major): remove.\n  assertNoRemovedIconProp('Breadcrumbs.Item', 'icon', 'back', removedIcon);\n\n  // The back arrow is semantic: it is the same \"up one level\" mark used elsewhere in the\n  // app, so it comes from `IconProvider` rather than from a prop on this item.\n  const backIcon = useSemanticIcon('back');\n\n  const componentClassName = clsx(\n    styles['breadcrumbs__item'],\n    variant === 'back' && styles['breadcrumbs__item-back'],\n    className,\n  );\n\n  const ellipsisButtonClassName = clsx(\n    styles['breadcrumbs__link'],\n    styles['breadcrumbs__ellipsis'],\n  );\n\n  const getInteractionElement = () => {\n    if (variant === 'collapsed') {\n      /* The collapsed variant is a button with ellipsis. Interaction spawns a Menu containing the collapsed breadcrumb links. */\n      return (\n        <Menu>\n          <Menu.PlainButton\n            aria-label=\"Show more breadcrumbs\"\n            className={ellipsisButtonClassName}\n          >\n            …\n          </Menu.PlainButton>\n          <Menu.Items>{menuItems}</Menu.Items>\n        </Menu>\n      );\n    } else if (variant === 'back') {\n      /* The back variant is a left pointing icon that usually links to the second last breadcrumb href. */\n      return (\n        // The name belongs on the link rather than on the icon inside it, for the same\n        // reason as `InputChip`'s action button: a provider can set `back` to a node, whose\n        // accessible treatment is its author's, which would leave this link unnamed.\n        <a\n          // `text` is optional, and this variant renders an icon rather than a label, so\n          // without a fallback the link reaches assistive tech unnamed.\n          aria-label={text || 'Back'}\n          className={styles['breadcrumbs__link']}\n          href={href as string}\n        >\n          {hasSlotContent(backIcon) && (\n            <IconSlot\n              className={styles['breadcrumbs__back-icon']}\n              content={backIcon}\n              purpose=\"decorative\"\n            />\n          )}\n        </a>\n      );\n    } else {\n      /* The default variant displays the prop text and links the prop href. */\n      return (\n        <a className={styles['breadcrumbs__link']} href={href as string}>\n          {text}\n        </a>\n      );\n    }\n  };\n\n  return (\n    <li className={componentClassName} {...other}>\n      {getInteractionElement()}\n      <Text aria-hidden as=\"span\" className={styles['breadcrumbs__separator']}>\n        {separator}\n      </Text>\n    </li>\n  );\n};\n\nBreadcrumbs.displayName = 'Breadcrumbs';\nCustomSeparatorBreadcrumbsItem.displayName = 'Breadcrumbs.Item';\n\nBreadcrumbs.Item = CustomSeparatorBreadcrumbsItem;\n"],"names":["React"],"mappings":";;;;;;;;;;;AAiBM,SAAU,oBAAoB,CAAC,QAAmB,EAAA;IACtD,MAAM,aAAa,GAAGA,cAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC;IACtD,OAAO,aAAa,CAAC,MAAM,CAAC,CAAC,YAA6B,EAAE,KAAK,KAAI;QACnE,IAAK,KAAiC,CAAC,IAAI,KAA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