import React, { CSSProperties } from "react"; import { MaterialIcon } from "@shared/components/material-icon"; import { Text } from "@shared/components/text"; import { Button } from "@shared/contentful/blocks/button"; import { ButtonProps as ContentfulButtonProps } from "@shared/contentful/blocks/button/types"; import { useOutsideClick } from "@shared/hooks/use-outside-click"; import { cx } from "@shared/utils"; type ComponentButtonGroup = { anchorId?: string | null; title?: string | null; items?: { items?: ContentfulButtonProps[] | null } | null; }; type Link = ContentfulButtonProps | ComponentButtonGroup; type LinkGroupsProps = { link?: Link; anchorName: string; /** * Tailwind text-color class to apply to the top-level link/group * label. Lets the parent navbar invert text color (e.g. `text-white`) * when rendered on a dark background. Submenu items keep their * default `text-text-link` so they remain readable on the white * popover background. */ linkColorClassName?: string; }; const isButton = (link: Link): link is ContentfulButtonProps => { // If your group never has `href`, this is a simple and effective guard return typeof (link as ContentfulButtonProps).href === "string"; }; export const DesktopLinkGroups: React.FC = ({ link, anchorName, linkColorClassName, }) => { const [isOpen, setIsOpen] = React.useState(false); const ref = React.useRef(null); useOutsideClick(ref, () => setIsOpen(false)); if (!link) return null; // Single button if (isButton(link)) { return (
0 ? "pointer-events-auto translate-y-0 opacity-100 duration-75" : "pointer-events-none -translate-y-2 opacity-0 duration-0" )} style={ { positionAnchor: fullAnchorName, top: "anchor(bottom)", left: "calc((anchor(left) + anchor(right)) / 2)", translate: "-50% 0", } as CSSProperties } >
    { // Close the popup when a submenu link is clicked. // Required for Pages Router where this component persists across // client-side navigations and `isOpen` would otherwise stay true. const target = event.target as HTMLElement | null; if (target?.closest("a")) { setIsOpen(false); } }} > {subMenu.map((site, index) => { return (
  • ); })}
); };