Skip to content

Commit

Permalink
Added modal a11y features and standards (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
beckettrowan authored Jun 23, 2024
1 parent c47705c commit f0cf6ba
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
51 changes: 35 additions & 16 deletions src/components/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
// =============================================================================


import {$$, $body, $N, AnimationResponse, Browser, CustomElementView, ElementView, MediaView, register, Router} from '../';
import {last} from '@mathigon/core';
import {$$, $body, $N, AnimationResponse, Browser, CustomElementView, ElementView, MediaView, register, Router, stopEvent} from '../';


const $modalBackground = $N('div', {class: 'modal-background'}, $body);
Expand All @@ -13,24 +14,23 @@ let backgroundAnimation: AnimationResponse|undefined;
let $openModal: Modal|undefined = undefined;
let lastFocusElement: HTMLElement|undefined = undefined;

const TITLE_ID = 'boost-modal-title';

function tryClose() {
if ($openModal && $openModal.canClose) $openModal.close();
}

$modalBackground.on('click', tryClose);
$body.onKey('Escape', tryClose);
$body.onKey('Escape', (e: Event) => {
stopEvent(e);
tryClose();
});
Router.on('change', tryClose);

$modalBackground.on('scrollwheel touchmove', (e: Event) => {
e.preventDefault();
e.stopPropagation();
});
$modalBackground.on('scrollwheel touchmove', stopEvent);

$body.onKey('Space ArrowUp ArrowDown PageDown PageUp', (e: Event) => {
if ($openModal) {
e.preventDefault();
e.stopPropagation();
}
if ($openModal) stopEvent(e);
});

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -71,6 +71,23 @@ export class Modal extends CustomElementView {

// Used for Modal.confirm()
for (const $btn of this.$$('.btn')) $btn.on('click', () => this.trigger('btn-click', $btn));

// a11y
this.setAttr('tabindex', -1);
this.setAttr('role', 'dialog');
this.setAttr('aria-modal', 'true');
this.setAttr('aria-labelledby', TITLE_ID);
this.onKey('Tab', (e: KeyboardEvent) => {
if (!this.isOpen) return;
const $focus = this.$$('input:not([type=hidden]), a, button, textarea, [tabindex=0]');
if (e.shiftKey && e.target === $focus[0]._el) {
e.preventDefault();
last($focus).focus();
} else if (!e.shiftKey && e.target === last($focus)._el) {
e.preventDefault();
$focus[0].focus();
}
});
}

open(noAnimation = false) {
Expand Down Expand Up @@ -100,12 +117,12 @@ export class Modal extends CustomElementView {
this.enter('pop', 250).promise.then(() => this.css('transform', ''));
}

this.setAttr('role', 'dialog');
this.trigger('open');

// a11y
this.$('h2')?.setAttr('id', TITLE_ID);
lastFocusElement = document.activeElement as HTMLElement;
const $focus = this.$('input, a, button, textarea, [tabindex="0"]');
if ($focus) $focus.focus();
(this.$('input:not([type=hidden]), textarea') || this).focus();

this.trigger('open');

window.ga?.('send', 'event', 'Modal', this.id);
window.gtag?.('event', 'modal', {action: this.id});
Expand All @@ -114,12 +131,14 @@ export class Modal extends CustomElementView {
close(keepBg = false, noEvent = false) {
if (!this.isOpen) return;
this.isOpen = false;
this.removeAttr('role');
$openModal = undefined;

if (this.$iframe) this.$iframe.setAttr('src', '');
if (this.$video) this.$video.pause();

// a11y
this.$(`#${TITLE_ID}`)?.removeAttr('id');

if (!keepBg) backgroundAnimation = $modalBackground.exit('fade', 250);
this.exit('pop', 250).promise.then(() => this.css('transform', ''));
if (!noEvent) this.trigger('close');
Expand Down
5 changes: 5 additions & 0 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export function getEventTarget(event: ScreenEvent) {
return $(document.elementFromPoint(posn.x, posn.y) || undefined);
}

export function stopEvent(event: Event) {
event.stopPropagation();
event.preventDefault();
}


// -----------------------------------------------------------------------------
// Click Events
Expand Down

0 comments on commit f0cf6ba

Please sign in to comment.