@luxalgo/vela-ui 0.3.0 → 0.4.1

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 (177) hide show
  1. package/CHANGELOG.md +165 -132
  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/button.js +2 -2
  8. package/dist/callout-bubble.js +3 -3
  9. package/dist/checkbox.js +3 -3
  10. package/dist/{chunk-OJ2SEMN6.js → chunk-5A54GODF.js} +2 -2
  11. package/dist/{chunk-UEO6W37Z.js → chunk-7LYWI32I.js} +2 -2
  12. package/dist/{chunk-IOOYDSSK.js → chunk-BIU66EHS.js} +2 -2
  13. package/dist/{chunk-KKS6NEGI.js → chunk-BWSOMG3G.js} +1 -1
  14. package/dist/{chunk-DXDCKZ2M.js → chunk-CZRHHVSB.js} +1 -1
  15. package/dist/{chunk-C3PXY2SW.js → chunk-EDYD5XKJ.js} +9 -0
  16. package/dist/chunk-GUIYFQ27.js +30 -0
  17. package/dist/{chunk-WWHNUKSZ.js → chunk-JEGB72M4.js} +8 -8
  18. package/dist/{chunk-B2EZB5ZG.js → chunk-ODPGOUQT.js} +4 -3
  19. package/dist/{chunk-TBXUF5YX.js → chunk-RH53NQGE.js} +18 -4
  20. package/dist/{chunk-OQXFQMZM.js → chunk-TGJNHUYV.js} +2 -2
  21. package/dist/collapsible.js +2 -2
  22. package/dist/color-picker.js +4 -4
  23. package/dist/context-menu.js +4 -4
  24. package/dist/date-picker.js +1 -1
  25. package/dist/dialog.js +4 -4
  26. package/dist/donut.d.ts +1 -1
  27. package/dist/drawer.js +1 -1
  28. package/dist/dropdown-menu.d.ts +2 -0
  29. package/dist/dropdown-menu.js +4 -4
  30. package/dist/empty.js +1 -1
  31. package/dist/field.js +9 -9
  32. package/dist/{stat.d.ts → figure.d.ts} +9 -9
  33. package/dist/figure.js +41 -0
  34. package/dist/glyph-select.js +4 -4
  35. package/dist/hover-card.js +1 -1
  36. package/dist/icons.js +1 -1
  37. package/dist/index.d.ts +2 -2
  38. package/dist/index.js +1 -1
  39. package/dist/input-group.js +2 -2
  40. package/dist/item.js +1 -1
  41. package/dist/manifest.json +75 -74
  42. package/dist/number-input.js +3 -3
  43. package/dist/popover.js +2 -2
  44. package/dist/select.d.ts +5 -1
  45. package/dist/select.js +5 -5
  46. package/dist/slider.js +1 -1
  47. package/dist/sort-button.js +2 -2
  48. package/dist/switch.js +1 -1
  49. package/dist/tabs.js +2 -2
  50. package/dist/{theme-Ct-q58xz.d.ts → theme-TefB8vtl.d.ts} +2 -2
  51. package/dist/theme.d.ts +2 -2
  52. package/dist/theme.js +1 -1
  53. package/dist/theme.schema.json +30 -30
  54. package/dist/toast.d.ts +2 -2
  55. package/dist/toast.js +2 -2
  56. package/dist/toggle-group.js +2 -2
  57. package/dist/tooltip.js +1 -1
  58. package/dist/zag.d.ts +11 -2
  59. package/dist/zag.js +1 -1
  60. package/docs/theming.md +217 -217
  61. package/docs/what-to-change.md +20 -20
  62. package/docs/writing-a-component.md +250 -250
  63. package/llms.txt +3 -3
  64. package/package.json +90 -90
  65. package/registry/badge.ts +119 -119
  66. package/registry/bar-chart.ts +215 -215
  67. package/registry/button.ts +387 -387
  68. package/registry/callout-bubble.ts +347 -347
  69. package/registry/card.ts +379 -379
  70. package/registry/checkbox.ts +234 -234
  71. package/registry/collapsible.ts +271 -271
  72. package/registry/color-picker.ts +582 -582
  73. package/registry/context-menu.ts +208 -208
  74. package/registry/data-table.ts +215 -215
  75. package/registry/date-picker.ts +483 -483
  76. package/registry/dialog.ts +551 -551
  77. package/registry/donut.ts +145 -145
  78. package/registry/drawer.ts +636 -636
  79. package/registry/dropdown-menu.ts +765 -762
  80. package/registry/empty.ts +91 -91
  81. package/registry/field.ts +584 -584
  82. package/registry/{stat.ts → figure.ts} +96 -96
  83. package/registry/glyph-select.ts +268 -268
  84. package/registry/hover-card.ts +222 -222
  85. package/registry/input-group.ts +229 -229
  86. package/registry/input.ts +181 -181
  87. package/registry/item.ts +376 -376
  88. package/registry/kbd.ts +64 -64
  89. package/registry/line-chart.ts +218 -218
  90. package/registry/month-grid.ts +328 -328
  91. package/registry/number-input.ts +443 -443
  92. package/registry/popover.ts +368 -368
  93. package/registry/scatter-chart.ts +221 -221
  94. package/registry/select.ts +629 -620
  95. package/registry/slider.ts +259 -259
  96. package/registry/sort-button.ts +189 -189
  97. package/registry/sparkline.ts +114 -114
  98. package/registry/switch.ts +205 -205
  99. package/registry/tabs.ts +426 -426
  100. package/registry/text-area.ts +186 -186
  101. package/registry/toast.ts +965 -965
  102. package/registry/toggle-group.ts +295 -295
  103. package/registry/tooltip.ts +156 -156
  104. package/registry/treemap.ts +847 -847
  105. package/skills/theming-vela-ui/SKILL.md +137 -137
  106. package/skills/using-vela-ui-components/SKILL.md +141 -139
  107. package/skills/writing-vela-ui-components/SKILL.md +163 -163
  108. package/src/components/badge.ts +119 -119
  109. package/src/components/bar-chart.ts +215 -215
  110. package/src/components/button.ts +387 -387
  111. package/src/components/callout-bubble.ts +347 -347
  112. package/src/components/card.ts +379 -379
  113. package/src/components/checkbox.ts +234 -234
  114. package/src/components/collapsible.ts +271 -271
  115. package/src/components/color-picker.ts +582 -582
  116. package/src/components/context-menu.ts +208 -208
  117. package/src/components/data-table.ts +215 -215
  118. package/src/components/date-picker.ts +483 -483
  119. package/src/components/dialog.ts +551 -551
  120. package/src/components/donut.ts +145 -145
  121. package/src/components/drawer.ts +636 -636
  122. package/src/components/dropdown-menu.ts +765 -762
  123. package/src/components/empty.ts +91 -91
  124. package/src/components/field.ts +584 -584
  125. package/src/components/{stat.ts → figure.ts} +96 -96
  126. package/src/components/glyph-select.ts +268 -268
  127. package/src/components/hover-card.ts +222 -222
  128. package/src/components/input-group.ts +229 -229
  129. package/src/components/input.ts +181 -181
  130. package/src/components/item.ts +376 -376
  131. package/src/components/kbd.ts +64 -64
  132. package/src/components/line-chart.ts +218 -218
  133. package/src/components/month-grid.ts +328 -328
  134. package/src/components/number-input.ts +443 -443
  135. package/src/components/popover.ts +368 -368
  136. package/src/components/scatter-chart.ts +221 -221
  137. package/src/components/select.ts +629 -620
  138. package/src/components/slider.ts +259 -259
  139. package/src/components/sort-button.ts +189 -189
  140. package/src/components/sparkline.ts +114 -114
  141. package/src/components/switch.ts +205 -205
  142. package/src/components/tabs.ts +426 -426
  143. package/src/components/text-area.ts +186 -186
  144. package/src/components/toast.ts +965 -965
  145. package/src/components/toggle-group.ts +295 -295
  146. package/src/components/tooltip.ts +156 -156
  147. package/src/components/treemap.ts +847 -847
  148. package/src/index.ts +24 -24
  149. package/src/lib/calendar.ts +117 -117
  150. package/src/lib/chart.ts +72 -72
  151. package/src/lib/color.ts +89 -89
  152. package/src/lib/component-index.ts +163 -163
  153. package/src/lib/css.ts +48 -48
  154. package/src/lib/define.ts +45 -45
  155. package/src/lib/dispose.ts +36 -36
  156. package/src/lib/h.ts +55 -55
  157. package/src/lib/headless.ts +16 -16
  158. package/src/lib/i18n.ts +30 -30
  159. package/src/lib/icons.ts +253 -244
  160. package/src/lib/kit-text.ts +26 -26
  161. package/src/lib/page.ts +24 -24
  162. package/src/lib/palette.ts +81 -81
  163. package/src/lib/placement.ts +188 -188
  164. package/src/lib/scope.ts +135 -135
  165. package/src/lib/styles.ts +53 -53
  166. package/src/lib/surface-events.ts +31 -31
  167. package/src/lib/surface-exit.ts +87 -87
  168. package/src/lib/t.ts +68 -68
  169. package/src/lib/theme.ts +684 -670
  170. package/src/lib/tokens.ts +110 -110
  171. package/src/lib/variants.ts +32 -32
  172. package/src/lib/zag.ts +61 -46
  173. package/stylelint/index.mjs +60 -60
  174. package/stylelint/lint.mjs +81 -81
  175. package/stylelint/plugin.mjs +115 -115
  176. package/dist/chunk-P4MTEMT7.js +0 -23
  177. package/dist/stat.js +0 -41
@@ -1,387 +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 '../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
- /* 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
- }
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
+ /* 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
+ }