@luxalgo/vela-ui 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/CHANGELOG.md +153 -91
  2. package/LICENSE +201 -201
  3. package/NOTICE +28 -28
  4. package/README.md +86 -86
  5. package/cli/engine.mjs +282 -282
  6. package/cli/index.mjs +154 -154
  7. package/dist/badge.js +1 -1
  8. package/dist/bar-chart.d.ts +1 -1
  9. package/dist/bar-chart.js +1 -1
  10. package/dist/button.d.ts +1 -1
  11. package/dist/button.js +4 -4
  12. package/dist/callout-bubble.d.ts +1 -1
  13. package/dist/callout-bubble.js +4 -4
  14. package/dist/card.d.ts +52 -0
  15. package/dist/card.js +179 -0
  16. package/dist/checkbox.js +5 -5
  17. package/dist/{chunk-B5ZSRUDQ.js → chunk-BWSOMG3G.js} +2 -2
  18. package/dist/{chunk-G7IQLY3A.js → chunk-C3PXY2SW.js} +5 -0
  19. package/dist/{chunk-CC3DDNFC.js → chunk-DXDCKZ2M.js} +4 -4
  20. package/dist/chunk-GUIYFQ27.js +30 -0
  21. package/dist/{chunk-WGULXRWD.js → chunk-KUWTI2PE.js} +4 -4
  22. package/dist/{chunk-Z5PINFQO.js → chunk-QGWAN3EP.js} +5 -5
  23. package/dist/{chunk-V2Q4MRMS.js → chunk-RH53NQGE.js} +30 -4
  24. package/dist/{chunk-WBGSYCV2.js → chunk-SKFAYTDK.js} +4 -4
  25. package/dist/{chunk-MJ4RUGX7.js → chunk-TGJNHUYV.js} +3 -3
  26. package/dist/{chunk-WXH3EFV5.js → chunk-VC7SVSDK.js} +4 -4
  27. package/dist/{chunk-7VMN7NKJ.js → chunk-YT2BSP7O.js} +3 -3
  28. package/dist/collapsible.d.ts +1 -1
  29. package/dist/collapsible.js +3 -3
  30. package/dist/color-picker.js +5 -5
  31. package/dist/context-menu.js +5 -5
  32. package/dist/data-table.d.ts +1 -1
  33. package/dist/data-table.js +1 -1
  34. package/dist/date-picker.d.ts +1 -1
  35. package/dist/date-picker.js +2 -2
  36. package/dist/dialog.d.ts +9 -1
  37. package/dist/dialog.js +15 -5
  38. package/dist/donut.d.ts +1 -1
  39. package/dist/donut.js +1 -1
  40. package/dist/drawer.d.ts +1 -1
  41. package/dist/drawer.js +2 -2
  42. package/dist/dropdown-menu.d.ts +1 -1
  43. package/dist/dropdown-menu.js +5 -5
  44. package/dist/empty.d.ts +1 -1
  45. package/dist/empty.js +2 -2
  46. package/dist/field.js +13 -13
  47. package/dist/{stat.d.ts → figure.d.ts} +9 -9
  48. package/dist/figure.js +41 -0
  49. package/dist/glyph-select.js +5 -5
  50. package/dist/hover-card.d.ts +1 -1
  51. package/dist/hover-card.js +3 -3
  52. package/dist/icons.js +1 -1
  53. package/dist/index.d.ts +4 -2
  54. package/dist/index.js +3 -3
  55. package/dist/input-group.d.ts +1 -1
  56. package/dist/input-group.js +4 -4
  57. package/dist/input.js +2 -2
  58. package/dist/item.d.ts +1 -1
  59. package/dist/item.js +2 -2
  60. package/dist/kbd.d.ts +1 -1
  61. package/dist/kbd.js +2 -2
  62. package/dist/line-chart.d.ts +1 -1
  63. package/dist/line-chart.js +1 -1
  64. package/dist/manifest.json +691 -74
  65. package/dist/month-grid.js +1 -1
  66. package/dist/number-input.d.ts +1 -1
  67. package/dist/number-input.js +5 -5
  68. package/dist/popover.js +3 -3
  69. package/dist/scatter-chart.d.ts +1 -1
  70. package/dist/scatter-chart.js +1 -1
  71. package/dist/select.d.ts +1 -1
  72. package/dist/select.js +7 -7
  73. package/dist/slider.d.ts +1 -1
  74. package/dist/slider.js +2 -2
  75. package/dist/sort-button.js +4 -4
  76. package/dist/sparkline.d.ts +1 -1
  77. package/dist/sparkline.js +1 -1
  78. package/dist/switch.d.ts +1 -1
  79. package/dist/switch.js +3 -3
  80. package/dist/tabs.d.ts +1 -1
  81. package/dist/tabs.js +4 -4
  82. package/dist/text-area.d.ts +1 -1
  83. package/dist/text-area.js +2 -2
  84. package/dist/theme-TefB8vtl.d.ts +341 -0
  85. package/dist/theme.d.ts +42 -192
  86. package/dist/theme.js +1 -1
  87. package/dist/theme.schema.json +321 -30
  88. package/dist/toast.d.ts +49 -0
  89. package/dist/toast.js +463 -0
  90. package/dist/toggle-group.d.ts +1 -1
  91. package/dist/toggle-group.js +4 -4
  92. package/dist/tooltip.js +2 -2
  93. package/dist/treemap.d.ts +80 -0
  94. package/dist/treemap.js +470 -0
  95. package/dist/zag.d.ts +11 -2
  96. package/dist/zag.js +1 -1
  97. package/docs/theming.md +217 -217
  98. package/docs/what-to-change.md +20 -20
  99. package/docs/writing-a-component.md +250 -250
  100. package/llms.txt +6 -3
  101. package/package.json +90 -90
  102. package/registry/badge.ts +119 -119
  103. package/registry/bar-chart.ts +215 -215
  104. package/registry/button.ts +387 -379
  105. package/registry/callout-bubble.ts +347 -347
  106. package/registry/card.ts +379 -0
  107. package/registry/checkbox.ts +234 -234
  108. package/registry/collapsible.ts +271 -271
  109. package/registry/color-picker.ts +582 -582
  110. package/registry/context-menu.ts +208 -208
  111. package/registry/data-table.ts +215 -215
  112. package/registry/date-picker.ts +483 -483
  113. package/registry/dialog.ts +551 -535
  114. package/registry/donut.ts +145 -145
  115. package/registry/drawer.ts +636 -636
  116. package/registry/dropdown-menu.ts +762 -762
  117. package/registry/empty.ts +91 -91
  118. package/registry/field.ts +584 -584
  119. package/registry/{stat.ts → figure.ts} +96 -96
  120. package/registry/glyph-select.ts +268 -268
  121. package/registry/hover-card.ts +222 -222
  122. package/registry/input-group.ts +229 -229
  123. package/registry/input.ts +181 -181
  124. package/registry/item.ts +376 -376
  125. package/registry/kbd.ts +64 -64
  126. package/registry/line-chart.ts +218 -218
  127. package/registry/month-grid.ts +328 -328
  128. package/registry/number-input.ts +443 -443
  129. package/registry/popover.ts +368 -368
  130. package/registry/scatter-chart.ts +221 -221
  131. package/registry/select.ts +620 -620
  132. package/registry/slider.ts +259 -259
  133. package/registry/sort-button.ts +189 -189
  134. package/registry/sparkline.ts +114 -114
  135. package/registry/switch.ts +205 -205
  136. package/registry/tabs.ts +426 -426
  137. package/registry/text-area.ts +186 -186
  138. package/registry/toast.ts +965 -0
  139. package/registry/toggle-group.ts +295 -295
  140. package/registry/tooltip.ts +156 -156
  141. package/registry/treemap.ts +847 -0
  142. package/skills/theming-vela-ui/SKILL.md +137 -137
  143. package/skills/using-vela-ui-components/SKILL.md +139 -137
  144. package/skills/writing-vela-ui-components/SKILL.md +163 -163
  145. package/src/components/badge.ts +119 -119
  146. package/src/components/bar-chart.ts +215 -215
  147. package/src/components/button.ts +387 -379
  148. package/src/components/callout-bubble.ts +347 -347
  149. package/src/components/card.ts +379 -0
  150. package/src/components/checkbox.ts +234 -234
  151. package/src/components/collapsible.ts +271 -271
  152. package/src/components/color-picker.ts +582 -582
  153. package/src/components/context-menu.ts +208 -208
  154. package/src/components/data-table.ts +215 -215
  155. package/src/components/date-picker.ts +483 -483
  156. package/src/components/dialog.ts +551 -535
  157. package/src/components/donut.ts +145 -145
  158. package/src/components/drawer.ts +636 -636
  159. package/src/components/dropdown-menu.ts +762 -762
  160. package/src/components/empty.ts +91 -91
  161. package/src/components/field.ts +584 -584
  162. package/src/components/{stat.ts → figure.ts} +96 -96
  163. package/src/components/glyph-select.ts +268 -268
  164. package/src/components/hover-card.ts +222 -222
  165. package/src/components/input-group.ts +229 -229
  166. package/src/components/input.ts +181 -181
  167. package/src/components/item.ts +376 -376
  168. package/src/components/kbd.ts +64 -64
  169. package/src/components/line-chart.ts +218 -218
  170. package/src/components/month-grid.ts +328 -328
  171. package/src/components/number-input.ts +443 -443
  172. package/src/components/popover.ts +368 -368
  173. package/src/components/scatter-chart.ts +221 -221
  174. package/src/components/select.ts +620 -620
  175. package/src/components/slider.ts +259 -259
  176. package/src/components/sort-button.ts +189 -189
  177. package/src/components/sparkline.ts +114 -114
  178. package/src/components/switch.ts +205 -205
  179. package/src/components/tabs.ts +426 -426
  180. package/src/components/text-area.ts +186 -186
  181. package/src/components/toast.ts +965 -0
  182. package/src/components/toggle-group.ts +295 -295
  183. package/src/components/tooltip.ts +156 -156
  184. package/src/components/treemap.ts +847 -0
  185. package/src/index.ts +24 -24
  186. package/src/lib/calendar.ts +117 -117
  187. package/src/lib/chart.ts +72 -72
  188. package/src/lib/color.ts +89 -89
  189. package/src/lib/component-index.ts +163 -151
  190. package/src/lib/css.ts +48 -48
  191. package/src/lib/define.ts +45 -45
  192. package/src/lib/dispose.ts +36 -36
  193. package/src/lib/h.ts +55 -55
  194. package/src/lib/headless.ts +16 -16
  195. package/src/lib/i18n.ts +30 -30
  196. package/src/lib/icons.ts +244 -237
  197. package/src/lib/kit-text.ts +26 -26
  198. package/src/lib/page.ts +24 -24
  199. package/src/lib/palette.ts +81 -81
  200. package/src/lib/placement.ts +188 -188
  201. package/src/lib/scope.ts +135 -135
  202. package/src/lib/styles.ts +53 -53
  203. package/src/lib/surface-events.ts +31 -31
  204. package/src/lib/surface-exit.ts +87 -87
  205. package/src/lib/t.ts +68 -68
  206. package/src/lib/theme.ts +684 -664
  207. package/src/lib/tokens.ts +110 -110
  208. package/src/lib/variants.ts +32 -32
  209. package/src/lib/zag.ts +61 -46
  210. package/stylelint/index.mjs +60 -60
  211. package/stylelint/lint.mjs +81 -81
  212. package/stylelint/plugin.mjs +115 -115
  213. package/dist/chunk-P4MTEMT7.js +0 -23
  214. package/dist/stat.js +0 -41
  215. package/dist/{chunk-U3EH42YY.js → chunk-5BJV5FY5.js} +1 -1
  216. package/dist/{chunk-E2FE6V2N.js → chunk-ALAHGSLC.js} +1 -1
  217. package/dist/{chunk-C3ICYMI3.js → chunk-FYVQO5TN.js} +1 -1
@@ -1,379 +1,387 @@
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 '@luxalgo/vela-ui/icons';
12
- import { css, defineStyles } from '@luxalgo/vela-ui';
13
- import { defineComponent } from '@luxalgo/vela-ui';
14
- import { h } from '@luxalgo/vela-ui';
15
- import { t } from '@luxalgo/vela-ui';
16
- import { variants, type VariantProps } from '@luxalgo/vela-ui';
17
- import { createKbd } from '@luxalgo/vela-ui/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
- }
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 '@luxalgo/vela-ui/icons';
12
+ import { css, defineStyles } from '@luxalgo/vela-ui';
13
+ import { defineComponent } from '@luxalgo/vela-ui';
14
+ import { h } from '@luxalgo/vela-ui';
15
+ import { t } from '@luxalgo/vela-ui';
16
+ import { variants, type VariantProps } from '@luxalgo/vela-ui';
17
+ import { createKbd } from '@luxalgo/vela-ui/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
+ /* A key cap's own colors vanish on a fill that matches them (the solid
136
+ variants): inside a button it takes the button's text and draws only its outline. */
137
+ & > [data-keys] > [data-slot='kbd'] {
138
+ border-color: ${t.border};
139
+ background: transparent;
140
+ color: inherit;
141
+ box-shadow: none;
142
+ }
143
+ &[data-size='sm'],
144
+ &[data-size='icon-sm'] {
145
+ height: ${buttonDef.vars['height-sm']};
146
+ font-size: ${buttonDef.vars['font-size-sm']};
147
+ }
148
+ &[data-size='xs'],
149
+ &[data-size='icon-xs'] {
150
+ height: ${buttonDef.vars['height-xs']};
151
+ padding: 0 8px;
152
+ font-size: ${buttonDef.vars['font-size-xs']};
153
+ border-radius: ${t.radius.sm};
154
+ }
155
+ &[data-size='lg'],
156
+ &[data-size='icon-lg'] {
157
+ height: ${buttonDef.vars['height-lg']};
158
+ padding: 0 14px;
159
+ }
160
+ &[data-size='icon'] {
161
+ width: ${buttonDef.vars.height};
162
+ padding: 0;
163
+ }
164
+ &[data-size='icon-sm'] {
165
+ width: ${buttonDef.vars['height-sm']};
166
+ padding: 0;
167
+ }
168
+ &[data-size='icon-xs'] {
169
+ width: ${buttonDef.vars['height-xs']};
170
+ padding: 0;
171
+ }
172
+ &[data-size='icon-lg'] {
173
+ width: ${buttonDef.vars['height-lg']};
174
+ padding: 0;
175
+ }
176
+ &:disabled {
177
+ cursor: default;
178
+ opacity: 0.5;
179
+ }
180
+ &:focus-visible {
181
+ outline: 2px solid ${t.focus};
182
+ outline-offset: 1px;
183
+ }
184
+ &:not(:disabled):hover,
185
+ &[aria-expanded='true'] {
186
+ background: ${t.hover};
187
+ color: ${t.fg.bright};
188
+ border-color: ${t.fg.muted};
189
+ }
190
+ &[data-variant='default'] {
191
+ border-color: ${t.selected.bg};
192
+ background: ${t.selected.bg};
193
+ color: ${t.selected.fg};
194
+ }
195
+ &[data-variant='default']:not(:disabled):hover,
196
+ &[data-variant='default'][aria-expanded='true'] {
197
+ background: ${t.selected.bg};
198
+ color: ${t.selected.fg};
199
+ opacity: 0.85;
200
+ border-color: ${t.selected.bg};
201
+ }
202
+ &[data-variant='secondary'] {
203
+ background: ${t.surface.sunken};
204
+ border-color: ${t.border};
205
+ }
206
+ &[data-variant='secondary']:not(:disabled):hover,
207
+ &[data-variant='secondary'][aria-expanded='true'] {
208
+ border-color: ${t.border.strong};
209
+ }
210
+ &[data-variant='ghost'] {
211
+ border-color: transparent;
212
+ color: ${t.fg.muted};
213
+ }
214
+ &[data-variant='ghost']:not(:disabled):hover,
215
+ &[data-variant='ghost'][aria-expanded='true'] {
216
+ border-color: transparent;
217
+ }
218
+ &[data-variant='destructive'] {
219
+ border-color: transparent;
220
+ background: color-mix(in srgb, ${t.danger} 14%, transparent);
221
+ color: ${t.danger};
222
+ }
223
+ &[data-variant='destructive']:not(:disabled):hover {
224
+ border-color: transparent;
225
+ background: color-mix(in srgb, ${t.danger} 24%, transparent);
226
+ color: ${t.danger};
227
+ }
228
+ &[data-variant='link'] {
229
+ border-color: transparent;
230
+ background: transparent;
231
+ color: ${t.accent};
232
+ padding-inline: 0;
233
+ text-underline-offset: 4px;
234
+ }
235
+ &[data-variant='link']:not(:disabled):hover {
236
+ border-color: transparent;
237
+ background: transparent;
238
+ color: ${t.accent};
239
+ text-decoration: underline;
240
+ }
241
+ &[data-variant='up'],
242
+ &[data-variant='up']:not(:disabled):hover {
243
+ border-color: transparent;
244
+ background: ${t.up};
245
+ color: ${t.fg.onFill};
246
+ }
247
+ &[data-variant='down'],
248
+ &[data-variant='down']:not(:disabled):hover {
249
+ border-color: transparent;
250
+ background: ${t.down};
251
+ color: ${t.fg.onFill};
252
+ }
253
+ &[data-variant='up']:not(:disabled):hover,
254
+ &[data-variant='down']:not(:disabled):hover {
255
+ opacity: 0.88;
256
+ }
257
+ &[data-pressed] {
258
+ background: ${t.active};
259
+ color: ${t.fg.bright};
260
+ }
261
+ &[data-variant='favorite'] {
262
+ border-color: transparent;
263
+ color: ${t.fg.muted};
264
+ }
265
+ &[data-variant='favorite']:not(:disabled):hover {
266
+ border-color: transparent;
267
+ }
268
+ &[data-variant='favorite'][data-pressed] {
269
+ background: transparent;
270
+ color: ${t.highlight};
271
+ }
272
+ &[data-variant='favorite'][data-pressed]:where(:not(:disabled)):hover {
273
+ background: ${t.hover};
274
+ color: ${t.highlight};
275
+ }
276
+ }
277
+ }
278
+ `;
279
+
280
+ const injectStyles = defineStyles(BUTTON_STYLE_ID, BUTTON_CSS);
281
+
282
+ const iconPart = (id: string, side: 'inline-start' | 'inline-end'): HTMLElement => {
283
+ const el = iconEl(id, document);
284
+ el.dataset.slot = buttonDef.slot('icon');
285
+ el.dataset.icon = side;
286
+ return el;
287
+ };
288
+
289
+ const keysPart = (keys: readonly string[] | undefined, side: 'inline-start' | 'inline-end'): HTMLElement | null =>
290
+ keys?.length ? h('span', { slot: buttonDef.slot('keys'), data: { keys: side } }, ...keys.map((key) => createKbd({ text: key }).el)) : null;
291
+
292
+ class ButtonImpl implements ButtonHandle {
293
+ readonly el: HTMLButtonElement;
294
+ private readonly iconOnly: boolean;
295
+ private readonly textNode: Text | null;
296
+ private readonly toggle: boolean;
297
+ private readonly controlled: boolean;
298
+ private readonly icon: string | undefined;
299
+ private readonly iconPressed: string | undefined;
300
+ private iconStart: HTMLElement | null;
301
+ private shownIcon: string | undefined;
302
+ private label: string | undefined;
303
+ private pressedState: boolean | undefined;
304
+
305
+ constructor(props: ButtonProps) {
306
+ injectStyles(document);
307
+ const { variant, size } = buttonVariants.resolve(props);
308
+ const favorite = variant === 'favorite';
309
+ this.iconOnly = size.startsWith('icon');
310
+ this.label = props.label ?? props.text;
311
+ this.controlled = props.pressed !== undefined;
312
+ this.toggle = this.controlled || props.defaultPressed !== undefined || props.onPressedChange !== undefined;
313
+ this.pressedState = props.pressed ?? props.defaultPressed ?? (this.toggle ? false : undefined);
314
+ this.icon = props.icon ?? (favorite ? 'star' : undefined);
315
+ this.iconPressed = props.iconPressed ?? (favorite ? 'star-filled' : undefined);
316
+ this.iconStart = this.icon ? iconPart(this.icon, 'inline-start') : null;
317
+ this.shownIcon = this.icon;
318
+ this.textNode = !this.iconOnly && props.text !== undefined ? document.createTextNode(props.text) : null;
319
+ const onClick = (e: MouseEvent): void => {
320
+ if (this.toggle) {
321
+ const next = !this.pressedState;
322
+ if (!this.controlled) this.setPressed(next);
323
+ props.onPressedChange?.(next);
324
+ }
325
+ props.onClick?.(e);
326
+ };
327
+ this.el = h(
328
+ 'button',
329
+ {
330
+ className: props.className ? `vela-btn ${props.className}` : 'vela-btn',
331
+ slot: buttonDef.slot('root'),
332
+ attrs: { type: 'button', ...buttonVariants({ variant, size }) },
333
+ on: this.toggle || props.onClick ? { click: onClick } : undefined,
334
+ },
335
+ this.iconStart,
336
+ this.iconOnly ? null : keysPart(props.keys, 'inline-start'),
337
+ this.textNode,
338
+ this.iconOnly ? null : keysPart(props.keysEnd, 'inline-end'),
339
+ props.iconEnd && !this.iconOnly ? iconPart(props.iconEnd, 'inline-end') : null,
340
+ );
341
+ this.el.disabled = props.disabled ?? false;
342
+ this.paint();
343
+ }
344
+
345
+ get pressed(): boolean | undefined {
346
+ return this.pressedState;
347
+ }
348
+
349
+ setPressed(pressed: boolean): void {
350
+ this.pressedState = pressed;
351
+ this.paint();
352
+ }
353
+
354
+ setDisabled(disabled: boolean): void {
355
+ this.el.disabled = disabled;
356
+ }
357
+
358
+ setText(text: string): void {
359
+ this.label = text;
360
+ if (this.textNode) this.textNode.data = text;
361
+ this.paint();
362
+ }
363
+
364
+ destroy(): void {}
365
+
366
+ private paint(): void {
367
+ const el = this.el;
368
+ if (this.iconStart && this.iconPressed) {
369
+ const icon = this.pressedState ? this.iconPressed : this.icon!;
370
+ if (icon !== this.shownIcon) {
371
+ const next = iconPart(icon, 'inline-start');
372
+ this.iconStart.replaceWith(next);
373
+ this.iconStart = next;
374
+ this.shownIcon = icon;
375
+ }
376
+ }
377
+ el.toggleAttribute('data-pressed', this.pressedState === true);
378
+ if (this.pressedState === undefined) el.removeAttribute('aria-pressed');
379
+ else el.setAttribute('aria-pressed', String(this.pressedState));
380
+ if (this.iconOnly && this.label !== undefined) el.setAttribute('aria-label', this.label);
381
+ else el.removeAttribute('aria-label');
382
+ }
383
+ }
384
+
385
+ export function createButton(props: ButtonProps = {}): ButtonHandle {
386
+ return new ButtonImpl(props);
387
+ }