@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,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 '@luxalgo/vela-ui';
5
+ import { chartFrame, compactNumber, escapeXml, showTip, valueAxis } from '@luxalgo/vela-ui/chart';
6
+ import { defineComponent } from '@luxalgo/vela-ui';
7
+ import { t } from '@luxalgo/vela-ui';
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,303 @@
1
+ // Button: a <button> with optional icons before and after its text. `default` is filled with
2
+ // the selection color (OK, Apply), `outline` bordered (the dialogs' Cancel), `secondary` a
3
+ // sunken fill, `ghost` bare until hovered (toolbar and header icons), `destructive` tinted
4
+ // with the danger color, `link` text that underlines on hover, `up` and `down` filled with the
5
+ // market's colors (buy and sell). Heights: `xs` 26px, `sm` 30px
6
+ // (panels), `default` 34px (dialog footers), `lg` 40px (touch); the `icon*` sizes are the
7
+ // same heights, square, for an icon alone, which `label` names.
8
+ import { iconEl } from '@luxalgo/vela-ui/icons';
9
+ import { css, defineStyles } from '@luxalgo/vela-ui';
10
+ import { defineComponent } from '@luxalgo/vela-ui';
11
+ import { h } from '@luxalgo/vela-ui';
12
+ import { t } from '@luxalgo/vela-ui';
13
+ import { variants, type VariantProps } from '@luxalgo/vela-ui';
14
+
15
+ export const buttonDef = defineComponent({
16
+ name: 'button',
17
+ parts: ['root', 'icon'],
18
+ settings: {
19
+ radius: t.radius.md,
20
+ height: '34px',
21
+ 'height-xs': '26px',
22
+ 'height-sm': '30px',
23
+ 'height-lg': '40px',
24
+ 'font-size': '14px',
25
+ 'font-size-xs': '12px',
26
+ 'font-size-sm': '13px',
27
+ },
28
+ });
29
+
30
+ export const buttonVariants = variants({
31
+ variant: ['default', 'outline', 'secondary', 'ghost', 'destructive', 'link', 'up', 'down'],
32
+ size: ['default', 'xs', 'sm', 'lg', 'icon', 'icon-xs', 'icon-sm', 'icon-lg'],
33
+ });
34
+
35
+ export type ButtonVariant = (typeof buttonVariants.spec.variant)[number];
36
+ export type ButtonSize = (typeof buttonVariants.spec.size)[number];
37
+
38
+ export interface ButtonProps extends VariantProps<typeof buttonVariants> {
39
+ /** The visible text (the `icon*` sizes show none; `label` names them). */
40
+ text?: string;
41
+ /** Icon id from the kit's registry, before the text (the icon of an `icon*` size). */
42
+ icon?: string;
43
+ /** Icon id after the text (a menu trigger's chevron). */
44
+ iconEnd?: string;
45
+ /** Accessible name; required for the `icon*` sizes, defaults to `text`. */
46
+ label?: string;
47
+ /** A toggle button's state (`aria-pressed`). Omitted: not a toggle. */
48
+ pressed?: boolean;
49
+ disabled?: boolean;
50
+ onClick?: (e: MouseEvent) => void;
51
+ /** Extra class names (layout hooks). */
52
+ className?: string;
53
+ }
54
+
55
+ export interface ButtonHandle {
56
+ readonly el: HTMLButtonElement;
57
+ readonly pressed: boolean | undefined;
58
+ setPressed(pressed: boolean): void;
59
+ setDisabled(disabled: boolean): void;
60
+ /** New text (or, for an `icon*` size, a new accessible name). */
61
+ setText(text: string): void;
62
+ destroy(): void;
63
+ }
64
+
65
+ export const BUTTON_STYLE_ID = 'vela-ui-button';
66
+
67
+ export const BUTTON_CSS = css`
68
+ @layer vela.ui {
69
+ [data-slot='button'] {
70
+ cursor: pointer;
71
+ height: ${buttonDef.vars.height};
72
+ padding: 0 11px;
73
+ border-radius: ${buttonDef.vars.radius};
74
+ border: 1px solid ${t.border.strong};
75
+ background: transparent;
76
+ color: ${t.fg};
77
+ font-weight: 400;
78
+ font-size: ${buttonDef.vars['font-size']};
79
+ font-family: inherit;
80
+ white-space: nowrap;
81
+ transition:
82
+ background ${t.dur.fast} ease,
83
+ color ${t.dur.fast} ease,
84
+ opacity ${t.dur.fast} ease,
85
+ border-color ${t.dur.fast} ease;
86
+
87
+ &:where(:not([hidden])) {
88
+ display: inline-flex;
89
+ align-items: center;
90
+ justify-content: center;
91
+ gap: 6px;
92
+ }
93
+ /* Icons draw at 1em: one size for every button size but the smallest. The slot
94
+ is sized too, so an icon registered without width and height fills it. */
95
+ & > [data-icon] {
96
+ font-size: 16px;
97
+ width: 1em;
98
+ height: 1em;
99
+ flex: none;
100
+
101
+ & > svg {
102
+ width: 100%;
103
+ height: 100%;
104
+ }
105
+ }
106
+ &[data-size='xs'] > [data-icon],
107
+ &[data-size='icon-xs'] > [data-icon] {
108
+ font-size: 12px;
109
+ }
110
+ &[data-size='sm'],
111
+ &[data-size='icon-sm'] {
112
+ height: ${buttonDef.vars['height-sm']};
113
+ font-size: ${buttonDef.vars['font-size-sm']};
114
+ }
115
+ &[data-size='xs'],
116
+ &[data-size='icon-xs'] {
117
+ height: ${buttonDef.vars['height-xs']};
118
+ padding: 0 8px;
119
+ font-size: ${buttonDef.vars['font-size-xs']};
120
+ border-radius: ${t.radius.sm};
121
+ }
122
+ &[data-size='lg'],
123
+ &[data-size='icon-lg'] {
124
+ height: ${buttonDef.vars['height-lg']};
125
+ padding: 0 14px;
126
+ }
127
+ &[data-size='icon'] {
128
+ width: ${buttonDef.vars.height};
129
+ padding: 0;
130
+ }
131
+ &[data-size='icon-sm'] {
132
+ width: ${buttonDef.vars['height-sm']};
133
+ padding: 0;
134
+ }
135
+ &[data-size='icon-xs'] {
136
+ width: ${buttonDef.vars['height-xs']};
137
+ padding: 0;
138
+ }
139
+ &[data-size='icon-lg'] {
140
+ width: ${buttonDef.vars['height-lg']};
141
+ padding: 0;
142
+ }
143
+ &:disabled {
144
+ cursor: default;
145
+ opacity: 0.5;
146
+ }
147
+ &:focus-visible {
148
+ outline: 2px solid ${t.focus};
149
+ outline-offset: 1px;
150
+ }
151
+ &:not(:disabled):hover,
152
+ &[aria-expanded='true'] {
153
+ background: ${t.hover};
154
+ color: ${t.fg.bright};
155
+ border-color: ${t.fg.muted};
156
+ }
157
+ &[data-variant='default'] {
158
+ border-color: ${t.selected.bg};
159
+ background: ${t.selected.bg};
160
+ color: ${t.selected.fg};
161
+ }
162
+ &[data-variant='default']:not(:disabled):hover,
163
+ &[data-variant='default'][aria-expanded='true'] {
164
+ background: ${t.selected.bg};
165
+ color: ${t.selected.fg};
166
+ opacity: 0.85;
167
+ border-color: ${t.selected.bg};
168
+ }
169
+ &[data-variant='secondary'] {
170
+ background: ${t.surface.sunken};
171
+ border-color: ${t.border};
172
+ }
173
+ &[data-variant='secondary']:not(:disabled):hover,
174
+ &[data-variant='secondary'][aria-expanded='true'] {
175
+ border-color: ${t.border.strong};
176
+ }
177
+ &[data-variant='ghost'] {
178
+ border-color: transparent;
179
+ color: ${t.fg.muted};
180
+ }
181
+ &[data-variant='ghost']:not(:disabled):hover,
182
+ &[data-variant='ghost'][aria-expanded='true'] {
183
+ border-color: transparent;
184
+ }
185
+ &[data-variant='destructive'] {
186
+ border-color: transparent;
187
+ background: color-mix(in srgb, ${t.danger} 14%, transparent);
188
+ color: ${t.danger};
189
+ }
190
+ &[data-variant='destructive']:not(:disabled):hover {
191
+ border-color: transparent;
192
+ background: color-mix(in srgb, ${t.danger} 24%, transparent);
193
+ color: ${t.danger};
194
+ }
195
+ &[data-variant='link'] {
196
+ border-color: transparent;
197
+ background: transparent;
198
+ color: ${t.accent};
199
+ padding-inline: 0;
200
+ text-underline-offset: 4px;
201
+ }
202
+ &[data-variant='link']:not(:disabled):hover {
203
+ border-color: transparent;
204
+ background: transparent;
205
+ color: ${t.accent};
206
+ text-decoration: underline;
207
+ }
208
+ &[data-variant='up'],
209
+ &[data-variant='up']:not(:disabled):hover {
210
+ border-color: transparent;
211
+ background: ${t.up};
212
+ color: ${t.fg.onFill};
213
+ }
214
+ &[data-variant='down'],
215
+ &[data-variant='down']:not(:disabled):hover {
216
+ border-color: transparent;
217
+ background: ${t.down};
218
+ color: ${t.fg.onFill};
219
+ }
220
+ &[data-variant='up']:not(:disabled):hover,
221
+ &[data-variant='down']:not(:disabled):hover {
222
+ opacity: 0.88;
223
+ }
224
+ &[data-pressed] {
225
+ background: ${t.active};
226
+ color: ${t.fg.bright};
227
+ }
228
+ }
229
+ }
230
+ `;
231
+
232
+ const injectStyles = defineStyles(BUTTON_STYLE_ID, BUTTON_CSS);
233
+
234
+ class ButtonImpl implements ButtonHandle {
235
+ readonly el: HTMLButtonElement;
236
+ private readonly iconOnly: boolean;
237
+ private readonly textNode: Text | null;
238
+ private label: string | undefined;
239
+ private pressedState: boolean | undefined;
240
+
241
+ constructor(props: ButtonProps) {
242
+ injectStyles(document);
243
+ const { variant, size } = buttonVariants.resolve(props);
244
+ this.iconOnly = size.startsWith('icon');
245
+ this.label = props.label ?? props.text;
246
+ this.pressedState = props.pressed;
247
+ const icon = (id: string, side: 'inline-start' | 'inline-end'): HTMLElement => {
248
+ const el = iconEl(id, document);
249
+ el.dataset.slot = buttonDef.slot('icon');
250
+ el.dataset.icon = side;
251
+ return el;
252
+ };
253
+ this.textNode = !this.iconOnly && props.text !== undefined ? document.createTextNode(props.text) : null;
254
+ this.el = h(
255
+ 'button',
256
+ {
257
+ className: props.className ? `vela-btn ${props.className}` : 'vela-btn',
258
+ slot: buttonDef.slot('root'),
259
+ attrs: { type: 'button', ...buttonVariants({ variant, size }) },
260
+ on: props.onClick ? { click: props.onClick } : undefined,
261
+ },
262
+ props.icon ? icon(props.icon, 'inline-start') : null,
263
+ this.textNode,
264
+ props.iconEnd && !this.iconOnly ? icon(props.iconEnd, 'inline-end') : null,
265
+ );
266
+ this.el.disabled = props.disabled ?? false;
267
+ this.paint();
268
+ }
269
+
270
+ get pressed(): boolean | undefined {
271
+ return this.pressedState;
272
+ }
273
+
274
+ setPressed(pressed: boolean): void {
275
+ this.pressedState = pressed;
276
+ this.paint();
277
+ }
278
+
279
+ setDisabled(disabled: boolean): void {
280
+ this.el.disabled = disabled;
281
+ }
282
+
283
+ setText(text: string): void {
284
+ this.label = text;
285
+ if (this.textNode) this.textNode.data = text;
286
+ this.paint();
287
+ }
288
+
289
+ destroy(): void {}
290
+
291
+ private paint(): void {
292
+ const el = this.el;
293
+ el.toggleAttribute('data-pressed', this.pressedState === true);
294
+ if (this.pressedState === undefined) el.removeAttribute('aria-pressed');
295
+ else el.setAttribute('aria-pressed', String(this.pressedState));
296
+ if (this.iconOnly && this.label !== undefined) el.setAttribute('aria-label', this.label);
297
+ else el.removeAttribute('aria-label');
298
+ }
299
+ }
300
+
301
+ export function createButton(props: ButtonProps = {}): ButtonHandle {
302
+ return new ButtonImpl(props);
303
+ }