The Open Standard for Generative UI
Enhance DefaultLegend component with button width measurement and layout adjustments (#154)
Ankit Dasdf1b67533658
diff --git a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/DefaultLegend.tsx b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/DefaultLegend.tsxindex 37f9ac691..a3b02963c 100644--- a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/DefaultLegend.tsx+++ b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/DefaultLegend.tsx@@ -1,6 +1,6 @@import clsx from "clsx";import { ChevronDownIcon, ChevronUpIcon } from "lucide-react";-import React, { memo, useMemo } from "react";+import React, { memo, useCallback, useMemo, useState } from "react";import { Button } from "../../../Button/Button";import { type LegendItem } from "../../types";import { calculateVisibleItems, getToggleButtonText } from "./utils/defaultLegendUtils";@@ -31,10 +31,27 @@ const DefaultLegend = memo(},ref,) => {- // Only memoize expensive calculations+ const [buttonWidth, setButtonWidth] = useState(0);++ // We use a callback ref to measure the button's width as soon as it's mounted.+ // This is more reliable than useEffect with ref.current in the dependency array,+ // as it correctly triggers a re-render when the node is available.+ // This is a workaround for the fact that the button's width is not available immediately.+ // and we need the actual button width to calculate the layout of the legend items.+ const buttonRef = useCallback(+ (node: HTMLButtonElement | null) => {+ if (node) {+ if (node.clientWidth !== buttonWidth) {+ setButtonWidth(node.clientWidth);+ }+ }+ },+ [buttonWidth],+ );+const { visibleItems, hasMoreItems } = useMemo(() => {- return calculateVisibleItems(items, containerWidth);- }, [items, containerWidth]);+ return calculateVisibleItems(items, containerWidth, buttonWidth);+ }, [items, containerWidth, buttonWidth]);const displayItems = useMemo(() => {return isExpanded ? items : visibleItems;@@ -96,6 +113,7 @@ const DefaultLegend = memo(<Buttonvariant="tertiary"size="small"+ ref={buttonRef}className="crayon-chart-legend-toggle-button"onClick={handleToggleExpanded}iconRight={diff --git a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/defaultLegend.scss b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/defaultLegend.scssindex 5f67e3c58..d6f938a7e 100644--- a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/defaultLegend.scss+++ b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/defaultLegend.scss@@ -74,13 +74,12 @@&-toggle-button {@include cssUtils.typography(label, extra-small);color: cssUtils.$primary-text;- height: 16px;padding-left: cssUtils.$spacing-2xs;padding-right: cssUtils.$spacing-3xs;&-icon {- width: 12px;- height: 12px;+ width: 1em;+ height: 1em;color: cssUtils.$primary-text;}}diff --git a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/utils/defaultLegendUtils.ts b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/utils/defaultLegendUtils.tsindex 0134f7f27..0cb456e77 100644--- a/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/utils/defaultLegendUtils.ts+++ b/js/packages/react-ui/src/components/Charts/shared/DefaultLegend/utils/defaultLegendUtils.ts@@ -1,15 +1,31 @@import { LegendItem } from "../../../types";+import { getCanvasContext } from "../../../utils/styleUtils";-// Constants-export const SHOW_MORE_BUTTON_WIDTH = 65;-export const CHARACTER_WIDTH = 7;-export const INDICATOR_WIDTH = 10;-export const GAP_WIDTH = 12;+const CHARACTER_WIDTH = 7; // Now only used as a fallback+const INDICATOR_WIDTH = 10;+const GAP_WIDTH = 12;++// Memoize canvas context to avoid recreating it on every call+let canvasContext: CanvasRenderingContext2D | null;+const getMemoizedCanvasContext = (): CanvasRenderingContext2D | null => {+ if (canvasContext === undefined) {+ canvasContext = getCanvasContext();+ }+ return canvasContext;+};/*** Calculate the estimated width of a legend item*/export const calculateItemWidth = (label: string): number => {+ const context = getMemoizedCanvasContext();++ if (context) {+ // If canvas is supported, measure text width accurately+ return context.measureText(label).width + INDICATOR_WIDTH + GAP_WIDTH;+ }++ // Fallback for SSR or if canvas is not supportedreturn label.length * CHARACTER_WIDTH + INDICATOR_WIDTH + GAP_WIDTH;};@@ -19,6 +35,7 @@ export const calculateItemWidth = (label: string): number => {export const calculateVisibleItems = (items: LegendItem[],containerWidth?: number,+ buttonWidth?: number,): {visibleItems: LegendItem[];hasMoreItems: boolean;@@ -29,7 +46,7 @@ export const calculateVisibleItems = (}// Reserve space for "show more" button- const availableWidth = containerWidth - SHOW_MORE_BUTTON_WIDTH;+ const availableWidth = containerWidth - (buttonWidth ?? 0);let currentWidth = 0;let visibleCount = 0;@@ -40,8 +57,9 @@ export const calculateVisibleItems = (const itemWidth = calculateItemWidth(item.label);- if (currentWidth + itemWidth <= availableWidth) {- currentWidth += itemWidth;+ // + GAP for the gap between the items+ if (currentWidth + itemWidth + GAP_WIDTH <= availableWidth) {+ currentWidth += itemWidth + GAP_WIDTH;visibleCount++;} else {break;@@ -77,5 +95,5 @@ export const getToggleButtonText = (}const hiddenCount = totalItems - visibleItemsCount;- return hiddenCount === 1 ? "1 more" : `${hiddenCount} more`;+ return `${hiddenCount} more`;};