@internetarchive/modal-manager 2.0.4 → 2.0.5-webdev-8155.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +29 -29
- package/.github/workflows/ci.yml +40 -30
- package/.github/workflows/gh-pages-main.yml +42 -42
- package/.github/workflows/pr-preview.yml +40 -40
- package/.prettierrc +4 -0
- package/.vscode/extensions.json +10 -0
- package/.vscode/tasks.json +12 -0
- package/LICENSE +661 -661
- package/README.md +139 -139
- package/custom-elements.json +170 -170
- package/demo/app-root.ts +366 -0
- package/dist/demo/app-root.d.ts +22 -0
- package/dist/demo/app-root.js +338 -0
- package/dist/demo/app-root.js.map +1 -0
- package/dist/index.d.ts +7 -7
- package/dist/index.js +5 -5
- package/dist/src/assets/arrow-left-icon.d.ts +2 -2
- package/dist/src/assets/arrow-left-icon.js +2 -2
- package/dist/src/assets/ia-logo-icon.d.ts +2 -2
- package/dist/src/assets/ia-logo-icon.js +2 -2
- package/dist/src/modal-config.d.ts +104 -104
- package/dist/src/modal-config.js +23 -24
- package/dist/src/modal-config.js.map +1 -1
- package/dist/src/modal-manager-host-bridge-interface.d.ts +12 -12
- package/dist/src/modal-manager-host-bridge-interface.js +1 -1
- package/dist/src/modal-manager-host-bridge.d.ts +34 -34
- package/dist/src/modal-manager-host-bridge.js +62 -62
- package/dist/src/modal-manager-host-bridge.js.map +1 -1
- package/dist/src/modal-manager-interface.d.ts +27 -27
- package/dist/src/modal-manager-interface.js +1 -1
- package/dist/src/modal-manager-mode.d.ts +10 -10
- package/dist/src/modal-manager-mode.js +11 -11
- package/dist/src/modal-manager.d.ts +137 -137
- package/dist/src/modal-manager.js +243 -249
- package/dist/src/modal-manager.js.map +1 -1
- package/dist/src/modal-template.d.ts +41 -41
- package/dist/src/modal-template.js +119 -118
- package/dist/src/modal-template.js.map +1 -1
- package/dist/src/shoelace/active-elements.d.ts +15 -15
- package/dist/src/shoelace/active-elements.js +28 -27
- package/dist/src/shoelace/active-elements.js.map +1 -1
- package/dist/src/shoelace/modal.d.ts +24 -24
- package/dist/src/shoelace/modal.js +130 -131
- package/dist/src/shoelace/modal.js.map +1 -1
- package/dist/src/shoelace/tabbable.d.ts +9 -9
- package/dist/src/shoelace/tabbable.js +168 -169
- package/dist/src/shoelace/tabbable.js.map +1 -1
- package/dist/test/modal-config.test.d.ts +1 -1
- package/dist/test/modal-config.test.js +68 -69
- package/dist/test/modal-config.test.js.map +1 -1
- package/dist/test/modal-manager.test.d.ts +1 -1
- package/dist/test/modal-manager.test.js +306 -309
- package/dist/test/modal-manager.test.js.map +1 -1
- package/dist/test/modal-template.test.d.ts +1 -1
- package/dist/test/modal-template.test.js +158 -167
- package/dist/test/modal-template.test.js.map +1 -1
- package/dist/vite.config.d.ts +2 -2
- package/dist/vite.config.js +22 -22
- package/dist/vitest.config.ci.d.ts +2 -0
- package/dist/vitest.config.ci.js +24 -0
- package/dist/vitest.config.ci.js.map +1 -0
- package/docs/assets/css/main.css +2678 -2678
- package/docs/classes/_src_modal_config_.modalconfig.html +429 -429
- package/docs/classes/_src_modal_manager_.modalmanager.html +7702 -7702
- package/docs/classes/_src_modal_manager_host_bridge_.modalmanagerhostbridge.html +409 -409
- package/docs/classes/_src_modal_template_.modaltemplate.html +7096 -7096
- package/docs/enums/_src_modal_manager_mode_.modalmanagermode.html +196 -196
- package/docs/globals.html +150 -150
- package/docs/index.html +252 -252
- package/docs/interfaces/_src_modal_manager_host_bridge_interface_.modalmanagerhostbridgeinterface.html +210 -210
- package/docs/interfaces/_src_modal_manager_interface_.modalmanagerinterface.html +7095 -7095
- package/docs/modules/_index_.html +208 -208
- package/docs/modules/_src_modal_config_.html +146 -146
- package/docs/modules/_src_modal_manager_.html +146 -146
- package/docs/modules/_src_modal_manager_host_bridge_.html +146 -146
- package/docs/modules/_src_modal_manager_host_bridge_interface_.html +146 -146
- package/docs/modules/_src_modal_manager_interface_.html +146 -146
- package/docs/modules/_src_modal_manager_mode_.html +146 -146
- package/docs/modules/_src_modal_template_.html +146 -146
- package/docs/modules/_test_modal_config_test_.html +106 -106
- package/docs/modules/_test_modal_manager_test_.html +106 -106
- package/docs/modules/_test_modal_template_test_.html +106 -106
- package/eslint.config.mjs +53 -0
- package/index.html +33 -300
- package/package.json +73 -85
- package/renovate.json +7 -7
- package/src/modal-config.ts +14 -14
- package/src/modal-manager-host-bridge.ts +2 -2
- package/src/modal-manager.ts +305 -305
- package/src/modal-template.ts +4 -2
- package/src/shoelace/LICENSE.md +6 -6
- package/src/shoelace/active-elements.ts +3 -2
- package/src/shoelace/modal.ts +5 -5
- package/src/shoelace/tabbable.ts +4 -3
- package/test/modal-config.test.ts +4 -4
- package/test/modal-manager.test.ts +402 -393
- package/test/modal-template.test.ts +42 -35
- package/tsconfig.json +25 -21
- package/vitest.config.ci.ts +27 -0
- package/.eslintrc.js +0 -14
- package/karma.conf.js +0 -24
package/src/modal-manager.ts
CHANGED
|
@@ -1,305 +1,305 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LitElement,
|
|
3
|
-
html,
|
|
4
|
-
css,
|
|
5
|
-
CSSResult,
|
|
6
|
-
TemplateResult,
|
|
7
|
-
PropertyValues,
|
|
8
|
-
} from 'lit';
|
|
9
|
-
import { property, customElement, query } from 'lit/decorators.js';
|
|
10
|
-
|
|
11
|
-
import Modal from './shoelace/modal';
|
|
12
|
-
import { getDeepestActiveElement } from './shoelace/active-elements';
|
|
13
|
-
|
|
14
|
-
import './modal-template';
|
|
15
|
-
import { ModalTemplate } from './modal-template';
|
|
16
|
-
import { ModalConfig } from './modal-config';
|
|
17
|
-
import { ModalManagerHostBridge } from './modal-manager-host-bridge';
|
|
18
|
-
import { ModalManagerInterface } from './modal-manager-interface';
|
|
19
|
-
import { ModalManagerHostBridgeInterface } from './modal-manager-host-bridge-interface';
|
|
20
|
-
import { ModalManagerMode } from './modal-manager-mode';
|
|
21
|
-
|
|
22
|
-
@customElement('modal-manager')
|
|
23
|
-
export class ModalManager extends LitElement implements ModalManagerInterface {
|
|
24
|
-
/**
|
|
25
|
-
* The current mode of the ModalManager
|
|
26
|
-
*
|
|
27
|
-
* Current options are `modal` or `closed`
|
|
28
|
-
*
|
|
29
|
-
* @type {ModalManagerMode}
|
|
30
|
-
* @memberof ModalManager
|
|
31
|
-
*/
|
|
32
|
-
@property({ type: String, reflect: true }) mode: ModalManagerMode =
|
|
33
|
-
ModalManagerMode.Closed;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Custom content to display in the modal's content slot
|
|
37
|
-
*
|
|
38
|
-
* @type {(TemplateResult | undefined)}
|
|
39
|
-
* @memberof ModalManager
|
|
40
|
-
*/
|
|
41
|
-
@property({ type: Object }) customModalContent?: TemplateResult;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* This hostBridge handles environmental-specific interactions such as adding classes
|
|
45
|
-
* to the body tag or event listeners needed to support the modal manager in the host environment.
|
|
46
|
-
*
|
|
47
|
-
* There is a default `ModalManagerHostBridge`, but consumers can override it with a custom
|
|
48
|
-
* `ModalManagerHostBridgeInterface`
|
|
49
|
-
*
|
|
50
|
-
* @type {ModalManagerHostBridgeInterface}
|
|
51
|
-
* @memberof ModalManager
|
|
52
|
-
*/
|
|
53
|
-
@property({ type: Object })
|
|
54
|
-
hostBridge: ModalManagerHostBridgeInterface = new ModalManagerHostBridge(
|
|
55
|
-
this
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Reference to the ModalTemplate DOM element
|
|
60
|
-
*
|
|
61
|
-
* @private
|
|
62
|
-
* @type {ModalTemplate}
|
|
63
|
-
* @memberof ModalManager
|
|
64
|
-
*/
|
|
65
|
-
@query('modal-template') private modalTemplate?: ModalTemplate;
|
|
66
|
-
|
|
67
|
-
// Imported tab handling from shoelace
|
|
68
|
-
public modal = new Modal(this);
|
|
69
|
-
|
|
70
|
-
async firstUpdated(): Promise<void> {
|
|
71
|
-
// Give the browser a chance to paint
|
|
72
|
-
|
|
73
|
-
await new Promise(r => setTimeout(r, 0));
|
|
74
|
-
|
|
75
|
-
if (this.closeOnBackdropClick) {
|
|
76
|
-
this.addEventListener('keydown', (e: KeyboardEvent) => {
|
|
77
|
-
if (e.key === 'Escape') {
|
|
78
|
-
this.backdropClicked();
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
disconnectedCallback() {
|
|
85
|
-
super.disconnectedCallback();
|
|
86
|
-
this.modal.deactivate();
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** @inheritdoc */
|
|
90
|
-
render(): TemplateResult {
|
|
91
|
-
return html`
|
|
92
|
-
<div class="container">
|
|
93
|
-
<div class="backdrop" @click=${this.backdropClicked}></div>
|
|
94
|
-
<modal-template
|
|
95
|
-
@closeButtonPressed=${this.closeButtonPressed}
|
|
96
|
-
@leftNavButtonPressed=${this.callUserPressedLeftNavButtonCallback}
|
|
97
|
-
tabindex="-1"
|
|
98
|
-
>
|
|
99
|
-
${this.customModalContent}
|
|
100
|
-
</modal-template>
|
|
101
|
-
</div>
|
|
102
|
-
`;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/** @inheritdoc */
|
|
106
|
-
getMode(): ModalManagerMode {
|
|
107
|
-
return this.mode;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/** @inheritdoc */
|
|
111
|
-
closeModal(): void {
|
|
112
|
-
this.mode = ModalManagerMode.Closed;
|
|
113
|
-
this.customModalContent = undefined;
|
|
114
|
-
if (this.modalTemplate) this.modalTemplate.config = new ModalConfig();
|
|
115
|
-
this.modal.deactivate();
|
|
116
|
-
|
|
117
|
-
// Return focus to the triggering element, if possible
|
|
118
|
-
(this.triggeringElement as HTMLElement)?.focus?.();
|
|
119
|
-
this.triggeringElement = undefined;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Whether the modal should close if the user taps on the backdrop
|
|
124
|
-
*
|
|
125
|
-
* @private
|
|
126
|
-
* @memberof ModalManager
|
|
127
|
-
*/
|
|
128
|
-
private closeOnBackdropClick = true;
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* The element that had focus when the modal was opened, so that we can return focus
|
|
132
|
-
* to it after the modal closes.
|
|
133
|
-
*/
|
|
134
|
-
private triggeringElement?: Element;
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* A callback if the user closes the modal
|
|
138
|
-
*
|
|
139
|
-
* @private
|
|
140
|
-
* @memberof ModalManager
|
|
141
|
-
*/
|
|
142
|
-
private userClosedModalCallback?: () => void;
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* A callback if the user presses the left nav button
|
|
146
|
-
*
|
|
147
|
-
* @private
|
|
148
|
-
* @memberof ModalManager
|
|
149
|
-
*/
|
|
150
|
-
private userPressedLeftNavButtonCallback?: () => void;
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Call the userClosedModalCallback and reset it if it exists
|
|
154
|
-
*
|
|
155
|
-
* @private
|
|
156
|
-
* @memberof ModalManager
|
|
157
|
-
*/
|
|
158
|
-
private callUserClosedModalCallback(): void {
|
|
159
|
-
// we assign the callback to a temp var and undefine it before calling it
|
|
160
|
-
// otherwise, we run into the potential for an infinite loop if the
|
|
161
|
-
// callback triggers another `showModal()`, which would execute `userClosedModalCallback`
|
|
162
|
-
const callback = this.userClosedModalCallback;
|
|
163
|
-
this.userClosedModalCallback = undefined;
|
|
164
|
-
if (callback) callback();
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Call the user pressed left nav button callback and reset it if it exists
|
|
169
|
-
*
|
|
170
|
-
* @private
|
|
171
|
-
* @memberof ModalManager
|
|
172
|
-
*/
|
|
173
|
-
private callUserPressedLeftNavButtonCallback(): void {
|
|
174
|
-
// avoids an infinite showModal() loop, as above
|
|
175
|
-
const callback = this.userPressedLeftNavButtonCallback;
|
|
176
|
-
this.userPressedLeftNavButtonCallback = undefined;
|
|
177
|
-
if (callback) callback();
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/** @inheritdoc */
|
|
181
|
-
async showModal(options: {
|
|
182
|
-
config: ModalConfig;
|
|
183
|
-
customModalContent?: TemplateResult;
|
|
184
|
-
userClosedModalCallback?: () => void;
|
|
185
|
-
userPressedLeftNavButtonCallback?: () => void;
|
|
186
|
-
}): Promise<void> {
|
|
187
|
-
// If the dialog is being opened, make note of what element was focused beforehand
|
|
188
|
-
if (this.mode === ModalManagerMode.Closed) this.captureFocusedElement();
|
|
189
|
-
|
|
190
|
-
this.closeOnBackdropClick = options.config.closeOnBackdropClick;
|
|
191
|
-
this.userClosedModalCallback = options.userClosedModalCallback;
|
|
192
|
-
this.userPressedLeftNavButtonCallback =
|
|
193
|
-
options.userPressedLeftNavButtonCallback;
|
|
194
|
-
this.customModalContent = options.customModalContent;
|
|
195
|
-
this.mode = ModalManagerMode.Open;
|
|
196
|
-
if (this.modalTemplate) {
|
|
197
|
-
this.modalTemplate.config = options.config;
|
|
198
|
-
await this.modalTemplate.updateComplete;
|
|
199
|
-
this.modalTemplate.focus();
|
|
200
|
-
}
|
|
201
|
-
this.modal.activate();
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Sets the triggering element to the one that is currently focused, as deep
|
|
206
|
-
* within Shadow DOM as possible.
|
|
207
|
-
*/
|
|
208
|
-
private captureFocusedElement(): void {
|
|
209
|
-
this.triggeringElement = getDeepestActiveElement();
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/** @inheritdoc */
|
|
213
|
-
updated(changed: PropertyValues): void {
|
|
214
|
-
/* istanbul ignore else */
|
|
215
|
-
if (changed.has('mode')) {
|
|
216
|
-
this.handleModeChange();
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Called when the backdrop is clicked
|
|
222
|
-
*
|
|
223
|
-
* @private
|
|
224
|
-
* @memberof ModalManager
|
|
225
|
-
*/
|
|
226
|
-
private backdropClicked(): void {
|
|
227
|
-
if (this.closeOnBackdropClick) {
|
|
228
|
-
this.closeModal();
|
|
229
|
-
this.callUserClosedModalCallback();
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Handle the mode change
|
|
235
|
-
*
|
|
236
|
-
* @private
|
|
237
|
-
* @memberof ModalManager
|
|
238
|
-
*/
|
|
239
|
-
private handleModeChange(): void {
|
|
240
|
-
this.hostBridge.handleModeChange(this.mode);
|
|
241
|
-
this.emitModeChangeEvent();
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Emit a modeChange event
|
|
246
|
-
*
|
|
247
|
-
* @private
|
|
248
|
-
* @memberof ModalManager
|
|
249
|
-
*/
|
|
250
|
-
private emitModeChangeEvent(): void {
|
|
251
|
-
const event = new CustomEvent('modeChanged', {
|
|
252
|
-
detail: { mode: this.mode },
|
|
253
|
-
});
|
|
254
|
-
this.dispatchEvent(event);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Called when the modal close button is pressed. Closes the modal.
|
|
259
|
-
*
|
|
260
|
-
* @private
|
|
261
|
-
* @memberof ModalManager
|
|
262
|
-
*/
|
|
263
|
-
private closeButtonPressed(): void {
|
|
264
|
-
this.closeModal();
|
|
265
|
-
this.callUserClosedModalCallback();
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/** @inheritdoc */
|
|
269
|
-
static get styles(): CSSResult {
|
|
270
|
-
const modalBackdropColor = css`var(--modalBackdropColor, rgba(10, 10, 10, 0.9))`;
|
|
271
|
-
const modalBackdropZindex = css`var(--modalBackdropZindex, 1000)`;
|
|
272
|
-
|
|
273
|
-
const modalWidth = css`var(--modalWidth, 32rem)`;
|
|
274
|
-
const modalMaxWidth = css`var(--modalMaxWidth, 95%)`;
|
|
275
|
-
const modalZindex = css`var(--modalZindex, 2000)`;
|
|
276
|
-
|
|
277
|
-
return css`
|
|
278
|
-
.container {
|
|
279
|
-
width: 100%;
|
|
280
|
-
height: 100%;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
.backdrop {
|
|
284
|
-
position: fixed;
|
|
285
|
-
top: 0;
|
|
286
|
-
left: 0;
|
|
287
|
-
background-color: ${modalBackdropColor};
|
|
288
|
-
width: 100%;
|
|
289
|
-
height: 100%;
|
|
290
|
-
z-index: ${modalBackdropZindex};
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
modal-template {
|
|
294
|
-
outline: 0;
|
|
295
|
-
position: fixed;
|
|
296
|
-
top: 0;
|
|
297
|
-
left: 50%;
|
|
298
|
-
transform: translate(-50%, 0);
|
|
299
|
-
z-index: ${modalZindex};
|
|
300
|
-
width: ${modalWidth};
|
|
301
|
-
max-width: ${modalMaxWidth};
|
|
302
|
-
}
|
|
303
|
-
`;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
LitElement,
|
|
3
|
+
html,
|
|
4
|
+
css,
|
|
5
|
+
CSSResult,
|
|
6
|
+
TemplateResult,
|
|
7
|
+
PropertyValues,
|
|
8
|
+
} from 'lit';
|
|
9
|
+
import { property, customElement, query } from 'lit/decorators.js';
|
|
10
|
+
|
|
11
|
+
import Modal from './shoelace/modal';
|
|
12
|
+
import { getDeepestActiveElement } from './shoelace/active-elements';
|
|
13
|
+
|
|
14
|
+
import './modal-template';
|
|
15
|
+
import { ModalTemplate } from './modal-template';
|
|
16
|
+
import { ModalConfig } from './modal-config';
|
|
17
|
+
import { ModalManagerHostBridge } from './modal-manager-host-bridge';
|
|
18
|
+
import { ModalManagerInterface } from './modal-manager-interface';
|
|
19
|
+
import { ModalManagerHostBridgeInterface } from './modal-manager-host-bridge-interface';
|
|
20
|
+
import { ModalManagerMode } from './modal-manager-mode';
|
|
21
|
+
|
|
22
|
+
@customElement('modal-manager')
|
|
23
|
+
export class ModalManager extends LitElement implements ModalManagerInterface {
|
|
24
|
+
/**
|
|
25
|
+
* The current mode of the ModalManager
|
|
26
|
+
*
|
|
27
|
+
* Current options are `modal` or `closed`
|
|
28
|
+
*
|
|
29
|
+
* @type {ModalManagerMode}
|
|
30
|
+
* @memberof ModalManager
|
|
31
|
+
*/
|
|
32
|
+
@property({ type: String, reflect: true }) mode: ModalManagerMode =
|
|
33
|
+
ModalManagerMode.Closed;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Custom content to display in the modal's content slot
|
|
37
|
+
*
|
|
38
|
+
* @type {(TemplateResult | undefined)}
|
|
39
|
+
* @memberof ModalManager
|
|
40
|
+
*/
|
|
41
|
+
@property({ type: Object }) customModalContent?: TemplateResult;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* This hostBridge handles environmental-specific interactions such as adding classes
|
|
45
|
+
* to the body tag or event listeners needed to support the modal manager in the host environment.
|
|
46
|
+
*
|
|
47
|
+
* There is a default `ModalManagerHostBridge`, but consumers can override it with a custom
|
|
48
|
+
* `ModalManagerHostBridgeInterface`
|
|
49
|
+
*
|
|
50
|
+
* @type {ModalManagerHostBridgeInterface}
|
|
51
|
+
* @memberof ModalManager
|
|
52
|
+
*/
|
|
53
|
+
@property({ type: Object })
|
|
54
|
+
hostBridge: ModalManagerHostBridgeInterface = new ModalManagerHostBridge(
|
|
55
|
+
this,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Reference to the ModalTemplate DOM element
|
|
60
|
+
*
|
|
61
|
+
* @private
|
|
62
|
+
* @type {ModalTemplate}
|
|
63
|
+
* @memberof ModalManager
|
|
64
|
+
*/
|
|
65
|
+
@query('modal-template') private modalTemplate?: ModalTemplate;
|
|
66
|
+
|
|
67
|
+
// Imported tab handling from shoelace
|
|
68
|
+
public modal = new Modal(this);
|
|
69
|
+
|
|
70
|
+
async firstUpdated(): Promise<void> {
|
|
71
|
+
// Give the browser a chance to paint
|
|
72
|
+
|
|
73
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
74
|
+
|
|
75
|
+
if (this.closeOnBackdropClick) {
|
|
76
|
+
this.addEventListener('keydown', (e: KeyboardEvent) => {
|
|
77
|
+
if (e.key === 'Escape') {
|
|
78
|
+
this.backdropClicked();
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
disconnectedCallback() {
|
|
85
|
+
super.disconnectedCallback();
|
|
86
|
+
this.modal.deactivate();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** @inheritdoc */
|
|
90
|
+
render(): TemplateResult {
|
|
91
|
+
return html`
|
|
92
|
+
<div class="container">
|
|
93
|
+
<div class="backdrop" @click=${this.backdropClicked}></div>
|
|
94
|
+
<modal-template
|
|
95
|
+
@closeButtonPressed=${this.closeButtonPressed}
|
|
96
|
+
@leftNavButtonPressed=${this.callUserPressedLeftNavButtonCallback}
|
|
97
|
+
tabindex="-1"
|
|
98
|
+
>
|
|
99
|
+
${this.customModalContent}
|
|
100
|
+
</modal-template>
|
|
101
|
+
</div>
|
|
102
|
+
`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** @inheritdoc */
|
|
106
|
+
getMode(): ModalManagerMode {
|
|
107
|
+
return this.mode;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** @inheritdoc */
|
|
111
|
+
closeModal(): void {
|
|
112
|
+
this.mode = ModalManagerMode.Closed;
|
|
113
|
+
this.customModalContent = undefined;
|
|
114
|
+
if (this.modalTemplate) this.modalTemplate.config = new ModalConfig();
|
|
115
|
+
this.modal.deactivate();
|
|
116
|
+
|
|
117
|
+
// Return focus to the triggering element, if possible
|
|
118
|
+
(this.triggeringElement as HTMLElement)?.focus?.();
|
|
119
|
+
this.triggeringElement = undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Whether the modal should close if the user taps on the backdrop
|
|
124
|
+
*
|
|
125
|
+
* @private
|
|
126
|
+
* @memberof ModalManager
|
|
127
|
+
*/
|
|
128
|
+
private closeOnBackdropClick = true;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The element that had focus when the modal was opened, so that we can return focus
|
|
132
|
+
* to it after the modal closes.
|
|
133
|
+
*/
|
|
134
|
+
private triggeringElement?: Element;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A callback if the user closes the modal
|
|
138
|
+
*
|
|
139
|
+
* @private
|
|
140
|
+
* @memberof ModalManager
|
|
141
|
+
*/
|
|
142
|
+
private userClosedModalCallback?: () => void;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* A callback if the user presses the left nav button
|
|
146
|
+
*
|
|
147
|
+
* @private
|
|
148
|
+
* @memberof ModalManager
|
|
149
|
+
*/
|
|
150
|
+
private userPressedLeftNavButtonCallback?: () => void;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Call the userClosedModalCallback and reset it if it exists
|
|
154
|
+
*
|
|
155
|
+
* @private
|
|
156
|
+
* @memberof ModalManager
|
|
157
|
+
*/
|
|
158
|
+
private callUserClosedModalCallback(): void {
|
|
159
|
+
// we assign the callback to a temp var and undefine it before calling it
|
|
160
|
+
// otherwise, we run into the potential for an infinite loop if the
|
|
161
|
+
// callback triggers another `showModal()`, which would execute `userClosedModalCallback`
|
|
162
|
+
const callback = this.userClosedModalCallback;
|
|
163
|
+
this.userClosedModalCallback = undefined;
|
|
164
|
+
if (callback) callback();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Call the user pressed left nav button callback and reset it if it exists
|
|
169
|
+
*
|
|
170
|
+
* @private
|
|
171
|
+
* @memberof ModalManager
|
|
172
|
+
*/
|
|
173
|
+
private callUserPressedLeftNavButtonCallback(): void {
|
|
174
|
+
// avoids an infinite showModal() loop, as above
|
|
175
|
+
const callback = this.userPressedLeftNavButtonCallback;
|
|
176
|
+
this.userPressedLeftNavButtonCallback = undefined;
|
|
177
|
+
if (callback) callback();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** @inheritdoc */
|
|
181
|
+
async showModal(options: {
|
|
182
|
+
config: ModalConfig;
|
|
183
|
+
customModalContent?: TemplateResult;
|
|
184
|
+
userClosedModalCallback?: () => void;
|
|
185
|
+
userPressedLeftNavButtonCallback?: () => void;
|
|
186
|
+
}): Promise<void> {
|
|
187
|
+
// If the dialog is being opened, make note of what element was focused beforehand
|
|
188
|
+
if (this.mode === ModalManagerMode.Closed) this.captureFocusedElement();
|
|
189
|
+
|
|
190
|
+
this.closeOnBackdropClick = options.config.closeOnBackdropClick;
|
|
191
|
+
this.userClosedModalCallback = options.userClosedModalCallback;
|
|
192
|
+
this.userPressedLeftNavButtonCallback =
|
|
193
|
+
options.userPressedLeftNavButtonCallback;
|
|
194
|
+
this.customModalContent = options.customModalContent;
|
|
195
|
+
this.mode = ModalManagerMode.Open;
|
|
196
|
+
if (this.modalTemplate) {
|
|
197
|
+
this.modalTemplate.config = options.config;
|
|
198
|
+
await this.modalTemplate.updateComplete;
|
|
199
|
+
this.modalTemplate.focus();
|
|
200
|
+
}
|
|
201
|
+
this.modal.activate();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Sets the triggering element to the one that is currently focused, as deep
|
|
206
|
+
* within Shadow DOM as possible.
|
|
207
|
+
*/
|
|
208
|
+
private captureFocusedElement(): void {
|
|
209
|
+
this.triggeringElement = getDeepestActiveElement();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** @inheritdoc */
|
|
213
|
+
updated(changed: PropertyValues): void {
|
|
214
|
+
/* istanbul ignore else */
|
|
215
|
+
if (changed.has('mode')) {
|
|
216
|
+
this.handleModeChange();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Called when the backdrop is clicked
|
|
222
|
+
*
|
|
223
|
+
* @private
|
|
224
|
+
* @memberof ModalManager
|
|
225
|
+
*/
|
|
226
|
+
private backdropClicked(): void {
|
|
227
|
+
if (this.closeOnBackdropClick) {
|
|
228
|
+
this.closeModal();
|
|
229
|
+
this.callUserClosedModalCallback();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Handle the mode change
|
|
235
|
+
*
|
|
236
|
+
* @private
|
|
237
|
+
* @memberof ModalManager
|
|
238
|
+
*/
|
|
239
|
+
private handleModeChange(): void {
|
|
240
|
+
this.hostBridge.handleModeChange(this.mode);
|
|
241
|
+
this.emitModeChangeEvent();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Emit a modeChange event
|
|
246
|
+
*
|
|
247
|
+
* @private
|
|
248
|
+
* @memberof ModalManager
|
|
249
|
+
*/
|
|
250
|
+
private emitModeChangeEvent(): void {
|
|
251
|
+
const event = new CustomEvent('modeChanged', {
|
|
252
|
+
detail: { mode: this.mode },
|
|
253
|
+
});
|
|
254
|
+
this.dispatchEvent(event);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Called when the modal close button is pressed. Closes the modal.
|
|
259
|
+
*
|
|
260
|
+
* @private
|
|
261
|
+
* @memberof ModalManager
|
|
262
|
+
*/
|
|
263
|
+
private closeButtonPressed(): void {
|
|
264
|
+
this.closeModal();
|
|
265
|
+
this.callUserClosedModalCallback();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** @inheritdoc */
|
|
269
|
+
static get styles(): CSSResult {
|
|
270
|
+
const modalBackdropColor = css`var(--modalBackdropColor, rgba(10, 10, 10, 0.9))`;
|
|
271
|
+
const modalBackdropZindex = css`var(--modalBackdropZindex, 1000)`;
|
|
272
|
+
|
|
273
|
+
const modalWidth = css`var(--modalWidth, 32rem)`;
|
|
274
|
+
const modalMaxWidth = css`var(--modalMaxWidth, 95%)`;
|
|
275
|
+
const modalZindex = css`var(--modalZindex, 2000)`;
|
|
276
|
+
|
|
277
|
+
return css`
|
|
278
|
+
.container {
|
|
279
|
+
width: 100%;
|
|
280
|
+
height: 100%;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
.backdrop {
|
|
284
|
+
position: fixed;
|
|
285
|
+
top: 0;
|
|
286
|
+
left: 0;
|
|
287
|
+
background-color: ${modalBackdropColor};
|
|
288
|
+
width: 100%;
|
|
289
|
+
height: 100%;
|
|
290
|
+
z-index: ${modalBackdropZindex};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
modal-template {
|
|
294
|
+
outline: 0;
|
|
295
|
+
position: fixed;
|
|
296
|
+
top: 0;
|
|
297
|
+
left: 50%;
|
|
298
|
+
transform: translate(-50%, 0);
|
|
299
|
+
z-index: ${modalZindex};
|
|
300
|
+
width: ${modalWidth};
|
|
301
|
+
max-width: ${modalMaxWidth};
|
|
302
|
+
}
|
|
303
|
+
`;
|
|
304
|
+
}
|
|
305
|
+
}
|
package/src/modal-template.ts
CHANGED
|
@@ -256,7 +256,8 @@ export class ModalTemplate extends LitElement {
|
|
|
256
256
|
.logo-icon {
|
|
257
257
|
border-radius: 100%;
|
|
258
258
|
border: 3px solid #fff;
|
|
259
|
-
box-shadow:
|
|
259
|
+
box-shadow:
|
|
260
|
+
0 0 0 1px rgba(0, 0, 0, 0.18),
|
|
260
261
|
0 2px 2px 0 rgba(0, 0, 0, 0.08);
|
|
261
262
|
width: ${modalLogoSize};
|
|
262
263
|
height: ${modalLogoSize};
|
|
@@ -291,7 +292,8 @@ export class ModalTemplate extends LitElement {
|
|
|
291
292
|
padding: 0;
|
|
292
293
|
cursor: pointer;
|
|
293
294
|
background-color: white;
|
|
294
|
-
box-shadow:
|
|
295
|
+
box-shadow:
|
|
296
|
+
0 0 0 1px rgba(0, 0, 0, 0.18),
|
|
295
297
|
0 4px 4px 0 rgba(0, 0, 0, 0.08);
|
|
296
298
|
}
|
|
297
299
|
|
package/src/shoelace/LICENSE.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
Copyright (c) 2020 A Beautiful Site, LLC
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
-
|
|
5
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
-
|
|
1
|
+
Copyright (c) 2020 A Beautiful Site, LLC
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
7
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*
|
|
1
|
+
/* c8 ignore start */
|
|
2
2
|
/**
|
|
3
3
|
* Use a generator so we can iterate and possibly break early.
|
|
4
4
|
* @example
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* }
|
|
14
14
|
*/
|
|
15
15
|
export function* activeElements(
|
|
16
|
-
activeElement: Element | null = document.activeElement
|
|
16
|
+
activeElement: Element | null = document.activeElement,
|
|
17
17
|
): Generator<Element> {
|
|
18
18
|
if (activeElement === null || activeElement === undefined) return;
|
|
19
19
|
|
|
@@ -31,3 +31,4 @@ export function* activeElements(
|
|
|
31
31
|
export function getDeepestActiveElement() {
|
|
32
32
|
return [...activeElements()].pop();
|
|
33
33
|
}
|
|
34
|
+
/* c8 ignore end */
|