Skip to content

Commit

Permalink
feat(carousel): respect reduced-motion during autoplay (#2319)
Browse files Browse the repository at this point in the history
  • Loading branch information
saiponnada authored Nov 8, 2024
1 parent c006353 commit a41ae29
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-tips-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ebay/ebayui-core": minor
---

feat(carousel): respect reduced-motion during autoplay
14 changes: 14 additions & 0 deletions src/common/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ export function getMaxWidth(el: HTMLElement) {
el.style.width = width;
return result;
}

/**
* Determines if the user has requested reduced motion in their system preferences.
*
* This function checks the `prefers-reduced-motion` media query to see if the user
* has indicated that they prefer reduced motion. This can be useful for improving
* accessibility by disabling animations or transitions for users who may be sensitive
* to motion.
*
* @returns {boolean} `true` if the user prefers reduced motion, `false` otherwise.
*/
export const useReducedMotion =
typeof window !== "undefined" &&
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
4 changes: 4 additions & 0 deletions src/components/ebay-carousel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ Descrete or Continuious carousel component. Can show items as a slide or various

When scrolling items, focusable elements that are not visible in the carousel should not be tabbable by default. When the carousel scrolls, it then removes the tabindex to allow the item to be focusable.
In order to preserve the tabindex on an item, pass `data-carousel-tabindex="-1"` attribute to a given focusable element in order to default to that tabindex instead of removing the tabindex when the item is visible.

## Reduced motion

The carousel doesnot autoplay by respecting the `prefers-reduced-motion` media query. Toggle your reduced motion settings to view autoplay example with the default behavior and reduced motion behavior.
6 changes: 6 additions & 0 deletions src/components/ebay-carousel/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { processHtmlAttributes } from "../../common/html-attributes";
import { onScrollDebounced as onScroll } from "./utils/on-scroll-debounced";
import { scrollTransition } from "./utils/scroll-transition";
import type { WithNormalizedProps } from "../../global";
import { useReducedMotion } from "../../common/dom";

type Direction = typeof LEFT | typeof RIGHT;
// Used for carousel slide direction.
Expand Down Expand Up @@ -517,6 +518,11 @@ class Carousel extends Marko.Component<Input, State> {
});
this.skipScrolling = false;

// If user had reduced motion turned on in OS settings, pause autoplay.
if(useReducedMotion) {
this.state.paused = true;
}

if (isNativeScrolling(this.listEl)) {
config.nativeScrolling = true;
this.once(
Expand Down
5 changes: 1 addition & 4 deletions src/components/ebay-progress-bar-expressive/component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { AttrString } from "marko/tags-html";
import type { WithNormalizedProps } from "../../global";

export const useReducedMotion =
typeof window !== "undefined" &&
window.matchMedia("(prefers-reduced-motion: reduce)").matches;
import { useReducedMotion } from "../../common/dom";

export const messageDurationStandard = 1500;
export const messageDurationReducedMotionMultiplier = 1.5;
Expand Down
2 changes: 1 addition & 1 deletion src/components/ebay-progress-bar-expressive/index.marko
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { processHtmlAttributes } from "../../common/html-attributes";
import { useReducedMotion } from "./component";
import { useReducedMotion } from "../../common/dom";
$ const {
a11yText = "Loading...",
class: inputClass,
Expand Down

0 comments on commit a41ae29

Please sign in to comment.