-
Notifications
You must be signed in to change notification settings - Fork 25
/
backdrop.js
203 lines (171 loc) · 4.96 KB
/
backdrop.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import '../colors/colors.js';
import { css, html, LitElement } from 'lit';
import { cssEscape, getComposedChildren, getComposedParent, isVisible } from '../../helpers/dom.js';
const BACKDROP_HIDDEN = 'data-d2l-backdrop-hidden';
const BACKDROP_ARIA_HIDDEN = 'data-d2l-backdrop-aria-hidden';
const TRANSITION_DURATION = 200;
const reduceMotion = matchMedia('(prefers-reduced-motion: reduce)').matches;
const modals = new Set();
let scrollOverflow = null;
/**
* A component for displaying a semi-transparent backdrop behind a specified sibling element. It also hides elements other than the target from assistive technologies by applying 'role="presentation"' and 'aria-hidden="true"'.
*/
class Backdrop extends LitElement {
static get properties() {
return {
/**
* REQUIRED: id of the target element to display backdrop behind
* @type {string}
*/
forTarget: { type: String, attribute: 'for-target' },
/**
* Disables the fade-out transition while the backdrop is being hidden
* @type {boolean}
*/
noAnimateHide: { type: Boolean, attribute: 'no-animate-hide' },
/**
* Used to control whether the backdrop is shown
* @type {boolean}
*/
shown: { type: Boolean },
_state: { type: String, reflect: true }
};
}
static get styles() {
return [ css`
:host {
background-color: var(--d2l-color-regolith);
height: 0;
left: 0;
opacity: 0;
position: fixed;
top: 0;
transition: opacity ${TRANSITION_DURATION}ms ease-in;
width: 0;
z-index: 999;
}
:host([slow-transition]) {
transition: opacity 1200ms ease-in;
}
:host([_state="showing"]) {
opacity: 0.7;
}
:host([_state="showing"]),
:host([_state="hiding"]) {
height: 100%;
width: 100%;
}
:host([_state=null][no-animate-hide]) {
transition: none;
}
@media (prefers-reduced-motion: reduce) {
:host,
:host([slow-transition]) {
transition: none;
}
}
`];
}
constructor() {
super();
this.shown = false;
this.noAnimateHide = false;
this._state = null;
}
disconnectedCallback() {
// allow body scrolling, show hidden elements, if backdrop is removed from the DOM
allowBodyScroll(this);
if (this._hiddenElements) {
showAccessible(this._hiddenElements);
this._hiddenElements = null;
}
this._state = null;
super.disconnectedCallback();
}
render() {
return html`<div></div>`;
}
updated(changedProperties) {
super.updated(changedProperties);
if (!changedProperties.has('shown')) return;
if (this.shown) {
if (this._state === null) {
preventBodyScroll(this);
this._hiddenElements = hideAccessible(this.parentNode.querySelector(`#${cssEscape(this.forTarget)}`));
}
this._state = 'showing';
} else if (this._state === 'showing') {
const hide = () => {
if (!this.shown) {
allowBodyScroll(this);
showAccessible(this._hiddenElements);
this._hiddenElements = null;
this._state = null;
}
};
if (!reduceMotion && !this.noAnimateHide && isVisible(this)) {
this.addEventListener('transitionend', hide, { once: true });
this._state = 'hiding';
setTimeout(() => this._state === 'hiding' && hide(), TRANSITION_DURATION + 100);
} else {
hide();
}
}
}
}
export function allowBodyScroll(modal) {
if (!modals.has(modal)) return;
modals.delete(modal);
if (!modals.size) {
document.body.style.overflow = scrollOverflow;
scrollOverflow = null;
}
}
export function preventBodyScroll(modal) {
if (!modals.size) {
scrollOverflow = document.body.style.overflow;
document.body.style.overflow = 'hidden';
}
modals.add(modal);
}
function hideAccessible(target) {
const hiddenElements = [];
const path = [target];
const hideAccessibleChildren = function(parent) {
const children = getComposedChildren(parent);
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (child.tagName === 'SCRIPT' || child.tagName === 'STYLE') continue;
if (path.indexOf(child) !== -1) continue;
if (child.hasAttribute(BACKDROP_HIDDEN)) continue;
if (child.hasAttribute('aria-hidden')) {
child.setAttribute(BACKDROP_ARIA_HIDDEN, child.getAttribute('aria-hidden'));
}
child.setAttribute('aria-hidden', 'true');
child.setAttribute(BACKDROP_HIDDEN, BACKDROP_HIDDEN);
hiddenElements.push(child);
}
};
let parent = getComposedParent(target);
while (parent !== document.documentElement && parent !== null) {
if (parent.nodeType === Node.ELEMENT_NODE) {
path.push(parent);
hideAccessibleChildren(parent);
}
parent = getComposedParent(parent);
}
return hiddenElements;
}
function showAccessible(elems) {
for (let i = 0; i < elems.length; i++) {
const elem = elems[i];
if (elem.hasAttribute(BACKDROP_ARIA_HIDDEN)) {
elem.setAttribute('aria-hidden', elem.getAttribute(BACKDROP_ARIA_HIDDEN));
elem.removeAttribute(BACKDROP_ARIA_HIDDEN);
} else {
elem.removeAttribute('aria-hidden');
}
elem.removeAttribute(BACKDROP_HIDDEN);
}
}
customElements.define('d2l-backdrop', Backdrop);