@@ -9,7 +9,14 @@ import {
9
9
PlusIcon ,
10
10
} from '@ultraviolet/icons'
11
11
import type { ReactNode , RefObject } from 'react'
12
- import { useEffect , useLayoutEffect , useMemo , useRef , useState } from 'react'
12
+ import {
13
+ useCallback ,
14
+ useEffect ,
15
+ useLayoutEffect ,
16
+ useMemo ,
17
+ useRef ,
18
+ useState ,
19
+ } from 'react'
13
20
import { Button } from '../Button'
14
21
import { Stack } from '../Stack'
15
22
import { Tag } from '../Tag'
@@ -340,11 +347,6 @@ const SelectBar = ({
340
347
const measureRef = useRef < HTMLDivElement > ( null )
341
348
const arrowRef = useRef < HTMLDivElement > ( null )
342
349
const refPlusTag = useRef < HTMLDivElement > ( null )
343
- // width - width of the arrow (in px) - padding between tags (in px)
344
- const [ innerWidth , setInnerWidth ] = useState (
345
- innerRef . current ?. offsetWidth ??
346
- 0 - ( arrowRef . current ?. offsetWidth ?? 0 ) - SIZES_TAG . paddings ,
347
- )
348
350
const [ overflowAmount , setOverflowAmount ] = useState ( 0 )
349
351
const [ overflow , setOverflow ] = useState ( false )
350
352
const [ lastElementMaxWidth , setLastElementMaxWidth ] = useState ( 0 )
@@ -387,6 +389,16 @@ const SelectBar = ({
387
389
setDisplayShadowCopy ( true )
388
390
} , [ selectedData . selectedValues . length ] )
389
391
392
+ const getWidth = useCallback ( ( ) => {
393
+ if ( refTag . current ) {
394
+ return refTag . current . offsetWidth
395
+ }
396
+
397
+ return (
398
+ innerRef . current ?. offsetWidth ??
399
+ 0 - ( arrowRef . current ?. offsetWidth ?? 0 ) - SIZES_TAG . paddings
400
+ )
401
+ } , [ innerRef . current ?. offsetWidth ] )
390
402
// We then want to measure the tags length before displaying them
391
403
// so we can determine if there is an overflow or not
392
404
// We use useLayoutEffect to ensure the measurement is done before the browser paints
@@ -399,6 +411,7 @@ const SelectBar = ({
399
411
if ( measureRef . current && selectedData . selectedValues . length > 0 ) {
400
412
const toMeasureElements : HTMLCollection = measureRef . current . children
401
413
const toMeasureElementsArray = [ ...toMeasureElements ]
414
+ const innerWidth = getWidth ( )
402
415
403
416
const {
404
417
measuredVisibleTags,
@@ -506,32 +519,15 @@ const SelectBar = ({
506
519
setDisplayShadowCopy ( false )
507
520
} , [
508
521
displayShadowCopy ,
509
- innerWidth ,
510
522
potentiallyNonOverflowedValues ,
511
523
selectedData . selectedValues . length ,
524
+ getWidth ,
512
525
] )
513
526
514
527
useEffect ( ( ) => {
515
528
setSelectedData ( { type : 'update' } )
516
529
} , [ setSelectedData , options ] )
517
530
518
- useEffect ( ( ) => {
519
- const getWidth = ( ) => {
520
- if ( refTag . current ) {
521
- setInnerWidth ( refTag . current . offsetWidth )
522
- } else {
523
- setInnerWidth (
524
- innerRef . current ?. offsetWidth ??
525
- 0 - ( arrowRef . current ?. offsetWidth ?? 0 ) - SIZES_TAG . paddings ,
526
- )
527
- }
528
- }
529
- getWidth ( )
530
- window . addEventListener ( 'resize' , getWidth )
531
-
532
- return ( ) => window . removeEventListener ( 'resize' , getWidth )
533
- } , [ innerRef , refTag , selectedData . selectedValues ] )
534
-
535
531
const shouldDisplayValues = useMemo ( ( ) => {
536
532
if ( multiselect ) {
537
533
return (
0 commit comments