forked from mystiq/hydrogen-web
basic focus trapping of lightbox
This commit is contained in:
parent
708893022a
commit
5a31bc5f2b
1 changed files with 34 additions and 1 deletions
|
@ -42,11 +42,14 @@ export class LightboxView extends TemplateView {
|
||||||
const details = t.div({
|
const details = t.div({
|
||||||
className: "details"
|
className: "details"
|
||||||
}, [t.strong(vm => vm.name), t.br(), "uploaded by ", t.strong(vm => vm.sender), vm => ` at ${vm.time} on ${vm.date}.`]);
|
}, [t.strong(vm => vm.name), t.br(), "uploaded by ", t.strong(vm => vm.sender), vm => ` at ${vm.time} on ${vm.date}.`]);
|
||||||
return t.div({
|
const dialog = t.div({
|
||||||
|
role: "dialog",
|
||||||
className: "lightbox",
|
className: "lightbox",
|
||||||
onClick: evt => this.clickToClose(evt),
|
onClick: evt => this.clickToClose(evt),
|
||||||
onKeydown: evt => this.closeOnEscKey(evt)
|
onKeydown: evt => this.closeOnEscKey(evt)
|
||||||
}, [image, loading, details, close]);
|
}, [image, loading, details, close]);
|
||||||
|
trapFocus(t, dialog);
|
||||||
|
return dialog;
|
||||||
}
|
}
|
||||||
|
|
||||||
clickToClose(evt) {
|
clickToClose(evt) {
|
||||||
|
@ -61,3 +64,33 @@ export class LightboxView extends TemplateView {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function trapFocus(t, element) {
|
||||||
|
const elements = focusables(element);
|
||||||
|
const first = elements[0];
|
||||||
|
const last = elements[elements.length - 1];
|
||||||
|
|
||||||
|
t.addEventListener(element, "keydown", evt => {
|
||||||
|
if (evt.key === "Tab") {
|
||||||
|
if (evt.shiftKey) {
|
||||||
|
if (document.activeElement === first) {
|
||||||
|
last.focus();
|
||||||
|
evt.preventDefault();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (document.activeElement === last) {
|
||||||
|
first.focus();
|
||||||
|
evt.preventDefault();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
Promise.resolve().then(() => {
|
||||||
|
first.focus();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function focusables(element) {
|
||||||
|
return element.querySelectorAll('a[href], button, textarea, input, select');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue