The Open Standard for Generative UI
Enhance SingleStackedBarChart and StackedLegend components (#213)
subhamdf382afa85db
diff --git a/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/SingleStackedBarChart.tsx b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/SingleStackedBarChart.tsxindex 408b22ba9..2170f120a 100644--- a/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/SingleStackedBarChart.tsx+++ b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/SingleStackedBarChart.tsx@@ -34,6 +34,7 @@ export const SingleStackedBar = <T extends SingleStackedBarData>({}: SingleStackedBarProps<T>) => {const [isLegendExpanded, setIsLegendExpanded] = useState(false);const [activeIndex, setActiveIndex] = useState<number | null>(null);+ const [hoveredLegendKey, setHoveredLegendKey] = useState<string | null>(null);const wrapperRef = useRef<HTMLDivElement>(null);const [containerWidth, setContainerWidth] = useState(0);const [tooltipPosition, setTooltipPosition] = useState<{ x: number; y: number } | null>(null);@@ -97,28 +98,37 @@ export const SingleStackedBar = <T extends SingleStackedBarData>({);// Handle legend item hover with tooltip positioning- const handleLegendItemHover = useCallback((hoverIndex: number | null) => {- setActiveIndex(hoverIndex);- if (hoverIndex !== null) {- // Try to position tooltip above the hovered segment- const segmentEl = wrapperRef.current?.querySelectorAll(- ".crayon-single-stacked-bar-chart-segment",- )?.[hoverIndex] as HTMLDivElement | undefined;- if (segmentEl) {- const rect = segmentEl.getBoundingClientRect();- const containerRect = wrapperRef.current?.getBoundingClientRect();- if (containerRect) {- const relativeX = rect.left + rect.width / 2 - containerRect.left;- const relativeY = rect.top - containerRect.top;- setTooltipPosition({ x: relativeX, y: relativeY });- } else {- setTooltipPosition({ x: rect.left + rect.width / 2, y: rect.top });+ const handleLegendItemHover = useCallback(+ (hoverIndex: number | null) => {+ setActiveIndex(hoverIndex);+ if (hoverIndex !== null) {+ const segment = segments[hoverIndex];+ if (segment) {+ const legendKey = `${segment.category}-${hoverIndex}`;+ setHoveredLegendKey(legendKey);}+ // Try to position tooltip above the hovered segment+ const segmentEl = wrapperRef.current?.querySelectorAll(+ ".crayon-single-stacked-bar-chart-segment",+ )?.[hoverIndex] as HTMLDivElement | undefined;+ if (segmentEl) {+ const rect = segmentEl.getBoundingClientRect();+ const containerRect = wrapperRef.current?.getBoundingClientRect();+ if (containerRect) {+ const relativeX = rect.left + rect.width / 2 - containerRect.left;+ const relativeY = rect.top - containerRect.top;+ setTooltipPosition({ x: relativeX, y: relativeY });+ } else {+ setTooltipPosition({ x: rect.left + rect.width / 2, y: rect.top });+ }+ }+ } else {+ setHoveredLegendKey(null);+ setTooltipPosition(null);}- } else {- setTooltipPosition(null);- }- }, []);+ },+ [segments],+ );// Segmented progress barreturn (@@ -145,6 +155,8 @@ export const SingleStackedBar = <T extends SingleStackedBarData>({}}onMouseEnter={(e) => {setActiveIndex(index);+ const legendKey = `${segment.category}-${index}`;+ setHoveredLegendKey(legendKey);const rect = (e.currentTarget as HTMLDivElement).getBoundingClientRect();const containerRect = wrapperRef.current?.getBoundingClientRect();if (containerRect) {@@ -156,7 +168,10 @@ export const SingleStackedBar = <T extends SingleStackedBarData>({setTooltipPosition({ x: rect.left + rect.width / 2, y: rect.top });}}}- onMouseLeave={() => setActiveIndex(null)}+ onMouseLeave={() => {+ setActiveIndex(null);+ setHoveredLegendKey(null);+ }}><div className="crayon-single-stacked-bar-chart-segment-line" /></div>@@ -189,9 +204,13 @@ export const SingleStackedBar = <T extends SingleStackedBarData>({<StackedLegenditems={stackedLegendItems}containerWidth={containerWidth}+ onItemHover={setHoveredLegendKey}+ activeKey={hoveredLegendKey}onLegendItemHover={handleLegendItemHover}separatorshowTitle={false}+ layout="showMore"+ className="crayon-single-stacked-bar-chart-stacked-legend"/>)}</div>diff --git a/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/singleStackedBarChart.scss b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/singleStackedBarChart.scssindex 4a7af4bd4..5c4d237a1 100644--- a/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/singleStackedBarChart.scss+++ b/js/packages/react-ui/src/components/Charts/SingleStackedBarChart/singleStackedBarChart.scss@@ -64,3 +64,6 @@.crayon-single-stacked-bar-chart-animated {transition: width 0.6s cubic-bezier(0.4, 0, 0.2, 1);}+.crayon-single-stacked-bar-chart-stacked-legend {+ margin-top: cssUtils.$spacing-s;+}diff --git a/js/packages/react-ui/src/components/Charts/shared/StackedLegend/StackedLegend.tsx b/js/packages/react-ui/src/components/Charts/shared/StackedLegend/StackedLegend.tsxindex d2e3c5f2e..f578e1559 100644--- a/js/packages/react-ui/src/components/Charts/shared/StackedLegend/StackedLegend.tsx+++ b/js/packages/react-ui/src/components/Charts/shared/StackedLegend/StackedLegend.tsx@@ -1,3 +1,4 @@+import clsx from "clsx";import { ChevronDown, ChevronUp } from "lucide-react";import { useEffect, useRef, useState } from "react";import { Button } from "../../../Button";@@ -5,6 +6,8 @@ import { IconButton } from "../../../IconButton";import { Separator } from "../../../Separator";import { StackedLegendItem } from "../../types";+export type StackedLegendLayout = "auto" | "showMore" | "scrollable";+interface StackedLegendProps {items: StackedLegendItem[];onItemHover?: (key: string | null) => void;@@ -14,6 +17,9 @@ interface StackedLegendProps {title?: string;separator?: boolean;showTitle?: boolean;+ layout?: StackedLegendLayout;+ className?: string;+ style?: React.CSSProperties;}const formatPercentage = (value: number, total: number): string => {@@ -34,6 +40,9 @@ export const StackedLegend = ({containerWidth,separator = false,showTitle = true,+ layout = "auto",+ className,+ style,}: StackedLegendProps) => {const containerRef = useRef<HTMLDivElement>(null);const listRef = useRef<HTMLDivElement>(null);@@ -42,9 +51,14 @@ export const StackedLegend = ({const [showAll, setShowAll] = useState(false);const [isOverflowing, setIsOverflowing] = useState(false);+ // Determine the actual layout to useconst isShowMoreLayout =- containerWidth !== undefined &&- (containerWidth < SHOW_MORE_BREAKPOINT || items.length > LEGEND_ITEM_LIMIT);+ layout === "showMore" ||+ (layout === "auto" &&+ containerWidth !== undefined &&+ (containerWidth < SHOW_MORE_BREAKPOINT || items.length > LEGEND_ITEM_LIMIT));++ const isScrollableLayout = layout === "scrollable" || (layout === "auto" && !isShowMoreLayout);const handleMouseEnter = (key: string, index: number) => {onItemHover?.(key);@@ -118,40 +132,43 @@ export const StackedLegend = ({return (<divref={containerRef}- className="crayon-stacked-legend-container"+ className={clsx("crayon-stacked-legend-container", className)}style={{width: containerWidth ? `${containerWidth}px` : "100%",+ ...style,}}>- <div className="crayon-stacked-legend-header">- {showTitle && (- <div className="crayon-stacked-legend-header-title">{items.length} values</div>- )}- <div className="crayon-stacked-legend-header-buttons">- {!isShowMoreLayout && isOverflowing && (- <>- <IconButton- className="crayon-stacked-legend-scroll-button crayon-stacked-legend-scroll-up"- onClick={scrollUp}- aria-label="Scroll legend up"- icon={<ChevronUp />}- variant="secondary"- size="extra-small"- disabled={!showUpButton}- />- <IconButton- className="crayon-stacked-legend-scroll-button crayon-stacked-legend-scroll-down"- onClick={scrollDown}- aria-label="Scroll legend down"- icon={<ChevronDown />}- variant="secondary"- size="extra-small"- disabled={!showDownButton}- />- </>+ {(showTitle || (isScrollableLayout && isOverflowing)) && (+ <div className="crayon-stacked-legend-header">+ {showTitle && (+ <div className="crayon-stacked-legend-header-title">{items.length} values</div>)}+ <div className="crayon-stacked-legend-header-buttons">+ {isScrollableLayout && isOverflowing && (+ <>+ <IconButton+ className="crayon-stacked-legend-scroll-button crayon-stacked-legend-scroll-up"+ onClick={scrollUp}+ aria-label="Scroll legend up"+ icon={<ChevronUp />}+ variant="secondary"+ size="extra-small"+ disabled={!showUpButton}+ />+ <IconButton+ className="crayon-stacked-legend-scroll-button crayon-stacked-legend-scroll-down"+ onClick={scrollDown}+ aria-label="Scroll legend down"+ icon={<ChevronDown />}+ variant="secondary"+ size="extra-small"+ disabled={!showDownButton}+ />+ </>+ )}+ </div></div>- </div>+ )}<div ref={listRef} className="crayon-stacked-legend">{itemsToDisplay.map((item, index) => (<>diff --git a/js/packages/react-ui/src/components/Charts/shared/StackedLegend/stackedLegend.scss b/js/packages/react-ui/src/components/Charts/shared/StackedLegend/stackedLegend.scssindex 75580e80f..14636aca0 100644--- a/js/packages/react-ui/src/components/Charts/shared/StackedLegend/stackedLegend.scss+++ b/js/packages/react-ui/src/components/Charts/shared/StackedLegend/stackedLegend.scss@@ -142,11 +142,13 @@&-show-more-button {width: 100%;justify-content: center;+ margin-top: cssUtils.$spacing-2xs;}&-show-less-button {width: 100%;justify-content: center;+ margin-top: cssUtils.$spacing-2xs;}}