@@ -22,12 +22,10 @@ const BACK_KEYS: Record<Direction, string[]> = {
22
22
rtl : [ 'ArrowDown' , 'Home' , 'ArrowRight' , 'PageDown' ] ,
23
23
} ;
24
24
25
- const [
26
- CollectionProvider ,
27
- CollectionSlot ,
28
- CollectionItemSlot ,
29
- useCollectionItem ,
30
- ] = createCollection < React . ElementRef < typeof SliderThumb > , { } > ( ) ;
25
+ const [ CollectionProvider , CollectionSlot , CollectionItemSlot , useCollection ] = createCollection <
26
+ React . ElementRef < typeof SliderThumb > ,
27
+ { }
28
+ > ( ) ;
31
29
32
30
/* -------------------------------------------------------------------------------------------------
33
31
* Slider
@@ -490,34 +488,44 @@ SliderRange.displayName = RANGE_NAME;
490
488
const THUMB_NAME = 'SliderThumb' ;
491
489
const THUMB_DEFAULT_TAG = 'span' ;
492
490
493
- type SliderThumbOwnProps = Omit < Polymorphic . OwnProps < typeof SliderThumbImpl > , 'value' | ' index'> ;
491
+ type SliderThumbOwnProps = Omit < Polymorphic . OwnProps < typeof SliderThumbImpl > , 'index' > ;
494
492
type SliderThumbPrimitive = Polymorphic . ForwardRefComponent <
495
493
Polymorphic . IntrinsicElement < typeof SliderThumbImpl > ,
496
494
SliderThumbOwnProps
497
495
> ;
498
496
499
497
const SliderThumb = React . forwardRef ( ( props , forwardedRef ) => {
500
- const context = useSliderContext ( THUMB_NAME ) ;
501
- const { getItems } = useCollectionItem ( ) ;
498
+ const { getItems } = useCollection ( ) ;
502
499
const [ thumb , setThumb ] = React . useState < React . ElementRef < typeof SliderThumbImpl > | null > ( null ) ;
503
500
const composedRefs = useComposedRefs ( forwardedRef , ( node ) => setThumb ( node ) ) ;
501
+ const isInitialRenderRef = React . useRef ( true ) ;
504
502
const index = React . useMemo (
505
- ( ) => ( thumb ? getItems ( ) . findIndex ( ( item ) => item . ref . current === thumb ) : 0 ) ,
503
+ ( ) => ( thumb ? getItems ( ) . findIndex ( ( item ) => item . ref . current === thumb ) : - 1 ) ,
506
504
[ getItems , thumb ]
507
505
) ;
508
- const value = context . values [ index ] ;
509
506
510
- return value !== undefined ? (
511
- < SliderThumbImpl { ...props } ref = { composedRefs } index = { index } value = { value } />
507
+ React . useEffect ( ( ) => {
508
+ isInitialRenderRef . current = false ;
509
+ } , [ ] ) ;
510
+
511
+ /**
512
+ * Until we figure out SSR indexes, we hide the thumbs on initial render while we work out
513
+ * the index. Otherwise, SSR will place the thumbs in the wrong position and snap into the
514
+ * right position during hydration which would be jarring.
515
+ */
516
+ return isInitialRenderRef . current || index !== - 1 ? (
517
+ < SliderThumbImpl
518
+ { ...props }
519
+ ref = { composedRefs }
520
+ index = { index }
521
+ style = { isInitialRenderRef . current ? { display : 'none' } : undefined }
522
+ />
512
523
) : null ;
513
524
} ) as SliderThumbPrimitive ;
514
525
515
526
type SliderThumbImplOwnProps = Polymorphic . Merge <
516
527
Polymorphic . OwnProps < typeof Primitive > ,
517
- {
518
- value : number ;
519
- index : number ;
520
- }
528
+ { index : number }
521
529
> ;
522
530
523
531
type SliderThumbImplPrimitive = Polymorphic . ForwardRefComponent <
@@ -526,12 +534,13 @@ type SliderThumbImplPrimitive = Polymorphic.ForwardRefComponent<
526
534
> ;
527
535
528
536
const SliderThumbImpl = React . forwardRef ( ( props , forwardedRef ) => {
529
- const { as = THUMB_DEFAULT_TAG , index, value , ...thumbProps } = props ;
537
+ const { as = THUMB_DEFAULT_TAG , index, ...thumbProps } = props ;
530
538
const context = useSliderContext ( THUMB_NAME ) ;
531
539
const orientation = React . useContext ( SliderOrientationContext ) ;
532
540
const [ thumb , setThumb ] = React . useState < HTMLSpanElement | null > ( null ) ;
533
541
const composedRefs = useComposedRefs ( forwardedRef , ( node ) => setThumb ( node ) ) ;
534
542
const size = useSize ( thumb ) ;
543
+ const value = context . values [ index ] ;
535
544
const percent = convertValueToPercentage ( value , context . min , context . max ) ;
536
545
const label = getLabel ( index , context . values . length ) ;
537
546
const orientationSize = size ?. [ orientation . size ] ;
0 commit comments