@luxalgo/vela-ui 0.3.0 → 0.4.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.
Files changed (177) hide show
  1. package/CHANGELOG.md +165 -132
  2. package/LICENSE +201 -201
  3. package/NOTICE +28 -28
  4. package/README.md +86 -86
  5. package/cli/engine.mjs +282 -282
  6. package/cli/index.mjs +154 -154
  7. package/dist/button.js +2 -2
  8. package/dist/callout-bubble.js +3 -3
  9. package/dist/checkbox.js +3 -3
  10. package/dist/{chunk-OJ2SEMN6.js → chunk-5A54GODF.js} +2 -2
  11. package/dist/{chunk-UEO6W37Z.js → chunk-7LYWI32I.js} +2 -2
  12. package/dist/{chunk-IOOYDSSK.js → chunk-BIU66EHS.js} +2 -2
  13. package/dist/{chunk-KKS6NEGI.js → chunk-BWSOMG3G.js} +1 -1
  14. package/dist/{chunk-DXDCKZ2M.js → chunk-CZRHHVSB.js} +1 -1
  15. package/dist/{chunk-C3PXY2SW.js → chunk-EDYD5XKJ.js} +9 -0
  16. package/dist/chunk-GUIYFQ27.js +30 -0
  17. package/dist/{chunk-WWHNUKSZ.js → chunk-JEGB72M4.js} +8 -8
  18. package/dist/{chunk-B2EZB5ZG.js → chunk-ODPGOUQT.js} +4 -3
  19. package/dist/{chunk-TBXUF5YX.js → chunk-RH53NQGE.js} +18 -4
  20. package/dist/{chunk-OQXFQMZM.js → chunk-TGJNHUYV.js} +2 -2
  21. package/dist/collapsible.js +2 -2
  22. package/dist/color-picker.js +4 -4
  23. package/dist/context-menu.js +4 -4
  24. package/dist/date-picker.js +1 -1
  25. package/dist/dialog.js +4 -4
  26. package/dist/donut.d.ts +1 -1
  27. package/dist/drawer.js +1 -1
  28. package/dist/dropdown-menu.d.ts +2 -0
  29. package/dist/dropdown-menu.js +4 -4
  30. package/dist/empty.js +1 -1
  31. package/dist/field.js +9 -9
  32. package/dist/{stat.d.ts → figure.d.ts} +9 -9
  33. package/dist/figure.js +41 -0
  34. package/dist/glyph-select.js +4 -4
  35. package/dist/hover-card.js +1 -1
  36. package/dist/icons.js +1 -1
  37. package/dist/index.d.ts +2 -2
  38. package/dist/index.js +1 -1
  39. package/dist/input-group.js +2 -2
  40. package/dist/item.js +1 -1
  41. package/dist/manifest.json +75 -74
  42. package/dist/number-input.js +3 -3
  43. package/dist/popover.js +2 -2
  44. package/dist/select.d.ts +5 -1
  45. package/dist/select.js +5 -5
  46. package/dist/slider.js +1 -1
  47. package/dist/sort-button.js +2 -2
  48. package/dist/switch.js +1 -1
  49. package/dist/tabs.js +2 -2
  50. package/dist/{theme-Ct-q58xz.d.ts → theme-TefB8vtl.d.ts} +2 -2
  51. package/dist/theme.d.ts +2 -2
  52. package/dist/theme.js +1 -1
  53. package/dist/theme.schema.json +30 -30
  54. package/dist/toast.d.ts +2 -2
  55. package/dist/toast.js +2 -2
  56. package/dist/toggle-group.js +2 -2
  57. package/dist/tooltip.js +1 -1
  58. package/dist/zag.d.ts +11 -2
  59. package/dist/zag.js +1 -1
  60. package/docs/theming.md +217 -217
  61. package/docs/what-to-change.md +20 -20
  62. package/docs/writing-a-component.md +250 -250
  63. package/llms.txt +3 -3
  64. package/package.json +90 -90
  65. package/registry/badge.ts +119 -119
  66. package/registry/bar-chart.ts +215 -215
  67. package/registry/button.ts +387 -387
  68. package/registry/callout-bubble.ts +347 -347
  69. package/registry/card.ts +379 -379
  70. package/registry/checkbox.ts +234 -234
  71. package/registry/collapsible.ts +271 -271
  72. package/registry/color-picker.ts +582 -582
  73. package/registry/context-menu.ts +208 -208
  74. package/registry/data-table.ts +215 -215
  75. package/registry/date-picker.ts +483 -483
  76. package/registry/dialog.ts +551 -551
  77. package/registry/donut.ts +145 -145
  78. package/registry/drawer.ts +636 -636
  79. package/registry/dropdown-menu.ts +765 -762
  80. package/registry/empty.ts +91 -91
  81. package/registry/field.ts +584 -584
  82. package/registry/{stat.ts → figure.ts} +96 -96
  83. package/registry/glyph-select.ts +268 -268
  84. package/registry/hover-card.ts +222 -222
  85. package/registry/input-group.ts +229 -229
  86. package/registry/input.ts +181 -181
  87. package/registry/item.ts +376 -376
  88. package/registry/kbd.ts +64 -64
  89. package/registry/line-chart.ts +218 -218
  90. package/registry/month-grid.ts +328 -328
  91. package/registry/number-input.ts +443 -443
  92. package/registry/popover.ts +368 -368
  93. package/registry/scatter-chart.ts +221 -221
  94. package/registry/select.ts +629 -620
  95. package/registry/slider.ts +259 -259
  96. package/registry/sort-button.ts +189 -189
  97. package/registry/sparkline.ts +114 -114
  98. package/registry/switch.ts +205 -205
  99. package/registry/tabs.ts +426 -426
  100. package/registry/text-area.ts +186 -186
  101. package/registry/toast.ts +965 -965
  102. package/registry/toggle-group.ts +295 -295
  103. package/registry/tooltip.ts +156 -156
  104. package/registry/treemap.ts +847 -847
  105. package/skills/theming-vela-ui/SKILL.md +137 -137
  106. package/skills/using-vela-ui-components/SKILL.md +141 -139
  107. package/skills/writing-vela-ui-components/SKILL.md +163 -163
  108. package/src/components/badge.ts +119 -119
  109. package/src/components/bar-chart.ts +215 -215
  110. package/src/components/button.ts +387 -387
  111. package/src/components/callout-bubble.ts +347 -347
  112. package/src/components/card.ts +379 -379
  113. package/src/components/checkbox.ts +234 -234
  114. package/src/components/collapsible.ts +271 -271
  115. package/src/components/color-picker.ts +582 -582
  116. package/src/components/context-menu.ts +208 -208
  117. package/src/components/data-table.ts +215 -215
  118. package/src/components/date-picker.ts +483 -483
  119. package/src/components/dialog.ts +551 -551
  120. package/src/components/donut.ts +145 -145
  121. package/src/components/drawer.ts +636 -636
  122. package/src/components/dropdown-menu.ts +765 -762
  123. package/src/components/empty.ts +91 -91
  124. package/src/components/field.ts +584 -584
  125. package/src/components/{stat.ts → figure.ts} +96 -96
  126. package/src/components/glyph-select.ts +268 -268
  127. package/src/components/hover-card.ts +222 -222
  128. package/src/components/input-group.ts +229 -229
  129. package/src/components/input.ts +181 -181
  130. package/src/components/item.ts +376 -376
  131. package/src/components/kbd.ts +64 -64
  132. package/src/components/line-chart.ts +218 -218
  133. package/src/components/month-grid.ts +328 -328
  134. package/src/components/number-input.ts +443 -443
  135. package/src/components/popover.ts +368 -368
  136. package/src/components/scatter-chart.ts +221 -221
  137. package/src/components/select.ts +629 -620
  138. package/src/components/slider.ts +259 -259
  139. package/src/components/sort-button.ts +189 -189
  140. package/src/components/sparkline.ts +114 -114
  141. package/src/components/switch.ts +205 -205
  142. package/src/components/tabs.ts +426 -426
  143. package/src/components/text-area.ts +186 -186
  144. package/src/components/toast.ts +965 -965
  145. package/src/components/toggle-group.ts +295 -295
  146. package/src/components/tooltip.ts +156 -156
  147. package/src/components/treemap.ts +847 -847
  148. package/src/index.ts +24 -24
  149. package/src/lib/calendar.ts +117 -117
  150. package/src/lib/chart.ts +72 -72
  151. package/src/lib/color.ts +89 -89
  152. package/src/lib/component-index.ts +163 -163
  153. package/src/lib/css.ts +48 -48
  154. package/src/lib/define.ts +45 -45
  155. package/src/lib/dispose.ts +36 -36
  156. package/src/lib/h.ts +55 -55
  157. package/src/lib/headless.ts +16 -16
  158. package/src/lib/i18n.ts +30 -30
  159. package/src/lib/icons.ts +253 -244
  160. package/src/lib/kit-text.ts +26 -26
  161. package/src/lib/page.ts +24 -24
  162. package/src/lib/palette.ts +81 -81
  163. package/src/lib/placement.ts +188 -188
  164. package/src/lib/scope.ts +135 -135
  165. package/src/lib/styles.ts +53 -53
  166. package/src/lib/surface-events.ts +31 -31
  167. package/src/lib/surface-exit.ts +87 -87
  168. package/src/lib/t.ts +68 -68
  169. package/src/lib/theme.ts +684 -670
  170. package/src/lib/tokens.ts +110 -110
  171. package/src/lib/variants.ts +32 -32
  172. package/src/lib/zag.ts +61 -46
  173. package/stylelint/index.mjs +60 -60
  174. package/stylelint/lint.mjs +81 -81
  175. package/stylelint/plugin.mjs +115 -115
  176. package/dist/chunk-P4MTEMT7.js +0 -23
  177. package/dist/stat.js +0 -41
@@ -1,551 +1,551 @@
1
- // Dialog: a backdrop and a panel with a header and a body, over Zag's dialog machine (focus
2
- // trap, dismissal, ARIA). The body is the caller's: pass a Node, or fill `body` after
3
- // creation. `settings` and `form` are the chart settings and the indicator / drawing
4
- // settings looks. In a shell in the mobile layout every dialog fills the shell.
5
- import * as dialog from '@zag-js/dialog';
6
- import { iconEl } from '@luxalgo/vela-ui/icons';
7
- import { css, defineStyles } from '@luxalgo/vela-ui';
8
- import { defineComponent } from '@luxalgo/vela-ui';
9
- import { disposeWithin } from '@luxalgo/vela-ui/dispose';
10
- import { msg, textOf, type Translator } from '@luxalgo/vela-ui/i18n';
11
- import { kitTranslator } from '@luxalgo/vela-ui/kit-text';
12
- import { t } from '@luxalgo/vela-ui';
13
- import { injectStyles as injectSheet } from '@luxalgo/vela-ui/styles';
14
- import { announceSurface } from '@luxalgo/vela-ui/surface-events';
15
- import { holdForExit, type SurfaceExit } from '@luxalgo/vela-ui/surface-exit';
16
- import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '@luxalgo/vela-ui/zag';
17
- import { closeOpenPopovers, eventDismissedPopover, isPopoverOpen } from '@luxalgo/vela-ui/popover';
18
-
19
- export interface DialogControllerOptions {
20
- /** Trap focus + backdrop (default true). Non-modal = floating panel. */
21
- modal?: boolean;
22
- closeOnEscape?: boolean;
23
- closeOnInteractOutside?: boolean;
24
- /** Element to focus on open (default: the machine's first-tabbable pick). */
25
- initialFocusEl?: () => HTMLElement | null;
26
- onOpenChange?: (open: boolean) => void;
27
- }
28
-
29
- export type DialogService = dialog.Service;
30
- export type DialogApi = dialog.Api;
31
-
32
- export interface DialogController {
33
- machine: typeof dialog.machine;
34
- props: Partial<dialog.Props>;
35
- connect(service: DialogService): DialogApi;
36
- }
37
-
38
- /** Zag's dialog machine with the options mapped onto it, for any view layer. */
39
- export function dialogController(opts: DialogControllerOptions = {}): DialogController {
40
- return {
41
- machine: dialog.machine,
42
- props: {
43
- id: nextUid('vela-dialog'),
44
- modal: opts.modal ?? true,
45
- closeOnEscape: opts.closeOnEscape ?? true,
46
- closeOnInteractOutside: opts.closeOnInteractOutside ?? false,
47
- initialFocusEl: opts.initialFocusEl,
48
- onOpenChange: (d: dialog.OpenChangeDetails) => opts.onOpenChange?.(d.open),
49
- } satisfies Partial<dialog.Props>,
50
- connect: (service: DialogService): DialogApi => dialog.connect(service, normalizeProps),
51
- };
52
- }
53
-
54
- export const dialogDef = defineComponent({
55
- name: 'dialog',
56
- parts: ['backdrop', 'positioner', 'content', 'header', 'title', 'close', 'body', 'footer'],
57
- settings: { radius: t.radius.lg },
58
- });
59
-
60
- export type DialogVariant = 'default' | 'settings' | 'form';
61
-
62
- export interface DialogOptions extends DialogControllerOptions {
63
- title?: string;
64
- content?: Node | ((body: HTMLElement) => void);
65
- host?: HTMLElement;
66
- /** Drag the dialog by its header (the reference dialogs move; search stays fixed). */
67
- draggable?: boolean;
68
- /** Darken the page behind the dialog (default false — the chart stays readable while
69
- * dialogs edit live content). Pass true for a dimming scrim; the backdrop still
70
- * catches interact-outside dismissal either way. */
71
- dimBackdrop?: boolean;
72
- /** Vertical placement of the card. Default `top` (current 10vh pad). */
73
- align?: 'top' | 'center';
74
- /** Pin the overlay to `host` (absolute) instead of the viewport (fixed). */
75
- contained?: boolean;
76
- /** Footer pinned below the scrollable body. */
77
- footer?: Node | ((el: HTMLElement) => void);
78
- /** Extra nodes before the title (e.g. a mobile section burger). */
79
- headerStart?: Node;
80
- /** Skip default body padding — the caller owns the body layout. */
81
- flush?: boolean;
82
- /** `settings`: the chart settings card; `form`: the indicator and drawing settings card. */
83
- variant?: DialogVariant;
84
- /** Extra class on the panel. */
85
- className?: string;
86
- /** Close when the backdrop itself is pressed (not when a portaled popover is). */
87
- closeOnBackdrop?: boolean;
88
- /** The translator of the close button's name ("Close"). Default: the kit build's, else
89
- * English. */
90
- i18n?: Translator;
91
- }
92
-
93
- export type DialogProps = DialogOptions;
94
-
95
- const CLOSE = msg('ui.dialog.close', 'Close');
96
-
97
- const injectStyles = defineStyles(
98
- 'vela-ui-dialog-5',
99
- css`
100
- @layer vela.ui {
101
- [data-slot='dialog-backdrop'] {
102
- position: fixed;
103
- inset: 0;
104
- background: ${t.backdrop};
105
- z-index: ${t.z.dialog};
106
-
107
- /* Non-dimming variant: still catches outside clicks, but the page stays readable. */
108
- &[data-clear] {
109
- background: transparent;
110
- }
111
- &[data-contained] {
112
- position: absolute;
113
- }
114
- &:is([data-layout='mobile'] *) {
115
- position: absolute;
116
- }
117
- }
118
- [data-slot='dialog-positioner'] {
119
- position: fixed;
120
- inset: 0;
121
- display: flex;
122
- align-items: flex-start;
123
- justify-content: center;
124
- padding-top: 10vh;
125
- z-index: ${t.z.dialog};
126
-
127
- &[data-contained] {
128
- position: absolute;
129
- }
130
- &[data-align='center'] {
131
- align-items: center;
132
- padding-top: 0;
133
- }
134
- /* In a shell in the mobile size class every dialog fills the shell: desktop
135
- cards are unusable at phone widths, and the shell's bounds, not the
136
- viewport, are the honest "screen" for an embedded chart. */
137
- &:is([data-layout='mobile'] *) {
138
- position: absolute;
139
- padding: 0;
140
- align-items: stretch;
141
- }
142
- }
143
- [data-slot='dialog-header'] {
144
- display: flex;
145
- align-items: center;
146
- padding-block: 9px;
147
- padding-inline: 16px 9px;
148
- border-bottom: 1px solid ${t.border};
149
- user-select: none;
150
- }
151
- [data-slot='dialog-title'] {
152
- flex: 1;
153
- font-size: 17px;
154
- font-weight: 600;
155
- letter-spacing: 0.2px;
156
- color: ${t.fg.bright};
157
- }
158
- [data-slot='dialog-close'] {
159
- all: unset;
160
- cursor: pointer;
161
- width: 30px;
162
- height: 30px;
163
- display: flex;
164
- align-items: center;
165
- justify-content: center;
166
- border-radius: ${t.radius.sm};
167
- color: ${t.fg.muted};
168
- line-height: 1;
169
- font-size: 15px;
170
-
171
- &:hover {
172
- background: ${t.hover};
173
- color: ${t.fg.bright};
174
- }
175
- &:is([data-layout='mobile'] *) {
176
- width: 40px;
177
- height: 40px;
178
- }
179
- }
180
- [data-slot='dialog-body'] {
181
- padding: ${t.space[4]};
182
- overflow: auto;
183
- min-height: 0;
184
- flex: 1 1 auto;
185
-
186
- &[data-flush] {
187
- padding: 0;
188
- overflow: hidden;
189
- display: flex;
190
- flex-direction: column;
191
- }
192
- &:is([data-layout='mobile'] *) {
193
- padding-bottom: calc(${t.space[4]} + env(safe-area-inset-bottom, 0px));
194
- }
195
- }
196
- [data-slot='dialog-footer'] {
197
- flex: 0 0 auto;
198
- }
199
- [data-slot='dialog-content'] {
200
- background: ${t.surface};
201
- color: ${t.fg};
202
- border: 1px solid ${t.border.strong};
203
- border-radius: ${dialogDef.vars.radius};
204
- box-shadow: ${t.shadow.dialog};
205
- font-size: 13px;
206
- min-width: 300px;
207
- max-width: min(92vw, 560px);
208
- max-height: 70vh;
209
- display: flex;
210
- flex-direction: column;
211
- overflow: hidden;
212
- outline: 2px solid transparent;
213
-
214
- &[data-state='open'] {
215
- animation: vela-dialog-in ${t.dur.med} ${t.ease};
216
- }
217
- /* A plain dialog's footer: its buttons at the end, padded like the body above it.
218
- The settings and form variants lay theirs out themselves. */
219
- &[data-variant='default'] [data-slot='dialog-footer'] {
220
- display: flex;
221
- justify-content: flex-end;
222
- gap: ${t.space[2]};
223
- padding: 0 ${t.space[4]} ${t.space[4]};
224
- }
225
- &[data-variant='settings'] {
226
- width: fit-content;
227
- min-width: min(560px, 94vw);
228
- max-width: min(720px, 94vw);
229
- max-height: 70vh;
230
- font-size: 13px;
231
- cursor: default;
232
- }
233
- &[data-variant='form'] {
234
- width: fit-content;
235
- min-width: min(380px, 90%);
236
- max-width: min(640px, 94%);
237
- max-height: 82%;
238
- font-size: 14px;
239
- }
240
- /* Form dialogs (indicator inputs, drawing settings): the tab strip or the body
241
- owns the line under the header, and the footer carries its own top line. */
242
- &[data-variant='form'] [data-slot='dialog-header'] {
243
- align-items: flex-start;
244
- padding: 16px 20px 20px;
245
- border-bottom: none;
246
- }
247
- &[data-variant='form'] [data-slot='dialog-title'] {
248
- font-size: 20px;
249
- line-height: 28px;
250
- }
251
- &[data-variant='form'] [data-slot='dialog-footer'] {
252
- padding: 16px 20px;
253
- display: flex;
254
- justify-content: flex-end;
255
- gap: 12px;
256
- border-top: 1px solid ${t.border};
257
- }
258
- &:is([data-layout='mobile'] *) {
259
- width: 100%;
260
- min-width: 0;
261
- max-width: none;
262
- max-height: none;
263
- flex: 1 1 auto;
264
- border: none;
265
- border-radius: 0;
266
- }
267
- }
268
- @keyframes vela-dialog-in {
269
- from {
270
- opacity: 0;
271
- transform: translateY(6px) scale(0.98);
272
- }
273
- to {
274
- opacity: 1;
275
- transform: translateY(0) scale(1);
276
- }
277
- }
278
- }
279
- `,
280
- );
281
-
282
- /* A desktop drag offset (an inline transform) must not survive the flip to the mobile
283
- layout, so this one rule stays outside the layers and important. */
284
- const MOBILE_DRAG_CSS = "[data-layout='mobile'] .vela-dialog { transform: none !important; }";
285
-
286
- const VARIANT_CLASSES: Record<string, DialogVariant> = { 'vela-dialog--settings': 'settings', 'vela-dialog--form': 'form' };
287
-
288
- export class Dialog {
289
- /** Caller-owned content area — append your form/panel here. */
290
- readonly body: HTMLElement;
291
- readonly panel: HTMLElement;
292
- readonly titleEl: HTMLElement;
293
- readonly positioner: HTMLElement;
294
- readonly backdrop: HTMLElement;
295
- readonly footer: HTMLElement | null;
296
- private readonly closeButton: HTMLElement;
297
- private readonly handle: HandleOf<typeof dialog.machine>;
298
- private readonly ctrl: DialogController;
299
-
300
- constructor(opts: DialogOptions = {}) {
301
- const doc = (opts.host ?? document.body).ownerDocument;
302
- injectStyles(doc);
303
- injectSheet('vela-ui-dialog-mobile', MOBILE_DRAG_CSS, doc);
304
- const host = opts.host ?? doc.body;
305
- const part = (el: HTMLElement, name: (typeof dialogDef.parts)[number]): HTMLElement => {
306
- el.dataset.slot = dialogDef.slot(name);
307
- return el;
308
- };
309
-
310
- this.backdrop = part(doc.createElement('div'), 'backdrop');
311
- this.backdrop.className = 'vela-dialog-backdrop vela-ui-layer';
312
- if (opts.dimBackdrop !== true) {
313
- this.backdrop.classList.add('vela-dialog-backdrop--clear');
314
- this.backdrop.dataset.clear = '';
315
- }
316
- this.positioner = part(doc.createElement('div'), 'positioner');
317
- this.positioner.className = 'vela-dialog-positioner vela-ui-layer';
318
- if (opts.contained) {
319
- this.backdrop.dataset.contained = '';
320
- this.positioner.dataset.contained = '';
321
- }
322
- this.positioner.dataset.align = opts.align ?? 'top';
323
- this.panel = part(doc.createElement('div'), 'content');
324
- this.panel.className = 'vela-dialog';
325
- const classes = (opts.className ?? '').split(/\s+/).filter(Boolean);
326
- const variant = opts.variant ?? classes.map((c) => VARIANT_CLASSES[c]).find((v) => v !== undefined) ?? 'default';
327
- this.panel.dataset.variant = variant;
328
- if (variant !== 'default') this.panel.classList.add(`vela-dialog--${variant}`);
329
- for (const c of classes) this.panel.classList.add(c);
330
- // Programmatically focusable — the mobile chrome routes the machine's initial
331
- // focus here (see the controller wiring below).
332
- this.panel.tabIndex = -1;
333
- this.panel.addEventListener('keydown', (e) => {
334
- if (e.key !== 'Escape') e.stopPropagation();
335
- });
336
-
337
- const header = part(doc.createElement('div'), 'header');
338
- header.className = 'vela-dialog-header';
339
- if (opts.draggable) {
340
- header.style.cursor = 'move';
341
- let sx = 0;
342
- let sy = 0;
343
- let ox = 0;
344
- let oy = 0;
345
- let dragging = false;
346
- header.addEventListener('pointerdown', (e) => {
347
- if ((e.target as HTMLElement).closest('.vela-dialog-close')) return;
348
- dragging = true;
349
- sx = e.clientX - ox;
350
- sy = e.clientY - oy;
351
- header.setPointerCapture(e.pointerId);
352
- });
353
- header.addEventListener('pointermove', (e) => {
354
- if (!dragging) return;
355
- ox = e.clientX - sx;
356
- oy = e.clientY - sy;
357
- this.panel.style.transform = `translate(${ox}px, ${oy}px)`;
358
- });
359
- header.addEventListener('pointerup', () => (dragging = false));
360
- }
361
- if (opts.headerStart) header.appendChild(opts.headerStart);
362
- const title = part(doc.createElement('div'), 'title');
363
- title.className = 'vela-dialog-title';
364
- title.textContent = opts.title ?? '';
365
- this.titleEl = title;
366
- const close = part(doc.createElement('button'), 'close');
367
- close.className = 'vela-dialog-close';
368
- this.closeButton = close;
369
- this.setI18n(opts.i18n ?? kitTranslator());
370
- close.appendChild(iconEl('close', doc));
371
- header.append(title, close);
372
-
373
- this.body = part(doc.createElement('div'), 'body');
374
- this.body.className = 'vela-dialog-body';
375
- if (opts.flush) this.body.dataset.flush = '';
376
- if (opts.content instanceof Node) this.body.appendChild(opts.content);
377
- else if (typeof opts.content === 'function') opts.content(this.body);
378
-
379
- this.panel.append(header, this.body);
380
- if (opts.footer) {
381
- const foot = part(doc.createElement('div'), 'footer');
382
- foot.className = 'vela-dialog-footer';
383
- if (opts.footer instanceof Node) foot.appendChild(opts.footer);
384
- else opts.footer(foot);
385
- this.panel.appendChild(foot);
386
- this.footer = foot;
387
- } else {
388
- this.footer = null;
389
- }
390
- this.positioner.appendChild(this.panel);
391
- host.append(this.backdrop, this.positioner);
392
- if (opts.closeOnBackdrop) {
393
- const closeOn = (e: Event): void => {
394
- if (e.target !== this.backdrop && e.target !== this.positioner) return;
395
- // A portaled popover (color picker, select list) is logically INSIDE the
396
- // dialog: the first outside click dismisses it, not the dialog itself.
397
- if (isPopoverOpen() || eventDismissedPopover(e)) {
398
- closeOpenPopovers();
399
- return;
400
- }
401
- this.hide();
402
- };
403
- this.backdrop.addEventListener('pointerdown', closeOn);
404
- this.positioner.addEventListener('pointerdown', closeOn);
405
- }
406
-
407
- this.ctrl = dialogController({
408
- ...opts,
409
- // Mobile chrome (fullscreen presentation): opening must never land focus on
410
- // an input — that pops the on-screen keyboard over the just-opened dialog.
411
- // Focus goes to the panel; a caller that WANTS the keyboard focuses its
412
- // input explicitly. Desktop keeps the caller's pick, else the machine's
413
- // first-tabbable default.
414
- initialFocusEl: () => {
415
- if (this.positioner.closest('[data-layout="mobile"]')) return this.panel;
416
- return opts.initialFocusEl?.() ?? null;
417
- },
418
- });
419
- const mid = String(this.ctrl.props.id);
420
- this.handle = runMachine(this.ctrl.machine, this.ctrl.props, (service) => {
421
- const api = this.ctrl.connect(service);
422
- // Close announces while the panel still shows (before the close props land), open once it does.
423
- const closing = this.wasOpen && !api.open;
424
- if (closing) this.announce(false);
425
- spreadProps(this.backdrop, api.getBackdropProps(), mid);
426
- spreadProps(this.positioner, api.getPositionerProps(), mid);
427
- spreadProps(this.panel, api.getContentProps(), mid);
428
- spreadProps(title, api.getTitleProps(), mid);
429
- spreadProps(close, api.getCloseTriggerProps(), mid);
430
- // Zag dialogs expect CONDITIONAL rendering (their `hidden` reaches the panel and
431
- // backdrop only, and the panel's authored display beats it) — the view toggles
432
- // visibility from `api.open` itself, once the exit animation of a close has run.
433
- if (api.open) {
434
- this.exit?.cancel();
435
- this.exit = null;
436
- this.setShown(true);
437
- } else if (closing) {
438
- this.holdExit();
439
- } else if (!this.exit) {
440
- this.setShown(false);
441
- }
442
- if (api.open && !this.wasOpen) this.announce(true);
443
- if (this.wasOpen && !api.open) this.restoreOpener();
444
- this.wasOpen = api.open;
445
- });
446
- }
447
-
448
- private opener: HTMLElement | null = null;
449
- private wasOpen = false;
450
- /** The panel and scrim's exit animation after a close; a reopen cancels it. */
451
- private exit: SurfaceExit | null = null;
452
- private destroyed = false;
453
-
454
- private setShown(shown: boolean): void {
455
- this.backdrop.style.display = shown ? '' : 'none';
456
- this.positioner.style.display = shown ? '' : 'none';
457
- }
458
-
459
- /** Keep the just-closed dialog up through its exit animation, then hide it — or
460
- * remove it, when it was destroyed meanwhile. `spreadProps` re-applies the close
461
- * props' `hidden` only when its value changes, so lifting it keeps the panel and
462
- * scrim up until the exit ends. The positioner spans the viewport: inert, it lets
463
- * clicks through to the page while the panel animates out. */
464
- private holdExit(): void {
465
- const held = [this.panel, this.backdrop];
466
- for (const el of held) el.hidden = false;
467
- this.exit = holdForExit(
468
- held,
469
- () => {
470
- this.exit = null;
471
- if (this.destroyed) {
472
- this.detach();
473
- } else if (!this.open) {
474
- for (const el of held) el.hidden = true;
475
- this.setShown(false);
476
- }
477
- },
478
- { inert: [this.positioner] },
479
- );
480
- }
481
-
482
- private detach(): void {
483
- this.backdrop.remove();
484
- this.positioner.remove();
485
- }
486
-
487
- /** Closing hands focus back to whatever opened it (Escape, the ✕, hide(), or a
488
- * teardown that outruns the machine's close notification). */
489
- private restoreOpener(): void {
490
- const opener = this.opener;
491
- this.opener = null;
492
- if (opener?.isConnected) opener.focus({ preventScroll: true });
493
- }
494
-
495
- private announce(open: boolean): void {
496
- const opener = this.opener === this.panel.ownerDocument.body ? null : this.opener;
497
- announceSurface(this.panel, open, 'dialog', opener);
498
- }
499
-
500
- /** Speak the dialog's own words (the close button's name) through `i18n`, for a language
501
- * that changes while the dialog lives; null is English. */
502
- setI18n(i18n: Translator | null): void {
503
- this.closeButton.setAttribute('aria-label', textOf(i18n, CLOSE));
504
- }
505
-
506
- get open(): boolean {
507
- return this.ctrl.connect(this.handle.service).open;
508
- }
509
-
510
- show(): void {
511
- this.opener = this.panel.ownerDocument.activeElement as HTMLElement | null;
512
- this.ctrl.connect(this.handle.service).setOpen(true);
513
- }
514
-
515
- hide(): void {
516
- this.ctrl.connect(this.handle.service).setOpen(false);
517
- }
518
-
519
- contains(node: Node | null): boolean {
520
- if (node == null) return false;
521
- if (this.panel.contains(node) || this.backdrop.contains(node) || this.positioner.contains(node)) return true;
522
- const el = node instanceof Element ? node : node.parentElement;
523
- // Portaled select lists / color chips / menus live on the document, not in the panel.
524
- return el?.closest('.vela-popover, .vela-menu') != null;
525
- }
526
-
527
- destroy(): void {
528
- // Closed but not projected yet — an owner disposing of the dialog as it reports
529
- // closing: project the close now, so it animates out like any other close and
530
- // leaves the DOM when its exit ends. A teardown while open leaves at once.
531
- if (this.wasOpen && !this.open) this.handle.flush();
532
- // Exit the open state before stopping so the machine unwinds its open-time
533
- // effects (focus restore, and the body pointer-events lock of modal dialogs)
534
- // instead of leaving them behind.
535
- if (this.open) this.hide();
536
- this.handle.stop();
537
- this.destroyed = true;
538
- // Torn down while open: the stopped machine never projects the close.
539
- if (this.wasOpen) {
540
- this.wasOpen = false;
541
- this.announce(false);
542
- }
543
- this.restoreOpener();
544
- disposeWithin(this.panel);
545
- if (!this.exit) this.detach();
546
- }
547
- }
548
-
549
- export function createDialog(props: DialogProps = {}): Dialog {
550
- return new Dialog(props);
551
- }
1
+ // Dialog: a backdrop and a panel with a header and a body, over Zag's dialog machine (focus
2
+ // trap, dismissal, ARIA). The body is the caller's: pass a Node, or fill `body` after
3
+ // creation. `settings` and `form` are the chart settings and the indicator / drawing
4
+ // settings looks. In a shell in the mobile layout every dialog fills the shell.
5
+ import * as dialog from '@zag-js/dialog';
6
+ import { iconEl } from '@luxalgo/vela-ui/icons';
7
+ import { css, defineStyles } from '@luxalgo/vela-ui';
8
+ import { defineComponent } from '@luxalgo/vela-ui';
9
+ import { disposeWithin } from '@luxalgo/vela-ui/dispose';
10
+ import { msg, textOf, type Translator } from '@luxalgo/vela-ui/i18n';
11
+ import { kitTranslator } from '@luxalgo/vela-ui/kit-text';
12
+ import { t } from '@luxalgo/vela-ui';
13
+ import { injectStyles as injectSheet } from '@luxalgo/vela-ui/styles';
14
+ import { announceSurface } from '@luxalgo/vela-ui/surface-events';
15
+ import { holdForExit, type SurfaceExit } from '@luxalgo/vela-ui/surface-exit';
16
+ import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '@luxalgo/vela-ui/zag';
17
+ import { closeOpenPopovers, eventDismissedPopover, isPopoverOpen } from '@luxalgo/vela-ui/popover';
18
+
19
+ export interface DialogControllerOptions {
20
+ /** Trap focus + backdrop (default true). Non-modal = floating panel. */
21
+ modal?: boolean;
22
+ closeOnEscape?: boolean;
23
+ closeOnInteractOutside?: boolean;
24
+ /** Element to focus on open (default: the machine's first-tabbable pick). */
25
+ initialFocusEl?: () => HTMLElement | null;
26
+ onOpenChange?: (open: boolean) => void;
27
+ }
28
+
29
+ export type DialogService = dialog.Service;
30
+ export type DialogApi = dialog.Api;
31
+
32
+ export interface DialogController {
33
+ machine: typeof dialog.machine;
34
+ props: Partial<dialog.Props>;
35
+ connect(service: DialogService): DialogApi;
36
+ }
37
+
38
+ /** Zag's dialog machine with the options mapped onto it, for any view layer. */
39
+ export function dialogController(opts: DialogControllerOptions = {}): DialogController {
40
+ return {
41
+ machine: dialog.machine,
42
+ props: {
43
+ id: nextUid('vela-dialog'),
44
+ modal: opts.modal ?? true,
45
+ closeOnEscape: opts.closeOnEscape ?? true,
46
+ closeOnInteractOutside: opts.closeOnInteractOutside ?? false,
47
+ initialFocusEl: opts.initialFocusEl,
48
+ onOpenChange: (d: dialog.OpenChangeDetails) => opts.onOpenChange?.(d.open),
49
+ } satisfies Partial<dialog.Props>,
50
+ connect: (service: DialogService): DialogApi => dialog.connect(service, normalizeProps),
51
+ };
52
+ }
53
+
54
+ export const dialogDef = defineComponent({
55
+ name: 'dialog',
56
+ parts: ['backdrop', 'positioner', 'content', 'header', 'title', 'close', 'body', 'footer'],
57
+ settings: { radius: t.radius.lg },
58
+ });
59
+
60
+ export type DialogVariant = 'default' | 'settings' | 'form';
61
+
62
+ export interface DialogOptions extends DialogControllerOptions {
63
+ title?: string;
64
+ content?: Node | ((body: HTMLElement) => void);
65
+ host?: HTMLElement;
66
+ /** Drag the dialog by its header (the reference dialogs move; search stays fixed). */
67
+ draggable?: boolean;
68
+ /** Darken the page behind the dialog (default false — the chart stays readable while
69
+ * dialogs edit live content). Pass true for a dimming scrim; the backdrop still
70
+ * catches interact-outside dismissal either way. */
71
+ dimBackdrop?: boolean;
72
+ /** Vertical placement of the card. Default `top` (current 10vh pad). */
73
+ align?: 'top' | 'center';
74
+ /** Pin the overlay to `host` (absolute) instead of the viewport (fixed). */
75
+ contained?: boolean;
76
+ /** Footer pinned below the scrollable body. */
77
+ footer?: Node | ((el: HTMLElement) => void);
78
+ /** Extra nodes before the title (e.g. a mobile section burger). */
79
+ headerStart?: Node;
80
+ /** Skip default body padding — the caller owns the body layout. */
81
+ flush?: boolean;
82
+ /** `settings`: the chart settings card; `form`: the indicator and drawing settings card. */
83
+ variant?: DialogVariant;
84
+ /** Extra class on the panel. */
85
+ className?: string;
86
+ /** Close when the backdrop itself is pressed (not when a portaled popover is). */
87
+ closeOnBackdrop?: boolean;
88
+ /** The translator of the close button's name ("Close"). Default: the kit build's, else
89
+ * English. */
90
+ i18n?: Translator;
91
+ }
92
+
93
+ export type DialogProps = DialogOptions;
94
+
95
+ const CLOSE = msg('ui.dialog.close', 'Close');
96
+
97
+ const injectStyles = defineStyles(
98
+ 'vela-ui-dialog-5',
99
+ css`
100
+ @layer vela.ui {
101
+ [data-slot='dialog-backdrop'] {
102
+ position: fixed;
103
+ inset: 0;
104
+ background: ${t.backdrop};
105
+ z-index: ${t.z.dialog};
106
+
107
+ /* Non-dimming variant: still catches outside clicks, but the page stays readable. */
108
+ &[data-clear] {
109
+ background: transparent;
110
+ }
111
+ &[data-contained] {
112
+ position: absolute;
113
+ }
114
+ &:is([data-layout='mobile'] *) {
115
+ position: absolute;
116
+ }
117
+ }
118
+ [data-slot='dialog-positioner'] {
119
+ position: fixed;
120
+ inset: 0;
121
+ display: flex;
122
+ align-items: flex-start;
123
+ justify-content: center;
124
+ padding-top: 10vh;
125
+ z-index: ${t.z.dialog};
126
+
127
+ &[data-contained] {
128
+ position: absolute;
129
+ }
130
+ &[data-align='center'] {
131
+ align-items: center;
132
+ padding-top: 0;
133
+ }
134
+ /* In a shell in the mobile size class every dialog fills the shell: desktop
135
+ cards are unusable at phone widths, and the shell's bounds, not the
136
+ viewport, are the honest "screen" for an embedded chart. */
137
+ &:is([data-layout='mobile'] *) {
138
+ position: absolute;
139
+ padding: 0;
140
+ align-items: stretch;
141
+ }
142
+ }
143
+ [data-slot='dialog-header'] {
144
+ display: flex;
145
+ align-items: center;
146
+ padding-block: 9px;
147
+ padding-inline: 16px 9px;
148
+ border-bottom: 1px solid ${t.border};
149
+ user-select: none;
150
+ }
151
+ [data-slot='dialog-title'] {
152
+ flex: 1;
153
+ font-size: 17px;
154
+ font-weight: 600;
155
+ letter-spacing: 0.2px;
156
+ color: ${t.fg.bright};
157
+ }
158
+ [data-slot='dialog-close'] {
159
+ all: unset;
160
+ cursor: pointer;
161
+ width: 30px;
162
+ height: 30px;
163
+ display: flex;
164
+ align-items: center;
165
+ justify-content: center;
166
+ border-radius: ${t.radius.sm};
167
+ color: ${t.fg.muted};
168
+ line-height: 1;
169
+ font-size: 15px;
170
+
171
+ &:hover {
172
+ background: ${t.hover};
173
+ color: ${t.fg.bright};
174
+ }
175
+ &:is([data-layout='mobile'] *) {
176
+ width: 40px;
177
+ height: 40px;
178
+ }
179
+ }
180
+ [data-slot='dialog-body'] {
181
+ padding: ${t.space[4]};
182
+ overflow: auto;
183
+ min-height: 0;
184
+ flex: 1 1 auto;
185
+
186
+ &[data-flush] {
187
+ padding: 0;
188
+ overflow: hidden;
189
+ display: flex;
190
+ flex-direction: column;
191
+ }
192
+ &:is([data-layout='mobile'] *) {
193
+ padding-bottom: calc(${t.space[4]} + env(safe-area-inset-bottom, 0px));
194
+ }
195
+ }
196
+ [data-slot='dialog-footer'] {
197
+ flex: 0 0 auto;
198
+ }
199
+ [data-slot='dialog-content'] {
200
+ background: ${t.surface};
201
+ color: ${t.fg};
202
+ border: 1px solid ${t.border.strong};
203
+ border-radius: ${dialogDef.vars.radius};
204
+ box-shadow: ${t.shadow.dialog};
205
+ font-size: 13px;
206
+ min-width: 300px;
207
+ max-width: min(92vw, 560px);
208
+ max-height: 70vh;
209
+ display: flex;
210
+ flex-direction: column;
211
+ overflow: hidden;
212
+ outline: 2px solid transparent;
213
+
214
+ &[data-state='open'] {
215
+ animation: vela-dialog-in ${t.dur.med} ${t.ease};
216
+ }
217
+ /* A plain dialog's footer: its buttons at the end, padded like the body above it.
218
+ The settings and form variants lay theirs out themselves. */
219
+ &[data-variant='default'] [data-slot='dialog-footer'] {
220
+ display: flex;
221
+ justify-content: flex-end;
222
+ gap: ${t.space[2]};
223
+ padding: 0 ${t.space[4]} ${t.space[4]};
224
+ }
225
+ &[data-variant='settings'] {
226
+ width: fit-content;
227
+ min-width: min(560px, 94vw);
228
+ max-width: min(720px, 94vw);
229
+ max-height: 70vh;
230
+ font-size: 13px;
231
+ cursor: default;
232
+ }
233
+ &[data-variant='form'] {
234
+ width: fit-content;
235
+ min-width: min(380px, 90%);
236
+ max-width: min(640px, 94%);
237
+ max-height: 82%;
238
+ font-size: 14px;
239
+ }
240
+ /* Form dialogs (indicator inputs, drawing settings): the tab strip or the body
241
+ owns the line under the header, and the footer carries its own top line. */
242
+ &[data-variant='form'] [data-slot='dialog-header'] {
243
+ align-items: flex-start;
244
+ padding: 16px 20px 20px;
245
+ border-bottom: none;
246
+ }
247
+ &[data-variant='form'] [data-slot='dialog-title'] {
248
+ font-size: 20px;
249
+ line-height: 28px;
250
+ }
251
+ &[data-variant='form'] [data-slot='dialog-footer'] {
252
+ padding: 16px 20px;
253
+ display: flex;
254
+ justify-content: flex-end;
255
+ gap: 12px;
256
+ border-top: 1px solid ${t.border};
257
+ }
258
+ &:is([data-layout='mobile'] *) {
259
+ width: 100%;
260
+ min-width: 0;
261
+ max-width: none;
262
+ max-height: none;
263
+ flex: 1 1 auto;
264
+ border: none;
265
+ border-radius: 0;
266
+ }
267
+ }
268
+ @keyframes vela-dialog-in {
269
+ from {
270
+ opacity: 0;
271
+ transform: translateY(6px) scale(0.98);
272
+ }
273
+ to {
274
+ opacity: 1;
275
+ transform: translateY(0) scale(1);
276
+ }
277
+ }
278
+ }
279
+ `,
280
+ );
281
+
282
+ /* A desktop drag offset (an inline transform) must not survive the flip to the mobile
283
+ layout, so this one rule stays outside the layers and important. */
284
+ const MOBILE_DRAG_CSS = "[data-layout='mobile'] .vela-dialog { transform: none !important; }";
285
+
286
+ const VARIANT_CLASSES: Record<string, DialogVariant> = { 'vela-dialog--settings': 'settings', 'vela-dialog--form': 'form' };
287
+
288
+ export class Dialog {
289
+ /** Caller-owned content area — append your form/panel here. */
290
+ readonly body: HTMLElement;
291
+ readonly panel: HTMLElement;
292
+ readonly titleEl: HTMLElement;
293
+ readonly positioner: HTMLElement;
294
+ readonly backdrop: HTMLElement;
295
+ readonly footer: HTMLElement | null;
296
+ private readonly closeButton: HTMLElement;
297
+ private readonly handle: HandleOf<typeof dialog.machine>;
298
+ private readonly ctrl: DialogController;
299
+
300
+ constructor(opts: DialogOptions = {}) {
301
+ const doc = (opts.host ?? document.body).ownerDocument;
302
+ injectStyles(doc);
303
+ injectSheet('vela-ui-dialog-mobile', MOBILE_DRAG_CSS, doc);
304
+ const host = opts.host ?? doc.body;
305
+ const part = (el: HTMLElement, name: (typeof dialogDef.parts)[number]): HTMLElement => {
306
+ el.dataset.slot = dialogDef.slot(name);
307
+ return el;
308
+ };
309
+
310
+ this.backdrop = part(doc.createElement('div'), 'backdrop');
311
+ this.backdrop.className = 'vela-dialog-backdrop vela-ui-layer';
312
+ if (opts.dimBackdrop !== true) {
313
+ this.backdrop.classList.add('vela-dialog-backdrop--clear');
314
+ this.backdrop.dataset.clear = '';
315
+ }
316
+ this.positioner = part(doc.createElement('div'), 'positioner');
317
+ this.positioner.className = 'vela-dialog-positioner vela-ui-layer';
318
+ if (opts.contained) {
319
+ this.backdrop.dataset.contained = '';
320
+ this.positioner.dataset.contained = '';
321
+ }
322
+ this.positioner.dataset.align = opts.align ?? 'top';
323
+ this.panel = part(doc.createElement('div'), 'content');
324
+ this.panel.className = 'vela-dialog';
325
+ const classes = (opts.className ?? '').split(/\s+/).filter(Boolean);
326
+ const variant = opts.variant ?? classes.map((c) => VARIANT_CLASSES[c]).find((v) => v !== undefined) ?? 'default';
327
+ this.panel.dataset.variant = variant;
328
+ if (variant !== 'default') this.panel.classList.add(`vela-dialog--${variant}`);
329
+ for (const c of classes) this.panel.classList.add(c);
330
+ // Programmatically focusable — the mobile chrome routes the machine's initial
331
+ // focus here (see the controller wiring below).
332
+ this.panel.tabIndex = -1;
333
+ this.panel.addEventListener('keydown', (e) => {
334
+ if (e.key !== 'Escape') e.stopPropagation();
335
+ });
336
+
337
+ const header = part(doc.createElement('div'), 'header');
338
+ header.className = 'vela-dialog-header';
339
+ if (opts.draggable) {
340
+ header.style.cursor = 'move';
341
+ let sx = 0;
342
+ let sy = 0;
343
+ let ox = 0;
344
+ let oy = 0;
345
+ let dragging = false;
346
+ header.addEventListener('pointerdown', (e) => {
347
+ if ((e.target as HTMLElement).closest('.vela-dialog-close')) return;
348
+ dragging = true;
349
+ sx = e.clientX - ox;
350
+ sy = e.clientY - oy;
351
+ header.setPointerCapture(e.pointerId);
352
+ });
353
+ header.addEventListener('pointermove', (e) => {
354
+ if (!dragging) return;
355
+ ox = e.clientX - sx;
356
+ oy = e.clientY - sy;
357
+ this.panel.style.transform = `translate(${ox}px, ${oy}px)`;
358
+ });
359
+ header.addEventListener('pointerup', () => (dragging = false));
360
+ }
361
+ if (opts.headerStart) header.appendChild(opts.headerStart);
362
+ const title = part(doc.createElement('div'), 'title');
363
+ title.className = 'vela-dialog-title';
364
+ title.textContent = opts.title ?? '';
365
+ this.titleEl = title;
366
+ const close = part(doc.createElement('button'), 'close');
367
+ close.className = 'vela-dialog-close';
368
+ this.closeButton = close;
369
+ this.setI18n(opts.i18n ?? kitTranslator());
370
+ close.appendChild(iconEl('close', doc));
371
+ header.append(title, close);
372
+
373
+ this.body = part(doc.createElement('div'), 'body');
374
+ this.body.className = 'vela-dialog-body';
375
+ if (opts.flush) this.body.dataset.flush = '';
376
+ if (opts.content instanceof Node) this.body.appendChild(opts.content);
377
+ else if (typeof opts.content === 'function') opts.content(this.body);
378
+
379
+ this.panel.append(header, this.body);
380
+ if (opts.footer) {
381
+ const foot = part(doc.createElement('div'), 'footer');
382
+ foot.className = 'vela-dialog-footer';
383
+ if (opts.footer instanceof Node) foot.appendChild(opts.footer);
384
+ else opts.footer(foot);
385
+ this.panel.appendChild(foot);
386
+ this.footer = foot;
387
+ } else {
388
+ this.footer = null;
389
+ }
390
+ this.positioner.appendChild(this.panel);
391
+ host.append(this.backdrop, this.positioner);
392
+ if (opts.closeOnBackdrop) {
393
+ const closeOn = (e: Event): void => {
394
+ if (e.target !== this.backdrop && e.target !== this.positioner) return;
395
+ // A portaled popover (color picker, select list) is logically INSIDE the
396
+ // dialog: the first outside click dismisses it, not the dialog itself.
397
+ if (isPopoverOpen() || eventDismissedPopover(e)) {
398
+ closeOpenPopovers();
399
+ return;
400
+ }
401
+ this.hide();
402
+ };
403
+ this.backdrop.addEventListener('pointerdown', closeOn);
404
+ this.positioner.addEventListener('pointerdown', closeOn);
405
+ }
406
+
407
+ this.ctrl = dialogController({
408
+ ...opts,
409
+ // Mobile chrome (fullscreen presentation): opening must never land focus on
410
+ // an input — that pops the on-screen keyboard over the just-opened dialog.
411
+ // Focus goes to the panel; a caller that WANTS the keyboard focuses its
412
+ // input explicitly. Desktop keeps the caller's pick, else the machine's
413
+ // first-tabbable default.
414
+ initialFocusEl: () => {
415
+ if (this.positioner.closest('[data-layout="mobile"]')) return this.panel;
416
+ return opts.initialFocusEl?.() ?? null;
417
+ },
418
+ });
419
+ const mid = String(this.ctrl.props.id);
420
+ this.handle = runMachine(this.ctrl.machine, this.ctrl.props, (service) => {
421
+ const api = this.ctrl.connect(service);
422
+ // Close announces while the panel still shows (before the close props land), open once it does.
423
+ const closing = this.wasOpen && !api.open;
424
+ if (closing) this.announce(false);
425
+ spreadProps(this.backdrop, api.getBackdropProps(), mid);
426
+ spreadProps(this.positioner, api.getPositionerProps(), mid);
427
+ spreadProps(this.panel, api.getContentProps(), mid);
428
+ spreadProps(title, api.getTitleProps(), mid);
429
+ spreadProps(close, api.getCloseTriggerProps(), mid);
430
+ // Zag dialogs expect CONDITIONAL rendering (their `hidden` reaches the panel and
431
+ // backdrop only, and the panel's authored display beats it) — the view toggles
432
+ // visibility from `api.open` itself, once the exit animation of a close has run.
433
+ if (api.open) {
434
+ this.exit?.cancel();
435
+ this.exit = null;
436
+ this.setShown(true);
437
+ } else if (closing) {
438
+ this.holdExit();
439
+ } else if (!this.exit) {
440
+ this.setShown(false);
441
+ }
442
+ if (api.open && !this.wasOpen) this.announce(true);
443
+ if (this.wasOpen && !api.open) this.restoreOpener();
444
+ this.wasOpen = api.open;
445
+ });
446
+ }
447
+
448
+ private opener: HTMLElement | null = null;
449
+ private wasOpen = false;
450
+ /** The panel and scrim's exit animation after a close; a reopen cancels it. */
451
+ private exit: SurfaceExit | null = null;
452
+ private destroyed = false;
453
+
454
+ private setShown(shown: boolean): void {
455
+ this.backdrop.style.display = shown ? '' : 'none';
456
+ this.positioner.style.display = shown ? '' : 'none';
457
+ }
458
+
459
+ /** Keep the just-closed dialog up through its exit animation, then hide it — or
460
+ * remove it, when it was destroyed meanwhile. `spreadProps` re-applies the close
461
+ * props' `hidden` only when its value changes, so lifting it keeps the panel and
462
+ * scrim up until the exit ends. The positioner spans the viewport: inert, it lets
463
+ * clicks through to the page while the panel animates out. */
464
+ private holdExit(): void {
465
+ const held = [this.panel, this.backdrop];
466
+ for (const el of held) el.hidden = false;
467
+ this.exit = holdForExit(
468
+ held,
469
+ () => {
470
+ this.exit = null;
471
+ if (this.destroyed) {
472
+ this.detach();
473
+ } else if (!this.open) {
474
+ for (const el of held) el.hidden = true;
475
+ this.setShown(false);
476
+ }
477
+ },
478
+ { inert: [this.positioner] },
479
+ );
480
+ }
481
+
482
+ private detach(): void {
483
+ this.backdrop.remove();
484
+ this.positioner.remove();
485
+ }
486
+
487
+ /** Closing hands focus back to whatever opened it (Escape, the ✕, hide(), or a
488
+ * teardown that outruns the machine's close notification). */
489
+ private restoreOpener(): void {
490
+ const opener = this.opener;
491
+ this.opener = null;
492
+ if (opener?.isConnected) opener.focus({ preventScroll: true });
493
+ }
494
+
495
+ private announce(open: boolean): void {
496
+ const opener = this.opener === this.panel.ownerDocument.body ? null : this.opener;
497
+ announceSurface(this.panel, open, 'dialog', opener);
498
+ }
499
+
500
+ /** Speak the dialog's own words (the close button's name) through `i18n`, for a language
501
+ * that changes while the dialog lives; null is English. */
502
+ setI18n(i18n: Translator | null): void {
503
+ this.closeButton.setAttribute('aria-label', textOf(i18n, CLOSE));
504
+ }
505
+
506
+ get open(): boolean {
507
+ return this.ctrl.connect(this.handle.service).open;
508
+ }
509
+
510
+ show(): void {
511
+ this.opener = this.panel.ownerDocument.activeElement as HTMLElement | null;
512
+ this.ctrl.connect(this.handle.service).setOpen(true);
513
+ }
514
+
515
+ hide(): void {
516
+ this.ctrl.connect(this.handle.service).setOpen(false);
517
+ }
518
+
519
+ contains(node: Node | null): boolean {
520
+ if (node == null) return false;
521
+ if (this.panel.contains(node) || this.backdrop.contains(node) || this.positioner.contains(node)) return true;
522
+ const el = node instanceof Element ? node : node.parentElement;
523
+ // Portaled select lists / color chips / menus live on the document, not in the panel.
524
+ return el?.closest('.vela-popover, .vela-menu') != null;
525
+ }
526
+
527
+ destroy(): void {
528
+ // Closed but not projected yet — an owner disposing of the dialog as it reports
529
+ // closing: project the close now, so it animates out like any other close and
530
+ // leaves the DOM when its exit ends. A teardown while open leaves at once.
531
+ if (this.wasOpen && !this.open) this.handle.flush();
532
+ // Exit the open state before stopping so the machine unwinds its open-time
533
+ // effects (focus restore, and the body pointer-events lock of modal dialogs)
534
+ // instead of leaving them behind.
535
+ if (this.open) this.hide();
536
+ this.handle.stop();
537
+ this.destroyed = true;
538
+ // Torn down while open: the stopped machine never projects the close.
539
+ if (this.wasOpen) {
540
+ this.wasOpen = false;
541
+ this.announce(false);
542
+ }
543
+ this.restoreOpener();
544
+ disposeWithin(this.panel);
545
+ if (!this.exit) this.detach();
546
+ }
547
+ }
548
+
549
+ export function createDialog(props: DialogProps = {}): Dialog {
550
+ return new Dialog(props);
551
+ }