@firstprateek/vastu 0.0.0-stage → 1.0.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.
Files changed (114) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +51 -2
  3. package/custom-elements.json +3046 -0
  4. package/dist/dev/all.d.ts +9 -0
  5. package/dist/dev/all.d.ts.map +1 -0
  6. package/dist/dev/all.js +10 -0
  7. package/dist/dev/all.js.map +1 -0
  8. package/dist/dev/button/button.d.ts +98 -0
  9. package/dist/dev/button/button.d.ts.map +1 -0
  10. package/dist/dev/button/button.js +292 -0
  11. package/dist/dev/button/button.js.map +1 -0
  12. package/dist/dev/button/index.d.ts +4 -0
  13. package/dist/dev/button/index.d.ts.map +1 -0
  14. package/dist/dev/button/index.js +5 -0
  15. package/dist/dev/button/index.js.map +1 -0
  16. package/dist/dev/checkbox/checkbox.d.ts +71 -0
  17. package/dist/dev/checkbox/checkbox.d.ts.map +1 -0
  18. package/dist/dev/checkbox/checkbox.js +232 -0
  19. package/dist/dev/checkbox/checkbox.js.map +1 -0
  20. package/dist/dev/checkbox/index.d.ts +3 -0
  21. package/dist/dev/checkbox/index.d.ts.map +1 -0
  22. package/dist/dev/checkbox/index.js +5 -0
  23. package/dist/dev/checkbox/index.js.map +1 -0
  24. package/dist/dev/core/define.d.ts +3 -0
  25. package/dist/dev/core/define.d.ts.map +1 -0
  26. package/dist/dev/core/define.js +6 -0
  27. package/dist/dev/core/define.js.map +1 -0
  28. package/dist/dev/core/dev-mode.d.ts +6 -0
  29. package/dist/dev/core/dev-mode.d.ts.map +1 -0
  30. package/dist/dev/core/dev-mode.js +6 -0
  31. package/dist/dev/core/dev-mode.js.map +1 -0
  32. package/dist/dev/core/focus-ring.d.ts +3 -0
  33. package/dist/dev/core/focus-ring.d.ts.map +1 -0
  34. package/dist/dev/core/focus-ring.js +14 -0
  35. package/dist/dev/core/focus-ring.js.map +1 -0
  36. package/dist/dev/core/form-control.d.ts +47 -0
  37. package/dist/dev/core/form-control.d.ts.map +1 -0
  38. package/dist/dev/core/form-control.js +75 -0
  39. package/dist/dev/core/form-control.js.map +1 -0
  40. package/dist/dev/core/warn.d.ts +12 -0
  41. package/dist/dev/core/warn.d.ts.map +1 -0
  42. package/dist/dev/core/warn.js +35 -0
  43. package/dist/dev/core/warn.js.map +1 -0
  44. package/dist/dev/dialog/dialog.d.ts +77 -0
  45. package/dist/dev/dialog/dialog.d.ts.map +1 -0
  46. package/dist/dev/dialog/dialog.js +253 -0
  47. package/dist/dev/dialog/dialog.js.map +1 -0
  48. package/dist/dev/dialog/index.d.ts +4 -0
  49. package/dist/dev/dialog/index.d.ts.map +1 -0
  50. package/dist/dev/dialog/index.js +5 -0
  51. package/dist/dev/dialog/index.js.map +1 -0
  52. package/dist/dev/menu/index.d.ts +3 -0
  53. package/dist/dev/menu/index.d.ts.map +1 -0
  54. package/dist/dev/menu/index.js +5 -0
  55. package/dist/dev/menu/index.js.map +1 -0
  56. package/dist/dev/menu/menu.d.ts +71 -0
  57. package/dist/dev/menu/menu.d.ts.map +1 -0
  58. package/dist/dev/menu/menu.js +330 -0
  59. package/dist/dev/menu/menu.js.map +1 -0
  60. package/dist/dev/switch/index.d.ts +3 -0
  61. package/dist/dev/switch/index.d.ts.map +1 -0
  62. package/dist/dev/switch/index.js +5 -0
  63. package/dist/dev/switch/index.js.map +1 -0
  64. package/dist/dev/switch/switch.d.ts +69 -0
  65. package/dist/dev/switch/switch.d.ts.map +1 -0
  66. package/dist/dev/switch/switch.js +213 -0
  67. package/dist/dev/switch/switch.js.map +1 -0
  68. package/dist/dev/tabs/index.d.ts +3 -0
  69. package/dist/dev/tabs/index.d.ts.map +1 -0
  70. package/dist/dev/tabs/index.js +5 -0
  71. package/dist/dev/tabs/index.js.map +1 -0
  72. package/dist/dev/tabs/tabs.d.ts +64 -0
  73. package/dist/dev/tabs/tabs.d.ts.map +1 -0
  74. package/dist/dev/tabs/tabs.js +212 -0
  75. package/dist/dev/tabs/tabs.js.map +1 -0
  76. package/dist/dev/text-field/index.d.ts +3 -0
  77. package/dist/dev/text-field/index.d.ts.map +1 -0
  78. package/dist/dev/text-field/index.js +5 -0
  79. package/dist/dev/text-field/index.js.map +1 -0
  80. package/dist/dev/text-field/text-field.d.ts +91 -0
  81. package/dist/dev/text-field/text-field.d.ts.map +1 -0
  82. package/dist/dev/text-field/text-field.js +267 -0
  83. package/dist/dev/text-field/text-field.js.map +1 -0
  84. package/dist/dev/toast/index.d.ts +4 -0
  85. package/dist/dev/toast/index.d.ts.map +1 -0
  86. package/dist/dev/toast/index.js +5 -0
  87. package/dist/dev/toast/index.js.map +1 -0
  88. package/dist/dev/toast/toast.d.ts +71 -0
  89. package/dist/dev/toast/toast.d.ts.map +1 -0
  90. package/dist/dev/toast/toast.js +258 -0
  91. package/dist/dev/toast/toast.js.map +1 -0
  92. package/dist/prod/all.js +10 -0
  93. package/dist/prod/button/button.js +292 -0
  94. package/dist/prod/button/index.js +5 -0
  95. package/dist/prod/checkbox/checkbox.js +232 -0
  96. package/dist/prod/checkbox/index.js +5 -0
  97. package/dist/prod/core/define.js +6 -0
  98. package/dist/prod/core/dev-mode.js +1 -0
  99. package/dist/prod/core/focus-ring.js +14 -0
  100. package/dist/prod/core/form-control.js +75 -0
  101. package/dist/prod/core/warn.js +2 -0
  102. package/dist/prod/dialog/dialog.js +253 -0
  103. package/dist/prod/dialog/index.js +5 -0
  104. package/dist/prod/menu/index.js +5 -0
  105. package/dist/prod/menu/menu.js +330 -0
  106. package/dist/prod/switch/index.js +5 -0
  107. package/dist/prod/switch/switch.js +213 -0
  108. package/dist/prod/tabs/index.js +5 -0
  109. package/dist/prod/tabs/tabs.js +212 -0
  110. package/dist/prod/text-field/index.js +5 -0
  111. package/dist/prod/text-field/text-field.js +267 -0
  112. package/dist/prod/toast/index.js +5 -0
  113. package/dist/prod/toast/toast.js +258 -0
  114. package/package.json +95 -3
@@ -0,0 +1,232 @@
1
+ import { css, html, nothing, svg } from 'lit';
2
+ import { FormControl } from '../core/form-control.js';
3
+ import { checkTokensLoaded, devWarn } from '../core/warn.js';
4
+ /**
5
+ * A checkbox for a choice that is submitted with a form.
6
+ *
7
+ * @tag va-checkbox
8
+ * @summary Lets the user choose an option, or agree to something, as part of a form.
9
+ *
10
+ * @slot - The visible label. Clicking it toggles the checkbox.
11
+ * @slot help-text - Optional help text under the label. It becomes the checkbox's description.
12
+ *
13
+ * @csspart control - The square box.
14
+ * @csspart label - The label text.
15
+ * @csspart help-text - The help text.
16
+ *
17
+ * @cssprop {<color>} --va-checkbox-border - Border colour of the box when unchecked.
18
+ * @cssprop {<color>} --va-checkbox-bg-checked - Fill and border colour when checked or indeterminate.
19
+ * @cssprop {<color>} --va-checkbox-mark-fg - Colour of the tick or dash.
20
+ * @cssprop {<length>} --va-checkbox-size - Width and height of the box.
21
+ *
22
+ * @cssprop-applies --va-checkbox-border control:unchecked border-top-color
23
+ * @cssprop-applies --va-checkbox-bg-checked control:checked background-color
24
+ * @cssprop-applies --va-checkbox-mark-fg control:checked color
25
+ * @cssprop-applies --va-checkbox-size control width
26
+ *
27
+ * @keyboard Space | toggles | Checks or unchecks the box.
28
+ * @keyboard Tab | focus-next | Moves focus to the next control.
29
+ *
30
+ * @a11y-role checkbox
31
+ * @a11y-name-from text, accessible-label
32
+ * @replaces input[type="checkbox"]
33
+ * @a11y-name The slotted label text. Use `accessible-label` only when there is no visible label.
34
+ * @a11y-note `indeterminate` is announced as "mixed", for a box that stands for a partly selected group.
35
+ * @a11y-note Style states with `va-checkbox:state(checked)` and `:state(indeterminate)`; the `checked` attribute is the initial state only, as on a native checkbox.
36
+ *
37
+ * @guidance-use For an option submitted with a form, for agreeing to terms, or to select items in a list.
38
+ * @guidance-avoid For a setting that should apply immediately.
39
+ * @guidance-instead `va-switch` for settings that take effect immediately.
40
+ *
41
+ * @fires change - When the user checks or unchecks the box. Read `event.target.checked`.
42
+ * @fires input - When the user checks or unchecks the box, just before `change`.
43
+ * @event-trigger change | click
44
+ * @event-trigger input | click
45
+ */
46
+ export class VaCheckbox extends FormControl {
47
+ static { this.shadowRootOptions = { ...FormControl.shadowRootOptions, delegatesFocus: true }; }
48
+ static { this.properties = {
49
+ checked: { type: Boolean },
50
+ indeterminate: { type: Boolean },
51
+ value: {},
52
+ accessibleLabel: { attribute: 'accessible-label' },
53
+ }; }
54
+ #defaultChecked;
55
+ constructor() {
56
+ super();
57
+ this.checked = false;
58
+ this.indeterminate = false;
59
+ this.value = 'on';
60
+ this.accessibleLabel = '';
61
+ }
62
+ connectedCallback() {
63
+ super.connectedCallback();
64
+ // The default is the initial attribute, read once: moving the element must not change it.
65
+ this.#defaultChecked ??= this.hasAttribute('checked');
66
+ checkTokensLoaded(this);
67
+ }
68
+ firstUpdated() {
69
+ requestAnimationFrame(() => {
70
+ const text = this.#labelText();
71
+ if (!text && !this.accessibleLabel) {
72
+ devWarn('va-checkbox', 'This checkbox has no accessible name.', 'Put label text inside it, or add accessible-label="…" when there is no room for one (e.g. accessible-label="Select row").');
73
+ }
74
+ else if (text && this.accessibleLabel && !this.accessibleLabel.toLowerCase().includes(text.toLowerCase())) {
75
+ devWarn('va-checkbox', `accessible-label="${this.accessibleLabel}" replaces the visible label "${text}" for screen readers (WCAG 2.5.3).`, 'Remove accessible-label: the visible text is the name.');
76
+ }
77
+ });
78
+ }
79
+ /** The visible label: text not in the help-text slot. */
80
+ #labelText() {
81
+ return [...this.childNodes]
82
+ .filter((n) => !(n instanceof Element && n.slot))
83
+ .map((n) => n.textContent ?? '')
84
+ .join('')
85
+ .trim();
86
+ }
87
+ updated() {
88
+ this._setState('checked', this.checked);
89
+ this._setState('indeterminate', this.indeterminate);
90
+ this._internals.setFormValue(this.checked ? this.value : null);
91
+ const missing = this.required && !this.checked;
92
+ this._internals.setValidity(missing ? { valueMissing: true } : {}, missing ? 'Check this box to continue.' : '', this.shadowRoot?.querySelector('input') ?? undefined);
93
+ }
94
+ /** @internal */
95
+ formResetCallback() {
96
+ this.checked = this.#defaultChecked ?? false;
97
+ }
98
+ // The inner checkbox's native input event crosses the shadow root; the host fires its own instead.
99
+ #stopNative(event) {
100
+ event.stopPropagation();
101
+ }
102
+ #onChange(event) {
103
+ const input = event.target;
104
+ this.checked = input.checked;
105
+ this.indeterminate = false;
106
+ this._emit('input');
107
+ this._emit('change');
108
+ }
109
+ render() {
110
+ const mark = this.indeterminate
111
+ ? svg `<path d="M4 8h8" />`
112
+ : this.checked
113
+ ? svg `<path d="M3.5 8.5l3 3 6-7" />`
114
+ : nothing;
115
+ return html `
116
+ <label class="wrapper ${this._isDisabled ? 'disabled' : ''}">
117
+ <span class="control-wrap">
118
+ <input
119
+ class="native"
120
+ type="checkbox"
121
+ .checked=${this.checked}
122
+ .indeterminate=${this.indeterminate}
123
+ ?disabled=${this._isDisabled}
124
+ ?required=${this.required}
125
+ aria-label=${this.accessibleLabel || nothing}
126
+ aria-labelledby=${this.accessibleLabel ? nothing : 'label'}
127
+ aria-describedby="help-text"
128
+ @input=${this.#stopNative}
129
+ @change=${this.#onChange}
130
+ />
131
+ <span part="control" class="box ${this.checked || this.indeterminate ? 'on' : ''}" aria-hidden="true">
132
+ <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">${mark}</svg>
133
+ </span>
134
+ </span>
135
+ <span class="text">
136
+ <span part="label" id="label" class="label"><slot></slot></span>
137
+ <span part="help-text" id="help-text" class="help-text"><slot name="help-text"></slot></span>
138
+ </span>
139
+ </label>
140
+ `;
141
+ }
142
+ static { this.styles = css `
143
+ :host {
144
+ --_size: var(--va-checkbox-size, 1.125rem);
145
+ display: inline-flex;
146
+ font-family: var(--va-font-family-ui);
147
+ font-size: var(--va-font-size-md);
148
+ color: var(--va-color-text-default);
149
+ vertical-align: middle;
150
+ }
151
+ :host([hidden]) {
152
+ display: none;
153
+ }
154
+ .wrapper {
155
+ display: inline-flex;
156
+ align-items: flex-start;
157
+ gap: var(--va-space-inline-sm);
158
+ cursor: pointer;
159
+ /* Keeps the whole row at least 24px high for the WCAG 2.2 target size. */
160
+ min-height: 1.5rem;
161
+ padding-block: 0.1875rem;
162
+ box-sizing: border-box;
163
+ }
164
+ .control-wrap {
165
+ position: relative;
166
+ flex: none;
167
+ display: inline-flex;
168
+ }
169
+ /* The real checkbox covers the visible box, invisibly, so pointers and automation hit it directly. */
170
+ .native {
171
+ position: absolute;
172
+ inset: 0;
173
+ width: 100%;
174
+ height: 100%;
175
+ margin: 0;
176
+ opacity: 0;
177
+ cursor: inherit;
178
+ z-index: 1;
179
+ }
180
+ .box {
181
+ flex: none;
182
+ box-sizing: border-box;
183
+ display: grid;
184
+ place-items: center;
185
+ width: var(--_size);
186
+ height: var(--_size);
187
+ margin-top: 0.1rem;
188
+ border: 1.5px solid var(--va-checkbox-border, var(--va-color-border-control));
189
+ border-radius: var(--va-radius-control);
190
+ background-color: var(--va-color-surface-default);
191
+ color: var(--va-checkbox-mark-fg, var(--va-color-text-on-accent));
192
+ }
193
+ .box svg {
194
+ width: 100%;
195
+ height: 100%;
196
+ }
197
+ .box.on {
198
+ border-color: var(--va-checkbox-bg-checked, var(--va-color-accent-default));
199
+ background-color: var(--va-checkbox-bg-checked, var(--va-color-accent-default));
200
+ }
201
+ .native:focus-visible + .box {
202
+ outline: var(--va-size-focus-ring, 2px) solid var(--va-color-focus);
203
+ outline-offset: 2px;
204
+ }
205
+ .wrapper.disabled {
206
+ opacity: 0.5;
207
+ cursor: not-allowed;
208
+ }
209
+ .text {
210
+ display: flex;
211
+ flex-direction: column;
212
+ }
213
+ .help-text {
214
+ color: var(--va-color-text-muted);
215
+ font-size: var(--va-font-size-sm);
216
+ }
217
+ @media (forced-colors: active) {
218
+ .box {
219
+ border-color: ButtonText;
220
+ }
221
+ .box.on {
222
+ background-color: Highlight;
223
+ color: HighlightText;
224
+ forced-color-adjust: none;
225
+ }
226
+ .native:focus-visible + .box {
227
+ outline-color: Highlight;
228
+ }
229
+ }
230
+ `; }
231
+ }
232
+ //# sourceMappingURL=checkbox.js.map
@@ -0,0 +1,5 @@
1
+ import { define } from '../core/define.js';
2
+ import { VaCheckbox } from './checkbox.js';
3
+ export { VaCheckbox };
4
+ define('va-checkbox', VaCheckbox);
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ /** Defines a custom element once. A second definition of the same tag is a no-op, not an error. */
2
+ export function define(tag, ctor) {
3
+ if (!customElements.get(tag))
4
+ customElements.define(tag, ctor);
5
+ }
6
+ //# sourceMappingURL=define.js.map
@@ -0,0 +1 @@
1
+ export const DEV_MODE = false;
@@ -0,0 +1,14 @@
1
+ import { css } from 'lit';
2
+ /** The shared keyboard focus ring: visible only for keyboard focus, drawn outside the control. */
3
+ export const focusRing = css `
4
+ :focus-visible {
5
+ outline: var(--va-size-focus-ring, 2px) solid var(--va-color-focus, currentColor);
6
+ outline-offset: 2px;
7
+ }
8
+ @media (forced-colors: active) {
9
+ :focus-visible {
10
+ outline-color: Highlight;
11
+ }
12
+ }
13
+ `;
14
+ //# sourceMappingURL=focus-ring.js.map
@@ -0,0 +1,75 @@
1
+ import { LitElement } from 'lit';
2
+ /**
3
+ * Base class for Vastu form controls. It joins the control to its `<form>` through
4
+ * ElementInternals, so the control submits, resets, disables and validates like a native one.
5
+ * Subclasses call `setFormValue()` and `setValidity()`; everything else is handled here.
6
+ */
7
+ export class FormControl extends LitElement {
8
+ /** @internal */
9
+ static { this.formAssociated = true; }
10
+ static { this.properties = {
11
+ name: {},
12
+ disabled: { type: Boolean, reflect: true },
13
+ required: { type: Boolean, reflect: true },
14
+ _fieldsetDisabled: { state: true },
15
+ }; }
16
+ constructor() {
17
+ super();
18
+ this.name = '';
19
+ this.disabled = false;
20
+ this.required = false;
21
+ this._fieldsetDisabled = false;
22
+ this._internals = this.attachInternals();
23
+ }
24
+ /** The form this control belongs to, if any. */
25
+ get form() {
26
+ return this._internals.form;
27
+ }
28
+ /** The control's validity state, as on native controls. */
29
+ get validity() {
30
+ return this._internals.validity;
31
+ }
32
+ /** The message the browser shows when the control is invalid, or an empty string. */
33
+ get validationMessage() {
34
+ return this._internals.validationMessage;
35
+ }
36
+ /** Returns true if the control is valid; otherwise fires `invalid` and returns false. */
37
+ checkValidity() {
38
+ return this._internals.checkValidity();
39
+ }
40
+ /** Like `checkValidity()`, but also shows the browser's message to the user. */
41
+ reportValidity() {
42
+ return this._internals.reportValidity();
43
+ }
44
+ /**
45
+ * @internal Called by the browser when a `<fieldset disabled>` around the control changes.
46
+ * Like a native control, the author's own `disabled` is left alone: re-enabling the fieldset
47
+ * doesn't enable a control the author disabled.
48
+ */
49
+ formDisabledCallback(disabled) {
50
+ this._fieldsetDisabled = disabled;
51
+ }
52
+ /** Whether the control is disabled, by its own `disabled` or by a disabled fieldset. */
53
+ get _isDisabled() {
54
+ return this.disabled || this._fieldsetDisabled;
55
+ }
56
+ /** Sets or clears a custom state, for authors' :state(…) selectors. */
57
+ _setState(name, on) {
58
+ try {
59
+ if (on)
60
+ this._internals.states.add(name);
61
+ else
62
+ this._internals.states.delete(name);
63
+ }
64
+ catch {
65
+ // Browsers without CustomStateSet: the state is only for authors' styling.
66
+ }
67
+ }
68
+ /** @internal Called by the browser when the form resets. Subclasses restore their default. */
69
+ formResetCallback() { }
70
+ /** Fires a bubbling event on the host, as a native control would. */
71
+ _emit(type) {
72
+ this.dispatchEvent(new Event(type, { bubbles: true, composed: type === 'input' }));
73
+ }
74
+ }
75
+ //# sourceMappingURL=form-control.js.map
@@ -0,0 +1,2 @@
1
+ export function devWarn() {}
2
+ export function checkTokensLoaded() {}
@@ -0,0 +1,253 @@
1
+ import { LitElement, css, html, nothing } from 'lit';
2
+ import { focusRing } from '../core/focus-ring.js';
3
+ import { checkTokensLoaded, devWarn } from '../core/warn.js';
4
+ /**
5
+ * A modal dialog built on the native `<dialog>`: it sits in the top layer, makes the page behind it
6
+ * inert, keeps focus inside, closes with Escape and returns focus to where it came from.
7
+ *
8
+ * @tag va-dialog
9
+ * @summary Asks the user to confirm, decide or enter something before going back to the page.
10
+ *
11
+ * @slot - The dialog's content.
12
+ * @slot footer - Actions, such as Cancel and Confirm buttons. Put the primary action last.
13
+ *
14
+ * @csspart dialog - The native dialog element.
15
+ * @csspart header - The row with the heading and the close button.
16
+ * @csspart heading - The heading.
17
+ * @csspart close-button - The close button.
18
+ * @csspart body - The wrapper around the content.
19
+ * @csspart footer - The wrapper around the actions.
20
+ *
21
+ * @cssprop {<color>} --va-dialog-bg - Background colour.
22
+ * @cssprop {<length>} --va-dialog-width - Maximum width.
23
+ * @cssprop {<length>} --va-dialog-radius - Corner radius.
24
+ *
25
+ * @cssprop-applies --va-dialog-bg dialog:open background-color
26
+ * @cssprop-applies --va-dialog-width dialog:open max-width
27
+ * @cssprop-applies --va-dialog-radius dialog:open border-top-left-radius
28
+ *
29
+ * @keyboard open: Escape | closes | Closes the dialog and returns focus to the element that opened it.
30
+ *
31
+ * @a11y-role dialog
32
+ * @a11y-name-from heading, accessible-label
33
+ * @replaces dialog
34
+ * @a11y-name The `heading`, or `accessible-label` when there is no visible heading.
35
+ * @a11y-note The page behind is inert while the dialog is open, so focus and screen readers stay inside it.
36
+ * @a11y-note On open, focus goes to the first element with `autofocus`, else the first focusable control in the content, else the close button.
37
+ *
38
+ * @guidance-use For a decision the user must make before going on, such as confirming a deletion, or a short focused task.
39
+ * @guidance-avoid For information that doesn't need a response, or for long, multi-step flows.
40
+ * @guidance-instead `va-toast` for confirmations that need no response; a full page for long flows.
41
+ *
42
+ * @command --show | Opens the dialog.
43
+ * @command --close | Closes the dialog (fires `va-close` first).
44
+ * @fires va-open - After the dialog has opened.
45
+ * @fires {CustomEvent<{ reason: 'escape' | 'close-button' | 'command' | 'method' }>} va-close - Before the dialog closes, when the user or close() asks it to. Cancel it with `preventDefault()` to keep the dialog open.
46
+ * @fires {CustomEvent<{ reason: 'escape' | 'close-button' | 'command' | 'method' | 'property' }>} va-closed - After the dialog has closed, however it closed (`property` when `open` was set to false). Focus is back on the element that opened it.
47
+ * @event-trigger va-open | call show
48
+ * @event-trigger va-close | press Escape | open
49
+ * @event-trigger va-closed | press Escape | open
50
+ */
51
+ export class VaDialog extends LitElement {
52
+ static { this.properties = {
53
+ open: { type: Boolean, reflect: true },
54
+ heading: {},
55
+ accessibleLabel: { attribute: 'accessible-label' },
56
+ hideCloseButton: { type: Boolean, reflect: true, attribute: 'hide-close-button' },
57
+ }; }
58
+ #returnFocus = null;
59
+ #shown = false;
60
+ #closeReason = null;
61
+ constructor() {
62
+ super();
63
+ this.open = false;
64
+ this.heading = '';
65
+ this.accessibleLabel = '';
66
+ this.hideCloseButton = false;
67
+ this.addEventListener('command', this.#onCommand);
68
+ }
69
+ connectedCallback() {
70
+ super.connectedCallback();
71
+ checkTokensLoaded(this);
72
+ }
73
+ get #dialog() {
74
+ return this.shadowRoot?.querySelector('dialog') ?? null;
75
+ }
76
+ /** Opens the dialog as a modal. */
77
+ show() {
78
+ this.open = true;
79
+ }
80
+ /** Closes the dialog, unless a `va-close` listener cancels it. */
81
+ close() {
82
+ this.#requestClose('method');
83
+ }
84
+ #requestClose(reason) {
85
+ if (!this.open)
86
+ return;
87
+ const event = new CustomEvent('va-close', { detail: { reason }, bubbles: true, composed: true, cancelable: true });
88
+ if (!this.dispatchEvent(event))
89
+ return;
90
+ this.#closeReason = reason;
91
+ this.open = false;
92
+ }
93
+ #onCommand = (event) => {
94
+ if (event.command === '--show' || event.command === 'show-modal') {
95
+ // Safari doesn't focus a button on click, so remember the button that sent the command.
96
+ if (event.source instanceof HTMLElement)
97
+ this.#returnFocus = event.source;
98
+ this.show();
99
+ }
100
+ else if (event.command === '--close' || event.command === 'close')
101
+ this.#requestClose('command');
102
+ };
103
+ async updated(changed) {
104
+ if (!changed.has('open'))
105
+ return;
106
+ const dialog = this.#dialog;
107
+ if (!dialog)
108
+ return;
109
+ if (this.open && !dialog.open) {
110
+ if (!this.#returnFocus) {
111
+ let active = document.activeElement;
112
+ while (active?.shadowRoot?.activeElement)
113
+ active = active.shadowRoot.activeElement;
114
+ this.#returnFocus = active;
115
+ }
116
+ if (!this.heading && !this.accessibleLabel) {
117
+ devWarn('va-dialog', 'This dialog has no accessible name.', 'Add heading="…" (shown) or accessible-label="…" (not shown) that says what the dialog is for.');
118
+ }
119
+ dialog.showModal();
120
+ this.#shown = true;
121
+ this.#focusInitial();
122
+ this.dispatchEvent(new Event('va-open', { bubbles: true, composed: true }));
123
+ }
124
+ else if (!this.open && this.#shown) {
125
+ if (dialog.open)
126
+ dialog.close();
127
+ this.#shown = false;
128
+ this.#returnFocus?.focus();
129
+ this.#returnFocus = null;
130
+ const reason = this.#closeReason ?? 'property';
131
+ this.#closeReason = null;
132
+ this.dispatchEvent(new CustomEvent('va-closed', { detail: { reason }, bubbles: true, composed: true }));
133
+ }
134
+ }
135
+ #focusInitial() {
136
+ const focusable = 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"]), va-button, va-text-field, va-checkbox, va-switch';
137
+ const auto = this.querySelector('[autofocus]');
138
+ const first = [...this.querySelectorAll(focusable)].find((el) => el.slot !== 'footer' && !el.hasAttribute('disabled'));
139
+ const target = auto ?? first ?? this.shadowRoot?.querySelector('.close') ?? this.#dialog;
140
+ target?.focus();
141
+ }
142
+ #onCancel(event) {
143
+ // Escape: route through va-close so listeners can keep the dialog open.
144
+ event.preventDefault();
145
+ this.#requestClose('escape');
146
+ }
147
+ #onNativeClose() {
148
+ // The browser closed the dialog itself (a repeated Escape, for example): keep `open` in step.
149
+ if (this.open) {
150
+ this.#closeReason = 'escape';
151
+ this.open = false;
152
+ }
153
+ }
154
+ render() {
155
+ return html `
156
+ <dialog
157
+ part="dialog"
158
+ aria-labelledby=${this.heading ? 'heading' : nothing}
159
+ aria-label=${!this.heading && this.accessibleLabel ? this.accessibleLabel : nothing}
160
+ @cancel=${this.#onCancel}
161
+ @close=${this.#onNativeClose}
162
+ >
163
+ <div part="header" class="header">
164
+ ${this.heading ? html `<h2 part="heading" id="heading" class="heading">${this.heading}</h2>` : html `<span part="heading" class="heading"></span>`}
165
+ ${!this.hideCloseButton
166
+ ? html `<button part="close-button" class="close" type="button" aria-label="Close" @click=${() => this.#requestClose('close-button')}>
167
+ <svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2"><path d="M6 6l12 12M18 6L6 18" /></svg>
168
+ </button>`
169
+ : nothing}
170
+ </div>
171
+ <div part="body" class="body"><slot></slot></div>
172
+ <div part="footer" class="footer"><slot name="footer"></slot></div>
173
+ </dialog>
174
+ `;
175
+ }
176
+ static { this.styles = [
177
+ focusRing,
178
+ css `
179
+ :host {
180
+ display: contents;
181
+ }
182
+ dialog {
183
+ box-sizing: border-box;
184
+ width: calc(100% - 2rem);
185
+ max-width: var(--va-dialog-width, 32rem);
186
+ max-height: calc(100% - 2rem);
187
+ padding: 0;
188
+ border: 1px solid var(--va-color-border-subtle);
189
+ border-radius: var(--va-dialog-radius, var(--va-radius-surface));
190
+ background-color: var(--va-dialog-bg, var(--va-color-surface-default));
191
+ color: var(--va-color-text-default);
192
+ font-family: var(--va-font-family-ui);
193
+ box-shadow: 0 12px 40px rgb(0 0 0 / 0.25);
194
+ }
195
+ dialog::backdrop {
196
+ background: rgb(0 0 0 / 0.45);
197
+ }
198
+ .header {
199
+ display: flex;
200
+ align-items: flex-start;
201
+ justify-content: space-between;
202
+ gap: var(--va-space-inline-md);
203
+ padding: var(--va-space-inline-lg) var(--va-space-inline-lg) 0;
204
+ }
205
+ .heading {
206
+ margin: 0;
207
+ font-family: var(--va-font-family-reading);
208
+ font-size: 1.25rem;
209
+ font-weight: var(--va-font-weight-bold);
210
+ line-height: 1.3;
211
+ }
212
+ .close {
213
+ flex: none;
214
+ display: grid;
215
+ place-items: center;
216
+ width: 2rem;
217
+ height: 2rem;
218
+ margin: -0.25rem -0.5rem 0 0;
219
+ padding: 0;
220
+ border: none;
221
+ border-radius: var(--va-radius-control);
222
+ background: transparent;
223
+ color: var(--va-color-text-muted);
224
+ cursor: pointer;
225
+ }
226
+ .close:hover {
227
+ background: var(--va-color-surface-subtle);
228
+ color: var(--va-color-text-default);
229
+ }
230
+ .close svg {
231
+ width: 1.25rem;
232
+ height: 1.25rem;
233
+ }
234
+ .body {
235
+ padding: var(--va-space-inline-md) var(--va-space-inline-lg);
236
+ line-height: 1.5;
237
+ }
238
+ .footer {
239
+ display: flex;
240
+ flex-wrap: wrap;
241
+ justify-content: flex-end;
242
+ gap: var(--va-space-gap-md);
243
+ padding: 0 var(--va-space-inline-lg) var(--va-space-inline-lg);
244
+ }
245
+ @media (forced-colors: active) {
246
+ dialog {
247
+ border: 2px solid CanvasText;
248
+ }
249
+ }
250
+ `,
251
+ ]; }
252
+ }
253
+ //# sourceMappingURL=dialog.js.map
@@ -0,0 +1,5 @@
1
+ import { define } from '../core/define.js';
2
+ import { VaDialog } from './dialog.js';
3
+ export { VaDialog };
4
+ define('va-dialog', VaDialog);
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,5 @@
1
+ import { define } from '../core/define.js';
2
+ import { VaMenu } from './menu.js';
3
+ export { VaMenu };
4
+ define('va-menu', VaMenu);
5
+ //# sourceMappingURL=index.js.map