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

Respect user preference for reduced motion when autoplaying #1532

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion packages/vidstack/src/core/state/media-player-delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class MediaPlayerDelegate {
?.errorGroup('[vidstack] auto-play request failed')
.labelledLog(
'Message',
`Autoplay was requested but failed most likely due to browser autoplay policies.${muteMsg}`,
`Autoplay was requested but failed most likely due to browser autoplay policies or accessibility reasons.${muteMsg}`,
)
.labelledLog('Trigger Event', trigger)
.labelledLog('Error', error)
Expand Down
11 changes: 11 additions & 0 deletions packages/vidstack/src/core/state/media-request-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { MediaPlayerController } from '../api/player-controller';
import { boundTime } from '../api/player-state';
import { MediaControls } from '../controls';
import type { MediaStateManager } from './media-state-manager';
import { prefersReducedMotion } from '../../utils/aria';

/**
* This class is responsible for listening to media request events and calling the appropriate
Expand Down Expand Up @@ -155,6 +156,7 @@ export class MediaRequestManager extends MediaPlayerController implements MediaR
try {
const provider = peek(this.#$provider);
throwIfNotReadyForPlayback(provider, peek(canPlay));
throwIfAutoplayingWithReducedMotion(isAutoPlaying);
return await provider!.play();
} catch (error) {
if (__DEV__) this.#logError('play request failed', error, trigger);
Expand Down Expand Up @@ -887,6 +889,15 @@ function throwIfFullscreenNotSupported(
);
}

function throwIfAutoplayingWithReducedMotion(autoplaying: boolean) {
if (!prefersReducedMotion() || !autoplaying) return;
throw Error(
__DEV__
? '[vidstack] autoplay is blocked due to user preference for reduced motion'
: '[vidstack] autoplay blocked',
);
}

export class MediaRequestContext {
seeking = false;
looping = false;
Expand Down
5 changes: 5 additions & 0 deletions packages/vidstack/src/utils/aria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export function ariaBool(value: boolean): 'true' | 'false' {
export function $ariaBool(signal: ReadSignal<boolean>): ReadSignal<'true' | 'false'> {
return () => ariaBool(signal());
}

export function prefersReducedMotion() {
if (typeof window === 'undefined') return false;
return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
}