@luxalgo/vela-ui 0.0.0-stage → 0.1.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 (248) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +28 -0
  4. package/README.md +83 -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 +41 -0
  12. package/dist/button.js +10 -0
  13. package/dist/calendar.d.ts +22 -0
  14. package/dist/calendar.js +1 -0
  15. package/dist/callout-bubble.d.ts +98 -0
  16. package/dist/callout-bubble.js +173 -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-2Q4JRINP.js +403 -0
  23. package/dist/chunk-3Q32K4PE.js +85 -0
  24. package/dist/chunk-5QHXOAZA.js +351 -0
  25. package/dist/chunk-6NXY62JS.js +25 -0
  26. package/dist/chunk-6QCILIT2.js +52 -0
  27. package/dist/chunk-A2FJ7Y7D.js +55 -0
  28. package/dist/chunk-A72NWYNP.js +26 -0
  29. package/dist/chunk-AJ2IYSOI.js +553 -0
  30. package/dist/chunk-BXMWQIBO.js +66 -0
  31. package/dist/chunk-CH3DV6FD.js +280 -0
  32. package/dist/chunk-EZQXTO6K.js +89 -0
  33. package/dist/chunk-FCO3YZLT.js +81 -0
  34. package/dist/chunk-FN2BUFNM.js +10 -0
  35. package/dist/chunk-FVH6L7FM.js +18 -0
  36. package/dist/chunk-G7IQLY3A.js +183 -0
  37. package/dist/chunk-IDA2BT7D.js +29 -0
  38. package/dist/chunk-J2QJATCD.js +12 -0
  39. package/dist/chunk-LZWZV2ZS.js +18 -0
  40. package/dist/chunk-MMTMOV6R.js +31 -0
  41. package/dist/chunk-OBLIX3VN.js +227 -0
  42. package/dist/chunk-P4MTEMT7.js +23 -0
  43. package/dist/chunk-Q6WTDVH6.js +31 -0
  44. package/dist/chunk-S7USVSYP.js +50 -0
  45. package/dist/chunk-SAPURP46.js +133 -0
  46. package/dist/chunk-TAKSIV57.js +90 -0
  47. package/dist/chunk-V6TTTMTE.js +79 -0
  48. package/dist/chunk-VMCR3S2A.js +22 -0
  49. package/dist/chunk-X4YPSIW6.js +72 -0
  50. package/dist/chunk-X66RJAIS.js +14 -0
  51. package/dist/chunk-XJAPCCOJ.js +101 -0
  52. package/dist/chunk-ZB3BZHSN.js +219 -0
  53. package/dist/color-picker.d.ts +103 -0
  54. package/dist/color-picker.js +19 -0
  55. package/dist/color.d.ts +24 -0
  56. package/dist/color.js +1 -0
  57. package/dist/context-menu.d.ts +72 -0
  58. package/dist/context-menu.js +161 -0
  59. package/dist/css.d.ts +15 -0
  60. package/dist/css.js +4 -0
  61. package/dist/data-table-DtU3FX9j.d.ts +61 -0
  62. package/dist/data-table.d.ts +3 -0
  63. package/dist/data-table.js +88 -0
  64. package/dist/date-picker.d.ts +90 -0
  65. package/dist/date-picker.js +257 -0
  66. package/dist/define-D5u3F5Dj.d.ts +21 -0
  67. package/dist/dialog.d.ts +89 -0
  68. package/dist/dialog.js +258 -0
  69. package/dist/dispose.d.ts +9 -0
  70. package/dist/dispose.js +2 -0
  71. package/dist/donut.d.ts +27 -0
  72. package/dist/donut.js +67 -0
  73. package/dist/drawer.d.ts +119 -0
  74. package/dist/drawer.js +326 -0
  75. package/dist/dropdown-menu.d.ts +114 -0
  76. package/dist/dropdown-menu.js +18 -0
  77. package/dist/empty.d.ts +21 -0
  78. package/dist/empty.js +38 -0
  79. package/dist/field.d.ts +147 -0
  80. package/dist/field.js +328 -0
  81. package/dist/glyph-select.d.ts +57 -0
  82. package/dist/glyph-select.js +17 -0
  83. package/dist/headless.d.ts +6 -0
  84. package/dist/headless.js +9 -0
  85. package/dist/hover-card.d.ts +43 -0
  86. package/dist/hover-card.js +97 -0
  87. package/dist/i18n.d.ts +20 -0
  88. package/dist/i18n.js +1 -0
  89. package/dist/icons.d.ts +20 -0
  90. package/dist/icons.js +2 -0
  91. package/dist/index.d.ts +45 -0
  92. package/dist/index.js +13 -0
  93. package/dist/input-group.d.ts +57 -0
  94. package/dist/input-group.js +86 -0
  95. package/dist/input.d.ts +54 -0
  96. package/dist/input.js +9 -0
  97. package/dist/kbd.d.ts +19 -0
  98. package/dist/kbd.js +8 -0
  99. package/dist/kit-text.d.ts +8 -0
  100. package/dist/kit-text.js +2 -0
  101. package/dist/line-chart.d.ts +32 -0
  102. package/dist/line-chart.js +120 -0
  103. package/dist/manifest.json +3854 -0
  104. package/dist/number-input.d.ts +99 -0
  105. package/dist/number-input.js +14 -0
  106. package/dist/page.d.ts +7 -0
  107. package/dist/page.js +1 -0
  108. package/dist/palette.d.ts +55 -0
  109. package/dist/palette.js +1 -0
  110. package/dist/placement.d.ts +98 -0
  111. package/dist/placement.js +1 -0
  112. package/dist/popover.d.ts +107 -0
  113. package/dist/popover.js +15 -0
  114. package/dist/scatter-chart.d.ts +32 -0
  115. package/dist/scatter-chart.js +121 -0
  116. package/dist/select.d.ts +112 -0
  117. package/dist/select.js +19 -0
  118. package/dist/sparkline.d.ts +22 -0
  119. package/dist/sparkline.js +72 -0
  120. package/dist/stat.d.ts +29 -0
  121. package/dist/stat.js +41 -0
  122. package/dist/styles.d.ts +22 -0
  123. package/dist/styles.js +2 -0
  124. package/dist/surface-events.d.ts +17 -0
  125. package/dist/surface-events.js +1 -0
  126. package/dist/surface-exit.d.ts +25 -0
  127. package/dist/surface-exit.js +1 -0
  128. package/dist/switch.d.ts +54 -0
  129. package/dist/switch.js +92 -0
  130. package/dist/t.d.ts +89 -0
  131. package/dist/t.js +1 -0
  132. package/dist/tabs.d.ts +63 -0
  133. package/dist/tabs.js +150 -0
  134. package/dist/text-area.d.ts +54 -0
  135. package/dist/text-area.js +9 -0
  136. package/dist/theme.d.ts +188 -0
  137. package/dist/theme.js +8 -0
  138. package/dist/theme.schema.json +1745 -0
  139. package/dist/themes/dark.css +3 -0
  140. package/dist/themes/light.css +3 -0
  141. package/dist/toggle-group.d.ts +56 -0
  142. package/dist/toggle-group.js +130 -0
  143. package/dist/tokens.d.ts +16 -0
  144. package/dist/tokens.js +3 -0
  145. package/dist/tooltip.d.ts +47 -0
  146. package/dist/tooltip.js +74 -0
  147. package/dist/variants-B4stUBnY.d.ts +16 -0
  148. package/dist/zag.d.ts +19 -0
  149. package/dist/zag.js +2 -0
  150. package/docs/theming.md +211 -0
  151. package/docs/what-to-change.md +20 -0
  152. package/docs/writing-a-component.md +247 -0
  153. package/llms.txt +66 -0
  154. package/package.json +90 -6
  155. package/registry/badge.ts +119 -0
  156. package/registry/bar-chart.ts +215 -0
  157. package/registry/button.ts +303 -0
  158. package/registry/callout-bubble.ts +336 -0
  159. package/registry/checkbox.ts +234 -0
  160. package/registry/color-picker.ts +582 -0
  161. package/registry/context-menu.ts +208 -0
  162. package/registry/data-table.ts +215 -0
  163. package/registry/date-picker.ts +483 -0
  164. package/registry/dialog.ts +535 -0
  165. package/registry/donut.ts +145 -0
  166. package/registry/drawer.ts +636 -0
  167. package/registry/dropdown-menu.ts +762 -0
  168. package/registry/empty.ts +91 -0
  169. package/registry/field.ts +585 -0
  170. package/registry/glyph-select.ts +268 -0
  171. package/registry/hover-card.ts +222 -0
  172. package/registry/input-group.ts +229 -0
  173. package/registry/input.ts +181 -0
  174. package/registry/kbd.ts +64 -0
  175. package/registry/line-chart.ts +218 -0
  176. package/registry/number-input.ts +443 -0
  177. package/registry/popover.ts +368 -0
  178. package/registry/scatter-chart.ts +221 -0
  179. package/registry/select.ts +620 -0
  180. package/registry/sparkline.ts +114 -0
  181. package/registry/stat.ts +96 -0
  182. package/registry/switch.ts +205 -0
  183. package/registry/tabs.ts +392 -0
  184. package/registry/text-area.ts +186 -0
  185. package/registry/toggle-group.ts +295 -0
  186. package/registry/tooltip.ts +156 -0
  187. package/skills/theming-vela-ui/SKILL.md +137 -0
  188. package/skills/using-vela-ui-components/SKILL.md +134 -0
  189. package/skills/writing-vela-ui-components/SKILL.md +163 -0
  190. package/src/components/badge.ts +119 -0
  191. package/src/components/bar-chart.ts +215 -0
  192. package/src/components/button.ts +303 -0
  193. package/src/components/callout-bubble.ts +336 -0
  194. package/src/components/checkbox.ts +234 -0
  195. package/src/components/color-picker.ts +582 -0
  196. package/src/components/context-menu.ts +208 -0
  197. package/src/components/data-table.ts +215 -0
  198. package/src/components/date-picker.ts +483 -0
  199. package/src/components/dialog.ts +535 -0
  200. package/src/components/donut.ts +145 -0
  201. package/src/components/drawer.ts +636 -0
  202. package/src/components/dropdown-menu.ts +762 -0
  203. package/src/components/empty.ts +91 -0
  204. package/src/components/field.ts +585 -0
  205. package/src/components/glyph-select.ts +268 -0
  206. package/src/components/hover-card.ts +222 -0
  207. package/src/components/input-group.ts +229 -0
  208. package/src/components/input.ts +181 -0
  209. package/src/components/kbd.ts +64 -0
  210. package/src/components/line-chart.ts +218 -0
  211. package/src/components/number-input.ts +443 -0
  212. package/src/components/popover.ts +368 -0
  213. package/src/components/scatter-chart.ts +221 -0
  214. package/src/components/select.ts +620 -0
  215. package/src/components/sparkline.ts +114 -0
  216. package/src/components/stat.ts +96 -0
  217. package/src/components/switch.ts +205 -0
  218. package/src/components/tabs.ts +392 -0
  219. package/src/components/text-area.ts +186 -0
  220. package/src/components/toggle-group.ts +295 -0
  221. package/src/components/tooltip.ts +156 -0
  222. package/src/index.ts +24 -0
  223. package/src/lib/calendar.ts +112 -0
  224. package/src/lib/chart.ts +72 -0
  225. package/src/lib/color.ts +89 -0
  226. package/src/lib/component-index.ts +131 -0
  227. package/src/lib/css.ts +44 -0
  228. package/src/lib/define.ts +45 -0
  229. package/src/lib/dispose.ts +36 -0
  230. package/src/lib/h.ts +55 -0
  231. package/src/lib/headless.ts +16 -0
  232. package/src/lib/i18n.ts +30 -0
  233. package/src/lib/icons.ts +237 -0
  234. package/src/lib/kit-text.ts +26 -0
  235. package/src/lib/page.ts +24 -0
  236. package/src/lib/palette.ts +81 -0
  237. package/src/lib/placement.ts +188 -0
  238. package/src/lib/styles.ts +53 -0
  239. package/src/lib/surface-events.ts +31 -0
  240. package/src/lib/surface-exit.ts +87 -0
  241. package/src/lib/t.ts +68 -0
  242. package/src/lib/theme.ts +654 -0
  243. package/src/lib/tokens.ts +111 -0
  244. package/src/lib/variants.ts +32 -0
  245. package/src/lib/zag.ts +46 -0
  246. package/stylelint/index.mjs +60 -0
  247. package/stylelint/lint.mjs +81 -0
  248. package/stylelint/plugin.mjs +115 -0
@@ -0,0 +1,295 @@
1
+ // ToggleGroup: a few options side by side on one track, over Zag's toggle-group machine.
2
+ // `single` (default): one option is always chosen (a radio group, a segmented control);
3
+ // `multiple`: each option toggles on its own. The arrow keys move focus; Space and Enter
4
+ // press. The track and the chosen option are tints of the foreground, so both read on a dark
5
+ // and a light theme.
6
+ import * as toggleGroup from '@zag-js/toggle-group';
7
+ import { iconEl } from '../lib/icons';
8
+ import { css, defineStyles } from '../lib/css';
9
+ import { defineComponent } from '../lib/define';
10
+ import { ownTeardown } from '../lib/dispose';
11
+ import { h } from '../lib/h';
12
+ import { t } from '../lib/t';
13
+ import { variants, type VariantProps } from '../lib/variants';
14
+ import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '../lib/zag';
15
+
16
+ export const toggleGroupDef = defineComponent({
17
+ name: 'toggle-group',
18
+ parts: ['root', 'item', 'icon'],
19
+ settings: { radius: '8px', 'radius-sm': '7px', 'item-radius': t.radius.md, 'item-radius-sm': '5px' },
20
+ });
21
+
22
+ /** `default` = 32px; `sm` = 28px. */
23
+ export const toggleGroupVariants = variants({ size: ['default', 'sm'] });
24
+
25
+ export interface ToggleGroupItem {
26
+ value: string;
27
+ /** Visible text, or the accessible name of an icon-only option. */
28
+ label: string;
29
+ icon?: string;
30
+ disabled?: boolean;
31
+ /** Fill the option with a market color while it is on (buy and sell). */
32
+ tone?: 'up' | 'down';
33
+ }
34
+
35
+ interface ToggleGroupCommonProps extends VariantProps<typeof toggleGroupVariants> {
36
+ items: readonly ToggleGroupItem[];
37
+ /** Accessible name of the group. */
38
+ label?: string;
39
+ /** Options show their icon only; each label becomes its option's accessible name. */
40
+ iconOnly?: boolean;
41
+ dir?: 'ltr' | 'rtl';
42
+ /** Stretch across the parent, options sharing the width. */
43
+ fill?: boolean;
44
+ className?: string;
45
+ }
46
+
47
+ export interface ToggleGroupSingleProps extends ToggleGroupCommonProps {
48
+ type?: 'single';
49
+ /** The chosen option (default: the first enabled one). */
50
+ defaultValue?: string;
51
+ onValueChange?: (value: string) => void;
52
+ }
53
+
54
+ export interface ToggleGroupMultipleProps extends ToggleGroupCommonProps {
55
+ type: 'multiple';
56
+ /** The options turned on (default: none). */
57
+ defaultValue?: readonly string[];
58
+ onValueChange?: (value: string[]) => void;
59
+ }
60
+
61
+ export type ToggleGroupProps = ToggleGroupSingleProps | ToggleGroupMultipleProps;
62
+
63
+ export interface ToggleGroupHandle {
64
+ readonly el: HTMLElement;
65
+ /** The options turned on. */
66
+ readonly values: readonly string[];
67
+ /** Show options as on without calling `onValueChange`. */
68
+ setValue(value: string | readonly string[] | null): void;
69
+ setItems(items: readonly ToggleGroupItem[]): void;
70
+ destroy(): void;
71
+ }
72
+
73
+ export const TOGGLE_GROUP_STYLE_ID = 'vela-ui-toggle-group';
74
+
75
+ export const TOGGLE_GROUP_CSS = css`
76
+ @layer vela.ui {
77
+ [data-slot='toggle-group-item'] {
78
+ all: unset;
79
+ box-sizing: border-box;
80
+ display: inline-flex;
81
+ align-items: center;
82
+ justify-content: center;
83
+ gap: 6px;
84
+ min-width: 0;
85
+ padding: 0 10px;
86
+ border-radius: ${toggleGroupDef.vars['item-radius']};
87
+ color: ${t.fg.muted};
88
+ font-size: 13px;
89
+ font-weight: 500;
90
+ white-space: nowrap;
91
+ cursor: pointer;
92
+ -webkit-tap-highlight-color: transparent;
93
+ transition:
94
+ background ${t.dur.fast} ease,
95
+ color ${t.dur.fast} ease;
96
+
97
+ & > [data-slot='toggle-group-icon'] {
98
+ width: 16px;
99
+ height: 16px;
100
+ flex: none;
101
+ }
102
+ &[data-icon-only] {
103
+ padding: 0 8px;
104
+ }
105
+ &[data-state='off']:not([data-disabled]):hover {
106
+ color: ${t.fg.bright};
107
+ }
108
+ &[data-state='on'] {
109
+ background: color-mix(in srgb, ${t.fg.bright} 12%, transparent);
110
+ color: ${t.fg.bright};
111
+ }
112
+ &[data-state='on'][data-tone='up'] {
113
+ background: ${t.up};
114
+ color: ${t.fg.onFill};
115
+ }
116
+ &[data-state='on'][data-tone='down'] {
117
+ background: ${t.down};
118
+ color: ${t.fg.onFill};
119
+ }
120
+ &[data-disabled] {
121
+ cursor: default;
122
+ opacity: 0.45;
123
+ }
124
+ &:focus-visible {
125
+ outline: 2px solid ${t.focus};
126
+ outline-offset: -2px;
127
+ }
128
+ }
129
+ [data-slot='toggle-group'] {
130
+ align-items: stretch;
131
+ gap: 2px;
132
+ padding: 3px;
133
+ box-sizing: border-box;
134
+ height: 32px;
135
+ border-radius: ${toggleGroupDef.vars.radius};
136
+ background: color-mix(in srgb, ${t.fg.bright} 6%, transparent);
137
+
138
+ &:where(:not([hidden])) {
139
+ display: inline-flex;
140
+ }
141
+ &[data-size='sm'] {
142
+ height: 28px;
143
+ padding: 2px;
144
+ border-radius: ${toggleGroupDef.vars['radius-sm']};
145
+ }
146
+ &[data-fill] {
147
+ width: 100%;
148
+ }
149
+ &[data-fill]:where(:not([hidden])) {
150
+ display: flex;
151
+ }
152
+ &[data-fill] > [data-slot='toggle-group-item'] {
153
+ flex: 1 1 0;
154
+ }
155
+ &[data-size='sm'] > [data-slot='toggle-group-item'] {
156
+ padding: 0 8px;
157
+ border-radius: ${toggleGroupDef.vars['item-radius-sm']};
158
+ font-size: 12px;
159
+ }
160
+ }
161
+ }
162
+ `;
163
+
164
+ const injectStyles = defineStyles(TOGGLE_GROUP_STYLE_ID, TOGGLE_GROUP_CSS);
165
+
166
+ const keyOf = (values: readonly string[]): string => values.join('\u0000');
167
+
168
+ class ToggleGroupImpl implements ToggleGroupHandle {
169
+ readonly el: HTMLElement;
170
+ private readonly machine: HandleOf<typeof toggleGroup.machine>;
171
+ private readonly mid: string;
172
+ private readonly release: () => void;
173
+ private readonly iconOnly: boolean;
174
+ private readonly label: string | undefined;
175
+ private items: ToggleGroupItem[];
176
+ private readonly buttons = new Map<string, HTMLButtonElement>();
177
+ /** The options on as of the last write or report: the machine applies events on a
178
+ * microtask, so its own value lags a write. */
179
+ private current: string[];
180
+ /** A host write the machine has yet to report: that report is not the user's change. */
181
+ private silentNext: string | undefined;
182
+
183
+ constructor(props: ToggleGroupProps) {
184
+ injectStyles(document);
185
+ this.iconOnly = props.iconOnly ?? false;
186
+ this.label = props.label;
187
+ this.items = [...props.items];
188
+ const multiple = props.type === 'multiple';
189
+ this.current =
190
+ props.type === 'multiple' ? [...(props.defaultValue ?? [])] : [props.defaultValue ?? props.items.find((i) => !i.disabled)?.value].filter((v): v is string => v !== undefined);
191
+ this.mid = nextUid('vela-toggle-group');
192
+ this.el = h('div', {
193
+ className: props.className ? `vela-toggle-group ${props.className}` : 'vela-toggle-group',
194
+ slot: toggleGroupDef.slot('root'),
195
+ attrs: toggleGroupVariants(props),
196
+ data: { fill: props.fill === true },
197
+ });
198
+ this.build();
199
+ const onValueChange = props.onValueChange as ((v: string | string[]) => void) | undefined;
200
+ this.machine = runMachine(
201
+ toggleGroup.machine,
202
+ {
203
+ id: this.mid,
204
+ multiple,
205
+ // A single group never empties: pressing the chosen option keeps it chosen.
206
+ deselectable: false,
207
+ defaultValue: [...this.current],
208
+ dir: props.dir ?? 'ltr',
209
+ loopFocus: true,
210
+ rovingFocus: true,
211
+ onValueChange: ({ value }) => {
212
+ if (!multiple && value[0] === undefined) return;
213
+ const next = multiple ? [...value] : [value[0]!];
214
+ const key = keyOf(next);
215
+ // The machine compares arrays by identity: a press that changes nothing reports too.
216
+ if (key === keyOf(this.current)) {
217
+ if (this.silentNext === key) this.silentNext = undefined;
218
+ return;
219
+ }
220
+ this.current = next;
221
+ if (this.silentNext === key) {
222
+ this.silentNext = undefined;
223
+ return;
224
+ }
225
+ onValueChange?.(multiple ? [...next] : next[0]!);
226
+ },
227
+ },
228
+ (service) => this.render(service),
229
+ );
230
+ this.release = ownTeardown(this.el, () => this.machine.stop());
231
+ }
232
+
233
+ get values(): readonly string[] {
234
+ return this.current;
235
+ }
236
+
237
+ setValue(value: string | readonly string[] | null): void {
238
+ const next = value === null ? [] : typeof value === 'string' ? [value] : [...value];
239
+ const key = keyOf(next);
240
+ if (key === keyOf(this.current)) return;
241
+ this.current = next;
242
+ this.silentNext = key;
243
+ this.api().setValue(next);
244
+ }
245
+
246
+ setItems(items: readonly ToggleGroupItem[]): void {
247
+ this.items = [...items];
248
+ this.build();
249
+ const kept = this.current.filter((v) => this.items.some((i) => i.value === v));
250
+ if (kept.length !== this.current.length) this.setValue(kept);
251
+ this.machine.flush();
252
+ }
253
+
254
+ destroy(): void {
255
+ this.release();
256
+ }
257
+
258
+ private api(): toggleGroup.Api {
259
+ return toggleGroup.connect(this.machine.service, normalizeProps);
260
+ }
261
+
262
+ private build(): void {
263
+ this.buttons.clear();
264
+ const doc = this.el.ownerDocument;
265
+ this.el.replaceChildren(
266
+ ...this.items.map((item) => {
267
+ const icon = item.icon ? iconEl(item.icon, doc) : null;
268
+ if (icon) icon.dataset.slot = toggleGroupDef.slot('icon');
269
+ const iconOnly = this.iconOnly && icon !== null;
270
+ const b = h(
271
+ 'button',
272
+ { className: 'vela-toggle-group-item', slot: toggleGroupDef.slot('item'), data: { 'icon-only': iconOnly, tone: item.tone ?? null }, attrs: { 'aria-label': iconOnly ? item.label : null } },
273
+ icon,
274
+ iconOnly ? null : item.label,
275
+ );
276
+ this.buttons.set(item.value, b);
277
+ return b;
278
+ }),
279
+ );
280
+ }
281
+
282
+ private render(service: toggleGroup.Service): void {
283
+ const api = toggleGroup.connect(service, normalizeProps);
284
+ spreadProps(this.el, api.getRootProps(), this.mid);
285
+ if (this.label) this.el.setAttribute('aria-label', this.label);
286
+ for (const item of this.items) {
287
+ const b = this.buttons.get(item.value);
288
+ if (b) spreadProps(b, api.getItemProps({ value: item.value, disabled: item.disabled }), this.mid);
289
+ }
290
+ }
291
+ }
292
+
293
+ export function createToggleGroup(props: ToggleGroupProps): ToggleGroupHandle {
294
+ return new ToggleGroupImpl(props);
295
+ }
@@ -0,0 +1,156 @@
1
+ // Tooltip: a floating label over Zag's tooltip machine (floating-ui placement). The floating
2
+ // layer mounts into `host` (default: the trigger's nearest `.vela-ui` ancestor, else <body>)
3
+ // so the design tokens inherit. The trigger is the host's own element and keeps its slot.
4
+ import * as tooltip from '@zag-js/tooltip';
5
+ import { css, defineStyles } from '../lib/css';
6
+ import { defineComponent } from '../lib/define';
7
+ import { ownTeardown } from '../lib/dispose';
8
+ import { h } from '../lib/h';
9
+ import { t } from '../lib/t';
10
+ import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '../lib/zag';
11
+ import { splitPlacement, zagPositioning, type PlacementProps } from '../lib/placement';
12
+
13
+ export interface TooltipControllerOptions extends PlacementProps {
14
+ /** @deprecated Use `side` and `align`. */
15
+ placement?: tooltip.Placement;
16
+ /** ms before opening on hover (default 0 — the reference tooltips are instant). */
17
+ openDelay?: number;
18
+ closeDelay?: number;
19
+ /** Keep open while the pointer is over the content (rich tooltips). */
20
+ interactive?: boolean;
21
+ /** Share the trigger's DOM id with other machines composed on the SAME element
22
+ * (each Zag machine otherwise stamps its own id — last one wins and breaks the rest). */
23
+ triggerId?: string;
24
+ }
25
+
26
+ export type TooltipService = tooltip.Service;
27
+ export type TooltipApi = tooltip.Api;
28
+
29
+ export interface TooltipController {
30
+ machine: typeof tooltip.machine;
31
+ props: Partial<tooltip.Props>;
32
+ connect(service: TooltipService): TooltipApi;
33
+ }
34
+
35
+ /** Zag's tooltip machine with the options mapped onto it, for any view layer. */
36
+ export function tooltipController(opts: TooltipControllerOptions = {}): TooltipController {
37
+ return {
38
+ machine: tooltip.machine,
39
+ props: {
40
+ id: nextUid('vela-tooltip'),
41
+ openDelay: opts.openDelay ?? 0,
42
+ closeDelay: opts.closeDelay ?? 0,
43
+ interactive: opts.interactive ?? false,
44
+ ids: opts.triggerId ? { trigger: opts.triggerId } : undefined,
45
+ positioning: zagPositioning(opts, opts.placement ? splitPlacement(opts.placement) : { side: 'top', align: 'center' }),
46
+ } satisfies Partial<tooltip.Props>,
47
+ connect: (service: TooltipService): TooltipApi => tooltip.connect(service, normalizeProps),
48
+ };
49
+ }
50
+
51
+ export const tooltipDef = defineComponent({
52
+ name: 'tooltip',
53
+ parts: ['positioner', 'content'],
54
+ settings: { radius: t.radius.md },
55
+ });
56
+
57
+ export interface TooltipOptions extends TooltipControllerOptions {
58
+ content: string | Node | (() => Node);
59
+ host?: HTMLElement;
60
+ }
61
+
62
+ export interface TooltipProps extends TooltipOptions {
63
+ trigger: HTMLElement;
64
+ }
65
+
66
+ const injectStyles = defineStyles(
67
+ 'vela-ui-tooltip',
68
+ css`
69
+ @layer vela.ui {
70
+ [data-slot='tooltip-content'] {
71
+ background: ${t.bg};
72
+ color: ${t.fg};
73
+ border: 1px solid ${t.border.soft};
74
+ border-radius: ${tooltipDef.vars.radius};
75
+ box-shadow: ${t.shadow};
76
+ font-size: ${t.fontSize.md};
77
+ line-height: 1.4;
78
+ padding: ${t.space[1]} ${t.space[2]};
79
+ max-width: 280px;
80
+ pointer-events: none;
81
+ z-index: ${t.z.tooltip};
82
+
83
+ &[data-interactive] {
84
+ pointer-events: auto;
85
+ }
86
+ &[data-state='open'] {
87
+ animation: vela-tooltip-in ${t.dur.fast} ease;
88
+ }
89
+ }
90
+ @keyframes vela-tooltip-in {
91
+ from {
92
+ opacity: 0;
93
+ transform: scale(0.97);
94
+ }
95
+ to {
96
+ opacity: 1;
97
+ transform: scale(1);
98
+ }
99
+ }
100
+ }
101
+ `,
102
+ );
103
+
104
+ function resolveHost(trigger: HTMLElement, host?: HTMLElement): HTMLElement {
105
+ return host ?? (trigger.closest('.vela-ui') as HTMLElement | null) ?? trigger.ownerDocument.body;
106
+ }
107
+
108
+ export class Tooltip {
109
+ private readonly positioner: HTMLElement;
110
+ private readonly content: HTMLElement;
111
+ private readonly handle: HandleOf<typeof tooltip.machine>;
112
+ private readonly trigger: HTMLElement;
113
+ private readonly release: () => void;
114
+
115
+ constructor(trigger: HTMLElement, opts: TooltipOptions) {
116
+ this.trigger = trigger;
117
+ const doc = trigger.ownerDocument;
118
+ injectStyles(doc);
119
+ this.content = h('div', { className: 'vela-tooltip', slot: tooltipDef.slot('content') });
120
+ this.positioner = h('div', { className: 'vela-ui-layer', slot: tooltipDef.slot('positioner') }, this.content);
121
+ resolveHost(trigger, opts.host).appendChild(this.positioner);
122
+ this.setContent(opts.content);
123
+
124
+ const ctrl = tooltipController(opts);
125
+ // Scope every spread by this machine's id so several kit components can share a
126
+ // node (e.g. tooltip + menu on one button) without wiping each other's listeners.
127
+ const mid = String(ctrl.props.id);
128
+ if (opts.triggerId) trigger.id = opts.triggerId;
129
+ this.handle = runMachine(ctrl.machine, ctrl.props, (service) => {
130
+ const api = ctrl.connect(service);
131
+ spreadProps(trigger, api.getTriggerProps(), mid);
132
+ spreadProps(this.positioner, api.getPositionerProps(), mid);
133
+ spreadProps(this.content, api.getContentProps(), mid);
134
+ });
135
+ // On the trigger: discarding the trigger's subtree takes the tooltip, and the layer it
136
+ // mounted elsewhere, with it.
137
+ this.release = ownTeardown(trigger, () => {
138
+ this.handle.stop();
139
+ this.positioner.remove();
140
+ // Zag leaves aria-describedby etc. on the trigger; harmless, but drop the hook.
141
+ this.trigger.removeAttribute('data-scope');
142
+ });
143
+ }
144
+
145
+ setContent(content: string | Node | (() => Node)): void {
146
+ this.content.replaceChildren(typeof content === 'function' ? content() : content);
147
+ }
148
+
149
+ destroy(): void {
150
+ this.release();
151
+ }
152
+ }
153
+
154
+ export function createTooltip(props: TooltipProps): Tooltip {
155
+ return new Tooltip(props.trigger, props);
156
+ }
package/src/index.ts ADDED
@@ -0,0 +1,24 @@
1
+ export { h, append, addSlot, type Child, type ElementProps } from './lib/h';
2
+ export { css, defineStyles, injectLayerOrder, LAYER_ORDER } from './lib/css';
3
+ export { t, tokenNames, type Token } from './lib/t';
4
+ export { variants, type VariantFn, type VariantProps, type VariantSpec, type VariantValues } from './lib/variants';
5
+ export { defineComponent, type ComponentDef } from './lib/define';
6
+ export { disposeWithin, ownTeardown } from './lib/dispose';
7
+ export { placePopover, zagPositioning, type Align, type CollisionBoundary, type PlacementProps, type Side } from './lib/placement';
8
+ export {
9
+ createTheme,
10
+ applyTheme,
11
+ checkTheme,
12
+ DARK_CHART,
13
+ LIGHT_CHART,
14
+ TOKEN_NAMES,
15
+ type UiTheme,
16
+ type ThemeInput,
17
+ type ThemeSettings,
18
+ type CreateThemeOptions,
19
+ type ThemedChart,
20
+ type ComponentDefs,
21
+ type ComponentSettings,
22
+ type TokenName,
23
+ } from './lib/theme';
24
+ export type { ChartTheme } from './lib/tokens';
@@ -0,0 +1,112 @@
1
+ // Calendar names for the date picker in a translator's language: month and weekday names,
2
+ // the year label, the order of month and year in a header. English keeps the picker's own
3
+ // tables (`September`, `Sep`, `Su`); other languages read `Intl`, always on the Gregorian
4
+ // calendar because the picker produces Gregorian dates.
5
+ import type { Translator } from './i18n';
6
+
7
+ export interface CalendarSettings {
8
+ readonly locale: string;
9
+ readonly numberingSystem: string;
10
+ /** The locale's language is English: the picker's own tables apply. */
11
+ readonly english: boolean;
12
+ }
13
+
14
+ const EN_MONTHS_LONG = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
15
+ const EN_MONTHS_SHORT = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
16
+ const EN_WEEKDAYS_MIN = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'];
17
+
18
+ const BIDI_MARKS = /[\u200e\u200f\u061c]/g;
19
+ const stripBidiMarks = (text: string): string => text.replace(BIDI_MARKS, '');
20
+
21
+ /** The calendar settings of a translator (English, Latin digits, without one). */
22
+ export function calendarSettings(i18n: Translator | null | undefined): CalendarSettings {
23
+ const locale = i18n?.locale ?? 'en';
24
+ let language = locale.split(/[-_]/)[0] ?? locale;
25
+ try {
26
+ language = new Intl.Locale(locale).language;
27
+ } catch {
28
+ /* an unparsable tag keeps its first subtag */
29
+ }
30
+ return { locale, numberingSystem: i18n?.numberingSystem ?? 'latn', english: language.toLowerCase() === 'en' };
31
+ }
32
+
33
+ const dateFormats = new Map<string, Intl.DateTimeFormat>();
34
+
35
+ function dateFormat(settings: CalendarSettings, options: Intl.DateTimeFormatOptions): Intl.DateTimeFormat {
36
+ const key = `${settings.locale}|${settings.numberingSystem}|${JSON.stringify(options)}`;
37
+ let dtf = dateFormats.get(key);
38
+ if (!dtf) {
39
+ try {
40
+ dtf = new Intl.DateTimeFormat(settings.locale, { ...options, timeZone: 'UTC', calendar: 'gregory', numberingSystem: settings.numberingSystem });
41
+ } catch {
42
+ dtf = new Intl.DateTimeFormat('en', { ...options, timeZone: 'UTC', calendar: 'gregory' });
43
+ }
44
+ dateFormats.set(key, dtf);
45
+ }
46
+ return dtf;
47
+ }
48
+
49
+ const digitTables = new Map<string, string[] | null>();
50
+
51
+ /** ASCII digits rewritten in the settings' numbering system. */
52
+ function localizeDigits(text: string, settings: CalendarSettings): string {
53
+ const key = `${settings.locale}|${settings.numberingSystem}`;
54
+ let digits = digitTables.get(key);
55
+ if (digits === undefined) {
56
+ let nf: Intl.NumberFormat;
57
+ try {
58
+ nf = new Intl.NumberFormat(settings.locale, { numberingSystem: settings.numberingSystem, useGrouping: false, maximumFractionDigits: 0 });
59
+ } catch {
60
+ nf = new Intl.NumberFormat('en', { useGrouping: false, maximumFractionDigits: 0 });
61
+ }
62
+ const table = Array.from({ length: 10 }, (_, d) => stripBidiMarks(nf.format(d)));
63
+ digits = table.every((d, i) => d === String(i)) ? null : table;
64
+ digitTables.set(key, digits);
65
+ }
66
+ if (!digits) return text;
67
+ const map = digits;
68
+ return text.replace(/[0-9]/g, (d) => map[Number(d)]!);
69
+ }
70
+
71
+ /** Twelve month names, January first. */
72
+ export function monthNames(settings: CalendarSettings, width: 'long' | 'short'): string[] {
73
+ if (settings.english) return [...(width === 'long' ? EN_MONTHS_LONG : EN_MONTHS_SHORT)];
74
+ const dtf = dateFormat(settings, { month: width });
75
+ return Array.from({ length: 12 }, (_, m) => stripBidiMarks(dtf.format(Date.UTC(2000, m, 15))));
76
+ }
77
+
78
+ /** Seven short weekday names, Sunday first (index = `Date#getDay()`). */
79
+ export function weekdayNames(settings: CalendarSettings): string[] {
80
+ if (settings.english) return [...EN_WEEKDAYS_MIN];
81
+ const dtf = dateFormat(settings, { weekday: 'short' });
82
+ // 2 January 2000 was a Sunday.
83
+ return Array.from({ length: 7 }, (_, d) => stripBidiMarks(dtf.format(Date.UTC(2000, 0, 2 + d))));
84
+ }
85
+
86
+ /** A year as a header label (`2026`, `2026年`). */
87
+ export function yearLabel(settings: CalendarSettings, year: number): string {
88
+ if (settings.english) return localizeDigits(String(year), settings);
89
+ return stripBidiMarks(dateFormat(settings, { year: 'numeric' }).format(Date.UTC(year, 6, 1)));
90
+ }
91
+
92
+ /** An integer in the settings' digits, without grouping (`2026`, `٢٠٢٦`). */
93
+ export function plainInteger(settings: CalendarSettings, n: number): string {
94
+ return localizeDigits(String(Math.trunc(n)), settings);
95
+ }
96
+
97
+ const yearFirstCache = new Map<string, boolean>();
98
+
99
+ /** Whether the locale writes the year before the month (`2026年9月`, not `September 2026`). */
100
+ export function yearBeforeMonth(settings: CalendarSettings): boolean {
101
+ if (settings.english) return false;
102
+ const key = `${settings.locale}|${settings.numberingSystem}`;
103
+ let first = yearFirstCache.get(key);
104
+ if (first === undefined) {
105
+ const types = dateFormat(settings, { year: 'numeric', month: 'long' })
106
+ .formatToParts(Date.UTC(2026, 8, 15))
107
+ .map((p) => p.type);
108
+ first = types.indexOf('year') < types.indexOf('month');
109
+ yearFirstCache.set(key, first);
110
+ }
111
+ return first;
112
+ }
@@ -0,0 +1,72 @@
1
+ // What the kit's SVG charts share: round axis steps, a value axis with zero on a tick, compact
2
+ // numbers, and the hover tip that sits beside the point it reads.
3
+
4
+ /** A round step for `range` in about `ticks` steps: 1, 2 or 5 times a power of ten. */
5
+ export function niceStep(range: number, ticks: number): number {
6
+ const raw = range / Math.max(1, ticks);
7
+ const mag = 10 ** Math.floor(Math.log10(raw || 1));
8
+ const n = raw / mag;
9
+ return (n <= 1 ? 1 : n <= 2 ? 2 : n <= 5 ? 5 : 10) * mag;
10
+ }
11
+
12
+ /**
13
+ * A value axis spanning `lo..hi` in about `count` round steps: its bounds widened to whole
14
+ * steps, and its ticks from bound to bound with zero among them. One step serves both, so no
15
+ * tick falls outside the plot.
16
+ */
17
+ export function valueAxis(lo: number, hi: number, count: number): { min: number; max: number; step: number; ticks: number[] } {
18
+ const step = niceStep(hi - lo || 1, count);
19
+ const min = Math.floor(lo / step) * step;
20
+ const max = Math.ceil(hi / step) * step || step;
21
+ const n = Math.round((max - min) / step);
22
+ // Stepped by index, rounded to the step's precision: no float drift, and zero lands exactly.
23
+ const digits = Math.max(0, -Math.floor(Math.log10(step)) + 2);
24
+ const ticks = Array.from({ length: n + 1 }, (_, i) => Number((min + i * step).toFixed(digits)) || 0);
25
+ return { min, max, step, ticks };
26
+ }
27
+
28
+ /** `1.2k`, `-3.4M`, `0.25`. */
29
+ export function compactNumber(v: number): string {
30
+ const a = Math.abs(v);
31
+ const sign = v < 0 ? '-' : '';
32
+ if (a >= 1e6) return `${sign}${+(a / 1e6).toFixed(1)}M`;
33
+ if (a >= 1e3) return `${sign}${+(a / 1e3).toFixed(1)}k`;
34
+ return `${sign}${+a.toFixed(2)}`;
35
+ }
36
+
37
+ export function escapeXml(s: string): string {
38
+ return s.replace(/[<>&"]/g, (c) => ({ '<': '&lt;', '>': '&gt;', '&': '&amp;', '"': '&quot;' })[c]!);
39
+ }
40
+
41
+ /** Fill the tip with a readout and place it beside the point at (x, y), inside the box. */
42
+ export function showTip(tip: HTMLElement, content: string | Node, box: { w: number; h: number }, x: number, y: number): void {
43
+ const wasHidden = tip.hidden;
44
+ if (typeof content === 'string') tip.textContent = content;
45
+ else tip.replaceChildren(content);
46
+ tip.hidden = false;
47
+ // A tip that was hidden appears in place instead of gliding from where it last was.
48
+ tip.toggleAttribute('data-instant', wasHidden);
49
+ const tw = tip.offsetWidth;
50
+ const th = tip.offsetHeight;
51
+ const GAP = 16;
52
+ const left = x + GAP + tw <= box.w ? x + GAP : Math.max(0, x - GAP - tw);
53
+ tip.style.left = `${Math.round(left)}px`;
54
+ tip.style.top = `${Math.round(Math.max(0, Math.min(box.h - th, y - th / 2)))}px`;
55
+ }
56
+
57
+ const SVG_NS = 'http://www.w3.org/2000/svg';
58
+
59
+ /** A chart's root (sized by its parent), its SVG and its tip, redrawn as the root resizes. */
60
+ export function chartFrame(rootSlot: string, tipSlot: string, className: string | undefined, draw: () => void): { el: HTMLElement; svg: SVGSVGElement; tip: HTMLElement; disconnect(): void } {
61
+ const svg = document.createElementNS(SVG_NS, 'svg');
62
+ const tip = document.createElement('div');
63
+ tip.dataset.slot = tipSlot;
64
+ tip.hidden = true;
65
+ const el = document.createElement('div');
66
+ el.dataset.slot = rootSlot;
67
+ if (className) el.className = className;
68
+ el.append(svg, tip);
69
+ const ro = typeof ResizeObserver === 'function' ? new ResizeObserver(draw) : null;
70
+ ro?.observe(el);
71
+ return { el, svg, tip, disconnect: () => ro?.disconnect() };
72
+ }