-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodal.ts
148 lines (115 loc) · 3.77 KB
/
modal.ts
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
import { createFocusTrap, FocusTrap } from 'focus-trap';
import { goodbye, hello } from 'hello-goodbye';
export default class ModalElement extends HTMLElement {
public static attention: (el: Element) => void = (el) =>
el.animate(
[
{ transform: 'scale(1)' },
{ transform: 'scale(1.1)' },
{ transform: 'scale(1)' },
],
300
);
static get observedAttributes() {
return ['open'];
}
private focusTrap?: FocusTrap;
private connected: boolean = false;
public constructor() {
super();
const template = document.createElement('template');
template.innerHTML = `
<div part="backdrop" style="position: fixed; top: 0; left: 0; right: 0; bottom: 0"></div>
<div part="content" style="z-index: 1"><slot></slot></div>
`;
const shadow = this.attachShadow({ mode: 'open' });
shadow.appendChild(template.content.cloneNode(true));
this.backdrop!.addEventListener('click', () => {
if (this.hasAttribute('static')) {
ModalElement.attention?.(shadow.children[1]);
} else {
this.close();
}
});
this.focusTrap = createFocusTrap(this, {
escapeDeactivates: false,
allowOutsideClick: true,
preventScroll: true,
initialFocus: () =>
this.querySelector<HTMLElement>('[autofocus]') || undefined,
});
}
public connectedCallback(): void {
this.connected = true;
if (!this.content?.hasAttribute('role')) {
this.content?.setAttribute('role', 'dialog');
}
if (!this.content?.hasAttribute('aria-modal')) {
this.content?.setAttribute('aria-modal', 'true');
}
if (!this.content?.hasAttribute('tabindex')) {
this.content?.setAttribute('tabindex', '-1');
}
this.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && this.open) {
e.preventDefault();
e.stopPropagation();
this.close();
}
});
if (this.open) {
this.wasOpened();
}
}
public disconnectedCallback(): void {
this.connected = false;
}
get open() {
return this.hasAttribute('open');
}
set open(val) {
if (val) {
this.setAttribute('open', '');
} else {
this.removeAttribute('open');
}
}
public close() {
if (!this.open) return;
const event = new Event('beforeclose', { cancelable: true });
if (this.dispatchEvent(event)) {
this.open = false;
}
}
public attributeChangedCallback(
name: string,
oldValue: string,
newValue: string
): void {
if (name !== 'open' || !this.connected) return;
if (newValue !== null) {
this.wasOpened();
} else {
this.wasClosed();
}
}
private async wasOpened() {
document.documentElement.style.overflow = 'hidden';
this.hidden = false;
hello(this);
requestAnimationFrame(() => this.focusTrap?.activate());
this.dispatchEvent(new Event('open'));
}
private wasClosed() {
document.documentElement.style.overflow = '';
this.focusTrap?.deactivate();
goodbye(this).then(() => (this.hidden = true));
this.dispatchEvent(new Event('close'));
}
private get backdrop(): HTMLElement | undefined {
return this.shadowRoot?.firstElementChild as HTMLElement;
}
private get content(): HTMLElement | undefined {
return this.firstElementChild as HTMLElement;
}
}