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:
- Playing:
aria-live="off". Otherwise it interrupts screen reader every 3 seconds, user hears nothing; - After pause:
aria-live="polite". Now page turns are user-triggered, announcement is necessary feedback.
<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')">
role="region": becomes a navigable landmark;aria-roledescription="carousel": screen reader will say "carousel" instead of "region";aria-label: give it a name (new locale key, Chinese "轮播图", English "Carousel").
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:
- 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. - 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
- Single slide: timer still starts but each tick returns immediately — wasted; check
slideCount <= 1before starting to not create timer. loop=false: stop timer at last slide, button text still says "Pause autoplay" but timer no longer exists; to play again need to go back to a previous slide.autoplay=0: don't render pause button — no autoplay, no pause semantics.- While dragging timer stops, on release resume according to
isAutoPlaying; if user paused, dragging won't secretly start.
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.
- Unit tests 923 → 926 (pause semantics, Tab stop point and aria-current, reduced motion — three test cases)
- Real-device assertions 14/14 passed: after pause 3s index 3→3; after resume advances; 0.2.25 control group 3s 3→2
- Documentation site new example shows pause button at lower right
Conclusion
Autoplay isn't just "add a setInterval"; it's a behavior continuously occupying user attention. Three rules, in importance order:
- Must have an explicit pause mechanism, reachable by keyboard first thing — not hover, not inferred from focus;
aria-livefollows playback state:offwhen playing,politewhen paused;prefers-reduced-motionmust 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
- Table: Three hard constraints of virtual scrolling
- Radio / RadioGroup: ARIA and arrow key selection
- Transfer: Three bugs of tri-state checkbox
- DatePicker: Four boundaries of 42-cell date grid
- Select: Keyboard, ARIA and scrolling for searchable dropdown
- Carousel (this): Autoplay pause mechanism and announcement timing
Online demo: https://ui.ruanno.top/components/carousel Source: https://gitee.com/ruanno-top/ruanno-ui