Skip to content

Commit 28deb92

Browse files
committed
Hide thumbs while we work out indexes
1 parent f3d9c34 commit 28deb92

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

packages/react/slider/src/Slider.tsx

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ const BACK_KEYS: Record<Direction, string[]> = {
2222
rtl: ['ArrowDown', 'Home', 'ArrowRight', 'PageDown'],
2323
};
2424

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+
>();
3129

3230
/* -------------------------------------------------------------------------------------------------
3331
* Slider
@@ -490,34 +488,44 @@ SliderRange.displayName = RANGE_NAME;
490488
const THUMB_NAME = 'SliderThumb';
491489
const THUMB_DEFAULT_TAG = 'span';
492490

493-
type SliderThumbOwnProps = Omit<Polymorphic.OwnProps<typeof SliderThumbImpl>, 'value' | 'index'>;
491+
type SliderThumbOwnProps = Omit<Polymorphic.OwnProps<typeof SliderThumbImpl>, 'index'>;
494492
type SliderThumbPrimitive = Polymorphic.ForwardRefComponent<
495493
Polymorphic.IntrinsicElement<typeof SliderThumbImpl>,
496494
SliderThumbOwnProps
497495
>;
498496

499497
const SliderThumb = React.forwardRef((props, forwardedRef) => {
500-
const context = useSliderContext(THUMB_NAME);
501-
const { getItems } = useCollectionItem();
498+
const { getItems } = useCollection();
502499
const [thumb, setThumb] = React.useState<React.ElementRef<typeof SliderThumbImpl> | null>(null);
503500
const composedRefs = useComposedRefs(forwardedRef, (node) => setThumb(node));
501+
const isInitialRenderRef = React.useRef(true);
504502
const index = React.useMemo(
505-
() => (thumb ? getItems().findIndex((item) => item.ref.current === thumb) : 0),
503+
() => (thumb ? getItems().findIndex((item) => item.ref.current === thumb) : -1),
506504
[getItems, thumb]
507505
);
508-
const value = context.values[index];
509506

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+
/>
512523
) : null;
513524
}) as SliderThumbPrimitive;
514525

515526
type SliderThumbImplOwnProps = Polymorphic.Merge<
516527
Polymorphic.OwnProps<typeof Primitive>,
517-
{
518-
value: number;
519-
index: number;
520-
}
528+
{ index: number }
521529
>;
522530

523531
type SliderThumbImplPrimitive = Polymorphic.ForwardRefComponent<
@@ -526,12 +534,13 @@ type SliderThumbImplPrimitive = Polymorphic.ForwardRefComponent<
526534
>;
527535

528536
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;
530538
const context = useSliderContext(THUMB_NAME);
531539
const orientation = React.useContext(SliderOrientationContext);
532540
const [thumb, setThumb] = React.useState<HTMLSpanElement | null>(null);
533541
const composedRefs = useComposedRefs(forwardedRef, (node) => setThumb(node));
534542
const size = useSize(thumb);
543+
const value = context.values[index];
535544
const percent = convertValueToPercentage(value, context.min, context.max);
536545
const label = getLabel(index, context.values.length);
537546
const orientationSize = size?.[orientation.size];

0 commit comments

Comments
 (0)