Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Slider] Fix slider focus in scrollable context #3118

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .yarn/versions/fecf9f47.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
releases:
"@radix-ui/react-slider": patch

declined:
- primitives
20 changes: 20 additions & 0 deletions packages/react/slider/src/Slider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,26 @@ export const Strict = () => (
</React.StrictMode>
);

export const InScrollableContext = () => {
const [value, setValue] = React.useState(0);

return (
<div style={{ width: '40vw', overflowX: 'scroll', border: '1px' }}>
<Slider.Root
className={rootClass()}
style={{ width: '100vw' }}
value={[value]}
onValueChange={([newValue]: [number]) => setValue(newValue)}
>
<Slider.Track className={trackClass()}>
<Slider.Range className={rangeClass()} />
</Slider.Track>
<Slider.Thumb className={thumbClass()} />
</Slider.Root>
</div>
);
};

export const Chromatic = () => (
<>
<h1>Uncontrolled</h1>
Expand Down
4 changes: 2 additions & 2 deletions packages/react/slider/src/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const Slider = React.forwardRef<SliderElement, SliderProps>(
defaultProp: defaultValue,
onChange: (value) => {
const thumbs = [...thumbRefs.current];
thumbs[valueIndexToChangeRef.current]?.focus();
thumbs[valueIndexToChangeRef.current]?.focus({ preventScroll: true });
onValueChange(value);
},
});
Expand Down Expand Up @@ -431,7 +431,7 @@ const SliderImpl = React.forwardRef<SliderImplElement, SliderImplProps>(
// Touch devices have a delay before focusing so won't focus if touch immediately moves
// away from target (sliding). We want thumb to focus regardless.
if (context.thumbs.has(target)) {
target.focus();
target.focus({ preventScroll: true });
} else {
onSlideStart(event);
}
Expand Down