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 accessibility of progressive enhancement pattern #177

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 17 additions & 5 deletions src/lite-yt-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ class LiteYTEmbed extends HTMLElement {

// Set up play button, and its visually hidden label
if (!playBtnEl) {
playBtnEl = document.createElement('button');
playBtnEl.type = 'button';
playBtnEl.classList.add('lty-playbtn');
playBtnEl = this.createPlayBtn();
this.append(playBtnEl);
}
// If the play button is a link, convert it to a button so it is interactive but doesn’t trigger a navigation.
if (playBtnEl.tagName === 'A') {
const isFocused = playBtnEl === document.activeElement;
const btnEl = this.createPlayBtn();
playBtnEl.replaceWith(btnEl);
// Preserve focus state if link is replaced with button.
if (isFocused) btnEl.focus();
playBtnEl = btnEl;
}
if (!playBtnEl.textContent) {
const playBtnLabelEl = document.createElement('span');
playBtnLabelEl.className = 'lyt-visually-hidden';
Expand All @@ -46,8 +53,6 @@ class LiteYTEmbed extends HTMLElement {

this.addNoscriptIframe();

playBtnEl.removeAttribute('href');

// On hover (or tap), warm up the TCP connections we're (likely) about to use.
this.addEventListener('pointerover', LiteYTEmbed.warmConnections, {once: true});

Expand Down Expand Up @@ -193,6 +198,13 @@ class LiteYTEmbed extends HTMLElement {
return iframeEl;
}

createPlayBtn(){
const btnEl = document.createElement('button');
btnEl.type = 'button';
btnEl.classList.add('lty-playbtn');
return btnEl;
}

/**
* In the spirit of the `lowsrc` attribute and progressive JPEGs, we'll upgrade the reliable
* poster image to a higher resolution one, if it's available.
Expand Down