44import { forwardRef , Ref , useEffect , useMemo , useRef , useState } from "react" ;
55import clsx from "clsx" ;
66
7- import { useContainerQuery } from "@cloudscape-design/component-toolkit" ;
87import {
98 circleIndex ,
109 handleKey ,
@@ -27,13 +26,12 @@ import testClasses from "./test-classes/styles.css.js";
2726const TOOLTIP_BLUR_DELAY = 50 ;
2827const HIGHLIGHT_LOST_DELAY = 50 ;
2928const SCROLL_DELAY = 100 ;
30- const SHOULD_STACK_SIZE = 400 ;
3129
3230export interface ChartLegendProps {
3331 someHighlighted : boolean ;
3432 items : readonly LegendItem [ ] ;
3533 alignment : "horizontal" | "vertical" ;
36- type : "single" | "primary" | "secondary" ;
34+ contentAlignment : "single" | "primary" | "secondary" ;
3735 legendTitle ?: string ;
3836 ariaLabel ?: string ;
3937 actions ?: React . ReactNode ;
@@ -47,8 +45,8 @@ export interface ChartLegendProps {
4745
4846export const ChartLegend = ( {
4947 items,
50- type,
5148 alignment,
49+ contentAlignment,
5250 someHighlighted,
5351 legendTitle,
5452 ariaLabel,
@@ -60,9 +58,7 @@ export const ChartLegend = ({
6058 getTooltipContent,
6159 horizontalAlignment,
6260} : ChartLegendProps ) => {
63- const containerObjectRef = useRef < HTMLDivElement > ( ) ;
64- const [ shouldAutoStack , containerQueryRef ] = useContainerQuery ( ( rect ) => rect . borderBoxWidth <= SHOULD_STACK_SIZE ) ;
65- const containerRef = useMergeRefs ( containerQueryRef , containerObjectRef ) ;
61+ const containerRef = useRef < HTMLDivElement > ( null ) ;
6662 const elementsByIndexRef = useRef < Record < number , HTMLElement > > ( [ ] ) ;
6763 const elementsByIdRef = useRef < Record < string , HTMLElement > > ( { } ) ;
6864 const tooltipRef = useRef < HTMLElement > ( null ) ;
@@ -112,7 +108,7 @@ export const ChartLegend = ({
112108 if ( isMouseInContainer . current ) {
113109 return ;
114110 }
115- const container = containerObjectRef ?. current ;
111+ const container = containerRef ?. current ;
116112 const element = elementsByIndexRef . current ?. [ highlightedIndex ] ;
117113 if ( ! container || ! element ) {
118114 return ;
@@ -127,7 +123,7 @@ export const ChartLegend = ({
127123 container . scrollTo ( { top, behavior : "smooth" } ) ;
128124 }
129125 } , SCROLL_DELAY ) ;
130- } , [ containerObjectRef , items , scrollIntoViewControl ] ) ;
126+ } , [ containerRef , items , scrollIntoViewControl ] ) ;
131127
132128 const showHighlight = ( itemId : string ) => {
133129 const item = items . find ( ( item ) => item . id === itemId ) ;
@@ -191,8 +187,8 @@ export const ChartLegend = ({
191187 }
192188
193189 function getNextFocusTarget ( ) : null | HTMLElement {
194- if ( containerObjectRef . current ) {
195- const buttons : HTMLButtonElement [ ] = Array . from ( containerObjectRef . current . querySelectorAll ( `.${ styles . item } ` ) ) ;
190+ if ( containerRef . current ) {
191+ const buttons : HTMLButtonElement [ ] = Array . from ( containerRef . current . querySelectorAll ( `.${ styles . item } ` ) ) ;
196192 return buttons [ selectedIndex ] ?? null ;
197193 }
198194 return null ;
@@ -213,18 +209,18 @@ export const ChartLegend = ({
213209 navigationAPI . current ! . updateFocusTarget ( ) ;
214210 } ) ;
215211
216- const isStacked = alignment === "vertical" || shouldAutoStack ;
217- const isHorizontalSecondary = alignment === "horizontal" && type === "secondary" ;
212+ const isVertical = alignment === "vertical" ;
213+ const isHorizontalSecondary = alignment === "horizontal" && contentAlignment === "secondary" ;
218214
219215 const tooltipTrack = useRef < null | HTMLElement > ( null ) ;
220216 const tooltipTarget = items . find ( ( item ) => item . id === tooltipItemId ) ?? null ;
221217 tooltipTrack . current = tooltipItemId ? elementsByIdRef . current [ tooltipItemId ] : null ;
222218 const tooltipContent = tooltipTarget && getTooltipContent ( { legendItem : tooltipTarget } ) ;
223- const tooltipPosition = isStacked ? "left" : "bottom" ;
219+ const tooltipPosition = isVertical ? "left" : "bottom" ;
224220
225221 function getTitleAlignment ( ) {
226- if ( ! isStacked ) {
227- switch ( type ) {
222+ if ( ! isVertical ) {
223+ switch ( contentAlignment ) {
228224 case "primary" :
229225 return "left" ;
230226 case "secondary" :
@@ -261,19 +257,19 @@ export const ChartLegend = ({
261257 tabIndex = { - 1 }
262258 ref = { containerRef }
263259 className = { clsx ( styles . list , {
264- [ styles [ "list-side" ] ] : isStacked ,
265- [ styles [ "list-bottom" ] ] : ! isStacked ,
266- [ styles [ "list-bottom-end" ] ] : ! isStacked && isHorizontalSecondary ,
267- [ styles [ `list-bottom-${ horizontalAlignment } ` ] ] : ! isStacked && type === "single" ,
260+ [ styles [ "list-side" ] ] : isVertical ,
261+ [ styles [ "list-bottom" ] ] : ! isVertical ,
262+ [ styles [ "list-bottom-end" ] ] : ! isVertical && isHorizontalSecondary ,
263+ [ styles [ `list-bottom-${ horizontalAlignment } ` ] ] : ! isVertical && contentAlignment === "single" ,
268264 } ) }
269265 >
270266 { actions && (
271267 < >
272268 < div className = { clsx ( testClasses . actions , styles . actions , styles [ "actions-bottom" ] ) } >
273269 { actions }
274- { ! isStacked && < div className = { styles [ "actions-divider-bottom" ] } /> }
270+ { ! isVertical && < div className = { styles [ "actions-divider-bottom" ] } /> }
275271 </ div >
276- { isStacked && < div className = { styles [ "actions-divider-side" ] } /> }
272+ { isVertical && < div className = { styles [ "actions-divider-side" ] } /> }
277273 </ >
278274 ) }
279275 { items . map ( ( item , index ) => {
0 commit comments