push
This commit is contained in:
1 parent
bcf05e8063
commit
f37985b4c8
109 files changed
+22810
-6143
No files matched your search
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
modal.js — accessible dialog. openModal(content, { label, wide, onClose }) → close()
|
||||
trailerModal(youtubeKey, title) plays a YouTube trailer in an iframe.
|
||||
*/
|
||||
import { h } from '../util.js';
|
||||
import { icon } from './icons.js';
|
||||
|
||||
export function openModal(content, opts = {}) {
|
||||
const prevFocus = document.activeElement;
|
||||
const closeBtn = h('button.modal__close.icon-btn', { type: 'button', 'aria-label': 'Close' }, icon('close'));
|
||||
const dialog = h('div.modal__dialog', { class: opts.wide ? 'modal__dialog--wide' : null, role: 'dialog', 'aria-modal': 'true', 'aria-label': opts.label || 'Dialog', tabindex: '-1' }, closeBtn, content);
|
||||
const backdrop = h('div.modal', dialog);
|
||||
let closed = false;
|
||||
|
||||
function close() {
|
||||
if (closed) return;
|
||||
closed = true;
|
||||
document.removeEventListener('keydown', onKey, true);
|
||||
backdrop.classList.remove('is-open');
|
||||
document.documentElement.classList.remove('has-modal');
|
||||
setTimeout(() => backdrop.remove(), 250);
|
||||
if (opts.onClose) opts.onClose();
|
||||
if (prevFocus && prevFocus.focus) prevFocus.focus();
|
||||
}
|
||||
|
||||
function onKey(e) {
|
||||
if (e.key === 'Escape') {
|
||||
e.preventDefault();
|
||||
close();
|
||||
} else if (e.key === 'Tab') {
|
||||
const f = dialog.querySelectorAll('button, [href], input, select, textarea, iframe, [tabindex]:not([tabindex="-1"])');
|
||||
if (!f.length) return;
|
||||
const first = f[0];
|
||||
const last = f[f.length - 1];
|
||||
if (e.shiftKey && document.activeElement === first) {
|
||||
e.preventDefault();
|
||||
last.focus();
|
||||
} else if (!e.shiftKey && document.activeElement === last) {
|
||||
e.preventDefault();
|
||||
first.focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
closeBtn.addEventListener('click', close);
|
||||
backdrop.addEventListener('click', (e) => {
|
||||
if (e.target === backdrop) close();
|
||||
});
|
||||
document.addEventListener('keydown', onKey, true);
|
||||
document.body.appendChild(backdrop);
|
||||
document.documentElement.classList.add('has-modal');
|
||||
requestAnimationFrame(() => {
|
||||
backdrop.classList.add('is-open');
|
||||
closeBtn.focus();
|
||||
});
|
||||
return close;
|
||||
}
|
||||
|
||||
export function trailerModal(key, title) {
|
||||
const frame = h('iframe.modal__video', {
|
||||
src: `https://www.youtube-nocookie.com/embed/${encodeURIComponent(key)}?autoplay=1&rel=0&modestbranding=1`,
|
||||
title: title ? `${title} — trailer` : 'Trailer',
|
||||
allow: 'autoplay; encrypted-media; picture-in-picture; fullscreen',
|
||||
allowfullscreen: true,
|
||||
referrerpolicy: 'strict-origin-when-cross-origin',
|
||||
});
|
||||
return openModal(h('div.modal__media', frame), { label: title ? `${title} trailer` : 'Trailer', wide: true });
|
||||
}
|
||||
Reference in new issue
Block a user