跪拜 Guibai
← Back to the summary

Making an Auto-Playing Carousel Actually Pauseable: A Vue Component's WCAG Overhaul

A carousel with autoplay: every 3 seconds it flips a page. Trying to stop it — focus on the "next" button, press Enter, it just flips another page; release, it keeps going. There is no pause button on the page, and no announcement when switching.

Screen reader users are more passive: content gets swapped with no announcement. This article treats "autoplay" as an accessibility problem to fix, not just an animation parameter.

First measure: can it actually stop?

Contrast page uses 1-second interval (compressing 3 seconds to count easily), 4 screens, with arrows and dots. 0.2.25 measurements:

Metric 0.2.25
Pause control 0
First Tab stop Previous arrow
Focus on arrow for 3s Index 3 → 2 (keeps going)
aria-live None
aria-roledescription None
Indicator aria-current 0
Under system "reduce motion" Still auto-advances

Focus inside the carousel while user is interacting, it still swaps content every 3 seconds. WCAG 2.2.2 is direct: auto-updating content lasting more than 5 seconds must provide a mechanism to pause, stop, or hide. Arrows and dots solve "manual switching," not "stopping."

This kind of issue doesn't show up in demos: the screen is always moving, looks "normal." Only by placing focus inside and counting 3 seconds do you find nothing can stop it.

How it was measured

Same 4-screen carousel, load 0.2.25 from npm and locally built 0.2.26, interval set to 1 second. Status line printed every 120ms: index, aria-live, pause button and text, aria-pressed, aria-current count, current focus, matchMedia match.

Each operation reads state, asserts expected, then screenshot. Both versions run same 14 assertions: 0.2.25 group asserts "expected bad", 0.2.26 group asserts "should be good", all pass.

Source: timer runs from mount to unmount

Original autoplay only has two functions:

function startAutoplay() {
  if (props.autoplay <= 0 || timer !== null) return;
  timer = setInterval(() => { ... next(); }, props.autoplay);
}

onMounted(startAutoplay);
onBeforeUnmount(stopAutoplay);

No "pause" state, no system preference check, no page visibility check. setInterval runs from mount to unmount, only stopping temporarily while dragging and restarting on release.

prefers-reduced-motion doesn't affect it: that media query affects CSS transitions, but page flipping is JS timer driven. Reduced motion only turns off animations, not timers — this is the first unintuitive point.

Fix 1: Pause button must be keyboard's first stop

First add a real <button> with aria-pressed to express state, text toggling between "Pause autoplay / Resume autoplay":

<button class="rn-carousel__pause" :aria-pressed="userPaused"
        :aria-label="userPaused ? t('carousel.play') : t('carousel.pause')">

The key is DOM order: it comes before the arrows and dots. So one Tab lands on the pause button, not after going through "previous", "next", "slide 1" ... only to find it.

Measured first Tab stop: 0.2.25 is rn-carousel__arrow--prev, 0.2.26 is rn-carousel__pause.

Button defaults to opacity: 0, appears on hover, keyboard focus, or when paused. Invisible but still focusable is deliberate: mouse users see a clean image, keyboard users get a visible button with focus ring when tabbing to it.

Why transparent by default

Always showing breaks the visual; fully hiding makes it unfindable. Compromise is opacity: 0 with three show conditions: hover, :focus-within, and paused. Keyboard tabbing to it triggers :focus-within, button appears with focus ring; when paused it stays visible, reminding users "you can continue now." Transparent doesn't mean unclickable, but mouse users only see it when needed.

Why not "hover to auto-pause"

Common pattern is pause when mouse enters. Problem is it only works for mouse: keyboard user focuses inside carousel, timer keeps going; touch user has no hover. It improves experience but doesn't constitute the "mechanism" WCAG requires. Explicit button does.

Why not "focus to auto-pause"

Another common pattern: pause when focus enters. Testing reveals a deadlock: user clicks "Resume autoplay" while focus still on that button, the focus-pause logic immediately stops playback — button appears broken.

Explicit button avoids this: pause state is only determined by userPaused, not focus.

Fix 2: aria-live should follow playback state

Should the carousel announce content changes? Depends on whether it's auto-playing:

<div class="rn-carousel__track" :aria-live="isAutoPlaying ? 'off' : 'polite'">

Measured: 0.2.25 track has no aria-live; 0.2.26 is off while playing, becomes polite after pause.

I initially thought the opposite — "having a live region is better than none." Real rule is "keep quiet on automatic changes, speak on user actions"; always-on is noisier. If carousel embeds video or animation, same switching logic applies; the basis is always "who triggered this change."

Fix 3: Carousel's own semantics

<div> wrapping several screens, screen reader only knows there's a bunch of text. Add three things:

<div role="region" aria-roledescription="carousel" :aria-label="t('carousel.label')">

Indicators add aria-current="true", so screen reader knows "current slide". Measured aria-current matches: 0 → 1.

Fix 4: Reduced motion and background tabs

Two finishing touches:

  1. On mount read matchMedia("(prefers-reduced-motion: reduce)"), if matched, default to paused, button shows "Resume autoplay". User can still manually resume — respect preference without removing choice.
  2. Listen to visibilitychange: when page hidden, clearInterval, resume on return. Auto-advancing every 3 seconds in a background tab is meaningless and wastes battery.

Measured "reduce motion" simulation (CDP force reduce) waiting 2.6 seconds: 0.2.25 index moves to 2, 0.2.26 stays at 0, button text is "Resume autoplay".

I thought A, measured B

First: I thought "has arrows and dots" = operable. Measured keyboard first stop is previous arrow, Enter only flips one page; wait 3 seconds, index goes 3→2 — no control can stop it. Operable doesn't mean stoppable.

Second: I thought adding aria-live is enough. Measured always-on interrupts screen reader every 3 seconds; correct approach is off during playback, polite after pause. Same attribute, two states.

Third: I thought "focus in pauses" is most robust. Measured clicking "Resume" while focus still on button gets immediately stopped by its own focus logic, button appears broken. Explicit button decouples "user intent" from "focus position", avoiding the deadlock.

Fourth: I thought prefers-reduced-motion would automatically disable autoplay. Measured it only affects CSS transitions, setInterval completely unaffected; must separately read matchMedia and stop timer.

What if a different implementation?

Approach Can it stop? Problem
Only arrows/dots (original) No Can only manually flip, can't stop auto-update
Hover pause Mouse yes, keyboard no Doesn't constitute WCAG mechanism; touch no hover
Focus pause Keyboard yes Clicking "Resume" with focus still on button stops immediately
Explicit pause button (current) All input methods One more control, need to sync state with autoplay
No autoplay at all No need to stop Loses "auto showcase" product requirement

Difference between row 3 and 4 is essentially "infer intent from focus position" vs "let user explicitly express intent." Former always has edge cases in combined interactions, latter doesn't.

Boundaries: single slide, no loop, autoplay=0

These boundaries don't need new API, but each could make "pause" nominal in some state.

Results

0.2.26 released: pause/resume button (first DOM stop), dual-state aria-live, carousel semantics with aria-current, reduced-motion default pause, hidden page stops timer.

0.2.25: autoplay reaches 4th slide, status line shows no pause button, no aria-live

0.2.25 first Tab lands on previous arrow, no pause control on page

0.2.25 focus on arrow for 3 seconds: index 3 to 2, still autoplaying

0.2.26: same autoplay, pause button on right, aria-current hits 1

0.2.26 first Tab lands directly on pause button

0.2.26 click pause: button becomes resume, aria-live=polite, stays on slide 4 after 3s

0.2.26 click resume: aria-live back to off, index advances again

0.2.25 simulated reduced motion: index still moves to 2

0.2.26 simulated reduced motion: defaults to stopped at 0, button shows resume autoplay

Online example (0.2.26): hover shows pause button, current slide 1

After 3.5s of pause: still slide 1, button becomes play icon

After resume: index advances to slide 2

Reduced motion: page opens paused at slide 1

Conclusion

Autoplay isn't just "add a setInterval"; it's a behavior continuously occupying user attention. Three rules, in importance order:

  1. Must have an explicit pause mechanism, reachable by keyboard first thing — not hover, not inferred from focus;
  2. aria-live follows playback state: off when playing, polite when paused;
  3. prefers-reduced-motion must be checked separately, it doesn't affect JS timers; also stop timer when page hidden.

Most easily overlooked is the "first thing" in rule 1: pause button placed after arrows and dots still requires keyboard users to Tab 4 times to stop — feature exists but doesn't solve problem. Rule 3 also often skipped: prefers-reduced-motion looks like a CSS toggle, actually doesn't affect JS timers, must read matchMedia separately to decide whether to create timer.


Component Anatomy · Series Directory

Online demo: https://ui.ruanno.top/components/carousel Source: https://gitee.com/ruanno-top/ruanno-ui