@luxalgo/vela-ui 0.0.0-stage → 0.2.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 (269) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +28 -0
  4. package/README.md +86 -3
  5. package/cli/engine.mjs +282 -0
  6. package/cli/index.mjs +154 -0
  7. package/dist/badge.d.ts +26 -0
  8. package/dist/badge.js +44 -0
  9. package/dist/bar-chart.d.ts +40 -0
  10. package/dist/bar-chart.js +105 -0
  11. package/dist/button.d.ts +55 -0
  12. package/dist/button.js +11 -0
  13. package/dist/calendar.d.ts +24 -0
  14. package/dist/calendar.js +1 -0
  15. package/dist/callout-bubble.d.ts +103 -0
  16. package/dist/callout-bubble.js +177 -0
  17. package/dist/chart.d.ts +30 -0
  18. package/dist/chart.js +1 -0
  19. package/dist/checkbox.d.ts +60 -0
  20. package/dist/checkbox.js +12 -0
  21. package/dist/chunk-2GQIZM5Z.js +28 -0
  22. package/dist/chunk-3Q32K4PE.js +85 -0
  23. package/dist/chunk-6QCILIT2.js +52 -0
  24. package/dist/chunk-7VMN7NKJ.js +133 -0
  25. package/dist/chunk-A2FJ7Y7D.js +55 -0
  26. package/dist/chunk-A72NWYNP.js +26 -0
  27. package/dist/chunk-B5ZSRUDQ.js +227 -0
  28. package/dist/chunk-BXMWQIBO.js +66 -0
  29. package/dist/chunk-C3ICYMI3.js +25 -0
  30. package/dist/chunk-CC3DDNFC.js +117 -0
  31. package/dist/chunk-E2FE6V2N.js +81 -0
  32. package/dist/chunk-FN2BUFNM.js +10 -0
  33. package/dist/chunk-FVH6L7FM.js +18 -0
  34. package/dist/chunk-G7IQLY3A.js +183 -0
  35. package/dist/chunk-GVRX3UAV.js +82 -0
  36. package/dist/chunk-J2QJATCD.js +12 -0
  37. package/dist/chunk-LZWZV2ZS.js +18 -0
  38. package/dist/chunk-MJ4RUGX7.js +403 -0
  39. package/dist/chunk-MMTMOV6R.js +31 -0
  40. package/dist/chunk-P4MTEMT7.js +23 -0
  41. package/dist/chunk-Q6WTDVH6.js +31 -0
  42. package/dist/chunk-S7USVSYP.js +50 -0
  43. package/dist/chunk-TAKSIV57.js +90 -0
  44. package/dist/chunk-U3EH42YY.js +72 -0
  45. package/dist/chunk-V2Q4MRMS.js +573 -0
  46. package/dist/chunk-VMCR3S2A.js +22 -0
  47. package/dist/chunk-WBGSYCV2.js +101 -0
  48. package/dist/chunk-WGULXRWD.js +351 -0
  49. package/dist/chunk-WXH3EFV5.js +219 -0
  50. package/dist/chunk-X66RJAIS.js +14 -0
  51. package/dist/chunk-XBEL4K33.js +142 -0
  52. package/dist/chunk-Z5PINFQO.js +280 -0
  53. package/dist/collapsible.d.ts +49 -0
  54. package/dist/collapsible.js +106 -0
  55. package/dist/color-picker.d.ts +103 -0
  56. package/dist/color-picker.js +19 -0
  57. package/dist/color.d.ts +24 -0
  58. package/dist/color.js +1 -0
  59. package/dist/context-menu.d.ts +72 -0
  60. package/dist/context-menu.js +161 -0
  61. package/dist/css.d.ts +17 -0
  62. package/dist/css.js +4 -0
  63. package/dist/data-table.d.ts +40 -0
  64. package/dist/data-table.js +88 -0
  65. package/dist/date-picker.d.ts +90 -0
  66. package/dist/date-picker.js +257 -0
  67. package/dist/define-D5u3F5Dj.d.ts +21 -0
  68. package/dist/dialog.d.ts +89 -0
  69. package/dist/dialog.js +258 -0
  70. package/dist/dispose.d.ts +9 -0
  71. package/dist/dispose.js +2 -0
  72. package/dist/donut.d.ts +27 -0
  73. package/dist/donut.js +67 -0
  74. package/dist/drawer.d.ts +119 -0
  75. package/dist/drawer.js +326 -0
  76. package/dist/dropdown-menu.d.ts +114 -0
  77. package/dist/dropdown-menu.js +18 -0
  78. package/dist/empty.d.ts +21 -0
  79. package/dist/empty.js +38 -0
  80. package/dist/field.d.ts +147 -0
  81. package/dist/field.js +328 -0
  82. package/dist/glyph-select.d.ts +57 -0
  83. package/dist/glyph-select.js +17 -0
  84. package/dist/h-Bl37qUis.d.ts +24 -0
  85. package/dist/headless.d.ts +6 -0
  86. package/dist/headless.js +9 -0
  87. package/dist/hover-card.d.ts +43 -0
  88. package/dist/hover-card.js +97 -0
  89. package/dist/i18n.d.ts +20 -0
  90. package/dist/i18n.js +1 -0
  91. package/dist/icons.d.ts +20 -0
  92. package/dist/icons.js +2 -0
  93. package/dist/index.d.ts +51 -0
  94. package/dist/index.js +13 -0
  95. package/dist/input-group.d.ts +57 -0
  96. package/dist/input-group.js +87 -0
  97. package/dist/input.d.ts +54 -0
  98. package/dist/input.js +9 -0
  99. package/dist/item.d.ts +72 -0
  100. package/dist/item.js +131 -0
  101. package/dist/kbd.d.ts +19 -0
  102. package/dist/kbd.js +8 -0
  103. package/dist/kit-text.d.ts +8 -0
  104. package/dist/kit-text.js +2 -0
  105. package/dist/line-chart.d.ts +32 -0
  106. package/dist/line-chart.js +120 -0
  107. package/dist/manifest.json +4508 -0
  108. package/dist/month-grid.d.ts +45 -0
  109. package/dist/month-grid.js +157 -0
  110. package/dist/number-input.d.ts +99 -0
  111. package/dist/number-input.js +14 -0
  112. package/dist/page.d.ts +7 -0
  113. package/dist/page.js +1 -0
  114. package/dist/palette.d.ts +55 -0
  115. package/dist/palette.js +1 -0
  116. package/dist/placement.d.ts +98 -0
  117. package/dist/placement.js +1 -0
  118. package/dist/popover.d.ts +107 -0
  119. package/dist/popover.js +15 -0
  120. package/dist/scatter-chart.d.ts +32 -0
  121. package/dist/scatter-chart.js +121 -0
  122. package/dist/select.d.ts +112 -0
  123. package/dist/select.js +19 -0
  124. package/dist/slider.d.ts +46 -0
  125. package/dist/slider.js +111 -0
  126. package/dist/sort-button.d.ts +41 -0
  127. package/dist/sort-button.js +88 -0
  128. package/dist/sparkline.d.ts +22 -0
  129. package/dist/sparkline.js +72 -0
  130. package/dist/stat.d.ts +29 -0
  131. package/dist/stat.js +41 -0
  132. package/dist/styles.d.ts +22 -0
  133. package/dist/styles.js +2 -0
  134. package/dist/surface-events.d.ts +17 -0
  135. package/dist/surface-events.js +1 -0
  136. package/dist/surface-exit.d.ts +25 -0
  137. package/dist/surface-exit.js +1 -0
  138. package/dist/switch.d.ts +54 -0
  139. package/dist/switch.js +92 -0
  140. package/dist/t.d.ts +89 -0
  141. package/dist/t.js +1 -0
  142. package/dist/tabs.d.ts +65 -0
  143. package/dist/tabs.js +155 -0
  144. package/dist/text-area.d.ts +54 -0
  145. package/dist/text-area.js +9 -0
  146. package/dist/theme.d.ts +199 -0
  147. package/dist/theme.js +8 -0
  148. package/dist/theme.schema.json +1994 -0
  149. package/dist/themes/dark.css +3 -0
  150. package/dist/themes/light.css +3 -0
  151. package/dist/toggle-group.d.ts +56 -0
  152. package/dist/toggle-group.js +130 -0
  153. package/dist/tokens.d.ts +16 -0
  154. package/dist/tokens.js +3 -0
  155. package/dist/tooltip.d.ts +47 -0
  156. package/dist/tooltip.js +74 -0
  157. package/dist/variants-B4stUBnY.d.ts +16 -0
  158. package/dist/zag.d.ts +19 -0
  159. package/dist/zag.js +2 -0
  160. package/docs/theming.md +217 -0
  161. package/docs/what-to-change.md +20 -0
  162. package/docs/writing-a-component.md +250 -0
  163. package/llms.txt +71 -0
  164. package/package.json +90 -6
  165. package/registry/badge.ts +119 -0
  166. package/registry/bar-chart.ts +215 -0
  167. package/registry/button.ts +379 -0
  168. package/registry/callout-bubble.ts +347 -0
  169. package/registry/checkbox.ts +234 -0
  170. package/registry/collapsible.ts +271 -0
  171. package/registry/color-picker.ts +582 -0
  172. package/registry/context-menu.ts +208 -0
  173. package/registry/data-table.ts +215 -0
  174. package/registry/date-picker.ts +483 -0
  175. package/registry/dialog.ts +535 -0
  176. package/registry/donut.ts +145 -0
  177. package/registry/drawer.ts +636 -0
  178. package/registry/dropdown-menu.ts +762 -0
  179. package/registry/empty.ts +91 -0
  180. package/registry/field.ts +585 -0
  181. package/registry/glyph-select.ts +268 -0
  182. package/registry/hover-card.ts +222 -0
  183. package/registry/input-group.ts +229 -0
  184. package/registry/input.ts +181 -0
  185. package/registry/item.ts +376 -0
  186. package/registry/kbd.ts +64 -0
  187. package/registry/line-chart.ts +218 -0
  188. package/registry/month-grid.ts +328 -0
  189. package/registry/number-input.ts +443 -0
  190. package/registry/popover.ts +368 -0
  191. package/registry/scatter-chart.ts +221 -0
  192. package/registry/select.ts +620 -0
  193. package/registry/slider.ts +259 -0
  194. package/registry/sort-button.ts +189 -0
  195. package/registry/sparkline.ts +114 -0
  196. package/registry/stat.ts +96 -0
  197. package/registry/switch.ts +205 -0
  198. package/registry/tabs.ts +426 -0
  199. package/registry/text-area.ts +186 -0
  200. package/registry/toggle-group.ts +295 -0
  201. package/registry/tooltip.ts +156 -0
  202. package/skills/theming-vela-ui/SKILL.md +137 -0
  203. package/skills/using-vela-ui-components/SKILL.md +137 -0
  204. package/skills/writing-vela-ui-components/SKILL.md +163 -0
  205. package/src/components/badge.ts +119 -0
  206. package/src/components/bar-chart.ts +215 -0
  207. package/src/components/button.ts +379 -0
  208. package/src/components/callout-bubble.ts +347 -0
  209. package/src/components/checkbox.ts +234 -0
  210. package/src/components/collapsible.ts +271 -0
  211. package/src/components/color-picker.ts +582 -0
  212. package/src/components/context-menu.ts +208 -0
  213. package/src/components/data-table.ts +215 -0
  214. package/src/components/date-picker.ts +483 -0
  215. package/src/components/dialog.ts +535 -0
  216. package/src/components/donut.ts +145 -0
  217. package/src/components/drawer.ts +636 -0
  218. package/src/components/dropdown-menu.ts +762 -0
  219. package/src/components/empty.ts +91 -0
  220. package/src/components/field.ts +585 -0
  221. package/src/components/glyph-select.ts +268 -0
  222. package/src/components/hover-card.ts +222 -0
  223. package/src/components/input-group.ts +229 -0
  224. package/src/components/input.ts +181 -0
  225. package/src/components/item.ts +376 -0
  226. package/src/components/kbd.ts +64 -0
  227. package/src/components/line-chart.ts +218 -0
  228. package/src/components/month-grid.ts +328 -0
  229. package/src/components/number-input.ts +443 -0
  230. package/src/components/popover.ts +368 -0
  231. package/src/components/scatter-chart.ts +221 -0
  232. package/src/components/select.ts +620 -0
  233. package/src/components/slider.ts +259 -0
  234. package/src/components/sort-button.ts +189 -0
  235. package/src/components/sparkline.ts +114 -0
  236. package/src/components/stat.ts +96 -0
  237. package/src/components/switch.ts +205 -0
  238. package/src/components/tabs.ts +426 -0
  239. package/src/components/text-area.ts +186 -0
  240. package/src/components/toggle-group.ts +295 -0
  241. package/src/components/tooltip.ts +156 -0
  242. package/src/index.ts +24 -0
  243. package/src/lib/calendar.ts +117 -0
  244. package/src/lib/chart.ts +72 -0
  245. package/src/lib/color.ts +89 -0
  246. package/src/lib/component-index.ts +151 -0
  247. package/src/lib/css.ts +48 -0
  248. package/src/lib/define.ts +45 -0
  249. package/src/lib/dispose.ts +36 -0
  250. package/src/lib/h.ts +55 -0
  251. package/src/lib/headless.ts +16 -0
  252. package/src/lib/i18n.ts +30 -0
  253. package/src/lib/icons.ts +237 -0
  254. package/src/lib/kit-text.ts +26 -0
  255. package/src/lib/page.ts +24 -0
  256. package/src/lib/palette.ts +81 -0
  257. package/src/lib/placement.ts +188 -0
  258. package/src/lib/scope.ts +135 -0
  259. package/src/lib/styles.ts +53 -0
  260. package/src/lib/surface-events.ts +31 -0
  261. package/src/lib/surface-exit.ts +87 -0
  262. package/src/lib/t.ts +68 -0
  263. package/src/lib/theme.ts +664 -0
  264. package/src/lib/tokens.ts +111 -0
  265. package/src/lib/variants.ts +32 -0
  266. package/src/lib/zag.ts +46 -0
  267. package/stylelint/index.mjs +60 -0
  268. package/stylelint/lint.mjs +81 -0
  269. package/stylelint/plugin.mjs +115 -0
@@ -0,0 +1,762 @@
1
+ // DropdownMenu: MenuItemDescriptor[] projected into a floating list over Zag's menu machine
2
+ // (keyboard navigation, typeahead and dismissal belong to the machine). Items are data
3
+ // descriptors, the plugin contribution currency. An item carrying `submenu` becomes a
4
+ // trigger row owning a nested machine and list, registered as this machine's child: Zag
5
+ // drives hover intent, arrow-key traversal and the dismiss chain across levels.
6
+ import * as zagMenu from '@zag-js/menu';
7
+ import { msg } from '@luxalgo/vela-ui/i18n';
8
+ import { textOf } from '@luxalgo/vela-ui/i18n';
9
+ import type { Translator } from '@luxalgo/vela-ui/i18n';
10
+ import { iconEl } from '@luxalgo/vela-ui/icons';
11
+ import { kitTranslator } from '@luxalgo/vela-ui/kit-text';
12
+ import { css, defineStyles } from '@luxalgo/vela-ui';
13
+ import { defineComponent } from '@luxalgo/vela-ui';
14
+ import { addSlot } from '@luxalgo/vela-ui';
15
+ import { t } from '@luxalgo/vela-ui';
16
+ import { announceSurface } from '@luxalgo/vela-ui/surface-events';
17
+ import { holdForExit, type SurfaceExit } from '@luxalgo/vela-ui/surface-exit';
18
+ import { floatingLayerHost } from '@luxalgo/vela-ui/theme';
19
+ import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '@luxalgo/vela-ui/zag';
20
+ import { splitPlacement, zagPositioning, type PlacementProps } from '@luxalgo/vela-ui/placement';
21
+
22
+ /** One menu entry. `id` is the selection value reported to `onSelect`. */
23
+ export interface MenuItemDescriptor {
24
+ id: string;
25
+ label: string;
26
+ disabled?: boolean;
27
+ /** Draw a separator line above this item. */
28
+ separatorBefore?: boolean;
29
+ /** Right-aligned hint (e.g. a shortcut display from KeymapManager). */
30
+ hint?: string;
31
+ /** Selected state (undefined = plain item). A plain checked item renders as a
32
+ * highlighted row (brighter surface + bright ink) — or, in a menu built with
33
+ * `checkmarks`, as a leading ✓ with the row surface left free for hover. */
34
+ checked?: boolean;
35
+ /** Render as a SWITCH row (a right-aligned toggle pill reflecting `checked`)
36
+ * instead of the selected-row highlight, and keep the menu OPEN on selection —
37
+ * the shape for boolean settings living inside a dropdown. */
38
+ toggle?: boolean;
39
+ /** Icon id (see the `vela/ui` icon registry) rendered before the label. A level where
40
+ * any row has one gives its iconless rows an empty slot, so the labels align. */
41
+ icon?: string;
42
+ /** Favorite-star affordance at the row's right edge: `false` renders an outline
43
+ * star revealed on row hover, `true` a filled star that stays visible. Clicking
44
+ * it reports through the menu's `onFavorite` WITHOUT selecting (or closing) the
45
+ * row — undefined rows carry no star at all. */
46
+ favorite?: boolean;
47
+ /** Nested entries — the row becomes a submenu trigger opening its own list to the side.
48
+ * A branch is not selectable itself: `onSelect` only ever reports leaf ids. */
49
+ submenu?: readonly MenuItemDescriptor[];
50
+ }
51
+
52
+ export interface DropdownMenuControllerOptions extends PlacementProps {
53
+ items: readonly MenuItemDescriptor[];
54
+ onSelect?: (id: string) => void;
55
+ /** @deprecated Use `side` and `align`. */
56
+ placement?: zagMenu.Props['positioning'] extends infer P | undefined ? (P extends { placement?: infer PL } ? PL : never) : never;
57
+ onOpenChange?: (open: boolean) => void;
58
+ /** Pin the floating list to this rect (viewport coords). Used to keep an open
59
+ * menu still when its trigger moves — starring a timeframe adds a chip and
60
+ * would otherwise drag the list with the caret. `null` falls through to the
61
+ * live trigger. */
62
+ getAnchorRect?: () => { x: number; y: number; width: number; height: number } | null;
63
+ /** Share the trigger's DOM id with other machines composed on the same element. */
64
+ triggerId?: string;
65
+ /** Pin the machine's own id instead of taking a fresh one. A parent registers its
66
+ * submenus under this key, so rebuilding a branch replaces its entry rather than
67
+ * piling a new one on top. */
68
+ id?: string;
69
+ }
70
+
71
+ export type DropdownMenuService = zagMenu.Service;
72
+ export type DropdownMenuApi = zagMenu.Api;
73
+
74
+ export interface DropdownMenuController {
75
+ machine: typeof zagMenu.machine;
76
+ props: Partial<zagMenu.Props>;
77
+ connect(service: DropdownMenuService): DropdownMenuApi;
78
+ }
79
+
80
+ /** Zag's menu machine with the options mapped onto it, for any view layer. */
81
+ export function dropdownMenuController(opts: DropdownMenuControllerOptions): DropdownMenuController {
82
+ return {
83
+ machine: zagMenu.machine,
84
+ props: {
85
+ id: opts.id ?? nextUid('vela-menu'),
86
+ ids: opts.triggerId ? { trigger: opts.triggerId } : undefined,
87
+ positioning: {
88
+ ...zagPositioning(opts, opts.placement ? splitPlacement(opts.placement) : { side: 'bottom', align: 'start' }),
89
+ ...(opts.getAnchorRect ? { getAnchorRect: () => opts.getAnchorRect!() } : {}),
90
+ },
91
+ onSelect: (d: zagMenu.SelectionDetails) => opts.onSelect?.(d.value),
92
+ onOpenChange: (d: zagMenu.OpenChangeDetails) => opts.onOpenChange?.(d.open),
93
+ } satisfies Partial<zagMenu.Props>,
94
+ connect: (service: DropdownMenuService): DropdownMenuApi => zagMenu.connect(service, normalizeProps),
95
+ };
96
+ }
97
+
98
+ export const dropdownMenuDef = defineComponent({
99
+ name: 'dropdown-menu',
100
+ parts: ['content', 'item', 'icon', 'label', 'hint', 'mark', 'badge', 'star', 'switch', 'arrow', 'separator'],
101
+ settings: { radius: t.radius.md, 'switch-radius': '9px' },
102
+ });
103
+
104
+ const injectStyles = defineStyles(
105
+ 'vela-ui-menu',
106
+ css`
107
+ @layer vela.ui {
108
+ [data-slot~='dropdown-menu-content'] {
109
+ /* The list is a <ul>: without this, block rows (the separator) paint a ::marker dot. */
110
+ list-style: none;
111
+ margin: 0;
112
+ background: ${t.surface};
113
+ color: ${t.fg};
114
+ border: 1px solid ${t.border.strong};
115
+ border-radius: ${dropdownMenuDef.vars.radius};
116
+ box-shadow: ${t.shadow};
117
+ padding: 6px;
118
+ min-width: 180px;
119
+ /* Never past the room its clipping container leaves on the side it opened on
120
+ (Zag measures it onto the positioner): a menu in a short chart scrolls inside
121
+ itself instead of overflowing the chart and scrolling the page around it. */
122
+ max-height: min(60vh, var(--available-height, 60vh));
123
+ overflow-y: auto;
124
+ font-size: 13px;
125
+ z-index: ${t.z.menu};
126
+ outline: 2px solid transparent;
127
+
128
+ &[data-state='open'] {
129
+ animation: vela-menu-in ${t.dur.fast} ${t.ease};
130
+ }
131
+ }
132
+ @keyframes vela-menu-in {
133
+ from {
134
+ opacity: 0;
135
+ transform: translateY(-3px);
136
+ }
137
+ to {
138
+ opacity: 1;
139
+ transform: translateY(0);
140
+ }
141
+ }
142
+ [data-slot~='dropdown-menu-item'] {
143
+ display: flex;
144
+ align-items: center;
145
+ gap: 10px;
146
+ padding: 7px 10px;
147
+ border-radius: ${t.radius.sm};
148
+ cursor: pointer;
149
+ user-select: none;
150
+ white-space: nowrap;
151
+
152
+ &[data-highlighted] {
153
+ background: ${t.hover};
154
+ }
155
+ &[data-disabled] {
156
+ opacity: 0.4;
157
+ cursor: default;
158
+ }
159
+ & [data-slot~='dropdown-menu-icon'] {
160
+ width: 16px;
161
+ height: 16px;
162
+ font-size: 16px;
163
+ justify-content: center;
164
+ color: ${t.fg.muted};
165
+ }
166
+ & [data-slot~='dropdown-menu-label'] {
167
+ flex: 1 1 auto;
168
+ }
169
+ & [data-slot~='dropdown-menu-hint'] {
170
+ color: ${t.fg.faint};
171
+ font-size: ${t.fontSize.sm};
172
+ }
173
+ /* The active entry: the row surface carries the selection, a stronger wash
174
+ than the hover one plus bright ink. After the hover rule, so a selected row
175
+ stays visibly selected while highlighted. */
176
+ &[data-checked] {
177
+ background: ${t.hover.strong};
178
+ color: ${t.fg.bright};
179
+ }
180
+ /* Checkmark mode (context and action menus): every row reserves the leading
181
+ mark column so labels align; a checked row fills it with a ✓ and brightens
182
+ its ink, leaving the row surface free for the hover wash. */
183
+ & [data-slot~='dropdown-menu-mark'] {
184
+ width: 14px;
185
+ flex: none;
186
+ display: inline-flex;
187
+ align-items: center;
188
+ justify-content: center;
189
+ }
190
+ & [data-slot~='dropdown-menu-mark'] [data-slot~='dropdown-menu-icon'] {
191
+ width: 14px;
192
+ height: 14px;
193
+ font-size: 14px;
194
+ color: ${t.fg.bright};
195
+ }
196
+ &[data-checkmark] {
197
+ color: ${t.fg.bright};
198
+ }
199
+ /* Badge mode: the row icon sits in a rounded tile and the row grows to fit it.
200
+ An icon-less row of the same level keeps an invisible slot, so labels align. */
201
+ & [data-slot~='dropdown-menu-badge'] {
202
+ width: 24px;
203
+ height: 24px;
204
+ padding: 3px;
205
+ flex: none;
206
+ box-sizing: border-box;
207
+ display: inline-flex;
208
+ align-items: center;
209
+ justify-content: center;
210
+ border-radius: ${t.radius.md};
211
+ background: ${t.hover};
212
+ border: 1px solid ${t.border};
213
+ color: ${t.fg.muted};
214
+ transition:
215
+ background ${t.dur.fast} ${t.ease},
216
+ border-color ${t.dur.fast} ${t.ease},
217
+ color ${t.dur.fast} ${t.ease};
218
+ }
219
+ & [data-slot~='dropdown-menu-badge']:empty {
220
+ background: transparent;
221
+ border-color: transparent;
222
+ }
223
+ & [data-slot~='dropdown-menu-badge'] [data-slot~='dropdown-menu-icon'] {
224
+ color: inherit;
225
+ }
226
+ &[data-highlighted] [data-slot~='dropdown-menu-badge']:not(:empty) {
227
+ color: ${t.fg.bright};
228
+ }
229
+ /* The selected tile inverts: a bright fill under the surface-colored glyph.
230
+ After the hover rule, so a selected row keeps its tile while highlighted. */
231
+ &[data-checked] [data-slot~='dropdown-menu-badge']:not(:empty),
232
+ &[data-checkmark] [data-slot~='dropdown-menu-badge']:not(:empty) {
233
+ background: color-mix(in srgb, ${t.fg.bright} 83%, ${t.surface});
234
+ border-color: color-mix(in srgb, ${t.fg.bright} 40%, transparent);
235
+ color: ${t.surface};
236
+ }
237
+ /* The favorite star: reserved space always (it fades in on row hover instead
238
+ of shifting the layout), and a starred row keeps its filled star visible. */
239
+ & [data-slot~='dropdown-menu-star'] {
240
+ order: 98;
241
+ /* Pad the 16px icon slot into a bigger hit target without moving the row's
242
+ layout: the box is the icon plus its padding. */
243
+ width: 26px;
244
+ height: 22px;
245
+ margin-block: -3px;
246
+ margin-inline: 0 -5px;
247
+ padding: 3px 5px;
248
+ border-radius: ${t.radius.sm};
249
+ opacity: 0;
250
+ transition:
251
+ opacity ${t.dur.fast} ease,
252
+ background ${t.dur.fast} ease;
253
+ }
254
+ &[data-highlighted] [data-slot~='dropdown-menu-star'] {
255
+ opacity: 0.55;
256
+ }
257
+ & [data-slot~='dropdown-menu-star']:hover {
258
+ opacity: 1;
259
+ background: ${t.hover.strong};
260
+ }
261
+ & [data-slot~='dropdown-menu-star'][data-fav] {
262
+ opacity: 0.95;
263
+ color: ${t.highlight};
264
+ }
265
+ /* A submenu trigger row: a chevron at the end, and the row stays highlighted
266
+ while its own list is open, so the path you came down remains readable. */
267
+ & [data-slot~='dropdown-menu-arrow'] {
268
+ order: 99;
269
+ margin-inline-start: auto;
270
+ width: 12px;
271
+ flex: none;
272
+ font-size: 11px;
273
+ color: ${t.fg.faint};
274
+ }
275
+ &[data-state='open'] {
276
+ background: ${t.hover};
277
+ }
278
+ }
279
+ [data-slot~='dropdown-menu-content'][data-badges] > [data-slot~='dropdown-menu-item'] {
280
+ padding-block: 5px;
281
+ padding-inline: 6px 10px;
282
+ }
283
+ /* Switch rows (boolean settings in a dropdown): a toggle pill at the row's end,
284
+ the same control language as the settings dialog's toggles. */
285
+ [data-slot~='dropdown-menu-switch'] {
286
+ order: 99;
287
+ margin-inline-start: auto;
288
+ position: relative;
289
+ flex: none;
290
+ width: 34px;
291
+ height: 18px;
292
+ border-radius: ${dropdownMenuDef.vars['switch-radius']};
293
+ background: ${t.surface.overlay};
294
+ border: 1px solid ${t.border.soft};
295
+ transition:
296
+ background ${t.dur.med} ease,
297
+ border-color ${t.dur.med} ease;
298
+
299
+ &::after {
300
+ content: '';
301
+ position: absolute;
302
+ top: 2px;
303
+ inset-inline-start: 2px;
304
+ width: 12px;
305
+ height: 12px;
306
+ border-radius: 50%;
307
+ background: ${t.fg.muted};
308
+ transition:
309
+ transform ${t.dur.med} ease,
310
+ background ${t.dur.med} ease;
311
+ }
312
+ &[data-on] {
313
+ background: ${t.selected.bg};
314
+ border-color: ${t.selected.bg};
315
+ }
316
+ &[data-on]::after {
317
+ transform: translateX(16px);
318
+ background: ${t.selected.fg};
319
+ }
320
+ }
321
+ [data-slot~='dropdown-menu-separator'] {
322
+ height: 1px;
323
+ margin: 4px 6px;
324
+ background: ${t.border};
325
+ }
326
+ }
327
+ `,
328
+ );
329
+
330
+ /** An icon in a menu row, carrying the row icon part. */
331
+ function menuIcon(id: string, doc: Document): HTMLElement {
332
+ const el = iconEl(id, doc);
333
+ el.dataset.slot = dropdownMenuDef.slot('icon');
334
+ return el;
335
+ }
336
+
337
+ const TEXT = {
338
+ addFavorite: msg('ui.menu.addFavorite', 'Add to favorites'),
339
+ removeFavorite: msg('ui.menu.removeFavorite', 'Remove from favorites'),
340
+ };
341
+
342
+ export type DropdownMenuProps = DropdownMenuOptions;
343
+
344
+ export interface DropdownMenuOptions extends DropdownMenuControllerOptions {
345
+ /** Element that opens the menu on click (gets the machine's trigger props). */
346
+ trigger?: HTMLElement;
347
+ host?: HTMLElement;
348
+ /** Override the root list's min-width (the stylesheet default suits full-word labels;
349
+ * compact lists like the timeframe dropdown pass something snug). Submenus keep the
350
+ * default. */
351
+ minWidth?: string;
352
+ /** Star-toggle reports from items carrying `favorite` (the menu stays open — starring
353
+ * is a side action on a row, never a selection). */
354
+ onFavorite?: (id: string, on: boolean) => void;
355
+ /** Mark checked items with a leading ✓ instead of the selected-row background wash —
356
+ * the shape for pointer-anchored action menus (right-click context menus), where a
357
+ * washed row reads as hover state. A level holding checkable rows reserves the mark
358
+ * column on all its rows so labels align; an all-action level keeps its natural left
359
+ * edge. Submenus inherit the mode. Dropdown menus (a trigger button opening a
360
+ * picker) keep the default wash. */
361
+ checkmarks?: boolean;
362
+ /** Sit each row's icon in a squared badge (a 24px rounded tile with a faint fill and a soft
363
+ * border) instead of a bare glyph: its glyph brightens on a hovered row, and the tile
364
+ * inverts on the selected one. A
365
+ * level where some rows carry an icon reserves the badge column on all its rows so
366
+ * labels align; a level with no icon keeps its natural left edge. With `checkmarks`,
367
+ * the mark column leads and the badge follows. Submenus inherit the mode. */
368
+ iconBadges?: boolean;
369
+ /** The translator of the menu's own words (a star's name). Default: the kit build's, else English. */
370
+ i18n?: Translator | null;
371
+ }
372
+
373
+ /** Where a menu level opens: the shadcn props, or the deprecated `placement`. */
374
+ type MenuPlacement = PlacementProps & Pick<DropdownMenuControllerOptions, 'placement'>;
375
+
376
+ interface SurfaceOptions {
377
+ host: HTMLElement;
378
+ placement: MenuPlacement;
379
+ onSelect: (id: string) => void;
380
+ onOpenChange?: (open: boolean) => void;
381
+ trigger?: HTMLElement;
382
+ /** A submenu's branch row — announced as its opener (it takes trigger-ITEM props, not `trigger`'s). */
383
+ opener?: HTMLElement;
384
+ triggerId?: string;
385
+ id?: string;
386
+ minWidth?: string;
387
+ onFavorite?: (id: string, on: boolean) => void;
388
+ checkmarks?: boolean;
389
+ iconBadges?: boolean;
390
+ i18n: Translator | null;
391
+ /** A branch's level, opened beside its row. */
392
+ submenu?: boolean;
393
+ }
394
+
395
+ /** How far a row sits inside its list: the list's padding (6px) plus its border (1px). */
396
+ const ROW_INSET_PX = 7;
397
+ /** The space between a submenu and the list it opened from. */
398
+ const SUBMENU_GAP_PX = 4;
399
+
400
+ /**
401
+ * One menu level: a Zag machine plus the floating list it projects onto. The root menu is a
402
+ * surface and every branch spawns another, so nesting is depth-agnostic. Private — `Menu`
403
+ * is the public face.
404
+ */
405
+ class Surface {
406
+ readonly list: HTMLElement;
407
+ readonly handle: HandleOf<typeof zagMenu.machine>;
408
+ private readonly positioner: HTMLElement;
409
+ private readonly ctrl: DropdownMenuController;
410
+ private readonly mid: string;
411
+ private readonly doc: Document;
412
+ private readonly host: HTMLElement;
413
+ private readonly onSelect: (id: string) => void;
414
+ private readonly onFavorite?: (id: string, on: boolean) => void;
415
+ private readonly i18n: Translator | null;
416
+ private readonly checkmarks: boolean;
417
+ private readonly iconBadges: boolean;
418
+ private items: readonly MenuItemDescriptor[] = [];
419
+ /** Branch item id → the surface it opens. */
420
+ private readonly subs = new Map<string, Surface>();
421
+ /** Trigger rect captured when this level opened — the list stays put if the
422
+ * trigger then moves (favorite chips shifting the caret). */
423
+ private pinnedAnchor: { x: number; y: number; width: number; height: number } | null = null;
424
+ /** The open state last announced — see {@link announce}. */
425
+ private shown = false;
426
+ /** The list's exit animation after a close; a reopen cancels it. */
427
+ private exit: SurfaceExit | null = null;
428
+ private readonly opener: HTMLElement | null;
429
+
430
+ constructor(doc: Document, opts: SurfaceOptions) {
431
+ this.doc = doc;
432
+ this.host = opts.host;
433
+ this.onSelect = opts.onSelect;
434
+ this.onFavorite = opts.onFavorite;
435
+ this.i18n = opts.i18n;
436
+ this.checkmarks = opts.checkmarks === true;
437
+ this.iconBadges = opts.iconBadges === true;
438
+
439
+ this.positioner = doc.createElement('div');
440
+ this.positioner.className = 'vela-ui-layer';
441
+ this.list = doc.createElement('ul');
442
+ this.list.className = 'vela-menu';
443
+ this.list.dataset.slot = dropdownMenuDef.slot('content');
444
+ if (opts.minWidth) this.list.style.minWidth = opts.minWidth;
445
+ this.positioner.appendChild(this.list);
446
+ this.host.appendChild(this.positioner);
447
+
448
+ const trigger = opts.trigger;
449
+ this.opener = trigger ?? opts.opener ?? null;
450
+ this.ctrl = dropdownMenuController({
451
+ items: [],
452
+ id: opts.id,
453
+ ...opts.placement,
454
+ onSelect: (id) => this.onSelect(id),
455
+ getAnchorRect: () => this.pinnedAnchor,
456
+ onOpenChange: (open) => {
457
+ if (open && trigger) {
458
+ const r = trigger.getBoundingClientRect();
459
+ this.pinnedAnchor = { x: r.x, y: r.y, width: r.width, height: r.height };
460
+ } else {
461
+ this.pinnedAnchor = null;
462
+ }
463
+ opts.onOpenChange?.(open);
464
+ },
465
+ triggerId: opts.triggerId,
466
+ });
467
+ if (opts.submenu) {
468
+ // Zag lays a submenu against its row, which sits inside the parent list: push it out
469
+ // past the list's edge (on whichever side it opens) so it never covers the parent,
470
+ // and up by the same inset so its first row lines up with the row it opened from.
471
+ // Zag sets a submenu's gutter itself; an explicit offset outranks it.
472
+ this.ctrl.props.positioning = {
473
+ ...this.ctrl.props.positioning,
474
+ offset: { mainAxis: ROW_INSET_PX + SUBMENU_GAP_PX },
475
+ shift: -ROW_INSET_PX,
476
+ };
477
+ }
478
+ this.mid = String(this.ctrl.props.id);
479
+ if (opts.triggerId && trigger) trigger.id = opts.triggerId;
480
+ this.handle = runMachine(this.ctrl.machine, this.ctrl.props, (service) => {
481
+ const api = this.ctrl.connect(service);
482
+ // Close announces while the list still shows, open once it does.
483
+ const closing = this.shown && !api.open;
484
+ if (closing) this.announce(false);
485
+ if (api.open && this.exit) {
486
+ this.exit.cancel();
487
+ this.exit = null;
488
+ }
489
+ if (trigger) spreadProps(trigger, api.getTriggerProps(), this.mid);
490
+ spreadProps(this.positioner, api.getPositionerProps(), this.mid);
491
+ spreadProps(this.list, api.getContentProps(), this.mid);
492
+ this.project(api);
493
+ if (closing) this.holdExit();
494
+ if (api.open && !this.shown) this.announce(true);
495
+ });
496
+ }
497
+
498
+ /** Keep the just-closed list up through its exit animation. `spreadProps` re-applies
499
+ * `hidden` only when the machine's value for it changes, so the list stays shown until
500
+ * the exit ends — or until a reopen, which projects `hidden: false` itself. */
501
+ private holdExit(): void {
502
+ this.list.hidden = false;
503
+ this.exit = holdForExit(this.list, () => {
504
+ this.exit = null;
505
+ if (!this.shown) this.list.hidden = true;
506
+ });
507
+ }
508
+
509
+ get api(): zagMenu.Api {
510
+ return this.ctrl.connect(this.handle.service);
511
+ }
512
+
513
+ private announce(open: boolean): void {
514
+ this.shown = open;
515
+ announceSurface(this.list, open, 'menu', this.opener);
516
+ }
517
+
518
+ setItems(items: readonly MenuItemDescriptor[]): void {
519
+ this.items = items;
520
+ for (const sub of this.subs.values()) sub.destroy();
521
+ this.subs.clear();
522
+ this.build();
523
+ const api = this.api;
524
+ for (const sub of this.subs.values()) {
525
+ api.setChild(sub.handle.service);
526
+ sub.api.setParent(this.handle.service);
527
+ }
528
+ this.handle.flush();
529
+ // Those two are queued sends: a child only learns it IS a submenu on the next
530
+ // microtask, and a branch row's props (hover intent, the trigger-item part) are
531
+ // derived from that. Re-project once the queue has drained.
532
+ if (this.subs.size > 0) queueMicrotask(() => this.handle.flush());
533
+ }
534
+
535
+ destroy(): void {
536
+ for (const sub of this.subs.values()) sub.destroy();
537
+ this.subs.clear();
538
+ this.exit?.finish();
539
+ this.handle.stop();
540
+ // Torn down while open: the stopped machine never projects the close.
541
+ if (this.shown) this.announce(false);
542
+ this.positioner.remove();
543
+ }
544
+
545
+ private byId(id: string): MenuItemDescriptor | undefined {
546
+ return this.items.find((i) => i.id === id);
547
+ }
548
+
549
+ /** Push the machine's item props onto the rendered rows. A branch takes the submenu
550
+ * trigger props, which carry the child's ids and hover handlers; a leaf takes plain ones. */
551
+ private project(api: zagMenu.Api): void {
552
+ for (const li of this.list.children) {
553
+ const id = (li as HTMLElement).dataset.veiId;
554
+ if (!id) continue;
555
+ const sub = this.subs.get(id);
556
+ if (sub) {
557
+ spreadProps(li as HTMLElement, api.getTriggerItemProps(sub.api), this.mid);
558
+ continue;
559
+ }
560
+ const item = this.byId(id);
561
+ // Switch rows keep the menu OPEN on selection — flip, see, flip again.
562
+ spreadProps(li as HTMLElement, api.getItemProps({ value: id, disabled: item?.disabled, closeOnSelect: item?.toggle ? false : undefined }), this.mid);
563
+ }
564
+ }
565
+
566
+ private build(): void {
567
+ const doc = this.doc;
568
+ this.list.replaceChildren();
569
+ // Reserve the leading mark column only when THIS level actually holds checkable
570
+ // rows — an all-action list (e.g. the chart-body context menu) keeps its natural
571
+ // left edge instead of carrying an empty gutter.
572
+ const markable = this.checkmarks
573
+ && this.items.some((i) => !(i.submenu && i.submenu.length > 0) && !i.toggle && i.checked !== undefined);
574
+ // Same for icons: once a row of this level carries one, the others keep an empty
575
+ // icon slot (an empty badge in badge mode) so every label shares one left edge.
576
+ const iconic = this.items.some((i) => !!i.icon);
577
+ const badged = this.iconBadges && iconic;
578
+ if (badged) this.list.dataset.badges = '1';
579
+ else delete this.list.dataset.badges;
580
+ for (const item of this.items) {
581
+ if (item.separatorBefore) {
582
+ const sep = doc.createElement('li');
583
+ sep.className = 'vela-menu-sep';
584
+ sep.dataset.slot = dropdownMenuDef.slot('separator');
585
+ sep.setAttribute('role', 'separator');
586
+ this.list.appendChild(sep);
587
+ }
588
+ const branch = item.submenu !== undefined && item.submenu.length > 0;
589
+ const li = doc.createElement('li');
590
+ li.className = 'vela-menu-item';
591
+ li.dataset.slot = dropdownMenuDef.slot('item');
592
+ li.dataset.veiId = item.id;
593
+ if (item.toggle) {
594
+ // Switch row: the pill carries the state.
595
+ // (Zag owns the item's ARIA props; the pill below is decorative.)
596
+ li.dataset.toggle = '1';
597
+ }
598
+ if (markable) {
599
+ // Mark mode: a leading ✓ slot on every row of this level (empty when
600
+ // unchecked, so labels share a left edge); the row surface stays free
601
+ // for hover.
602
+ const mark = doc.createElement('span');
603
+ mark.className = 'vela-menu-mark';
604
+ mark.dataset.slot = dropdownMenuDef.slot('mark');
605
+ if (!branch && !item.toggle && item.checked) {
606
+ mark.appendChild(menuIcon('check', doc));
607
+ li.dataset.checkmark = '1';
608
+ }
609
+ li.appendChild(mark);
610
+ } else if (!branch && !item.toggle && item.checked) {
611
+ // Selection reads from the row surface: a stronger background wash
612
+ // marks the active entry (hover stays the lighter wash).
613
+ li.dataset.checked = '1';
614
+ }
615
+ if (badged) {
616
+ const badge = doc.createElement('span');
617
+ badge.className = 'vela-menu-badge';
618
+ badge.dataset.slot = dropdownMenuDef.slot('badge');
619
+ if (item.icon) badge.appendChild(menuIcon(item.icon, doc));
620
+ li.appendChild(badge);
621
+ } else if (item.icon) {
622
+ li.appendChild(menuIcon(item.icon, doc));
623
+ } else if (iconic) {
624
+ li.appendChild(menuIcon('', doc));
625
+ }
626
+ const label = doc.createElement('span');
627
+ label.className = 'vela-menu-label';
628
+ label.dataset.slot = dropdownMenuDef.slot('label');
629
+ label.textContent = item.label;
630
+ li.appendChild(label);
631
+ if (item.hint) {
632
+ const hint = doc.createElement('span');
633
+ hint.className = 'vela-menu-hint';
634
+ hint.dataset.slot = dropdownMenuDef.slot('hint');
635
+ hint.textContent = item.hint;
636
+ li.appendChild(hint);
637
+ }
638
+ if (item.favorite !== undefined && this.onFavorite) {
639
+ const on = item.favorite;
640
+ const star = menuIcon(on ? 'star-filled' : 'star', doc);
641
+ star.classList.add('vela-menu-star');
642
+ addSlot(star, dropdownMenuDef.slot('star'));
643
+ if (on) {
644
+ star.classList.add('vela-fav');
645
+ star.dataset.fav = '';
646
+ }
647
+ star.removeAttribute('aria-hidden'); // iconEl hides decorative icons; this one is a control
648
+ star.setAttribute('role', 'button');
649
+ star.setAttribute('aria-label', textOf(this.i18n, on ? TEXT.removeFavorite : TEXT.addFavorite));
650
+ star.setAttribute('aria-pressed', on ? 'true' : 'false');
651
+ // Swallow the whole pointer sequence: the machine selects rows on click,
652
+ // and a press that starts on the star must never reach the row.
653
+ for (const ev of ['pointerdown', 'pointerup', 'mousedown', 'mouseup'] as const) {
654
+ star.addEventListener(ev, (e) => e.stopPropagation());
655
+ }
656
+ star.addEventListener('click', (e) => {
657
+ e.stopPropagation();
658
+ e.preventDefault();
659
+ this.onFavorite?.(item.id, !on);
660
+ });
661
+ li.appendChild(star);
662
+ }
663
+ if (item.toggle) {
664
+ const sw = doc.createElement('span');
665
+ sw.className = 'vela-menu-switch' + (item.checked ? ' on' : '');
666
+ sw.dataset.slot = dropdownMenuDef.slot('switch');
667
+ if (item.checked) sw.dataset.on = '';
668
+ sw.setAttribute('aria-hidden', 'true');
669
+ li.appendChild(sw);
670
+ }
671
+ if (branch) {
672
+ li.dataset.branch = '1';
673
+ const arrow = menuIcon('chevron-right', doc);
674
+ arrow.classList.add('vela-menu-arrow');
675
+ addSlot(arrow, dropdownMenuDef.slot('arrow'));
676
+ li.appendChild(arrow);
677
+ const sub = new Surface(doc, {
678
+ host: this.host,
679
+ placement: { placement: 'right-start' },
680
+ submenu: true,
681
+ opener: li,
682
+ onSelect: this.onSelect,
683
+ onFavorite: this.onFavorite,
684
+ i18n: this.i18n,
685
+ checkmarks: this.checkmarks,
686
+ iconBadges: this.iconBadges,
687
+ id: `${this.mid}--${item.id}`,
688
+ });
689
+ sub.setItems(item.submenu ?? []);
690
+ this.subs.set(item.id, sub);
691
+ }
692
+ this.list.appendChild(li);
693
+ }
694
+ }
695
+ }
696
+
697
+ export class DropdownMenu {
698
+ private readonly root: Surface;
699
+
700
+ constructor(opts: DropdownMenuOptions) {
701
+ const anchor = opts.trigger ?? opts.host ?? document.body;
702
+ const doc = anchor.ownerDocument;
703
+ injectStyles(doc);
704
+ const host = floatingLayerHost(opts.host ?? anchor, doc.body);
705
+ this.root = new Surface(doc, {
706
+ host,
707
+ placement: {
708
+ placement: opts.placement,
709
+ side: opts.side,
710
+ align: opts.align,
711
+ sideOffset: opts.sideOffset,
712
+ alignOffset: opts.alignOffset,
713
+ avoidCollisions: opts.avoidCollisions,
714
+ collisionPadding: opts.collisionPadding,
715
+ collisionBoundary: opts.collisionBoundary,
716
+ },
717
+ onSelect: (id) => opts.onSelect?.(id),
718
+ onOpenChange: opts.onOpenChange,
719
+ trigger: opts.trigger,
720
+ triggerId: opts.triggerId,
721
+ id: opts.id,
722
+ minWidth: opts.minWidth,
723
+ onFavorite: opts.onFavorite,
724
+ i18n: opts.i18n ?? kitTranslator(),
725
+ checkmarks: opts.checkmarks,
726
+ iconBadges: opts.iconBadges,
727
+ });
728
+ this.root.setItems(opts.items);
729
+ }
730
+
731
+ get api(): zagMenu.Api {
732
+ return this.root.api;
733
+ }
734
+
735
+ open(): void {
736
+ this.api.setOpen(true);
737
+ }
738
+
739
+ /** Open anchored to a viewport point (context menus). */
740
+ openAt(clientX: number, clientY: number): void {
741
+ const api = this.api;
742
+ api.setOpen(true);
743
+ api.reposition({ getAnchorRect: () => ({ x: clientX, y: clientY, width: 0, height: 0 }) });
744
+ }
745
+
746
+ close(): void {
747
+ this.api.setOpen(false);
748
+ }
749
+
750
+ /** Swap the item descriptors (e.g. checked states) and re-project. */
751
+ setItems(items: readonly MenuItemDescriptor[]): void {
752
+ this.root.setItems(items);
753
+ }
754
+
755
+ destroy(): void {
756
+ this.root.destroy();
757
+ }
758
+ }
759
+
760
+ export function createDropdownMenu(props: DropdownMenuProps): DropdownMenu {
761
+ return new DropdownMenu(props);
762
+ }