@luxalgo/vela-ui 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/README.md +5 -2
- package/dist/badge.js +2 -2
- package/dist/bar-chart.d.ts +1 -1
- package/dist/bar-chart.js +2 -2
- package/dist/button.d.ts +18 -4
- package/dist/button.js +4 -3
- package/dist/calendar.d.ts +3 -1
- package/dist/calendar.js +1 -1
- package/dist/callout-bubble.d.ts +7 -2
- package/dist/callout-bubble.js +12 -8
- package/dist/checkbox.js +4 -4
- package/dist/{chunk-SAPURP46.js → chunk-7VMN7NKJ.js} +3 -3
- package/dist/{chunk-OBLIX3VN.js → chunk-B5ZSRUDQ.js} +3 -3
- package/dist/{chunk-6NXY62JS.js → chunk-C3ICYMI3.js} +2 -2
- package/dist/chunk-CC3DDNFC.js +117 -0
- package/dist/{chunk-FCO3YZLT.js → chunk-E2FE6V2N.js} +2 -2
- package/dist/{chunk-V6TTTMTE.js → chunk-GVRX3UAV.js} +4 -1
- package/dist/{chunk-2Q4JRINP.js → chunk-MJ4RUGX7.js} +4 -4
- package/dist/{chunk-X4YPSIW6.js → chunk-U3EH42YY.js} +2 -2
- package/dist/{chunk-AJ2IYSOI.js → chunk-V2Q4MRMS.js} +23 -3
- package/dist/{chunk-XJAPCCOJ.js → chunk-WBGSYCV2.js} +3 -3
- package/dist/{chunk-5QHXOAZA.js → chunk-WGULXRWD.js} +4 -4
- package/dist/{chunk-ZB3BZHSN.js → chunk-WXH3EFV5.js} +3 -3
- package/dist/chunk-XBEL4K33.js +142 -0
- package/dist/{chunk-CH3DV6FD.js → chunk-Z5PINFQO.js} +4 -4
- package/dist/collapsible.d.ts +49 -0
- package/dist/collapsible.js +106 -0
- package/dist/color-picker.js +6 -6
- package/dist/context-menu.js +5 -5
- package/dist/css.d.ts +3 -1
- package/dist/css.js +1 -1
- package/dist/data-table.d.ts +39 -2
- package/dist/data-table.js +2 -2
- package/dist/date-picker.d.ts +1 -1
- package/dist/date-picker.js +3 -3
- package/dist/dialog.d.ts +1 -1
- package/dist/dialog.js +5 -5
- package/dist/donut.d.ts +1 -1
- package/dist/donut.js +2 -2
- package/dist/drawer.d.ts +1 -1
- package/dist/drawer.js +3 -3
- package/dist/dropdown-menu.d.ts +1 -1
- package/dist/dropdown-menu.js +4 -4
- package/dist/empty.d.ts +1 -1
- package/dist/empty.js +2 -2
- package/dist/field.js +12 -12
- package/dist/glyph-select.js +5 -5
- package/dist/h-Bl37qUis.d.ts +24 -0
- package/dist/hover-card.d.ts +1 -1
- package/dist/hover-card.js +3 -3
- package/dist/index.d.ts +7 -1
- package/dist/index.js +3 -3
- package/dist/input-group.d.ts +1 -1
- package/dist/input-group.js +4 -3
- package/dist/input.js +3 -3
- package/dist/item.d.ts +72 -0
- package/dist/item.js +131 -0
- package/dist/kbd.d.ts +1 -1
- package/dist/kbd.js +3 -3
- package/dist/line-chart.d.ts +1 -1
- package/dist/line-chart.js +2 -2
- package/dist/manifest.json +665 -11
- package/dist/month-grid.d.ts +45 -0
- package/dist/month-grid.js +157 -0
- package/dist/number-input.d.ts +1 -1
- package/dist/number-input.js +4 -4
- package/dist/popover.js +4 -4
- package/dist/scatter-chart.d.ts +1 -1
- package/dist/scatter-chart.js +2 -2
- package/dist/select.js +6 -6
- package/dist/slider.d.ts +46 -0
- package/dist/slider.js +111 -0
- package/dist/sort-button.d.ts +41 -0
- package/dist/sort-button.js +88 -0
- package/dist/sparkline.d.ts +1 -1
- package/dist/sparkline.js +2 -2
- package/dist/stat.d.ts +1 -1
- package/dist/stat.js +2 -2
- package/dist/switch.d.ts +1 -1
- package/dist/switch.js +3 -3
- package/dist/tabs.d.ts +3 -1
- package/dist/tabs.js +12 -7
- package/dist/text-area.d.ts +1 -1
- package/dist/text-area.js +3 -3
- package/dist/theme.d.ts +17 -6
- package/dist/theme.js +2 -2
- package/dist/theme.schema.json +250 -1
- package/dist/toggle-group.d.ts +1 -1
- package/dist/toggle-group.js +3 -3
- package/dist/tooltip.js +3 -3
- package/docs/theming.md +6 -0
- package/docs/writing-a-component.md +4 -1
- package/llms.txt +8 -3
- package/package.json +1 -1
- package/registry/button.ts +97 -21
- package/registry/callout-bubble.ts +14 -3
- package/registry/collapsible.ts +271 -0
- package/registry/dropdown-menu.ts +3 -3
- package/registry/item.ts +376 -0
- package/registry/month-grid.ts +328 -0
- package/registry/slider.ts +259 -0
- package/registry/sort-button.ts +189 -0
- package/registry/tabs.ts +38 -4
- package/skills/using-vela-ui-components/SKILL.md +12 -9
- package/src/components/button.ts +97 -21
- package/src/components/callout-bubble.ts +14 -3
- package/src/components/collapsible.ts +271 -0
- package/src/components/dropdown-menu.ts +3 -3
- package/src/components/item.ts +376 -0
- package/src/components/month-grid.ts +328 -0
- package/src/components/slider.ts +259 -0
- package/src/components/sort-button.ts +189 -0
- package/src/components/tabs.ts +38 -4
- package/src/lib/calendar.ts +5 -0
- package/src/lib/component-index.ts +22 -2
- package/src/lib/css.ts +6 -2
- package/src/lib/scope.ts +135 -0
- package/src/lib/theme.ts +15 -5
- package/dist/chunk-EZQXTO6K.js +0 -89
- package/dist/chunk-IDA2BT7D.js +0 -29
- package/dist/data-table-DtU3FX9j.d.ts +0 -61
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// SortButton: the header of a column that sorts a list which is not a table (a watchlist's
|
|
2
|
+
// Last and Day %). A press steps the sort: `firstDirection`, the other direction, then none
|
|
3
|
+
// when `clearable`. An arrow shows the direction, and a screen reader hears it after the
|
|
4
|
+
// label. Several of them sort one list: the host keeps one sorted and clears the others with
|
|
5
|
+
// `setDirection(null)`.
|
|
6
|
+
import { css, defineStyles } from '@luxalgo/vela-ui';
|
|
7
|
+
import { defineComponent } from '@luxalgo/vela-ui';
|
|
8
|
+
import { msg, textOf, type Translator } from '@luxalgo/vela-ui/i18n';
|
|
9
|
+
import { h } from '@luxalgo/vela-ui';
|
|
10
|
+
import { iconEl } from '@luxalgo/vela-ui/icons';
|
|
11
|
+
import { kitTranslator } from '@luxalgo/vela-ui/kit-text';
|
|
12
|
+
import { t } from '@luxalgo/vela-ui';
|
|
13
|
+
import { variants, type VariantProps } from '@luxalgo/vela-ui';
|
|
14
|
+
import { nextUid } from '@luxalgo/vela-ui/zag';
|
|
15
|
+
|
|
16
|
+
export const sortButtonDef = defineComponent({
|
|
17
|
+
name: 'sort-button',
|
|
18
|
+
parts: ['root', 'icon', 'state'],
|
|
19
|
+
settings: { radius: t.radius.sm },
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
export const sortButtonVariants = variants({ align: ['start', 'end'] });
|
|
23
|
+
|
|
24
|
+
export type SortDirection = 'asc' | 'desc';
|
|
25
|
+
|
|
26
|
+
export interface SortButtonProps extends VariantProps<typeof sortButtonVariants> {
|
|
27
|
+
/** The column's name. */
|
|
28
|
+
label: string;
|
|
29
|
+
/** The sort it starts with (default none). */
|
|
30
|
+
direction?: SortDirection | null;
|
|
31
|
+
/** What the first press sorts by (default `desc`: the largest first). */
|
|
32
|
+
firstDirection?: SortDirection;
|
|
33
|
+
/** A press after both directions clears the sort (default true). */
|
|
34
|
+
clearable?: boolean;
|
|
35
|
+
/** A press changed the sort; null: unsorted. */
|
|
36
|
+
onSortChange?: (direction: SortDirection | null) => void;
|
|
37
|
+
/** A tooltip (`Sort by Last`). */
|
|
38
|
+
title?: string;
|
|
39
|
+
disabled?: boolean;
|
|
40
|
+
/** The translator of the words a screen reader hears ("sorted ascending"). Default: the
|
|
41
|
+
* kit build's, else English. */
|
|
42
|
+
i18n?: Translator;
|
|
43
|
+
/** Extra class names (layout hooks). */
|
|
44
|
+
className?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface SortButtonHandle {
|
|
48
|
+
readonly el: HTMLButtonElement;
|
|
49
|
+
readonly direction: SortDirection | null;
|
|
50
|
+
/** Show a sort without calling `onSortChange`. */
|
|
51
|
+
setDirection(direction: SortDirection | null): void;
|
|
52
|
+
setDisabled(disabled: boolean): void;
|
|
53
|
+
destroy(): void;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const TEXT = {
|
|
57
|
+
asc: msg('ui.sortButton.ascending', 'sorted ascending'),
|
|
58
|
+
desc: msg('ui.sortButton.descending', 'sorted descending'),
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const injectStyles = defineStyles(
|
|
62
|
+
'vela-ui-sort-button',
|
|
63
|
+
css`
|
|
64
|
+
@layer vela.ui {
|
|
65
|
+
[data-slot='sort-button']:where(:not([hidden])) {
|
|
66
|
+
all: unset;
|
|
67
|
+
box-sizing: border-box;
|
|
68
|
+
display: inline-flex;
|
|
69
|
+
align-items: center;
|
|
70
|
+
gap: 3px;
|
|
71
|
+
max-width: 100%;
|
|
72
|
+
border-radius: ${sortButtonDef.vars.radius};
|
|
73
|
+
color: ${t.fg.muted};
|
|
74
|
+
font-size: 11px;
|
|
75
|
+
font-weight: 500;
|
|
76
|
+
white-space: nowrap;
|
|
77
|
+
cursor: pointer;
|
|
78
|
+
user-select: none;
|
|
79
|
+
transition: color ${t.dur.fast} ease;
|
|
80
|
+
|
|
81
|
+
&[data-align='end'] {
|
|
82
|
+
justify-content: flex-end;
|
|
83
|
+
text-align: end;
|
|
84
|
+
}
|
|
85
|
+
&:disabled {
|
|
86
|
+
cursor: default;
|
|
87
|
+
opacity: 0.5;
|
|
88
|
+
}
|
|
89
|
+
&:focus-visible {
|
|
90
|
+
outline: 2px solid ${t.focus};
|
|
91
|
+
outline-offset: 1px;
|
|
92
|
+
}
|
|
93
|
+
&:not(:disabled):hover,
|
|
94
|
+
&[data-direction] {
|
|
95
|
+
color: ${t.fg.bright};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
[data-slot='sort-button-icon'] {
|
|
99
|
+
flex: none;
|
|
100
|
+
width: 1em;
|
|
101
|
+
height: 1em;
|
|
102
|
+
font-size: 11px;
|
|
103
|
+
line-height: 0;
|
|
104
|
+
|
|
105
|
+
& > svg {
|
|
106
|
+
width: 100%;
|
|
107
|
+
height: 100%;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
[data-slot='sort-button-state'] {
|
|
111
|
+
position: absolute;
|
|
112
|
+
width: 1px;
|
|
113
|
+
height: 1px;
|
|
114
|
+
overflow: hidden;
|
|
115
|
+
clip-path: inset(50%);
|
|
116
|
+
white-space: nowrap;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
`,
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
class SortButtonImpl implements SortButtonHandle {
|
|
123
|
+
readonly el: HTMLButtonElement;
|
|
124
|
+
private readonly state: HTMLElement;
|
|
125
|
+
private readonly i18n: Translator | null;
|
|
126
|
+
private current: SortDirection | null;
|
|
127
|
+
|
|
128
|
+
constructor(private readonly props: SortButtonProps) {
|
|
129
|
+
injectStyles(document);
|
|
130
|
+
this.i18n = props.i18n ?? kitTranslator();
|
|
131
|
+
this.current = props.direction ?? null;
|
|
132
|
+
const stateId = nextUid('vela-sort');
|
|
133
|
+
this.state = h('span', { slot: sortButtonDef.slot('state'), attrs: { id: stateId } });
|
|
134
|
+
this.el = h(
|
|
135
|
+
'button',
|
|
136
|
+
{
|
|
137
|
+
className: props.className,
|
|
138
|
+
slot: sortButtonDef.slot('root'),
|
|
139
|
+
attrs: { type: 'button', title: props.title ?? null, 'aria-label': props.label, 'aria-describedby': stateId, ...sortButtonVariants(props) },
|
|
140
|
+
on: { click: () => this.step() },
|
|
141
|
+
},
|
|
142
|
+
props.label,
|
|
143
|
+
this.state,
|
|
144
|
+
);
|
|
145
|
+
this.el.disabled = props.disabled ?? false;
|
|
146
|
+
this.paint();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
get direction(): SortDirection | null {
|
|
150
|
+
return this.current;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
setDirection(direction: SortDirection | null): void {
|
|
154
|
+
this.current = direction;
|
|
155
|
+
this.paint();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
setDisabled(disabled: boolean): void {
|
|
159
|
+
this.el.disabled = disabled;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
destroy(): void {}
|
|
163
|
+
|
|
164
|
+
private step(): void {
|
|
165
|
+
const first = this.props.firstDirection ?? 'desc';
|
|
166
|
+
const second: SortDirection = first === 'desc' ? 'asc' : 'desc';
|
|
167
|
+
const clearable = this.props.clearable ?? true;
|
|
168
|
+
this.current = this.current === null ? first : this.current === first ? second : clearable ? null : first;
|
|
169
|
+
this.paint();
|
|
170
|
+
this.props.onSortChange?.(this.current);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private paint(): void {
|
|
174
|
+
const dir = this.current;
|
|
175
|
+
this.el.querySelector(`[data-slot="${sortButtonDef.slot('icon')}"]`)?.remove();
|
|
176
|
+
if (dir) {
|
|
177
|
+
const icon = iconEl(dir === 'asc' ? 'arrow-up' : 'arrow-down', document);
|
|
178
|
+
icon.dataset.slot = sortButtonDef.slot('icon');
|
|
179
|
+
this.state.before(icon);
|
|
180
|
+
}
|
|
181
|
+
this.el.toggleAttribute('data-direction', dir !== null);
|
|
182
|
+
if (dir) this.el.dataset.direction = dir;
|
|
183
|
+
this.state.textContent = dir ? textOf(this.i18n, TEXT[dir]) : '';
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function createSortButton(props: SortButtonProps): SortButtonHandle {
|
|
188
|
+
return new SortButtonImpl(props);
|
|
189
|
+
}
|
package/registry/tabs.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// select; the caller shows what the selected tab stands for, through panels it attaches or as
|
|
4
4
|
// a filter of one view. `default` puts the tabs on one tinted track, `line` underlines the
|
|
5
5
|
// selected one (the indicator dialog), `filled` is the symbol picker's market strip, `soft`
|
|
6
|
-
// the drawings drawer's category strip, `nav` a vertical settings rail.
|
|
6
|
+
// the drawings drawer's category strip, `nav` a vertical settings rail. A tab with a
|
|
7
|
+
// `description` reads on two lines, its label over it (a day strip: the weekday, then the date).
|
|
7
8
|
import * as tabs from '@zag-js/tabs';
|
|
8
9
|
import { css, defineStyles } from '@luxalgo/vela-ui';
|
|
9
10
|
import { defineComponent } from '@luxalgo/vela-ui';
|
|
@@ -16,7 +17,7 @@ import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from
|
|
|
16
17
|
|
|
17
18
|
export const tabsDef = defineComponent({
|
|
18
19
|
name: 'tabs',
|
|
19
|
-
parts: ['list', 'trigger', 'icon', 'count', 'content'],
|
|
20
|
+
parts: ['list', 'trigger', 'icon', 'count', 'label', 'description', 'content'],
|
|
20
21
|
settings: { 'track-radius': '8px', 'trigger-radius': t.radius.md, 'filled-radius': '5px', 'soft-radius': '8px', 'count-radius': '8px' },
|
|
21
22
|
});
|
|
22
23
|
|
|
@@ -34,6 +35,8 @@ export interface TabItem {
|
|
|
34
35
|
icon?: string;
|
|
35
36
|
/** A count after the label (open orders, trades); null shows none. */
|
|
36
37
|
count?: number | null;
|
|
38
|
+
/** A second line under the label (a day strip's date under its weekday). */
|
|
39
|
+
description?: string;
|
|
37
40
|
}
|
|
38
41
|
|
|
39
42
|
export interface TabsProps extends VariantProps<typeof tabsVariants> {
|
|
@@ -234,6 +237,30 @@ export const TABS_CSS = css`
|
|
|
234
237
|
background: ${t.active};
|
|
235
238
|
color: ${t.fg.bright};
|
|
236
239
|
}
|
|
240
|
+
&[data-description] {
|
|
241
|
+
display: inline-flex;
|
|
242
|
+
flex-direction: column;
|
|
243
|
+
align-items: center;
|
|
244
|
+
justify-content: center;
|
|
245
|
+
gap: 2px;
|
|
246
|
+
}
|
|
247
|
+
&[data-description]:not([data-variant='line']) {
|
|
248
|
+
padding-block: 6px;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
[data-slot='tabs-list'][data-variant='default']:has(> [data-description]) {
|
|
252
|
+
height: auto;
|
|
253
|
+
}
|
|
254
|
+
[data-slot='tabs-label'] {
|
|
255
|
+
display: inline-flex;
|
|
256
|
+
align-items: center;
|
|
257
|
+
line-height: 16px;
|
|
258
|
+
}
|
|
259
|
+
[data-slot='tabs-description'] {
|
|
260
|
+
font-size: 11px;
|
|
261
|
+
font-weight: 400;
|
|
262
|
+
line-height: 14px;
|
|
263
|
+
opacity: 0.85;
|
|
237
264
|
}
|
|
238
265
|
}
|
|
239
266
|
`;
|
|
@@ -333,7 +360,7 @@ class TabsImpl implements TabsHandle {
|
|
|
333
360
|
this.items = this.items.map((i) => (i.value === value ? { ...i, count } : i));
|
|
334
361
|
b.querySelector(`[data-slot="${tabsDef.slot('count')}"]`)?.remove();
|
|
335
362
|
const el = countEl(count);
|
|
336
|
-
if (el) b.appendChild(el);
|
|
363
|
+
if (el) (b.querySelector(`[data-slot="${tabsDef.slot('label')}"]`) ?? b).appendChild(el);
|
|
337
364
|
}
|
|
338
365
|
|
|
339
366
|
trigger(value: string): HTMLButtonElement | null {
|
|
@@ -364,7 +391,14 @@ class TabsImpl implements TabsHandle {
|
|
|
364
391
|
icon = iconEl(item.icon, document);
|
|
365
392
|
icon.dataset.slot = tabsDef.slot('icon');
|
|
366
393
|
}
|
|
367
|
-
const b =
|
|
394
|
+
const b = item.description
|
|
395
|
+
? h(
|
|
396
|
+
'button',
|
|
397
|
+
{ className, slot: tabsDef.slot('trigger'), data: { variant: this.variant, description: true } },
|
|
398
|
+
h('span', { slot: tabsDef.slot('label') }, icon, item.label, countEl(item.count)),
|
|
399
|
+
h('span', { slot: tabsDef.slot('description'), text: item.description }),
|
|
400
|
+
)
|
|
401
|
+
: h('button', { className, slot: tabsDef.slot('trigger'), data: { variant: this.variant } }, icon, item.label, countEl(item.count));
|
|
368
402
|
this.triggers.set(item.value, b);
|
|
369
403
|
return b;
|
|
370
404
|
}),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: using-vela-ui-components
|
|
3
|
-
description: Builds interface pieces with @luxalgo/vela-ui, the component kit behind Vela charts - buttons, tabs, toggle groups, badges, dialogs, drawers, dropdown and context menus, tooltips, hover cards, popovers, form controls (checkbox, switch, input, select, number input, date and color pickers) and data components (data table, stat, sparkline, bar, line and scatter charts). Use when adding or wiring these components on a page or next to a Vela chart, when a menu, dialog or tooltip from the kit renders unstyled or in the wrong place, or when the user wants UI that matches their Vela chart, even if they do not name the kit.
|
|
3
|
+
description: Builds interface pieces with @luxalgo/vela-ui, the component kit behind Vela charts - buttons, tabs, toggle groups, badges, dialogs, drawers, dropdown and context menus, tooltips, hover cards, popovers, collapsible sections, list rows, form controls (checkbox, switch, slider, input, select, number input, date and color pickers) and data components (data table, sort buttons, month grid, stat, sparkline, bar, line and scatter charts). Use when adding or wiring these components on a page or next to a Vela chart, when a menu, dialog or tooltip from the kit renders unstyled or in the wrong place, or when the user wants UI that matches their Vela chart, even if they do not name the kit.
|
|
4
4
|
license: Apache-2.0
|
|
5
5
|
---
|
|
6
6
|
|
|
@@ -40,15 +40,17 @@ To find a component, its props, parts and an example, search
|
|
|
40
40
|
|
|
41
41
|
| Need | Component |
|
|
42
42
|
| --- | --- |
|
|
43
|
-
| An action | `button` (variants `default`, `outline`, `secondary`, `ghost`, `destructive`, `link`, `up`, `down`) |
|
|
44
|
-
| Switch between views | `tabs
|
|
43
|
+
| An action | `button` (variants `default`, `outline`, `secondary`, `ghost`, `destructive`, `link`, `up`, `down`, `favorite`; `keys` for key caps) |
|
|
44
|
+
| Switch between views | `tabs` (a `description` puts a second line under a label); a few exclusive options on one track: `toggle-group` |
|
|
45
45
|
| A status or a count | `badge`, `stat`, `kbd` |
|
|
46
46
|
| A list of actions from a button or a right-click | `dropdown-menu`, `context-menu` |
|
|
47
47
|
| A label or a card on hover | `tooltip`, `hover-card` |
|
|
48
48
|
| A modal or a sheet | `dialog`, `drawer` |
|
|
49
49
|
| Floating content of your own | `popover` |
|
|
50
|
-
| A
|
|
51
|
-
|
|
|
50
|
+
| A section that folds | `collapsible` |
|
|
51
|
+
| Rows of a list (a watchlist, alerts, results) | `item`, stacked by `createItemGroup` |
|
|
52
|
+
| A form | `input`, `input-group`, `text-area`, `number-input`, `select`, `checkbox`, `switch`, `slider`, `date-picker`, `color-picker` |
|
|
53
|
+
| Data | `data-table`, `sort-button` (sorts a list that is not a table), `month-grid` (a figure per day), `sparkline`, `line-chart`, `bar-chart`, `scatter-chart`, `donut`, `empty` |
|
|
52
54
|
|
|
53
55
|
## Rules that prevent the common bugs
|
|
54
56
|
|
|
@@ -56,10 +58,11 @@ To find a component, its props, parts and an example, search
|
|
|
56
58
|
takes it as `list: { host }`). Without it, the component looks for a host from the trigger,
|
|
57
59
|
finds none, and mounts in `<body>`, unthemed. When unsure whether the trigger is in the page
|
|
58
60
|
yet (inside a panel's `mount`, say), pass it.
|
|
59
|
-
2. **Start a value with `defaultValue
|
|
60
|
-
`
|
|
61
|
-
never call the listeners, so a host
|
|
62
|
-
are fixed: build a new one to
|
|
61
|
+
2. **Start a value with `defaultValue`, `defaultChecked`, `defaultOpen` or `defaultPressed`,
|
|
62
|
+
listen with the matching `on…Change`, and change it from code with `setValue`,
|
|
63
|
+
`setChecked`, `setOpen` or `setPressed`.** The setters never call the listeners, so a host
|
|
64
|
+
can refresh a control without loops. A select's options are fixed: build a new one to
|
|
65
|
+
change them. A slider also reports `onValueCommit` once a drag ends: run costly work there.
|
|
63
66
|
3. **Every floating part takes the same placement props:** `side`, `align`, `sideOffset`,
|
|
64
67
|
`alignOffset`, `avoidCollisions`, `collisionPadding`, `collisionBoundary`.
|
|
65
68
|
4. **Release what you discard.** Controls that run a state machine keep page-wide listeners
|
package/src/components/button.ts
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
// Button: a <button> with optional icons before and after its text
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// market's colors (buy and sell)
|
|
6
|
-
//
|
|
7
|
-
//
|
|
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.
|
|
8
11
|
import { iconEl } from '../lib/icons';
|
|
9
12
|
import { css, defineStyles } from '../lib/css';
|
|
10
13
|
import { defineComponent } from '../lib/define';
|
|
11
14
|
import { h } from '../lib/h';
|
|
12
15
|
import { t } from '../lib/t';
|
|
13
16
|
import { variants, type VariantProps } from '../lib/variants';
|
|
17
|
+
import { createKbd } from './kbd';
|
|
14
18
|
|
|
15
19
|
export const buttonDef = defineComponent({
|
|
16
20
|
name: 'button',
|
|
17
|
-
parts: ['root', 'icon'],
|
|
21
|
+
parts: ['root', 'icon', 'keys'],
|
|
18
22
|
settings: {
|
|
19
23
|
radius: t.radius.md,
|
|
20
24
|
height: '34px',
|
|
@@ -28,7 +32,7 @@ export const buttonDef = defineComponent({
|
|
|
28
32
|
});
|
|
29
33
|
|
|
30
34
|
export const buttonVariants = variants({
|
|
31
|
-
variant: ['default', 'outline', 'secondary', 'ghost', 'destructive', 'link', 'up', 'down'],
|
|
35
|
+
variant: ['default', 'outline', 'secondary', 'ghost', 'destructive', 'link', 'up', 'down', 'favorite'],
|
|
32
36
|
size: ['default', 'xs', 'sm', 'lg', 'icon', 'icon-xs', 'icon-sm', 'icon-lg'],
|
|
33
37
|
});
|
|
34
38
|
|
|
@@ -38,14 +42,27 @@ export type ButtonSize = (typeof buttonVariants.spec.size)[number];
|
|
|
38
42
|
export interface ButtonProps extends VariantProps<typeof buttonVariants> {
|
|
39
43
|
/** The visible text (the `icon*` sizes show none; `label` names them). */
|
|
40
44
|
text?: string;
|
|
41
|
-
/** Icon id from the kit's registry, before the text (the icon of an `icon*` size).
|
|
45
|
+
/** Icon id from the kit's registry, before the text (the icon of an `icon*` size).
|
|
46
|
+
* `favorite` defaults to `star`. */
|
|
42
47
|
icon?: string;
|
|
48
|
+
/** The icon while pressed (`star-filled` for `star`). `favorite` defaults to `star-filled`. */
|
|
49
|
+
iconPressed?: string;
|
|
43
50
|
/** Icon id after the text (a menu trigger's chevron). */
|
|
44
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[];
|
|
45
56
|
/** Accessible name; required for the `icon*` sizes, defaults to `text`. */
|
|
46
57
|
label?: string;
|
|
47
|
-
/** A toggle
|
|
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. */
|
|
48
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;
|
|
49
66
|
disabled?: boolean;
|
|
50
67
|
onClick?: (e: MouseEvent) => void;
|
|
51
68
|
/** Extra class names (layout hooks). */
|
|
@@ -55,6 +72,7 @@ export interface ButtonProps extends VariantProps<typeof buttonVariants> {
|
|
|
55
72
|
export interface ButtonHandle {
|
|
56
73
|
readonly el: HTMLButtonElement;
|
|
57
74
|
readonly pressed: boolean | undefined;
|
|
75
|
+
/** Show a toggle's state, without calling `onPressedChange`. */
|
|
58
76
|
setPressed(pressed: boolean): void;
|
|
59
77
|
setDisabled(disabled: boolean): void;
|
|
60
78
|
/** New text (or, for an `icon*` size, a new accessible name). */
|
|
@@ -107,6 +125,13 @@ export const BUTTON_CSS = css`
|
|
|
107
125
|
&[data-size='icon-xs'] > [data-icon] {
|
|
108
126
|
font-size: 12px;
|
|
109
127
|
}
|
|
128
|
+
& > [data-keys] {
|
|
129
|
+
flex: none;
|
|
130
|
+
gap: 2px;
|
|
131
|
+
}
|
|
132
|
+
& > [data-keys]:where(:not([hidden])) {
|
|
133
|
+
display: inline-flex;
|
|
134
|
+
}
|
|
110
135
|
&[data-size='sm'],
|
|
111
136
|
&[data-size='icon-sm'] {
|
|
112
137
|
height: ${buttonDef.vars['height-sm']};
|
|
@@ -225,43 +250,85 @@ export const BUTTON_CSS = css`
|
|
|
225
250
|
background: ${t.active};
|
|
226
251
|
color: ${t.fg.bright};
|
|
227
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
|
+
}
|
|
228
268
|
}
|
|
229
269
|
}
|
|
230
270
|
`;
|
|
231
271
|
|
|
232
272
|
const injectStyles = defineStyles(BUTTON_STYLE_ID, BUTTON_CSS);
|
|
233
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
|
+
|
|
234
284
|
class ButtonImpl implements ButtonHandle {
|
|
235
285
|
readonly el: HTMLButtonElement;
|
|
236
286
|
private readonly iconOnly: boolean;
|
|
237
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;
|
|
238
294
|
private label: string | undefined;
|
|
239
295
|
private pressedState: boolean | undefined;
|
|
240
296
|
|
|
241
297
|
constructor(props: ButtonProps) {
|
|
242
298
|
injectStyles(document);
|
|
243
299
|
const { variant, size } = buttonVariants.resolve(props);
|
|
300
|
+
const favorite = variant === 'favorite';
|
|
244
301
|
this.iconOnly = size.startsWith('icon');
|
|
245
302
|
this.label = props.label ?? props.text;
|
|
246
|
-
this.
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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;
|
|
253
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
|
+
};
|
|
254
319
|
this.el = h(
|
|
255
320
|
'button',
|
|
256
321
|
{
|
|
257
322
|
className: props.className ? `vela-btn ${props.className}` : 'vela-btn',
|
|
258
323
|
slot: buttonDef.slot('root'),
|
|
259
324
|
attrs: { type: 'button', ...buttonVariants({ variant, size }) },
|
|
260
|
-
on: props.onClick ? { click:
|
|
325
|
+
on: this.toggle || props.onClick ? { click: onClick } : undefined,
|
|
261
326
|
},
|
|
262
|
-
|
|
327
|
+
this.iconStart,
|
|
328
|
+
this.iconOnly ? null : keysPart(props.keys, 'inline-start'),
|
|
263
329
|
this.textNode,
|
|
264
|
-
|
|
330
|
+
this.iconOnly ? null : keysPart(props.keysEnd, 'inline-end'),
|
|
331
|
+
props.iconEnd && !this.iconOnly ? iconPart(props.iconEnd, 'inline-end') : null,
|
|
265
332
|
);
|
|
266
333
|
this.el.disabled = props.disabled ?? false;
|
|
267
334
|
this.paint();
|
|
@@ -290,6 +357,15 @@ class ButtonImpl implements ButtonHandle {
|
|
|
290
357
|
|
|
291
358
|
private paint(): void {
|
|
292
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
|
+
}
|
|
293
369
|
el.toggleAttribute('data-pressed', this.pressedState === true);
|
|
294
370
|
if (this.pressedState === undefined) el.removeAttribute('aria-pressed');
|
|
295
371
|
else el.setAttribute('aria-pressed', String(this.pressedState));
|
|
@@ -7,6 +7,7 @@ import { iconAt } from '../lib/icons';
|
|
|
7
7
|
import type { ChartTheme } from '../lib/tokens';
|
|
8
8
|
import { css, defineStyles } from '../lib/css';
|
|
9
9
|
import { defineComponent } from '../lib/define';
|
|
10
|
+
import { addSlot } from '../lib/h';
|
|
10
11
|
import { t } from '../lib/t';
|
|
11
12
|
import { Popover, type PlacementProps, type PopoverBoundary } from './popover';
|
|
12
13
|
|
|
@@ -185,6 +186,17 @@ export const CALLOUT_CSS = css`
|
|
|
185
186
|
|
|
186
187
|
const injectStyles = defineStyles(CALLOUT_STYLE_ID, CALLOUT_CSS);
|
|
187
188
|
|
|
189
|
+
/** A part of the deployed panel, as {@link calloutPart} gives it. */
|
|
190
|
+
export type CalloutPanelPart = Exclude<(typeof calloutBubbleDef.parts)[number], 'root'>;
|
|
191
|
+
|
|
192
|
+
/** Make `el` a part of a deployed panel, with the panel's look: for a panel built without a
|
|
193
|
+
* bubble, such as a chart's timeline-mark popup. Returns `el`. */
|
|
194
|
+
export function calloutPart<E extends HTMLElement>(el: E, part: CalloutPanelPart): E {
|
|
195
|
+
injectStyles(el.ownerDocument);
|
|
196
|
+
addSlot(el, calloutBubbleDef.slot(part));
|
|
197
|
+
return el;
|
|
198
|
+
}
|
|
199
|
+
|
|
188
200
|
export class CalloutBubble {
|
|
189
201
|
readonly el: HTMLElement;
|
|
190
202
|
private spec: CalloutBubbleSpec;
|
|
@@ -294,11 +306,10 @@ export class CalloutBubble {
|
|
|
294
306
|
|
|
295
307
|
private buildPanel(body: HTMLElement, panel: CalloutPanel): void {
|
|
296
308
|
const doc = body.ownerDocument;
|
|
297
|
-
const part = <K extends keyof HTMLElementTagNameMap>(tag: K, className: string, name:
|
|
309
|
+
const part = <K extends keyof HTMLElementTagNameMap>(tag: K, className: string, name: CalloutPanelPart): HTMLElementTagNameMap[K] => {
|
|
298
310
|
const el = doc.createElement(tag);
|
|
299
311
|
el.className = className;
|
|
300
|
-
el
|
|
301
|
-
return el;
|
|
312
|
+
return calloutPart(el, name);
|
|
302
313
|
};
|
|
303
314
|
const root = part('div', 'vela-callout-panel', 'panel');
|
|
304
315
|
if (panel.title) {
|