diff --git a/packages/styleguide/src/lib/Typography/Anchor/Anchor.mdx b/packages/styleguide/src/lib/Typography/Anchor/Anchor.mdx index 9ab4dff4c49..55681bd1875 100644 --- a/packages/styleguide/src/lib/Typography/Anchor/Anchor.mdx +++ b/packages/styleguide/src/lib/Typography/Anchor/Anchor.mdx @@ -27,6 +27,9 @@ export const parameters = { + + + ## Usage Use an anchor to navigate to another page, resource, or location on the same page. diff --git a/packages/styleguide/src/lib/Typography/Anchor/Anchor.stories.tsx b/packages/styleguide/src/lib/Typography/Anchor/Anchor.stories.tsx index d9b2703ac0b..41da65c6db5 100644 --- a/packages/styleguide/src/lib/Typography/Anchor/Anchor.stories.tsx +++ b/packages/styleguide/src/lib/Typography/Anchor/Anchor.stories.tsx @@ -1,9 +1,10 @@ -import { Anchor, GridBox, Text } from '@codecademy/gamut'; +import { Anchor, Box, GridBox, Text, ToolTip } from '@codecademy/gamut'; import { MiniArrowRightIcon, MiniInfoOutlineIcon, } from '@codecademy/gamut-icons'; import type { Meta, StoryObj } from '@storybook/react'; +import React, { useEffect, useRef, useState } from 'react'; import { PolymorphicAnchors, VariantsExample } from './Anchor.examples'; @@ -85,3 +86,79 @@ export const IconModes: Story = { export const PolymorphicAnchor: Story = { render: () => , }; + +export const TruncateWithTooltip: React.FC<{ + text: string; + toolTipString: string; +}> = ({ text, toolTipString }) => { + const containerRef = useRef(null); + const [adjustedText, setAdjustedText] = useState(text); + + const calculateMaxText = (containerWidth: number) => { + // Assuming an average character width of 8px, adjust as necessary + const averageCharWidth = 8; + const maxChars = Math.floor(containerWidth / averageCharWidth); + return maxChars; + }; + + const [shouldTruncate, setShouldTruncate] = useState(false); + + useEffect(() => { + const handleResize = () => { + if (containerRef.current) { + const { width } = containerRef.current.getBoundingClientRect(); + const maxTextLength = calculateMaxText(width); + const isTruncated = maxTextLength < text.length; + if (isTruncated) { + // Adjust the text to fit within the max length + const truncatedText = text.slice(0, maxTextLength) + '...'; + setAdjustedText(truncatedText); + } else { + setAdjustedText(text); + } + setShouldTruncate(isTruncated); + } + }; + handleResize(); // Initial check + window.addEventListener('resize', handleResize); + return () => { + window.removeEventListener('resize', handleResize); + }; + }); + + const anchor = ( + + {adjustedText} + + ); + + return ( + + {shouldTruncate ? ( + + {anchor} + + ) : ( + anchor + )} + + ); +}; + +export const TruncateWithToolTipExample: Story = { + render: () => ( + + ), +}; + +export const TruncateWithNoToolTipExample: Story = { + render: () => ( + + ), +};