import React from "react"; import { styled, theme } from "../theme"; import type * as WPDS from "../theme"; import { Tooltip } from "../tooltip"; const StyledContainer = styled("div", { display: "flex", flexDirection: "row", alignItems: "center", gap: "$025", }); const StyledTabText = styled("div", { display: "inline-flex", flexDirection: "column", maxWidth: "14rem", // use rems, as ch value shifts when text is active bold "&::after": { content: "attr(data-text)", fontWeight: theme.fontWeights.bold, height: 0, visibility: "hidden", }, }); const Truncate = styled("span", { maxWidth: "100%", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", }); const isTruncated = (el) => { return el && el.scrollWidth > el.clientWidth; }; export type TabsTriggerContentProps = { children?: React.ReactNode; /** Overrides for the input text styles. Padding overrides affect the input container and */ css?: WPDS.CSS; } & React.ComponentPropsWithoutRef; export const TabsTriggerContent = ({ children }: TabsTriggerContentProps) => { const internalRef = React.useRef(null); const [truncated, setTruncated] = React.useState(false); React.useEffect(() => { const element = internalRef?.current; setTruncated(isTruncated(element)); }, []); const childrenArray = React.Children.toArray(children); // the StyledTabText component is set to inline flex to include bolded spacer text // that prevents shifting. Additional Truncate is needed to show the ellipsis. Other // components like icons are simply returned const content = childrenArray.map((child) => { if (typeof child === "string") { return ( {child} ); } return child; }); return ( <> {truncated ? ( {content} {internalRef?.current?.innerText} ) : ( {content} )} ); }; TabsTriggerContent.displayName = "TabsTriggerContent";