generated from AegisJSProject/template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaegis-modal.js
227 lines (190 loc) · 5.66 KB
/
aegis-modal.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { html } from '@aegisjsproject/core/parsers/html.js';
import { AegisComponent } from '@aegisjsproject/component/base.js';
import { SYMBOLS, TRIGGERS } from '@aegisjsproject/component/consts.js';
import { styles } from './styles.js';
import { template } from './template.js';
import { toggleInert, renable } from './functions.js';
export class AegisModalElement extends AegisComponent {
#removeController;
#previouslyActive;
constructor() {
super({ role: 'dialog', delegatesFocus: true , styles, template });
this.returnValue = null;
}
async [SYMBOLS.render](type, { shadow, internals, name, newValue, assigned }) {
switch(type) {
case TRIGGERS.constructed:
internals.ariaHidden = this.open ? 'false' : 'true';
internals.ariaLabel = 'Aegis Modal Dialog';
internals.ariaModal = 'true';
this.hidden = ! this.open;
break;
case TRIGGERS.connected:
if (! this.parentElement.isSameNode(document.body)) {
console.warn('<aegis-modal> should be a direct descendant of <body>.');
}
break;
case TRIGGERS.disconnected:
if (this.open) {
this.close();
}
renable(this);
break;
case TRIGGERS.slotChanged:
if (name === 'header') {
internals.ariaLabel = assigned.length === 0
? 'Aegis Modal Dialog'
: assigned.map(el => el.textContent).join(' ');
}
break;
case TRIGGERS.attributeChanged:
if (name === 'open') {
if (typeof newValue === 'string') {
this.returnValue = null;
this.#previouslyActive = document.activeElement;
toggleInert(this, true);
this.hidden = false;
this.ariaHidden = 'false';
internals.states.add('--open');
internals.states.add('--modal');
shadow.getElementById('container').animate([
{ opacity: 0, transform: 'scale(0)' },
{ opacity: 1, transform: 'none' },
], {
fill: 'both',
duration: 400,
});
this.dispatchEvent(new Event('open'));
const controller = new AbortController();
document.body.addEventListener('keydown', ({ key }) => {
if (key === 'Escape') {
this.close();
}
}, {
passive: true,
signal: controller.signal,
});
this.addEventListener('close', () => controller.abort(), { once: true });
this.focus();
} else {
await shadow.getElementById('container').animate([
{ opacity: 0, transform: 'scale(0)' },
{ opacity: 1, transform: 'none' },
], {
fill: 'both',
duration: 400,
direction: 'reverse',
}).finished;
toggleInert(this, false);
this.hidden = true;
this.ariaHidden = 'true';
internals.states.delete('--open');
internals.states.delete('--modal');
this.dispatchEvent(new Event('close'));
if (this.#previouslyActive instanceof Element) {
this.#previouslyActive.focus();
this.#previouslyActive = null;
}
}
} else if (name === 'autoremove') {
if (typeof newValue !== 'string') {
if (this.#removeController instanceof AbortController) {
this.#removeController.abort();
}
this.#removeController = null;
} else if (! (this.#removeController instanceof AbortController)) {
this.#removeController = new AbortController();
this.addEventListener('close', () => {
this.remove();
this.#removeController.abort();
}, { signal: this.#removeController.signal, once: true });
}
}
break;
}
}
get whenOpened() {
const { resolve, promise } = Promise.withResolvers();
if (this.open) {
resolve();
} else {
this.addEventListener('open', () => resolve(), { once: true });
}
return promise;
}
get whenClosed() {
const { resolve, promise } = Promise.withResolvers();
if (this.open) {
this.addEventListener('close', () => resolve(this.returnValue), { once: true });
} else {
resolve(this.returnValue);
}
return promise;
}
get autoRemove() {
return this.hasAttribute('autoremove');
}
set autoRemove(val) {
this.toggleAttribute('autoremove');
}
get open() {
return this.hasAttribute('open');
}
set open(val) {
this.toggleAttribute('open', val);
}
show({ signal } = {}) {
if (! (signal instanceof AbortSignal)) {
this.open = true;
} else if (signal.aborted) {
this.open = false;
} else {
this.open = true;
const controller = new AbortController();
this.addEventListener('close', () => controller.abort(), { once: true });
signal.addEventListener('abort', () => this.close(), { once: true, signal: controller.signal });
}
}
close(value = null) {
this.returnValue = value;
this.open = false;
}
static get observedAttributes() {
return [...AegisComponent.observedAttributes, 'open', 'autoremove'];
}
static create({ header, body, signal }) {
if (typeof header === 'string') {
return AegisModalElement.create({
header: html`<div slot="header">${header}</div>`,
body: typeof body === 'string'? html`<div>${body}</div>` : body,
signal,
});
} else if (typeof body === 'string') {
return AegisModalElement.create({
header,
body: html`<div>${body}</div>`,
signal,
});
} else {
const modal = new AegisModalElement();
if (header instanceof HTMLElement) {
header.slot = 'header';
modal.append(header);
} else if (header instanceof DocumentFragment) {
modal.append(header);
}
if (body instanceof HTMLElement) {
modal.append(body);
} else if (body instanceof DocumentFragment) {
modal.append(body);
}
if (signal instanceof AbortSignal) {
modal.autoRemove = true;
document.body.append(modal);
modal.show({ signal });
}
return modal;
}
}
}
AegisModalElement.register('aegis-modal');