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