@m3e/dialog 1.0.0-rc.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.
@@ -0,0 +1,472 @@
1
+ /* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
2
+ import { css, CSSResultGroup, html, LitElement, nothing, unsafeCSS } from "lit";
3
+ import { customElement, property, query, state } from "lit/decorators.js";
4
+ import { ifDefined } from "lit/directives/if-defined.js";
5
+
6
+ import { DesignToken, EventAttribute, prefersReducedMotion, Role } from "@m3e/core";
7
+ import {} from "@m3e/core/a11y";
8
+
9
+ /**
10
+ * @summary
11
+ * A dialog that provides important prompts in a user flow.
12
+ *
13
+ * @description
14
+ * The `m3e-dialog` component presents important prompts, alerts, and actions in user flows.
15
+ * Designed according to Material 3 principles, it supports custom header, content, and
16
+ * close icon slots, ARIA accessibility, focus management, and theming via CSS custom properties.
17
+ *
18
+ * @example
19
+ * ```html
20
+ * <m3e-button variant="filled">
21
+ * <m3e-dialog-trigger for="dlg">Open Dialog</m3e-dialog-trigger>
22
+ * </m3e-button>
23
+ * <m3e-dialog id="dlg" dismissible onclosed="console.log(this.returnValue)">
24
+ * <span slot="header">Dialog Title</span>
25
+ * Dialog content goes here.
26
+ * <div slot="actions" end>
27
+ * <m3e-button autofocus><m3e-dialog-action return-value="ok">Close</m3e-dialog-action></m3e-button>
28
+ * </div>
29
+ * </m3e-dialog>
30
+ * ```
31
+ *
32
+ * @tag m3e-dialog
33
+ *
34
+ * @slot - Renders the content of the dialog.
35
+ * @slot header - Renders the header of the dialog.
36
+ * @slot close-icon - Renders the icon of the button used to close the dialog.
37
+ *
38
+ * @attr alert - Whether the dialog is an alert.
39
+ * @attr close-label - The accessible label given to the button used to dismiss the dialog.
40
+ * @attr disable-close -Whether users cannot click the backdrop or press escape to dismiss the dialog.
41
+ * @attr dismissible - Whether a button is presented that can be used to close the dialog.
42
+ * @attr no-focus-trap - Whether to disable focus trapping, which keeps keyboard `Tab` navigation within the dialog.
43
+ * @attr open - Whether the dialog is open.
44
+ *
45
+ * @fires opening - Emitted when the dialog begins to open.
46
+ * @fires opened - Emitted when the dialog has opened.
47
+ * @fires cancel - Emitted when the dialog is cancelled.
48
+ * @fires closing - Emitted when the dialog begins to close.
49
+ * @fires closed - Emitted when the dialog has closed.
50
+ *
51
+ * @cssprop --m3e-dialog-shape - Border radius of the dialog container.
52
+ * @cssprop --m3e-dialog-min-width - Minimum width of the dialog.
53
+ * @cssprop --m3e-dialog-max-width - Maximum width of the dialog.
54
+ * @cssprop --m3e-dialog-color - Foreground color of the dialog.
55
+ * @cssprop --m3e-dialog-container-color - Background color of the dialog container.
56
+ * @cssprop --m3e-dialog-scrim-color - Color of the scrim (backdrop overlay).
57
+ * @cssprop --m3e-dialog-scrim-opacity - Opacity of the scrim when open.
58
+ * @cssprop --m3e-dialog-header-container-color - Background color of the dialog header.
59
+ * @cssprop --m3e-dialog-header-color - Foreground color of the dialog header.
60
+ * @cssprop --m3e-dialog-header-font-size - Font size for the dialog header.
61
+ * @cssprop --m3e-dialog-header-font-weight - Font weight for the dialog header.
62
+ * @cssprop --m3e-dialog-header-line-height - Line height for the dialog header.
63
+ * @cssprop --m3e-dialog-header-tracking - Letter spacing for the dialog header.
64
+ * @cssprop --m3e-dialog-content-color - Foreground color of the dialog content.
65
+ * @cssprop --m3e-dialog-content-font-size - Font size for the dialog content.
66
+ * @cssprop --m3e-dialog-content-font-weight - Font weight for the dialog content.
67
+ * @cssprop --m3e-dialog-content-line-height - Line height for the dialog content.
68
+ * @cssprop --m3e-dialog-content-tracking - Letter spacing for the dialog content.
69
+ */
70
+ @customElement("m3e-dialog")
71
+ export class M3eDialogElement extends EventAttribute(
72
+ Role(LitElement, "none"),
73
+ "opening",
74
+ "opened",
75
+ "cancel",
76
+ "closing",
77
+ "closed"
78
+ ) {
79
+ /** The styles of the element. */
80
+ static override styles: CSSResultGroup = css`
81
+ :host {
82
+ display: contents;
83
+ }
84
+ .base {
85
+ font: inherit;
86
+ border: unset;
87
+ outline: unset;
88
+ padding: unset;
89
+ display: flex;
90
+ flex-direction: column;
91
+ position: fixed;
92
+ overflow: visible;
93
+ border-radius: var(--m3e-dialog-shape, ${DesignToken.shape.corner.extraLarge});
94
+ min-width: var(--m3e-dialog-min-width, 17.5rem);
95
+ max-width: var(--m3e-dialog-max-width, 35rem);
96
+ color: var(--m3e-dialog-color, ${DesignToken.color.onSurface});
97
+ background-color: var(--m3e-dialog-container-color, ${DesignToken.color.surfaceContainerHigh});
98
+ visibility: hidden;
99
+ opacity: 0;
100
+ transform-origin: top;
101
+ transform: translateY(-3.125rem) scaleY(0.8);
102
+ }
103
+ .base::backdrop {
104
+ background-color: color-mix(in srgb, var(--m3e-dialog-scrim-color, ${DesignToken.color.scrim}) 0%, transparent);
105
+ margin-right: -20px;
106
+ }
107
+ .base:not([open]) {
108
+ visibility: hidden;
109
+ opacity: 0;
110
+ transform: translateY(-3.125rem) scaleY(0.8);
111
+ transition: ${unsafeCSS(
112
+ `opacity ${DesignToken.motion.duration.short3} ${DesignToken.motion.easing.emphasized},
113
+ transform ${DesignToken.motion.duration.short3} ${DesignToken.motion.easing.emphasized},
114
+ overlay ${DesignToken.motion.duration.short3} ${DesignToken.motion.easing.emphasized} allow-discrete,
115
+ visibility ${DesignToken.motion.duration.short3} ${DesignToken.motion.easing.emphasized} allow-discrete`
116
+ )};
117
+ }
118
+ .base[open] {
119
+ visibility: visible;
120
+ opacity: 1;
121
+ transform: translateY(0) scaleY(1);
122
+ transition: ${unsafeCSS(
123
+ `opacity ${DesignToken.motion.duration.long2} ${DesignToken.motion.easing.emphasized},
124
+ transform ${DesignToken.motion.duration.long2} ${DesignToken.motion.easing.emphasized},
125
+ overlay ${DesignToken.motion.duration.long2} ${DesignToken.motion.easing.emphasized} allow-discrete,
126
+ visibility ${DesignToken.motion.duration.long2} ${DesignToken.motion.easing.emphasized} allow-discrete`
127
+ )};
128
+ }
129
+ .base:not([open])::backdrop {
130
+ transition: ${unsafeCSS(
131
+ `background-color ${DesignToken.motion.duration.short3} ${DesignToken.motion.easing.standard},
132
+ overlay ${DesignToken.motion.duration.short3} ${DesignToken.motion.easing.standard} allow-discrete,
133
+ visibility ${DesignToken.motion.duration.short3} ${DesignToken.motion.easing.standard} allow-discrete`
134
+ )};
135
+ }
136
+ .base[open]::backdrop {
137
+ background-color: color-mix(
138
+ in srgb,
139
+ var(--m3e-dialog-scrim-color, ${DesignToken.color.scrim}) var(--m3e-dialog-scrim-opacity, 32%),
140
+ transparent
141
+ );
142
+ transition: ${unsafeCSS(
143
+ `background-color ${DesignToken.motion.duration.long2} ${DesignToken.motion.easing.standard},
144
+ overlay ${DesignToken.motion.duration.long2} ${DesignToken.motion.easing.standard} allow-discrete,
145
+ visibility ${DesignToken.motion.duration.long2} ${DesignToken.motion.easing.standard} allow-discrete`
146
+ )};
147
+ }
148
+ @starting-style {
149
+ .base[open] {
150
+ opacity: 0;
151
+ transform: translateY(-3.125rem) scaleY(0.8);
152
+ }
153
+ .base[open]::backdrop {
154
+ background-color: color-mix(in srgb, var(--m3e-dialog-scrim-color, ${DesignToken.color.scrim}) 0%, transparent);
155
+ }
156
+ }
157
+ .header {
158
+ flex: none;
159
+ display: flex;
160
+ align-items: center;
161
+ padding: 1.5rem 1.5rem 1rem 1.5rem;
162
+ background-color: var(--m3e-dialog-header-container-color, transparent);
163
+ }
164
+ ::slotted([slot="header"]) {
165
+ margin: unset;
166
+ flex: 1 1 auto;
167
+ color: var(--m3e-dialog-header-color, inherit);
168
+ font-size: var(--m3e-dialog-header-font-size, ${DesignToken.typescale.standard.headline.small.fontSize});
169
+ font-weight: var(--m3e-dialog-header-font-weight, ${DesignToken.typescale.standard.headline.small.fontWeight});
170
+ line-height: var(--m3e-dialog-header-line-height, ${DesignToken.typescale.standard.headline.small.lineHeight});
171
+ letter-spacing: var(--m3e-dialog-header-tracking, ${DesignToken.typescale.standard.headline.small.tracking});
172
+ }
173
+ .content {
174
+ padding-inline: 1.5rem;
175
+ color: var(--m3e-dialog-content-color, ${DesignToken.color.onSurfaceVariant});
176
+ font-size: var(--m3e-dialog-content-font-size, ${DesignToken.typescale.standard.body.medium.fontSize});
177
+ font-weight: var(--m3e-dialog-content-font-weight, ${DesignToken.typescale.standard.body.medium.fontWeight});
178
+ line-height: var(--m3e-dialog-content-line-height, ${DesignToken.typescale.standard.body.medium.lineHeight});
179
+ letter-spacing: var(--m3e-dialog-content-tracking, ${DesignToken.typescale.standard.body.medium.tracking});
180
+ }
181
+ ::slotted([slot="actions"]) {
182
+ flex: none;
183
+ display: flex;
184
+ align-items: center;
185
+ min-height: 1.5rem;
186
+ padding: 1.5rem;
187
+ column-gap: 0.5rem;
188
+ }
189
+ ::slotted([slot="actions"][end]) {
190
+ justify-content: flex-end;
191
+ }
192
+ :host(:not(.-has-actions)) .content {
193
+ margin-bottom: 1.5rem;
194
+ }
195
+ .close {
196
+ margin-left: 0.5rem;
197
+ }
198
+ ::slotted([slot="close-icon"]),
199
+ .close-icon {
200
+ width: 1em;
201
+ font-size: var(--m3e-icon-button-icon-size, 1.5rem) !important;
202
+ }
203
+ @media (forced-colors: active) {
204
+ .base:not([open])::backdrop,
205
+ .base[open]::backdrop {
206
+ transition: none;
207
+ }
208
+ .base {
209
+ border-style: solid;
210
+ border-width: 1px;
211
+ border-color: CanvasText;
212
+ }
213
+ }
214
+ @media (prefers-reduced-motion) {
215
+ .base:not([open]),
216
+ .base[open],
217
+ .base:not([open])::backdrop,
218
+ .base[open]::backdrop {
219
+ transition: none;
220
+ }
221
+ }
222
+ `;
223
+
224
+ /** @private */ private static __nextId = 0;
225
+ /** @private */ #id = M3eDialogElement.__nextId++;
226
+
227
+ /** @private */ #open = false;
228
+ /** @private */ #escapePressedWithoutCancel = false;
229
+ /** @private */ @state() private _hasActions = false;
230
+ /** @private */ @query(".base") private readonly _base!: HTMLDialogElement;
231
+ /** @private */ @query(".content") private readonly _content!: HTMLDialogElement;
232
+
233
+ /**
234
+ * Whether the dialog is an alert.
235
+ * @default false
236
+ */
237
+ @property({ type: Boolean }) alert = false;
238
+
239
+ /**
240
+ * Whether the dialog is open.
241
+ * @default false
242
+ */
243
+ @property({ type: Boolean, reflect: true }) get open() {
244
+ return this.#open;
245
+ }
246
+ set open(value: boolean) {
247
+ if (value === this.#open) return;
248
+ this.#open = value;
249
+ if (this.#open) {
250
+ this.show();
251
+ } else {
252
+ this.hide();
253
+ }
254
+ }
255
+
256
+ /**
257
+ * Whether a button is presented that can be used to close the dialog.
258
+ * @default false
259
+ */
260
+ @property({ type: Boolean }) dismissible = false;
261
+
262
+ /**
263
+ * Whether users cannot click the backdrop or press ESC to dismiss the dialog.
264
+ * @default false
265
+ */
266
+ @property({ attribute: "disable-close", type: Boolean }) disableClose = false;
267
+
268
+ /**
269
+ * Whether to disable focus trapping, which keeps keyboard `Tab` navigation within the dialog.
270
+ * @default false
271
+ */
272
+ @property({ attribute: "no-focus-trap", type: Boolean }) noFocusTrap = false;
273
+
274
+ /**
275
+ * The accessible label given to the button used to dismiss the dialog.
276
+ * @default "Close"
277
+ */
278
+ @property({ attribute: "close-label" }) closeLabel = "Close";
279
+
280
+ /**
281
+ * The return value of the dialog.
282
+ * @default ""
283
+ */
284
+ returnValue = "";
285
+
286
+ /**
287
+ * Asynchronously opens the dialog.
288
+ * @returns {Promise<void>} A `Promise` that resolves when the dialog is open.
289
+ */
290
+ async show(): Promise<void> {
291
+ await this.updateComplete;
292
+
293
+ if (this._base.open) {
294
+ return;
295
+ }
296
+
297
+ if (!this.dispatchEvent(new Event("opening", { cancelable: true }))) {
298
+ this.open = false;
299
+ return;
300
+ }
301
+
302
+ this._base.showModal();
303
+ this._content.scrollTop = 0;
304
+ const focusable = this.querySelector<HTMLElement>("[autofocus]");
305
+
306
+ if (focusable) {
307
+ if (!prefersReducedMotion()) {
308
+ this._base.addEventListener("transitionend", () => focusable.focus(), {
309
+ once: true,
310
+ });
311
+ } else {
312
+ focusable.focus();
313
+ }
314
+ }
315
+
316
+ this.dispatchEvent(new Event("opened"));
317
+ }
318
+
319
+ /**
320
+ * Asynchronously closes the dialog.
321
+ * @param {string} returnValue The value to return.
322
+ * @returns {Promise<void>} A `Promise` that resolves when the dialog is closed.
323
+ */
324
+ async hide(returnValue: string = this.returnValue): Promise<void> {
325
+ if (!this.isConnected) {
326
+ this.open = false;
327
+ return;
328
+ }
329
+
330
+ await this.updateComplete;
331
+
332
+ if (!this._base.open) {
333
+ this.open = false;
334
+ return;
335
+ }
336
+
337
+ const prevReturnValue = this.returnValue;
338
+ this.returnValue = returnValue;
339
+
340
+ if (!this.dispatchEvent(new Event("closing", { cancelable: true }))) {
341
+ this.returnValue = prevReturnValue;
342
+ return;
343
+ }
344
+
345
+ this.open = false;
346
+ this._base.close(returnValue);
347
+ this.dispatchEvent(new Event("closed"));
348
+ }
349
+
350
+ /** @inheritdoc */
351
+ protected override render(): unknown {
352
+ return html`<dialog
353
+ class="base"
354
+ role="${ifDefined(this.alert ? "alertdialog" : undefined)}"
355
+ aria-labelledby="m3e-dialog-${this.#id}-header"
356
+ .returnValue="${this.returnValue}"
357
+ @close="${this.#handleClose}"
358
+ @cancel="${this.#handleCancel}"
359
+ @click="${this.#handleClick}"
360
+ @keydown="${this.#handleKeyDown}"
361
+ >
362
+ <m3e-elevation level="3"></m3e-elevation>
363
+ <m3e-focus-trap ?disabled="${this.noFocusTrap}">
364
+ <div class="header">
365
+ <slot name="header" id="m3e-dialog-${this.#id}-header"></slot>
366
+ ${this.#renderCloseButton()}
367
+ </div>
368
+ <m3e-scroll-container class="content" dividers="${this._hasActions ? "above-below" : "above"}">
369
+ <slot></slot>
370
+ </m3e-scroll-container>
371
+ <slot name="actions" @slotchange="${this.#handleActionsSlotChange}"></slot>
372
+ </m3e-focus-trap>
373
+ </dialog>`;
374
+ }
375
+
376
+ /** @private */
377
+ #renderCloseButton(): unknown {
378
+ return !this.dismissible
379
+ ? nothing
380
+ : html`<m3e-icon-button aria-label="${this.closeLabel}" class="close" @click="${this.hide}">
381
+ <slot name="close-icon">
382
+ <svg class="close-icon" viewBox="0 -960 960 960" fill="currentColor">
383
+ <path
384
+ d="m256-200-56-56 224-224-224-224 56-56 224 224 224-224 56 56-224 224 224 224-56 56-224-224-224 224Z"
385
+ />
386
+ </svg>
387
+ </slot>
388
+ </m3e-icon-button>`;
389
+ }
390
+
391
+ /** @private */
392
+ #handleClose(): void {
393
+ if (!this.#escapePressedWithoutCancel) {
394
+ return;
395
+ }
396
+ this.#escapePressedWithoutCancel = true;
397
+ this._base?.dispatchEvent(new Event("cancel", { cancelable: true }));
398
+ }
399
+
400
+ /** @private */
401
+ #handleCancel(e: Event): void {
402
+ if (e.target !== this._base) return;
403
+ this.#escapePressedWithoutCancel = false;
404
+ e.preventDefault();
405
+ if (!this.dispatchEvent(new Event("cancel", { cancelable: true }))) {
406
+ this.hide();
407
+ }
408
+ }
409
+
410
+ /** @private */
411
+ #handleClick(e: Event): void {
412
+ if (!this.disableClose && e.target === this._base) {
413
+ this.hide();
414
+ }
415
+ }
416
+
417
+ /** @private */
418
+ #handleKeyDown(e: KeyboardEvent): void {
419
+ if (e.key === "Escape" && !e.shiftKey && !e.ctrlKey) {
420
+ e.preventDefault();
421
+ if (!this.disableClose) {
422
+ this.hide();
423
+ }
424
+ }
425
+ }
426
+
427
+ /** @private */
428
+ #handleActionsSlotChange(e: Event): void {
429
+ this._hasActions = (<HTMLSlotElement>e.target).assignedNodes({ flatten: true }).length > 0;
430
+ this.classList.toggle("-has-actions", this._hasActions);
431
+ }
432
+ }
433
+
434
+ interface M3eDialogElementEventMap extends HTMLElementEventMap {
435
+ opening: Event;
436
+ opened: Event;
437
+ closing: Event;
438
+ closed: Event;
439
+ cancel: Event;
440
+ }
441
+
442
+ export interface M3eDialogElement {
443
+ addEventListener<K extends keyof M3eDialogElementEventMap>(
444
+ type: K,
445
+ listener: (this: M3eDialogElement, ev: M3eDialogElementEventMap[K]) => void,
446
+ options?: boolean | AddEventListenerOptions
447
+ ): void;
448
+
449
+ addEventListener(
450
+ type: string,
451
+ listener: EventListenerOrEventListenerObject,
452
+ options?: boolean | AddEventListenerOptions
453
+ ): void;
454
+
455
+ removeEventListener<K extends keyof M3eDialogElementEventMap>(
456
+ type: K,
457
+ listener: (this: M3eDialogElement, ev: M3eDialogElementEventMap[K]) => void,
458
+ options?: boolean | EventListenerOptions
459
+ ): void;
460
+
461
+ removeEventListener(
462
+ type: string,
463
+ listener: EventListenerOrEventListenerObject,
464
+ options?: boolean | EventListenerOptions
465
+ ): void;
466
+ }
467
+
468
+ declare global {
469
+ interface HTMLElementTagNameMap {
470
+ "m3e-dialog": M3eDialogElement;
471
+ }
472
+ }
@@ -0,0 +1,50 @@
1
+ import { css, CSSResultGroup, html, LitElement } from "lit";
2
+ import { customElement } from "lit/decorators.js";
3
+
4
+ import { AttachInternals, HtmlFor } from "@m3e/core";
5
+
6
+ import { M3eDialogElement } from "./DialogElement";
7
+
8
+ /**
9
+ * An element, nested within a clickable element, used to open a dialog.
10
+ * @tag m3e-dialog-trigger
11
+ */
12
+ @customElement("m3e-dialog-trigger")
13
+ export class M3eDialogTriggerElement extends HtmlFor(AttachInternals(LitElement)) {
14
+ /** The styles of the element. */
15
+ static override styles: CSSResultGroup = css`
16
+ :host {
17
+ display: contents;
18
+ }
19
+ `;
20
+
21
+ /** @private */
22
+ #clickHandler = (e: Event) => {
23
+ if (!e.defaultPrevented && this.control instanceof M3eDialogElement) {
24
+ this.control.show();
25
+ }
26
+ };
27
+
28
+ /** @inheritdoc */
29
+ override connectedCallback(): void {
30
+ super.connectedCallback();
31
+ this.parentElement?.addEventListener("click", this.#clickHandler);
32
+ }
33
+
34
+ /** @inheritdoc */
35
+ override disconnectedCallback(): void {
36
+ super.disconnectedCallback();
37
+ this.parentElement?.removeEventListener("click", this.#clickHandler);
38
+ }
39
+
40
+ /** @inheritdoc */
41
+ protected override render(): unknown {
42
+ return html`<slot></slot>`;
43
+ }
44
+ }
45
+
46
+ declare global {
47
+ interface HTMLElementTagNameMap {
48
+ "m3e-dialog-trigger": M3eDialogTriggerElement;
49
+ }
50
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./DialogActionElement";
2
+ export * from "./DialogElement";
3
+ export * from "./DialogTriggerElement";
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ "outDir": "./dist/src"
6
+ },
7
+ "include": ["src/**/*.ts", "**/*.mjs", "**/*.js"],
8
+ "exclude": []
9
+ }