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

fix: improve sd-video a11y #1644

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
67 changes: 49 additions & 18 deletions packages/components/src/components/video/video.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { expect, fixture, html, waitUntil } from '@open-wc/testing';
import { sendKeys } from '@web/test-runner-commands';
import sinon from 'sinon';
import type SdVideo from './video';

const defaultSlot = html`<video controls>
<source src="http://media.w3.org/2010/05/sintel/trailer.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>`;
const posterSlot = html`<img slot="poster" alt="poster" src="" />`;
const posterSlot = html`<img slot="poster" alt="A generic placeholder video" src="" />`;
const playIconSlot = html`<sd-icon library="system" name="start" color="primary"></sd-icon>`;
const variants = {
default: html`<sd-video>${defaultSlot}</sd-video>`,
Expand Down Expand Up @@ -85,29 +86,59 @@ describe('<sd-video>', () => {
});
});

describe('when "play-icon" is interacted with via Keyboard', () => {
it('emits "sd-play" event ', async () => {
const el: SdVideo = await fixture(variants.default);
const playSpy = sinon.spy();
el.addEventListener('sd-play', playSpy);
el.shadowRoot?.querySelector('button')?.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));
expect(playSpy.calledOnce).to.be.true;
describe('when interacted with via Keyboard', () => {
describe('"play-icon"', () => {
it('emits "sd-play" event', async () => {
const el: SdVideo = await fixture(variants.default);
const playSpy = sinon.spy();
el.addEventListener('sd-play', playSpy);
el.shadowRoot?.querySelector('button')?.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));
expect(playSpy.calledOnce).to.be.true;
});

it('toggles playing property', async () => {
const el: SdVideo = await fixture(variants.default);
el.shadowRoot?.querySelector('button')?.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));
expect(el.playing).to.be.true;
});

it('updates overlay class', async () => {
const el: SdVideo = await fixture(variants.overlay);
await el.updateComplete;
expect(el.shadowRoot!.querySelector('#overlay.opacity-100')).to.exist;

el.shadowRoot?.querySelector('button')?.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));
await el.updateComplete;
expect(el.shadowRoot!.querySelector('#overlay.opacity-0')).to.exist;
});
});

it('toggles playing property', async () => {
const el: SdVideo = await fixture(variants.default);
el.shadowRoot?.querySelector('button')?.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));
expect(el.playing).to.be.true;
});
// Handle tabbing for Safari and other Browsers, see https://github.com/microsoft/playwright/issues/2114#issuecomment-1517642401
const tabKey =
navigator.userAgent.includes('Safari') && !navigator.userAgent.includes('HeadlessChrome') ? 'Alt+Tab' : 'Tab';

it('updates overlay class', async () => {
const el: SdVideo = await fixture(variants.overlay);
it('video is not focusable before played', async () => {
const el: SdVideo = await fixture(variants.all);

await sendKeys({ press: tabKey });
await el.updateComplete;
expect(el.shadowRoot!.querySelector('#overlay.opacity-100')).to.exist;
await sendKeys({ press: tabKey });
await el.updateComplete;

expect(el.querySelector('video')).to.not.focus;
});

it('video is focusable after played', async () => {
const el: SdVideo = await fixture(variants.all);

el.shadowRoot?.querySelector('button')?.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));
el.shadowRoot?.querySelector('button')?.click();

await sendKeys({ press: tabKey });
await el.updateComplete;
expect(el.shadowRoot!.querySelector('#overlay.opacity-0')).to.exist;
await sendKeys({ press: tabKey });
await el.updateComplete;

expect(el.querySelector('video')).to.focus;
});
});

Expand Down
21 changes: 18 additions & 3 deletions packages/components/src/components/video/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export default class SdVideo extends SolidElement {
return null;
}

private get video(): HTMLVideoElement | null {
return this.querySelector('video');
}

/** Fade out poster after initial play. */
private fadeoutPoster(): void {
if (this.poster instanceof HTMLImageElement) {
Expand All @@ -65,19 +69,29 @@ export default class SdVideo extends SolidElement {
}
}

private setVideoInert(inert?: boolean): void {
if (inert) {
this.video?.setAttribute('inert', '');
} else {
this.video?.removeAttribute('inert');
}
}

/** Utility function to group play behaviors. */
private play() {
this.emit('sd-play');
this.playing = true;
this.video?.play();
this.fadeoutPoster();
this.setVideoInert(false);
}

/** Restrict keydown control to enter and space bar to mimic the native video tag behavior. If a KeyboardEvent is used, refocus on the native video element to give the user seamless keyboard control. */
private handleKeydown(e: MouseEvent | KeyboardEvent) {
if (e instanceof KeyboardEvent && (e.key === 'Enter' || e.key === ' ')) {
this.play();
setTimeout(() => {
this.querySelector('video')?.focus();
this.video?.focus();
});
}
}
Expand All @@ -95,6 +109,7 @@ export default class SdVideo extends SolidElement {

connectedCallback(): void {
super.connectedCallback();
this.setVideoInert(true);
this.resizeObserver = new ResizeObserver(() => this.handleButtonResize());

this.updateComplete.then(() => {
Expand All @@ -118,15 +133,15 @@ export default class SdVideo extends SolidElement {
@keydown=${this.handleKeydown}
class=${cx(
this.playing && 'pointer-events-none',
'w-full h-full absolute top-0 left-0 z-30 text-primary hover:text-primary-500 sd-interactive sd-interactive--reset focus-visible:focus-outline'
'group w-full h-full absolute top-0 left-0 z-30 text-primary hover:text-primary-500 sd-interactive sd-interactive--reset focus-visible:focus-outline'
)}
>
<div
part="play-button-bg"
class=${cx(
this.playing ? 'opacity-0' : 'opacity-100',
this.isBelowBreakpoint ? 'w-[48px] h-[48px]' : 'w-[96px] h-[96px]',
'absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 p-4 bg-white bg-opacity-75 rounded-full play-pause-transition'
'absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 p-4 bg-white bg-opacity-75 rounded-full play-pause-transition outline-2 outline-offset-2 group-focus-visible:outline'
)}
>
<slot name="play-icon" part="play-icon" class=${cx(this.isBelowBreakpoint ? 'text-[2rem]' : 'text-[4rem]')}>
Expand Down
4 changes: 4 additions & 0 deletions packages/docs/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export default {
from: '../../placeholders/src/videos',
to: '/placeholders/videos'
},
{
from: '../../placeholders/src/captions',
to: '/placeholders/captions'
},
{
from: '../../placeholders/src/audio',
to: '/placeholders/audio'
Expand Down
11 changes: 9 additions & 2 deletions packages/docs/src/stories/components/video.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ export const VideoElementWithPosterSlot = {
render: () => html`
<sd-video>
<video controls="" id="video-example" class="w-[854px] aspect-video">
<source src="./placeholders/videos/ui-placeholder-video.mp4" type="video/mp4" />
<source src="./placeholders/videos/ui-placeholder-video.webm" type="video/webm" />
<track
label="English"
kind="subtitles"
srclang="en"
src="./placeholders/captions/ui-placeholder-video.vtt"
default
/>
Your browser does not support the video tag.
</video>
<img
slot="poster"
alt="poster"
alt="A generic placeholder video"
paulovareiro29 marked this conversation as resolved.
Show resolved Hide resolved
class="w-[854px] aspect-video cover"
src="./placeholders/images/architecture.jpg"
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/stories/components/video.test.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const posterConstant: ConstantDefinition = {
type: 'slot',
name: 'poster',
value:
'<img slot="poster" alt="poster" class="w-[854px] aspect-video cover" src="./placeholders/images/architecture.jpg" />'
'<img slot="poster" alt="A generic placeholder video" class="w-[854px] aspect-video cover" src="./placeholders/images/architecture.jpg" />'
};
const iconConstant: ConstantDefinition = {
type: 'slot',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export const VideoWithDescription = {
<source src="./placeholders/videos/ui-placeholder-video.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>
<img slot="poster" alt="poster" class="aspect-video cover" src="./placeholders/images/architecture.jpg" />
<img
slot="poster"
alt="A generic placeholder video"
class="aspect-video cover"
src="./placeholders/images/architecture.jpg"
/>
<figcaption class="mt-4">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et
dolore magna aliquyam erat, sed diam voluptua.
Expand All @@ -42,7 +47,12 @@ export const VideoWithCopyright = {
<source src="./placeholders/videos/ui-placeholder-video.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>
<img slot="poster" alt="poster" class="aspect-video cover" src="./placeholders/images/architecture.jpg" />
<img
slot="poster"
alt="A generic placeholder video"
class="aspect-video cover"
src="./placeholders/images/architecture.jpg"
/>
</sd-video>
</div>
`
Expand Down
Loading
Loading