Skip to content

Commit 5cb5f28

Browse files
Respect prefers-reduced-motion in carousel scroll buttons (#1537)
Adjust the carousel scroll behavior to respect the user's prefers-reduced-motion setting, providing an instant scroll for users who have indicated a preference for reduced motion. Note that this does not solve the scroll-snapping: we need to figure out a way to scroll instantly when `prefers-reduced-motion` is enabled. That might require some work. So starting with this as a small improvement.
1 parent 2d52120 commit 5cb5f28

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

.changeset/honest-emus-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@obosbbl/grunnmuren-react": patch
3+
---
4+
5+
Respect prefers-reduced-motion when user clicks the prev/next buttons in the `<Carousel>` component.

packages/react/src/carousel/carousel.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,12 @@ const Carousel = ({
111111
const elementWithFocusVisible =
112112
carouselRef.current?.querySelector(':focus-visible');
113113

114+
const prefersReducedMotion = window.matchMedia(
115+
'(prefers-reduced-motion: reduce)',
116+
).matches;
117+
114118
carouselItemsRef.current.children[scrollTargetIndex]?.scrollIntoView({
115-
behavior: 'smooth',
119+
behavior: prefersReducedMotion ? 'instant' : 'smooth',
116120
inline: 'start',
117121
block: 'nearest',
118122
});

0 commit comments

Comments
 (0)