Using vela-ui components
Make a kit host first
Components take their theme and base styles from the nearest kit host, and floating parts (menus, popovers, select lists, tooltips, dialogs) mount into it. On a page of your own:
import { createTheme, ensureUIHost } from '@luxalgo/vela-ui/theme';
const app = document.getElementById('app')!;
ensureUIHost(app, createTheme()); // dark by default; the theming-vela-ui skill covers the restNext to a Vela chart, the chart's root already is a kit host: mount inside it, or pass a
plugin's ctx.host as host. Use ensureUIHost, not applyTheme, to set a host up:
applyTheme only themes an element, so a menu opened from it mounts unthemed in <body>.
One shape for every component
create<Name>(props) from @luxalgo/vela-ui/<name> returns a handle with el and
destroy(). Append el where it belongs.
import { createButton } from '@luxalgo/vela-ui/button';
const save = createButton({ text: 'Save', variant: 'default', size: 'sm', onClick: () => submit() });
app.append(save.el);To find a component, its props, parts and an example, search
node_modules/@luxalgo/vela-ui/dist/manifest.json for "name": "<name>", or run
npx @luxalgo/vela-ui list. Pick by need:
| Need | Component |
|---|---|
| An action | button (variants default, outline, secondary, ghost, destructive, link, up, down, favorite; keys for key caps) |
| Switch between views | tabs (a description puts a second line under a label); a few exclusive options on one track: toggle-group |
| A status or a count | badge, figure, kbd |
| A list of actions from a button or a right-click | dropdown-menu, context-menu |
| A label or a card on hover | tooltip, hover-card |
| A modal or a sheet | dialog, drawer |
| Floating content of your own | popover |
| A notice that leaves on its own (saved, failed, an alert fired) | toast: raise it with toast() / toast.success(…) from anywhere, shown by one createToaster() |
| A section that folds | collapsible |
| A panel or tile of figures, a chart with its title | card (inset for tiles inside a card; onSelect makes it clickable) |
| Rows of a list (a watchlist, alerts, results) | item, stacked by createItemGroup |
| A form | input, input-group, text-area, number-input, select, checkbox, switch, slider, date-picker, color-picker |
| 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, treemap (weighted tiles tinted by a value, in groups that can zoom), empty |
Rules that prevent the common bugs
- Pass
hostto a floating component built before its trigger is in the page (a select takes it aslist: { host }). Without it, the component looks for a host from the trigger, finds none, and mounts in<body>, unthemed. When unsure whether the trigger is in the page yet (inside a panel'smount, say), pass it. - Start a value with
defaultValue,defaultChecked,defaultOpenordefaultPressed, listen with the matchingon…Change, and change it from code withsetValue,setChecked,setOpenorsetPressed. The setters never call the listeners, so a host can refresh a control without loops. A select's options are fixed: build a new one to change them. A slider also reportsonValueCommitonce a drag ends: run costly work there. - Every floating part takes the same placement props:
side,align,sideOffset,alignOffset,avoidCollisions,collisionPadding,collisionBoundary. - Release what you discard. Controls that run a state machine keep page-wide listeners
until released: call
disposeWithin(root)(@luxalgo/vela-ui/dispose) before dropping a subtree, ordestroy()on the handles. A destroyed dialog, drawer or popover releases its content. - Menus take rows as data:
items: [{ id, label, icon?, hint?, checked?, toggle?, disabled?, title?, separatorBefore?, submenu? }]andonSelect(id);titleis the row's tooltip (why a disabled row is unavailable). A select's options takedisabledandtitlethe same way.checkmarks: trueshows checked rows with a leading check;iconBadges: trueputs icons in tiles. - Icons are ids from the registry (
icon: 'kebab'). Add your own withregisterIcon(id, svg)from@luxalgo/vela-ui/icons, drawn withcurrentColor. - Restyle through the theme or the parts'
data-slotattributes, never class names. Class names are layout hooks, not the styling contract. Your own elements beside the kit read the same tokens as CSS variables:color: var(--vela-fg-muted),background: var(--vela-surface-elev)(names indocs/theming.md). - An inline or authored
displaydefeats thehiddenattribute. Toggle visibility of your own elements one way only:hidden, orstyle.display, not both. - Two components called Switch.
@luxalgo/vela-ui/switchis the sliding switch. In@luxalgo/vela/ui0.10,Switchis the deprecated name ofCheckbox.
Example: an order form
import { createButton } from '@luxalgo/vela-ui/button';
import { createDropdownMenu } from '@luxalgo/vela-ui/dropdown-menu';
import { createInput } from '@luxalgo/vela-ui/input';
import { createSelect } from '@luxalgo/vela-ui/select';
import { createToggleGroup } from '@luxalgo/vela-ui/toggle-group';
import { createTooltip } from '@luxalgo/vela-ui/tooltip';
const side = createToggleGroup({
items: [{ value: 'buy', label: 'Buy', tone: 'up' }, { value: 'sell', label: 'Sell', tone: 'down' }],
defaultValue: 'buy',
fill: true,
onValueChange: (value) => (order.side = value),
});
const type = createSelect({
options: [{ value: 'market', label: 'Market' }, { value: 'limit', label: 'Limit' }],
defaultValue: 'limit',
fill: true,
onValueChange: (value) => (order.type = value),
});
const size = createInput({ placeholder: 'Size', onChange: (text) => (order.size = Number(text)) });
const submit = createButton({ text: 'Place order', variant: 'default', onClick: () => place(order) });
const more = createButton({ icon: 'kebab', size: 'icon-sm', variant: 'ghost', label: 'More' });
createDropdownMenu({
trigger: more.el,
host: app,
items: [{ id: 'reset', label: 'Reset form' }, { id: 'help', label: 'Order types…', separatorBefore: true }],
onSelect: (id) => (id === 'reset' ? reset() : openHelp()),
});
createTooltip({ trigger: submit.el, content: 'Sends the order to the paper account' });
app.append(side.el, type.el, size.el, submit.el, more.el);Check your work
- Typecheck: props are typed, so a wrong prop or variant fails there.
- In the browser, open each floating part and confirm it sits inside the themed host:
el.closest('[data-vela-theme]')is not null and its background is not transparent. npx @luxalgo/vela-ui checkfinds a second installed copy of the kit, which would load every component twice.
Further reading
node_modules/@luxalgo/vela-ui/dist/manifest.json: every component's import, props, parts, settings, variants and an example.node_modules/@luxalgo/vela-ui/docs/what-to-change.md: which file or setting a change belongs in.- The
theming-vela-uiskill restyles components;writing-vela-ui-componentsbuilds new ones.