@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,215 @@
1
+ // BarChart: vertical bars around a zero line with a hover readout (daily P&L, a distribution),
2
+ // in the profit tone above zero and the loss tone below unless a bar names its own; optionally
3
+ // a value axis with gridlines and a title, slanted labels, and dashed markers.
4
+ import { css, defineStyles } from '../lib/css';
5
+ import { chartFrame, compactNumber, escapeXml, showTip, valueAxis } from '../lib/chart';
6
+ import { defineComponent } from '../lib/define';
7
+ import { t } from '../lib/t';
8
+
9
+ export const barChartDef = defineComponent({
10
+ name: 'bar-chart',
11
+ parts: ['root', 'grid', 'label', 'bar', 'marker', 'tip'],
12
+ settings: { 'tip-radius': '3px', 'tip-shadow': t.shadow, 'tip-z': '2', 'tip-bg': t.surface.elev },
13
+ });
14
+
15
+ export interface BarDatum {
16
+ label: string;
17
+ value: number;
18
+ /** The bar's tone (default: by its sign). */
19
+ tone?: 'profit' | 'loss' | 'muted';
20
+ }
21
+
22
+ export interface BarChartProps {
23
+ /** The hover readout of a bar. */
24
+ format?: (d: BarDatum) => string | Node;
25
+ /** Label every Nth bar under the axis (default: as many as fit). */
26
+ labelEvery?: number;
27
+ /** A value axis on the start side, with gridlines. */
28
+ axes?: boolean;
29
+ /** The value axis's title, written up its side. */
30
+ axisTitle?: string;
31
+ /** Slant the bar labels (long labels on many bars). */
32
+ slantLabels?: boolean;
33
+ yFormat?: (v: number) => string;
34
+ /** What a bar's label reads under the axis (default: the label itself). */
35
+ labelFormat?: (label: string) => string;
36
+ /** Extra class names (layout hooks). */
37
+ className?: string;
38
+ }
39
+
40
+ export interface BarChartHandle {
41
+ readonly el: HTMLElement;
42
+ /** `markers` are dashed vertical lines at fractional bar positions (0: the first bar's
43
+ * start edge), each in its own ink. */
44
+ setData(data: readonly BarDatum[], markers?: ReadonlyArray<{ at: number; color: string }>): void;
45
+ destroy(): void;
46
+ }
47
+
48
+ const injectStyles = defineStyles(
49
+ 'vela-ui-bar-chart',
50
+ css`
51
+ @layer vela.ui {
52
+ [data-slot='bar-chart'] {
53
+ position: relative;
54
+ width: 100%;
55
+ height: 100%;
56
+
57
+ & > svg {
58
+ display: block;
59
+ width: 100%;
60
+ height: 100%;
61
+ overflow: visible;
62
+ }
63
+ }
64
+ [data-slot='bar-chart-grid'] {
65
+ stroke: color-mix(in srgb, ${t.fg.bright} 5%, transparent);
66
+
67
+ &[data-zero] {
68
+ stroke: color-mix(in srgb, ${t.fg.bright} 20%, transparent);
69
+ }
70
+ &[data-base] {
71
+ stroke: ${t.border};
72
+ }
73
+ }
74
+ [data-slot='bar-chart-label'] {
75
+ fill: ${t.fg.muted};
76
+ font-size: 10px;
77
+
78
+ &[data-title] {
79
+ font-size: 11px;
80
+ }
81
+ }
82
+ [data-slot='bar-chart-bar'] {
83
+ fill: ${t.profit};
84
+
85
+ &[data-tone='loss'] {
86
+ fill: ${t.loss};
87
+ }
88
+ &[data-tone='muted'] {
89
+ fill: ${t.fg.faint};
90
+ }
91
+ &[data-thin] {
92
+ opacity: 0.85;
93
+ }
94
+ }
95
+ [data-slot='bar-chart-marker'] {
96
+ stroke-width: 2;
97
+ stroke-dasharray: 4 3;
98
+ }
99
+ [data-slot='bar-chart-tip'] {
100
+ position: absolute;
101
+ z-index: ${barChartDef.vars['tip-z']};
102
+ top: 0;
103
+ pointer-events: none;
104
+ padding: 8px;
105
+ border-radius: ${barChartDef.vars['tip-radius']};
106
+ border: 1px solid ${t.border};
107
+ background: ${barChartDef.vars['tip-bg']};
108
+ color: ${t.fg.bright};
109
+ box-shadow: ${barChartDef.vars['tip-shadow']};
110
+ font-size: 12px;
111
+ line-height: 1.35;
112
+ white-space: nowrap;
113
+ font-variant-numeric: tabular-nums;
114
+ transition:
115
+ left ${t.dur.fast} ease-out,
116
+ top ${t.dur.fast} ease-out;
117
+
118
+ &[data-instant] {
119
+ transition: none;
120
+ }
121
+ }
122
+ }
123
+ `,
124
+ );
125
+
126
+ export function createBarChart(props: BarChartProps = {}): BarChartHandle {
127
+ injectStyles(document);
128
+ let data: readonly BarDatum[] = [];
129
+ let markers: ReadonlyArray<{ at: number; color: string }> = [];
130
+ const slot = (part: (typeof barChartDef.parts)[number]) => `data-slot="${barChartDef.slot(part)}"`;
131
+
132
+ const geometry = () => {
133
+ const w = frame.el.clientWidth;
134
+ const ht = frame.el.clientHeight;
135
+ if (w < 4 || ht < 24 || data.length === 0) return null;
136
+ const left = props.axes ? (props.axisTitle ? 54 : 40) : 0;
137
+ const top = props.axes ? 6 : 0;
138
+ const plotH = ht - top - (props.slantLabels ? 46 : 16);
139
+ const lo = Math.min(0, ...data.map((d) => d.value));
140
+ const hi = Math.max(0, ...data.map((d) => d.value));
141
+ const axis = props.axes ? valueAxis(lo, hi, 4) : { min: lo, max: hi, step: 0, ticks: [] as number[] };
142
+ const range = axis.max - axis.min || 1;
143
+ return { w, h: ht, left, top, plotH, bw: (w - left) / data.length, zero: top + (axis.max / range) * plotH, scale: plotH / range, ticks: axis.ticks };
144
+ };
145
+
146
+ const draw = (): void => {
147
+ const g = geometry();
148
+ if (!g) {
149
+ frame.svg.replaceChildren();
150
+ return;
151
+ }
152
+ const every = props.labelEvery ?? Math.max(1, Math.ceil(data.length / Math.max(1, Math.floor((g.w - g.left) / (props.slantLabels ? 32 : 56)))));
153
+ const label = (x: number, y: number, text: string, anchor: string, extra = ''): string =>
154
+ `<text ${slot('label')} x="${x.toFixed(1)}" y="${y.toFixed(1)}" text-anchor="${anchor}"${extra}>${escapeXml(text)}</text>`;
155
+ let body = '';
156
+ if (props.axes) {
157
+ const fmt = props.yFormat ?? compactNumber;
158
+ for (const v of g.ticks) {
159
+ const y = g.zero - v * g.scale;
160
+ body += `<line ${slot('grid')}${v === 0 ? ' data-zero=""' : ''} x1="${g.left}" x2="${g.w}" y1="${y.toFixed(1)}" y2="${y.toFixed(1)}"/>`;
161
+ body += label(g.left - 6, y + 3, fmt(v), 'end');
162
+ }
163
+ if (props.axisTitle) {
164
+ const cy = g.top + g.plotH / 2;
165
+ body += label(10, cy, props.axisTitle, 'middle', ` data-title="" transform="rotate(-90 10 ${cy.toFixed(1)})"`);
166
+ }
167
+ } else body += `<line ${slot('grid')} data-base="" x1="0" x2="${g.w}" y1="${g.zero}" y2="${g.zero}"/>`;
168
+ const barW = props.axes ? Math.max(1, Math.min(g.bw * 0.62, 48)) : Math.max(1, g.bw - Math.min(4, g.bw * 0.2));
169
+ data.forEach((d, i) => {
170
+ const cx = g.left + i * g.bw + g.bw / 2;
171
+ const ht = Math.max(1, Math.abs(d.value) * g.scale);
172
+ const y = d.value >= 0 ? g.zero - ht : g.zero;
173
+ const tone = d.tone ?? (d.value >= 0 ? 'profit' : 'loss');
174
+ body += `<rect ${slot('bar')} data-tone="${tone}"${props.axes ? '' : ' data-thin=""'} x="${(cx - barW / 2).toFixed(1)}" y="${y.toFixed(1)}" width="${barW.toFixed(1)}" height="${ht.toFixed(1)}" rx="${props.axes ? 2 : 1.5}"/>`;
175
+ if (i % every !== 0) return;
176
+ const ly = g.top + g.plotH + 12;
177
+ const text = props.labelFormat ? props.labelFormat(d.label) : d.label;
178
+ body += props.slantLabels ? label(cx, ly, text, 'end', ` transform="rotate(-45 ${cx.toFixed(1)} ${ly.toFixed(1)})"`) : label(cx, g.h - 3, text, 'middle');
179
+ });
180
+ for (const m of markers) {
181
+ const x = g.left + m.at * g.bw;
182
+ body += `<line ${slot('marker')} style="stroke: ${escapeXml(m.color)}" x1="${x.toFixed(1)}" x2="${x.toFixed(1)}" y1="${g.top}" y2="${(g.top + g.plotH).toFixed(1)}"/>`;
183
+ }
184
+ frame.svg.setAttribute('viewBox', `0 0 ${g.w} ${g.h}`);
185
+ frame.svg.innerHTML = body;
186
+ };
187
+
188
+ const frame = chartFrame(barChartDef.slot('root'), barChartDef.slot('tip'), props.className, draw);
189
+ frame.el.addEventListener('pointermove', (e) => {
190
+ const g = geometry();
191
+ if (!g) return;
192
+ const i = Math.floor((e.clientX - frame.el.getBoundingClientRect().left - g.left) / g.bw);
193
+ const d = data[i];
194
+ if (!d) {
195
+ frame.tip.hidden = true;
196
+ return;
197
+ }
198
+ const top = d.value >= 0 ? g.zero - Math.abs(d.value) * g.scale : g.zero + Math.abs(d.value) * g.scale;
199
+ showTip(frame.tip, props.format ? props.format(d) : `${d.label}: ${d.value}`, g, g.left + i * g.bw + g.bw / 2, top);
200
+ });
201
+ frame.el.addEventListener('pointerleave', () => (frame.tip.hidden = true));
202
+
203
+ return {
204
+ el: frame.el,
205
+ setData(next, nextMarkers = []) {
206
+ data = next;
207
+ markers = nextMarkers;
208
+ draw();
209
+ },
210
+ destroy() {
211
+ frame.disconnect();
212
+ frame.el.remove();
213
+ },
214
+ };
215
+ }
@@ -0,0 +1,379 @@
1
+ // Button: a <button> with optional icons and key caps before and after its text (`↵ Open`,
2
+ // `Esc Close` in a picker's footer). `default` is filled with the selection color (OK, Apply),
3
+ // `outline` bordered (the dialogs' Cancel), `secondary` a sunken fill, `ghost` bare until
4
+ // hovered (toolbar and header icons), `destructive` tinted with the danger color, `link` text
5
+ // that underlines on hover, `up` and `down` filled with the market's colors (buy and sell),
6
+ // `favorite` a bare star whose pressed state takes the favorites color. Heights: `xs` 26px,
7
+ // `sm` 30px (panels), `default` 34px (dialog footers), `lg` 40px (touch); the `icon*` sizes
8
+ // are the same heights, square, for an icon alone, which `label` names. A toggle button
9
+ // (`pressed`, `defaultPressed` or `onPressedChange`) says its state through `aria-pressed`;
10
+ // uncontrolled it flips on a press, given `pressed` it only asks, until `setPressed` moves it.
11
+ import { iconEl } from '../lib/icons';
12
+ import { css, defineStyles } from '../lib/css';
13
+ import { defineComponent } from '../lib/define';
14
+ import { h } from '../lib/h';
15
+ import { t } from '../lib/t';
16
+ import { variants, type VariantProps } from '../lib/variants';
17
+ import { createKbd } from './kbd';
18
+
19
+ export const buttonDef = defineComponent({
20
+ name: 'button',
21
+ parts: ['root', 'icon', 'keys'],
22
+ settings: {
23
+ radius: t.radius.md,
24
+ height: '34px',
25
+ 'height-xs': '26px',
26
+ 'height-sm': '30px',
27
+ 'height-lg': '40px',
28
+ 'font-size': '14px',
29
+ 'font-size-xs': '12px',
30
+ 'font-size-sm': '13px',
31
+ },
32
+ });
33
+
34
+ export const buttonVariants = variants({
35
+ variant: ['default', 'outline', 'secondary', 'ghost', 'destructive', 'link', 'up', 'down', 'favorite'],
36
+ size: ['default', 'xs', 'sm', 'lg', 'icon', 'icon-xs', 'icon-sm', 'icon-lg'],
37
+ });
38
+
39
+ export type ButtonVariant = (typeof buttonVariants.spec.variant)[number];
40
+ export type ButtonSize = (typeof buttonVariants.spec.size)[number];
41
+
42
+ export interface ButtonProps extends VariantProps<typeof buttonVariants> {
43
+ /** The visible text (the `icon*` sizes show none; `label` names them). */
44
+ text?: string;
45
+ /** Icon id from the kit's registry, before the text (the icon of an `icon*` size).
46
+ * `favorite` defaults to `star`. */
47
+ icon?: string;
48
+ /** The icon while pressed (`star-filled` for `star`). `favorite` defaults to `star-filled`. */
49
+ iconPressed?: string;
50
+ /** Icon id after the text (a menu trigger's chevron). */
51
+ iconEnd?: string;
52
+ /** Key caps before the text, one per key (`['↑', '↓']`). */
53
+ keys?: readonly string[];
54
+ /** Key caps after the text. */
55
+ keysEnd?: readonly string[];
56
+ /** Accessible name; required for the `icon*` sizes, defaults to `text`. */
57
+ label?: string;
58
+ /** A controlled toggle's state (`aria-pressed`): a press only calls `onPressedChange`, and
59
+ * `setPressed` moves it. Omitted with `defaultPressed` and `onPressedChange`: not a toggle. */
60
+ pressed?: boolean;
61
+ /** An uncontrolled toggle's first state: a press flips it and calls `onPressedChange`. */
62
+ defaultPressed?: boolean;
63
+ /** A press asked for this state (a toggle without `pressed` or `defaultPressed` starts
64
+ * unpressed). */
65
+ onPressedChange?: (pressed: boolean) => void;
66
+ disabled?: boolean;
67
+ onClick?: (e: MouseEvent) => void;
68
+ /** Extra class names (layout hooks). */
69
+ className?: string;
70
+ }
71
+
72
+ export interface ButtonHandle {
73
+ readonly el: HTMLButtonElement;
74
+ readonly pressed: boolean | undefined;
75
+ /** Show a toggle's state, without calling `onPressedChange`. */
76
+ setPressed(pressed: boolean): void;
77
+ setDisabled(disabled: boolean): void;
78
+ /** New text (or, for an `icon*` size, a new accessible name). */
79
+ setText(text: string): void;
80
+ destroy(): void;
81
+ }
82
+
83
+ export const BUTTON_STYLE_ID = 'vela-ui-button';
84
+
85
+ export const BUTTON_CSS = css`
86
+ @layer vela.ui {
87
+ [data-slot='button'] {
88
+ cursor: pointer;
89
+ height: ${buttonDef.vars.height};
90
+ padding: 0 11px;
91
+ border-radius: ${buttonDef.vars.radius};
92
+ border: 1px solid ${t.border.strong};
93
+ background: transparent;
94
+ color: ${t.fg};
95
+ font-weight: 400;
96
+ font-size: ${buttonDef.vars['font-size']};
97
+ font-family: inherit;
98
+ white-space: nowrap;
99
+ transition:
100
+ background ${t.dur.fast} ease,
101
+ color ${t.dur.fast} ease,
102
+ opacity ${t.dur.fast} ease,
103
+ border-color ${t.dur.fast} ease;
104
+
105
+ &:where(:not([hidden])) {
106
+ display: inline-flex;
107
+ align-items: center;
108
+ justify-content: center;
109
+ gap: 6px;
110
+ }
111
+ /* Icons draw at 1em: one size for every button size but the smallest. The slot
112
+ is sized too, so an icon registered without width and height fills it. */
113
+ & > [data-icon] {
114
+ font-size: 16px;
115
+ width: 1em;
116
+ height: 1em;
117
+ flex: none;
118
+
119
+ & > svg {
120
+ width: 100%;
121
+ height: 100%;
122
+ }
123
+ }
124
+ &[data-size='xs'] > [data-icon],
125
+ &[data-size='icon-xs'] > [data-icon] {
126
+ font-size: 12px;
127
+ }
128
+ & > [data-keys] {
129
+ flex: none;
130
+ gap: 2px;
131
+ }
132
+ & > [data-keys]:where(:not([hidden])) {
133
+ display: inline-flex;
134
+ }
135
+ &[data-size='sm'],
136
+ &[data-size='icon-sm'] {
137
+ height: ${buttonDef.vars['height-sm']};
138
+ font-size: ${buttonDef.vars['font-size-sm']};
139
+ }
140
+ &[data-size='xs'],
141
+ &[data-size='icon-xs'] {
142
+ height: ${buttonDef.vars['height-xs']};
143
+ padding: 0 8px;
144
+ font-size: ${buttonDef.vars['font-size-xs']};
145
+ border-radius: ${t.radius.sm};
146
+ }
147
+ &[data-size='lg'],
148
+ &[data-size='icon-lg'] {
149
+ height: ${buttonDef.vars['height-lg']};
150
+ padding: 0 14px;
151
+ }
152
+ &[data-size='icon'] {
153
+ width: ${buttonDef.vars.height};
154
+ padding: 0;
155
+ }
156
+ &[data-size='icon-sm'] {
157
+ width: ${buttonDef.vars['height-sm']};
158
+ padding: 0;
159
+ }
160
+ &[data-size='icon-xs'] {
161
+ width: ${buttonDef.vars['height-xs']};
162
+ padding: 0;
163
+ }
164
+ &[data-size='icon-lg'] {
165
+ width: ${buttonDef.vars['height-lg']};
166
+ padding: 0;
167
+ }
168
+ &:disabled {
169
+ cursor: default;
170
+ opacity: 0.5;
171
+ }
172
+ &:focus-visible {
173
+ outline: 2px solid ${t.focus};
174
+ outline-offset: 1px;
175
+ }
176
+ &:not(:disabled):hover,
177
+ &[aria-expanded='true'] {
178
+ background: ${t.hover};
179
+ color: ${t.fg.bright};
180
+ border-color: ${t.fg.muted};
181
+ }
182
+ &[data-variant='default'] {
183
+ border-color: ${t.selected.bg};
184
+ background: ${t.selected.bg};
185
+ color: ${t.selected.fg};
186
+ }
187
+ &[data-variant='default']:not(:disabled):hover,
188
+ &[data-variant='default'][aria-expanded='true'] {
189
+ background: ${t.selected.bg};
190
+ color: ${t.selected.fg};
191
+ opacity: 0.85;
192
+ border-color: ${t.selected.bg};
193
+ }
194
+ &[data-variant='secondary'] {
195
+ background: ${t.surface.sunken};
196
+ border-color: ${t.border};
197
+ }
198
+ &[data-variant='secondary']:not(:disabled):hover,
199
+ &[data-variant='secondary'][aria-expanded='true'] {
200
+ border-color: ${t.border.strong};
201
+ }
202
+ &[data-variant='ghost'] {
203
+ border-color: transparent;
204
+ color: ${t.fg.muted};
205
+ }
206
+ &[data-variant='ghost']:not(:disabled):hover,
207
+ &[data-variant='ghost'][aria-expanded='true'] {
208
+ border-color: transparent;
209
+ }
210
+ &[data-variant='destructive'] {
211
+ border-color: transparent;
212
+ background: color-mix(in srgb, ${t.danger} 14%, transparent);
213
+ color: ${t.danger};
214
+ }
215
+ &[data-variant='destructive']:not(:disabled):hover {
216
+ border-color: transparent;
217
+ background: color-mix(in srgb, ${t.danger} 24%, transparent);
218
+ color: ${t.danger};
219
+ }
220
+ &[data-variant='link'] {
221
+ border-color: transparent;
222
+ background: transparent;
223
+ color: ${t.accent};
224
+ padding-inline: 0;
225
+ text-underline-offset: 4px;
226
+ }
227
+ &[data-variant='link']:not(:disabled):hover {
228
+ border-color: transparent;
229
+ background: transparent;
230
+ color: ${t.accent};
231
+ text-decoration: underline;
232
+ }
233
+ &[data-variant='up'],
234
+ &[data-variant='up']:not(:disabled):hover {
235
+ border-color: transparent;
236
+ background: ${t.up};
237
+ color: ${t.fg.onFill};
238
+ }
239
+ &[data-variant='down'],
240
+ &[data-variant='down']:not(:disabled):hover {
241
+ border-color: transparent;
242
+ background: ${t.down};
243
+ color: ${t.fg.onFill};
244
+ }
245
+ &[data-variant='up']:not(:disabled):hover,
246
+ &[data-variant='down']:not(:disabled):hover {
247
+ opacity: 0.88;
248
+ }
249
+ &[data-pressed] {
250
+ background: ${t.active};
251
+ color: ${t.fg.bright};
252
+ }
253
+ &[data-variant='favorite'] {
254
+ border-color: transparent;
255
+ color: ${t.fg.muted};
256
+ }
257
+ &[data-variant='favorite']:not(:disabled):hover {
258
+ border-color: transparent;
259
+ }
260
+ &[data-variant='favorite'][data-pressed] {
261
+ background: transparent;
262
+ color: ${t.highlight};
263
+ }
264
+ &[data-variant='favorite'][data-pressed]:where(:not(:disabled)):hover {
265
+ background: ${t.hover};
266
+ color: ${t.highlight};
267
+ }
268
+ }
269
+ }
270
+ `;
271
+
272
+ const injectStyles = defineStyles(BUTTON_STYLE_ID, BUTTON_CSS);
273
+
274
+ const iconPart = (id: string, side: 'inline-start' | 'inline-end'): HTMLElement => {
275
+ const el = iconEl(id, document);
276
+ el.dataset.slot = buttonDef.slot('icon');
277
+ el.dataset.icon = side;
278
+ return el;
279
+ };
280
+
281
+ const keysPart = (keys: readonly string[] | undefined, side: 'inline-start' | 'inline-end'): HTMLElement | null =>
282
+ keys?.length ? h('span', { slot: buttonDef.slot('keys'), data: { keys: side } }, ...keys.map((key) => createKbd({ text: key }).el)) : null;
283
+
284
+ class ButtonImpl implements ButtonHandle {
285
+ readonly el: HTMLButtonElement;
286
+ private readonly iconOnly: boolean;
287
+ private readonly textNode: Text | null;
288
+ private readonly toggle: boolean;
289
+ private readonly controlled: boolean;
290
+ private readonly icon: string | undefined;
291
+ private readonly iconPressed: string | undefined;
292
+ private iconStart: HTMLElement | null;
293
+ private shownIcon: string | undefined;
294
+ private label: string | undefined;
295
+ private pressedState: boolean | undefined;
296
+
297
+ constructor(props: ButtonProps) {
298
+ injectStyles(document);
299
+ const { variant, size } = buttonVariants.resolve(props);
300
+ const favorite = variant === 'favorite';
301
+ this.iconOnly = size.startsWith('icon');
302
+ this.label = props.label ?? props.text;
303
+ this.controlled = props.pressed !== undefined;
304
+ this.toggle = this.controlled || props.defaultPressed !== undefined || props.onPressedChange !== undefined;
305
+ this.pressedState = props.pressed ?? props.defaultPressed ?? (this.toggle ? false : undefined);
306
+ this.icon = props.icon ?? (favorite ? 'star' : undefined);
307
+ this.iconPressed = props.iconPressed ?? (favorite ? 'star-filled' : undefined);
308
+ this.iconStart = this.icon ? iconPart(this.icon, 'inline-start') : null;
309
+ this.shownIcon = this.icon;
310
+ this.textNode = !this.iconOnly && props.text !== undefined ? document.createTextNode(props.text) : null;
311
+ const onClick = (e: MouseEvent): void => {
312
+ if (this.toggle) {
313
+ const next = !this.pressedState;
314
+ if (!this.controlled) this.setPressed(next);
315
+ props.onPressedChange?.(next);
316
+ }
317
+ props.onClick?.(e);
318
+ };
319
+ this.el = h(
320
+ 'button',
321
+ {
322
+ className: props.className ? `vela-btn ${props.className}` : 'vela-btn',
323
+ slot: buttonDef.slot('root'),
324
+ attrs: { type: 'button', ...buttonVariants({ variant, size }) },
325
+ on: this.toggle || props.onClick ? { click: onClick } : undefined,
326
+ },
327
+ this.iconStart,
328
+ this.iconOnly ? null : keysPart(props.keys, 'inline-start'),
329
+ this.textNode,
330
+ this.iconOnly ? null : keysPart(props.keysEnd, 'inline-end'),
331
+ props.iconEnd && !this.iconOnly ? iconPart(props.iconEnd, 'inline-end') : null,
332
+ );
333
+ this.el.disabled = props.disabled ?? false;
334
+ this.paint();
335
+ }
336
+
337
+ get pressed(): boolean | undefined {
338
+ return this.pressedState;
339
+ }
340
+
341
+ setPressed(pressed: boolean): void {
342
+ this.pressedState = pressed;
343
+ this.paint();
344
+ }
345
+
346
+ setDisabled(disabled: boolean): void {
347
+ this.el.disabled = disabled;
348
+ }
349
+
350
+ setText(text: string): void {
351
+ this.label = text;
352
+ if (this.textNode) this.textNode.data = text;
353
+ this.paint();
354
+ }
355
+
356
+ destroy(): void {}
357
+
358
+ private paint(): void {
359
+ const el = this.el;
360
+ if (this.iconStart && this.iconPressed) {
361
+ const icon = this.pressedState ? this.iconPressed : this.icon!;
362
+ if (icon !== this.shownIcon) {
363
+ const next = iconPart(icon, 'inline-start');
364
+ this.iconStart.replaceWith(next);
365
+ this.iconStart = next;
366
+ this.shownIcon = icon;
367
+ }
368
+ }
369
+ el.toggleAttribute('data-pressed', this.pressedState === true);
370
+ if (this.pressedState === undefined) el.removeAttribute('aria-pressed');
371
+ else el.setAttribute('aria-pressed', String(this.pressedState));
372
+ if (this.iconOnly && this.label !== undefined) el.setAttribute('aria-label', this.label);
373
+ else el.removeAttribute('aria-label');
374
+ }
375
+ }
376
+
377
+ export function createButton(props: ButtonProps = {}): ButtonHandle {
378
+ return new ButtonImpl(props);
379
+ }