Toolbar
A row of controls, such as buttons and toggles, with one tab stop.
Toolbar follows the Toolbar WAI-ARIA design pattern.
import { $, component$, type PropsOf, Slot, sync$, useSignal, useStyles$ } from '@builder.io/qwik';
import { ToggleGroup } from '@leve-ui/toggle-group';
import { Toolbar } from '@leve-ui/toolbar';
import {
LuAlignCenter,
LuAlignLeft,
LuAlignRight,
LuBold,
LuItalic,
LuUnderline,
} from '@qwikest/icons/lucide';
import styles from './default.css?inline';
// Example of a router link component (such as Qwik City's <Link>) that spreads received props onto <a>
const ClientLink = component$<PropsOf<'a'>>(({ onClick$, ...props }) => {
const visited = useSignal(false);
const preventDefault$ = sync$((event: MouseEvent) => {
event.preventDefault();
});
const handleClick$ = $(() => {
visited.value = true;
});
return (
<a
{...props}
data-visited={visited.value ? 'true' : undefined}
onClick$={[preventDefault$, handleClick$, onClick$]}
>
<Slot />
</a>
);
});
export default component$(() => {
useStyles$(styles);
return (
<Toolbar.Root aria-label="Formatting" class="toolbar-default">
{/* Toggle Group items inside a Toolbar join its arrow-key navigation */}
<ToggleGroup.Root type="multiple" aria-label="Text style" class="toolbar-default-group">
<ToggleGroup.Item value="bold" aria-label="Bold" class="toolbar-default-item">
<LuBold class="toolbar-default-icon" aria-hidden="true" />
</ToggleGroup.Item>
<ToggleGroup.Item value="italic" aria-label="Italic" class="toolbar-default-item">
<LuItalic class="toolbar-default-icon" aria-hidden="true" />
</ToggleGroup.Item>
<ToggleGroup.Item value="underline" aria-label="Underline" class="toolbar-default-item">
<LuUnderline class="toolbar-default-icon" aria-hidden="true" />
</ToggleGroup.Item>
</ToggleGroup.Root>
<Toolbar.Separator class="toolbar-default-separator" />
<ToggleGroup.Root
defaultValue={['left']}
aria-label="Text alignment"
class="toolbar-default-group"
>
<ToggleGroup.Item value="left" aria-label="Align left" class="toolbar-default-item">
<LuAlignLeft class="toolbar-default-icon" aria-hidden="true" />
</ToggleGroup.Item>
<ToggleGroup.Item value="center" aria-label="Align center" class="toolbar-default-item">
<LuAlignCenter class="toolbar-default-icon" aria-hidden="true" />
</ToggleGroup.Item>
<ToggleGroup.Item value="right" aria-label="Align right" class="toolbar-default-item">
<LuAlignRight class="toolbar-default-icon" aria-hidden="true" />
</ToggleGroup.Item>
</ToggleGroup.Root>
<Toolbar.Separator class="toolbar-default-separator" />
<Toolbar.Link as={ClientLink} href="/history" class="toolbar-default-link">
Edited 5 minutes ago
</Toolbar.Link>
<Toolbar.Button class="toolbar-default-button">Share</Toolbar.Button>
</Toolbar.Root>
);
});.toolbar-default {
display: flex;
align-items: center;
gap: 6px;
padding: 6px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font-family: inherit;
}
.toolbar-default-group {
display: inline-flex;
gap: 2px;
}
.toolbar-default-item {
display: inline-flex;
align-items: center;
justify-content: center;
width: 30px;
height: 30px;
border: none;
border-radius: 6px;
background: transparent;
color: light-dark(#475569, #c9ddf0);
cursor: pointer;
}
.toolbar-default-item:hover {
background: light-dark(#f1f5f9, #ffffff14);
color: light-dark(#0f172a, #ffffff);
}
/* Pressed */
.toolbar-default-item[data-state="on"] {
background: light-dark(#f1f5f9, #ffffff14);
color: #009dfd;
}
.toolbar-default-icon {
width: 16px;
height: 16px;
}
/* A vertical line between groups; the Toolbar sets data-orientation="vertical" on it */
.toolbar-default-separator[data-orientation="vertical"] {
align-self: stretch;
width: 1px;
margin: 2px 4px;
background: light-dark(#cbd5e1, #ffffff3d);
}
.toolbar-default-link {
margin-left: auto;
padding: 0 4px;
font-size: 13px;
color: light-dark(#475569, #c9ddf0);
text-decoration: none;
}
.toolbar-default-link:hover {
text-decoration: underline;
}
.toolbar-default-button {
padding: 6px 12px;
border: none;
border-radius: 6px;
background: #009dfd;
font: inherit;
font-size: 13px;
font-weight: 500;
color: #ffffff;
cursor: pointer;
}
.toolbar-default-button:hover {
background: #006ce9;
}
.toolbar-default-item:focus-visible,
.toolbar-default-link:focus-visible,
.toolbar-default-button:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}Installation#
Install the individual package, or use @leve-ui/core to get every component at once:
pnpm add @leve-ui/toolbarnpm install @leve-ui/toolbaryarn add @leve-ui/toolbarbun add @leve-ui/toolbarpnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/coreAnatomy#
import { Toolbar } from '@leve-ui/toolbar'; // or '@leve-ui/core'
<Toolbar.Root>
<Toolbar.Button />
<Toolbar.Separator />
<Toolbar.Link />
</Toolbar.Root>
| Part | Element | Description |
|---|---|---|
Toolbar.Root | <div> | The toolbar. Moves focus between its items. |
Toolbar.Button | <button> | A button in the toolbar. Use as to render another element or component. |
Toolbar.Link | <a> | A link in the toolbar. Use as to render another element or a router link component. |
Toolbar.Separator | <div> | A line between groups of items. |
A component passed to as on Toolbar.Button or Toolbar.Link must spread the props it receives onto a single element, including id, tabIndex, aria-*, data-*, and on*$ event handlers.
Toggle and Toggle Group items placed inside a Toolbar become toolbar items automatically: arrow keys move across all of them, and the Toggle Group stops handling arrow keys itself.
Examples#
Vertical#
orientation="vertical" stacks the items, so Up and Down arrows move focus. The separators become horizontal. Disabled items are skipped, and a Toggle inside the toolbar joins it.
import { component$, type PropsOf, Slot, useStyles$ } from '@builder.io/qwik';
import { Toggle } from '@leve-ui/toggle';
import { Toolbar } from '@leve-ui/toolbar';
import { LuGrid3X3, LuRedo2, LuUndo2 } from '@qwikest/icons/lucide';
import styles from './vertical.css?inline';
// Custom button component passed via `as` must spread received props onto one element
const IconButton = component$<PropsOf<'button'>>(({ type = 'button', ...props }) => (
<button type={type} {...props}>
<Slot />
</button>
));
export default component$(() => {
useStyles$(styles);
return (
<Toolbar.Root orientation="vertical" aria-label="Canvas" class="toolbar-vertical">
<Toolbar.Button as={IconButton} aria-label="Undo" class="toolbar-vertical-item">
<LuUndo2 class="toolbar-vertical-icon" aria-hidden="true" />
</Toolbar.Button>
<Toolbar.Button aria-label="Redo" disabled class="toolbar-vertical-item">
<LuRedo2 class="toolbar-vertical-icon" aria-hidden="true" />
</Toolbar.Button>
<Toolbar.Separator class="toolbar-vertical-separator" />
{/* A Toggle inside a Toolbar joins its arrow-key navigation */}
<Toggle.Root aria-label="Show grid" class="toolbar-vertical-item">
<LuGrid3X3 class="toolbar-vertical-icon" aria-hidden="true" />
</Toggle.Root>
</Toolbar.Root>
);
});.toolbar-vertical {
display: inline-flex;
flex-direction: column;
align-items: center;
gap: 4px;
padding: 6px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font-family: inherit;
}
.toolbar-vertical-item {
display: inline-flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border: none;
border-radius: 6px;
background: transparent;
color: light-dark(#475569, #c9ddf0);
cursor: pointer;
}
.toolbar-vertical-item:hover:not(:disabled) {
background: light-dark(#f1f5f9, #ffffff14);
color: light-dark(#0f172a, #ffffff);
}
/* Pressed Toggle */
.toolbar-vertical-item[data-state="on"] {
background: light-dark(#f1f5f9, #ffffff14);
color: #009dfd;
}
.toolbar-vertical-item:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.toolbar-vertical-item:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.toolbar-vertical-icon {
width: 16px;
height: 16px;
}
/* A horizontal line in a vertical toolbar */
.toolbar-vertical-separator[data-orientation="horizontal"] {
align-self: stretch;
height: 1px;
margin: 2px 4px;
background: light-dark(#cbd5e1, #ffffff3d);
}API Reference#
Root#
| Prop | Default | Type |
|---|---|---|
| orientation | 'horizontal' | 'horizontal' | 'vertical' The orientation of the toolbar, which decides the arrow keys that move focus. |
| loop | true | boolean Whether arrow keys wrap from the last item to the first and vice versa. |
Also accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-toolbar | Always present |
data-orientation | "horizontal" | "vertical" |
Button#
| Prop | Default | Type |
|---|---|---|
| as | 'button' | keyof HTMLElementTagNameMap | Component<any> The element or component to render instead of |
Also accepts all <button> attributes.
| Data attribute | Value |
|---|---|
data-leve-toolbar-button | Always present |
data-leve-toolbar-item | Always present (also on Toggle and Toggle Group items inside a Toolbar) |
data-disabled | Present when disabled |
Link#
| Prop | Default | Type |
|---|---|---|
| as | 'a' | keyof HTMLElementTagNameMap | Component<any> The element or component to render instead of |
Also accepts all <a> attributes.
| Data attribute | Value |
|---|---|
data-leve-toolbar-link | Always present |
data-leve-toolbar-item | Always present |
Separator#
No props of its own. Accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-toolbar-separator | Always present |
data-orientation | "vertical" in a horizontal toolbar, "horizontal" in a vertical one |
Accessibility#
Complies with the Toolbar WAI-ARIA design pattern.
Toolbar.Rootrendersrole="toolbar"andaria-orientation. Name it witharia-labeloraria-labelledby.- The toolbar has one tab stop: the first enabled item, then the item that was focused last. The other items have
tabindex="-1". Toolbar.Separatorrendersrole="separator"with anaria-orientationperpendicular to the toolbar.- Disabled buttons use the native
disabledattribute and are skipped by the arrow keys. - Give icon-only items an
aria-label.
Keyboard Interactions#
| Key | Description |
|---|---|
Tab | Moves focus into the toolbar (to the item focused last), or out of it. |
ArrowRight / ArrowLeft | In a horizontal toolbar, moves focus to the next / previous item. Wraps around when loop is true. |
ArrowDown / ArrowUp | In a vertical toolbar, moves focus to the next / previous item. Wraps around when loop is true. |
Home / End | Moves focus to the first / last item. |
Enter / Space | Activates the focused item. |