@redvars/peacock 3.14.0 → 3.15.0

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@redvars/peacock",
3
3
  "description": "The foundation for beautiful user interfaces",
4
4
  "license": "Apache-2.0",
5
- "version": "3.14.0",
5
+ "version": "3.15.0",
6
6
  "type": "module",
7
7
  "main": "dist/index.js",
8
8
  "module": "dist/index.js",
package/readme.md CHANGED
@@ -44,9 +44,9 @@ Visit [https://peacock.redvars.com](https://peacock.redvars.com) to view the doc
44
44
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
45
45
  <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@100..900&family=Noto+Sans:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
46
46
 
47
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@redvars/peacock@3.14.0/dist/assets/styles.css"></link>
47
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@redvars/peacock@3.15.0/dist/assets/styles.css"></link>
48
48
  <script type='module'
49
- src='https://cdn.jsdelivr.net/npm/@redvars/peacock@3.14.0/dist/loader.js'></script>
49
+ src='https://cdn.jsdelivr.net/npm/@redvars/peacock@3.15.0/dist/loader.js'></script>
50
50
  </head>
51
51
 
52
52
  <wc-button>Button</wc-button>
@@ -20,32 +20,53 @@
20
20
  --modal-elevation: var(--elevation-level3);
21
21
  }
22
22
 
23
+ /*
24
+ * Native dialog opened with showModal(): it renders in the browser's top layer, so no page
25
+ * z-index or stacking context can draw above it. It covers the viewport and stays transparent;
26
+ * the scrim and the container below are drawn inside it, in DOM order, without z-index.
27
+ */
28
+ .native-dialog {
29
+ background: transparent;
30
+ block-size: 100%;
31
+ border: 0;
32
+ color: inherit;
33
+ inline-size: 100%;
34
+ inset: 0;
35
+ margin: 0;
36
+ max-block-size: none;
37
+ max-inline-size: none;
38
+ overflow: hidden;
39
+ padding: 0;
40
+ position: fixed;
41
+
42
+ &::backdrop {
43
+ background: transparent;
44
+ }
45
+ }
46
+
23
47
  /* Scrim backdrop */
24
48
  .scrim {
25
49
  background-color: var(--modal-scrim-color);
26
50
  inset: 0;
27
51
  opacity: 0;
28
- pointer-events: none;
29
- position: fixed;
52
+ position: absolute;
30
53
  transition: opacity var(--modal-transition-duration) var(--modal-transition-easing);
31
- z-index: 1000;
32
54
  }
33
55
 
34
56
  .scrim.visible {
35
57
  opacity: 1;
36
- pointer-events: auto;
37
58
  }
38
59
 
39
- /* Dialog wrapper — centres the dialog on screen */
60
+ /* Dialog wrapper — centres the dialog on screen; a click on it (not the container) is a scrim click */
40
61
  .dialog-wrapper {
41
62
  align-items: center;
63
+ box-sizing: border-box;
42
64
  display: flex;
43
65
  inset: 0;
44
66
  justify-content: center;
45
67
  overflow-y: auto;
46
68
  padding: var(--spacing-400, 2rem) var(--spacing-200, 1rem);
47
- position: fixed;
48
- z-index: 1001;
69
+ position: absolute;
49
70
  }
50
71
 
51
72
  /* Dialog container */
@@ -57,16 +78,18 @@
57
78
  flex-direction: column;
58
79
  max-height: var(--modal-max-height);
59
80
  max-width: var(--modal-max-width);
60
- min-width: var(--modal-min-width);
61
- opacity: 0;
81
+ min-width: min(var(--modal-min-width), 100%);
62
82
  outline: none;
63
83
  overflow: hidden;
64
84
  position: relative;
65
- transform: scale(0.9);
66
85
  width: 100%;
67
86
 
87
+ /*
88
+ * The open state keeps no transform: a transformed ancestor would become the containing
89
+ * block of fixed-position menus, selects and tooltips opened inside the modal.
90
+ */
68
91
  &.open {
69
- animation: modal-enter var(--modal-transition-duration) var(--modal-transition-easing) forwards;
92
+ animation: modal-enter var(--modal-transition-duration) var(--modal-transition-easing) backwards;
70
93
  }
71
94
 
72
95
  &:not(.open) {
@@ -182,6 +205,17 @@
182
205
  position: absolute;
183
206
  }
184
207
 
208
+ @media (prefers-reduced-motion: reduce) {
209
+ .scrim {
210
+ transition: none;
211
+ }
212
+
213
+ .dialog.open,
214
+ .dialog:not(.open) {
215
+ animation: none;
216
+ }
217
+ }
218
+
185
219
  /* Animations */
186
220
  @keyframes modal-enter {
187
221
  from {
@@ -1,5 +1,5 @@
1
- import { LitElement, html, nothing } from 'lit';
2
- import { property, state } from 'lit/decorators.js';
1
+ import { LitElement, html, nothing } from 'lit';
2
+ import { property, query } from 'lit/decorators.js';
3
3
  import { classMap } from 'lit/directives/class-map.js';
4
4
  import IndividualComponent from '../IndividualComponent.js';
5
5
  import styles from './modal.scss';
@@ -11,6 +11,10 @@ type ModalSize = 'xs' | 'sm' | 'md' | 'lg' | 'fullscreen';
11
11
  * @tag wc-modal
12
12
  * @rawTag modal
13
13
  * @summary A dialog/modal for displaying content in a layer above the page, with optional header, body, and footer slots.
14
+ * @overview
15
+ * <p>The modal opens a native <code>&lt;dialog&gt;</code> with <code>showModal()</code>, so it renders in the
16
+ * browser's top layer: above sticky headers, app bars, cards and any stacking context on the page,
17
+ * whatever their z-index. Menus, selects and tooltips opened inside the modal render above it.</p>
14
18
  *
15
19
  * @cssprop --modal-container-color - Background color of the dialog container.
16
20
  * @cssprop --modal-scrim-color - Color of the scrim backdrop.
@@ -19,6 +23,8 @@ type ModalSize = 'xs' | 'sm' | 'md' | 'lg' | 'fullscreen';
19
23
  * @cssprop --modal-max-width - Maximum width of the dialog.
20
24
  * @cssprop --modal-max-height - Maximum height of the dialog.
21
25
  *
26
+ * @fires modal-close - Fired when the modal closes. `detail.reason` is `"close-button"`, `"scrim"`, `"escape"` or `"programmatic"`.
27
+ *
22
28
  * @example
23
29
  * ```html
24
30
  * MODAL
@@ -27,8 +33,12 @@ type ModalSize = 'xs' | 'sm' | 'md' | 'lg' | 'fullscreen';
27
33
  */
28
34
  @IndividualComponent
29
35
  export class Modal extends LitElement {
36
+ // ── Static ───────────────────────────────────────────────────────────────
37
+
30
38
  static styles = [styles];
31
39
 
40
+ // ── Properties ───────────────────────────────────────────────────────────
41
+
32
42
  /** Whether the modal is open. */
33
43
  @property({ type: Boolean, reflect: true }) open = false;
34
44
 
@@ -57,18 +67,66 @@ export class Modal extends LitElement {
57
67
  showLoader = false;
58
68
 
59
69
  /** When true, clicking the scrim will not close the modal. */
60
- @property({ type: Boolean, attribute: 'no-scrim-close' }) noScrimClose = false;
70
+ @property({ type: Boolean, attribute: 'no-scrim-close' }) noScrimClose =
71
+ false;
72
+
73
+ // ── Queries ──────────────────────────────────────────────────────────────
74
+
75
+ @query('dialog') private _dialog?: HTMLDialogElement;
76
+
77
+ @query('.dialog') private _panel?: HTMLElement;
78
+
79
+ // ── Lifecycle ────────────────────────────────────────────────────────────
61
80
 
62
- @state() private _visible = false;
81
+ disconnectedCallback() {
82
+ super.disconnectedCallback();
83
+ document.body.style.overflow = '';
84
+ if (this._dialog?.open) this._dialog.close();
85
+ }
86
+
87
+ protected updated(changedProperties: Map<string, unknown>) {
88
+ if (changedProperties.has('open')) this._syncDialog();
89
+ }
63
90
 
91
+ // ── Public methods ───────────────────────────────────────────────────────
92
+
93
+ /** Opens the modal. */
64
94
  show() {
65
95
  this.open = true;
66
96
  }
67
97
 
98
+ /** Closes the modal. */
68
99
  hide() {
69
100
  this._close('programmatic');
70
101
  }
71
102
 
103
+ // ── Private methods ──────────────────────────────────────────────────────
104
+
105
+ /** Opens the native dialog in the top layer, or closes it once the exit animation ends. */
106
+ private _syncDialog() {
107
+ const dialog = this._dialog;
108
+ if (!dialog) return;
109
+
110
+ if (this.open) {
111
+ if (!dialog.open) dialog.showModal();
112
+ document.body.style.overflow = 'hidden';
113
+ return;
114
+ }
115
+
116
+ document.body.style.overflow = '';
117
+ if (!dialog.open) return;
118
+ const animations = this._panel?.getAnimations() ?? [];
119
+ if (animations.length === 0) {
120
+ dialog.close();
121
+ return;
122
+ }
123
+ Promise.all(animations.map(animation => animation.finished))
124
+ .catch(() => undefined)
125
+ .then(() => {
126
+ if (!this.open && dialog.open) dialog.close();
127
+ });
128
+ }
129
+
72
130
  private _close(reason: string) {
73
131
  if (!this.open) return;
74
132
  this.open = false;
@@ -81,115 +139,109 @@ export class Modal extends LitElement {
81
139
  );
82
140
  }
83
141
 
84
- private _handleScrimClick() {
85
- if (!this.noScrimClose) {
86
- this._close('scrim');
87
- }
142
+ /** Escape: close through `open` so the property stays the source of truth. */
143
+ private _handleCancel(event: Event) {
144
+ event.preventDefault();
145
+ this._close('escape');
88
146
  }
89
147
 
90
- private _handleCloseClick() {
91
- this._close('close-button');
148
+ /** The browser closed the dialog by itself (for example a forced Escape): keep `open` in step. */
149
+ private _handleNativeClose() {
150
+ if (this.open) this._close('escape');
92
151
  }
93
152
 
94
- private readonly _handleKeydown = (e: KeyboardEvent) => {
95
- if (e.key === 'Escape') {
96
- this._close('escape');
97
- }
98
- };
99
-
100
- protected updated(changedProperties: Map<string, unknown>) {
101
- if (changedProperties.has('open')) {
102
- if (this.open) {
103
- this._visible = true;
104
- document.body.style.overflow = 'hidden';
105
- document.addEventListener('keydown', this._handleKeydown);
106
- } else {
107
- document.body.style.overflow = '';
108
- document.removeEventListener('keydown', this._handleKeydown);
109
- }
110
- }
153
+ /** A click that lands outside the dialog container, not on its content, is a scrim click. */
154
+ private _handleDialogClick(event: MouseEvent) {
155
+ const target = event.target as Element;
156
+ if (!target.classList?.contains('dialog-wrapper')) return;
157
+ if (!this.noScrimClose) this._close('scrim');
111
158
  }
112
159
 
113
- disconnectedCallback() {
114
- super.disconnectedCallback();
115
- document.body.style.overflow = '';
116
- document.removeEventListener('keydown', this._handleKeydown);
117
- }
118
-
119
- private _handleAnimationEnd(e: AnimationEvent) {
120
- if (e.animationName === 'modal-exit') {
121
- this._visible = false;
122
- }
160
+ private _handleCloseClick() {
161
+ this._close('close-button');
123
162
  }
124
163
 
125
- render() {
126
- if (!this.open && !this._visible) return nothing;
164
+ // ── Render helpers ───────────────────────────────────────────────────────
127
165
 
166
+ private _renderHeader() {
128
167
  const hasHeader = this.heading || this.subheading || !this.hideClose;
168
+ if (!hasHeader) return nothing;
129
169
 
130
170
  return html`
131
- <div
132
- class=${classMap({ scrim: true, visible: this.open })}
133
- @click=${this._handleScrimClick}
134
- aria-hidden="true"
135
- ></div>
136
-
137
- <div class="dialog-wrapper" role="presentation">
138
- <div
139
- class=${classMap({
140
- dialog: true,
141
- open: this.open,
142
- [`size-${this.size}`]: true,
143
- 'show-loader': this.showLoader,
144
- })}
145
- role="dialog"
146
- aria-modal="true"
147
- aria-labelledby=${this.heading ? 'modal-heading' : nothing}
148
- @animationend=${this._handleAnimationEnd}
149
- >
150
- ${hasHeader
151
- ? html`
152
- <div class="dialog-header">
153
- <div class="dialog-heading-section">
154
- ${this.subheading
155
- ? html`<p class="dialog-subheading">${this.subheading}</p>`
156
- : nothing}
157
- ${this.heading
158
- ? html`<h2 id="modal-heading" class="dialog-heading">
159
- ${this.heading}
160
- </h2>`
161
- : nothing}
162
- </div>
163
- ${!this.hideClose
164
- ? html`<wc-icon-button
165
- class="dialog-close"
166
- variant="text"
167
- aria-label="Close dialog"
168
- @click=${this._handleCloseClick}
169
- >
170
- <wc-icon name="close"></wc-icon>
171
- </wc-icon-button>`
172
- : nothing}
173
- </div>
174
- `
171
+ <div class="dialog-header">
172
+ <div class="dialog-heading-section">
173
+ ${this.subheading
174
+ ? html`<p class="dialog-subheading">${this.subheading}</p>`
175
+ : nothing}
176
+ ${this.heading
177
+ ? html`<h2 id="modal-heading" class="dialog-heading">
178
+ ${this.heading}
179
+ </h2>`
175
180
  : nothing}
181
+ </div>
182
+ ${!this.hideClose
183
+ ? html`<wc-icon-button
184
+ class="dialog-close"
185
+ variant="text"
186
+ aria-label="Close dialog"
187
+ @click=${this._handleCloseClick}
188
+ >
189
+ <wc-icon name="close"></wc-icon>
190
+ </wc-icon-button>`
191
+ : nothing}
192
+ </div>
193
+ `;
194
+ }
176
195
 
177
- <div class="dialog-content">
178
- <slot></slot>
179
- </div>
196
+ // ── Render ───────────────────────────────────────────────────────────────
180
197
 
181
- <div class="dialog-footer">
182
- <slot name="footer"></slot>
198
+ render() {
199
+ // The scrim click has its keyboard equivalent in Escape, which the native dialog reports as
200
+ // `cancel`; lit-a11y does not count that as a key listener.
201
+ /* eslint-disable lit-a11y/click-events-have-key-events */
202
+ return html`
203
+ <dialog
204
+ class="native-dialog"
205
+ part="dialog"
206
+ aria-labelledby=${this.heading ? 'modal-heading' : nothing}
207
+ @cancel=${this._handleCancel}
208
+ @close=${this._handleNativeClose}
209
+ @click=${this._handleDialogClick}
210
+ >
211
+ <div
212
+ class=${classMap({ scrim: true, visible: this.open })}
213
+ aria-hidden="true"
214
+ ></div>
215
+
216
+ <div class="dialog-wrapper">
217
+ <div
218
+ class=${classMap({
219
+ dialog: true,
220
+ open: this.open,
221
+ [`size-${this.size}`]: true,
222
+ 'show-loader': this.showLoader,
223
+ })}
224
+ >
225
+ ${this._renderHeader()}
226
+
227
+ <div class="dialog-content">
228
+ <slot></slot>
229
+ </div>
230
+
231
+ <div class="dialog-footer">
232
+ <slot name="footer"></slot>
233
+ </div>
234
+
235
+ ${this.showLoader
236
+ ? html`<div class="dialog-loader" aria-hidden="true">
237
+ <div class="dialog-loader-scrim"></div>
238
+ <wc-spinner></wc-spinner>
239
+ </div>`
240
+ : nothing}
183
241
  </div>
184
-
185
- ${this.showLoader
186
- ? html`<div class="dialog-loader" aria-hidden="true">
187
- <div class="dialog-loader-scrim"></div>
188
- <wc-spinner></wc-spinner>
189
- </div>`
190
- : nothing}
191
242
  </div>
192
- </div>
243
+ </dialog>
193
244
  `;
245
+ /* eslint-enable lit-a11y/click-events-have-key-events */
194
246
  }
195
247
  }