-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathfocus-trap.js
152 lines (131 loc) · 4.54 KB
/
focus-trap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { css, html, LitElement } from 'lit';
import { getNextFocusable, getPreviousFocusable } from '../../helpers/focus.js';
import { FocusMixin } from '../../mixins/focus/focus-mixin.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { isComposedAncestor } from '../../helpers/dom.js';
const traps = [];
/**
* A generic container component to trap user focus.
* @fires d2l-focus-trap-enter - Dispatched when focus enters the trap. May be used to override initial focus placement when focus enters the trap.
*/
class FocusTrap extends FocusMixin(LitElement) {
static get properties() {
return {
/**
* Whether the component should trap user focus.
* @type {boolean}
*/
trap: { type: Boolean },
_legacyPromptIds: { state: true }
};
}
static get styles() {
return css`
:host {
display: inline-block;
}
:host([hidden]) {
display: none;
}
`;
}
constructor() {
super();
this.trap = false;
this._handleBodyFocus = this._handleBodyFocus.bind(this);
this._handleLegacyPromptOpen = this._handleLegacyPromptOpen.bind(this);
this._handleLegacyPromptClose = this._handleLegacyPromptClose.bind(this);
this._legacyPromptIds = new Set();
}
static get focusElementSelector() {
return '.d2l-focus-trap-start';
}
connectedCallback() {
super.connectedCallback();
document.body.addEventListener('focus', this._handleBodyFocus, true);
document.body.addEventListener('d2l-legacy-prompt-open', this._handleLegacyPromptOpen);
document.body.addEventListener('d2l-legacy-prompt-close', this._handleLegacyPromptClose);
}
disconnectedCallback() {
super.disconnectedCallback();
document.body.removeEventListener('focus', this._handleBodyFocus, true);
document.body.removeEventListener('d2l-legacy-prompt-open', this._handleLegacyPromptOpen);
document.body.removeEventListener('d2l-legacy-prompt-close', this._handleLegacyPromptClose);
}
render() {
const tabindex = (this.trap && this._legacyPromptIds.size === 0) ? '0' : undefined;
return html`
<span class="d2l-focus-trap-start" @focusin="${this._handleStartFocusIn}" tabindex="${ifDefined(tabindex)}"></span>
<slot></slot>
<span class="d2l-focus-trap-end" @focusin="${this._handleEndFocusIn}" tabindex="${ifDefined(tabindex)}"></span>
`;
}
willUpdate(changedProperties) {
if (!changedProperties.has('trap')) return;
if (this.trap) {
traps.push(this);
} else {
const trapIndex = traps.findIndex(trap => trap === this);
if (trapIndex === -1) return;
traps.splice(trapIndex, 1);
}
}
static #exemptSelectors = [
'.d2l-focus-trap-exempt',
'.equatio-toolbar-wrapper',
'.equatio-toolbar-shadow-root-container'
].join(', ');
_focusFirst() {
const focusable = this.shadowRoot &&
getNextFocusable(this.shadowRoot.querySelector('.d2l-focus-trap-start'));
if (focusable) focusable.focus();
this.dispatchEvent(new CustomEvent('d2l-focus-trap-enter', { bubbles: true, composed: true }));
}
_getContainer() {
return this.shadowRoot && this.shadowRoot.querySelector('.d2l-focus-trap-start').parentNode;
}
_handleBodyFocus(e) {
const lastTrap = traps[traps.length - 1];
if (!this.trap || this._legacyPromptIds.size > 0 || lastTrap !== this) return;
const container = this._getContainer();
const target = e.composedPath()[0];
if (isComposedAncestor(container, target) || FocusTrap.#isExempt(target)) return;
this._focusFirst();
}
_handleEndFocusIn(e) {
const container = this._getContainer();
if (this.shadowRoot && (isComposedAncestor(container, e.relatedTarget) || FocusTrap.#isExempt(e.relatedTarget))) {
// user is exiting trap via forward tabbing...
const firstFocusable = getNextFocusable(this.shadowRoot.querySelector('.d2l-focus-trap-start'));
if (firstFocusable) {
firstFocusable.focus();
return;
}
}
this._focusFirst();
}
_handleLegacyPromptClose(e) {
this._legacyPromptIds.delete(e.detail.id);
this.requestUpdate();
}
_handleLegacyPromptOpen(e) {
this._legacyPromptIds.add(e.detail.id);
this.requestUpdate();
}
_handleStartFocusIn(e) {
const container = this._getContainer();
if (this.shadowRoot && (isComposedAncestor(container, e.relatedTarget) || FocusTrap.#isExempt(e.relatedTarget))) {
// user is exiting trap via back tabbing...
const lastFocusable = getPreviousFocusable(this.shadowRoot.querySelector('.d2l-focus-trap-end'));
if (lastFocusable) {
lastFocusable.focus();
return;
}
}
this._focusFirst();
}
static #isExempt(target) {
return !!target?.closest(this.#exemptSelectors);
}
}
customElements.define('d2l-focus-trap', FocusTrap);