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