Toggle
A button that is pressed or not pressed.
Toggle follows the toggle button variant of the Button WAI-ARIA design pattern. It renders a native <button> with aria-pressed.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Toggle } from '@leve-ui/toggle';
import { LuBold } from '@qwikest/icons/lucide';
import styles from './default.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Toggle.Root class="toggle-default" aria-label="Bold">
<LuBold class="toggle-default-icon" aria-hidden="true" />
</Toggle.Root>
);
});.toggle-default {
display: inline-flex;
align-items: center;
justify-content: center;
width: 36px;
height: 36px;
padding: 0;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#475569, #c9ddf0);
font-family: inherit;
cursor: pointer;
}
.toggle-default:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
/* Pressed */
.toggle-default[data-state="on"] {
border-color: #009dfd;
background: light-dark(#daf0ff, #009dfd26);
color: light-dark(#006ce9, #4cb8ff);
}
.toggle-default:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.toggle-default[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.toggle-default-icon {
width: 16px;
height: 16px;
}Installation#
Install the individual package, or use @leve-ui/core to get every component at once:
pnpm add @leve-ui/togglenpm install @leve-ui/toggleyarn add @leve-ui/togglebun add @leve-ui/togglepnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/coreAnatomy#
import { Toggle } from '@leve-ui/toggle'; // or '@leve-ui/core'
<Toggle.Root />
| Part | Element | Description |
|---|---|---|
Toggle.Root | <button> | The toggle button. Holds the pressed state. |
Examples#
Controlled#
bind:pressed controls the pressed state with a signal. The Reset button outside the toggle changes it.
Bold: off
import { component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Toggle } from '@leve-ui/toggle';
import { LuBold } from '@qwikest/icons/lucide';
import styles from './controlled.css?inline';
export default component$(() => {
useStyles$(styles);
const pressed = useSignal(false);
return (
<div class="toggle-controlled-container">
<div class="toggle-controlled-row">
<Toggle.Root class="toggle-controlled" aria-label="Bold" bind:pressed={pressed}>
<LuBold class="toggle-controlled-icon" aria-hidden="true" />
</Toggle.Root>
<button
type="button"
class="toggle-controlled-reset"
onClick$={() => {
pressed.value = false;
}}
>
Reset
</button>
</div>
<p class="toggle-controlled-status">Bold: {pressed.value ? 'on' : 'off'}</p>
</div>
);
});.toggle-controlled-container {
display: flex;
flex-direction: column;
gap: 12px;
font-family: inherit;
font-size: 14px;
}
.toggle-controlled-row {
display: flex;
align-items: center;
gap: 8px;
}
.toggle-controlled {
display: inline-flex;
align-items: center;
justify-content: center;
width: 36px;
height: 36px;
padding: 0;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#475569, #c9ddf0);
cursor: pointer;
}
.toggle-controlled:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
/* Pressed */
.toggle-controlled[data-state="on"] {
border-color: #009dfd;
background: light-dark(#daf0ff, #009dfd26);
color: light-dark(#006ce9, #4cb8ff);
}
.toggle-controlled:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.toggle-controlled[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.toggle-controlled-icon {
width: 16px;
height: 16px;
}
/* Secondary button */
.toggle-controlled-reset {
height: 32px;
padding: 0 12px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font: inherit;
font-size: 13px;
font-weight: 500;
cursor: pointer;
}
.toggle-controlled-reset:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.toggle-controlled-reset:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.toggle-controlled-status {
margin: 0;
font-size: 13px;
color: light-dark(#475569, #c9ddf0);
}Disabled#
disabled disables the toggle.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Toggle } from '@leve-ui/toggle';
import { LuBold } from '@qwikest/icons/lucide';
import styles from './disabled.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Toggle.Root class="toggle-disabled" aria-label="Bold" disabled>
<LuBold class="toggle-disabled-icon" aria-hidden="true" />
</Toggle.Root>
);
});.toggle-disabled {
display: inline-flex;
align-items: center;
justify-content: center;
width: 36px;
height: 36px;
padding: 0;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#475569, #c9ddf0);
font-family: inherit;
cursor: pointer;
}
.toggle-disabled:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
/* Pressed */
.toggle-disabled[data-state="on"] {
border-color: #009dfd;
background: light-dark(#daf0ff, #009dfd26);
color: light-dark(#006ce9, #4cb8ff);
}
.toggle-disabled:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.toggle-disabled[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.toggle-disabled-icon {
width: 16px;
height: 16px;
}Pressed#
defaultPressed makes the toggle pressed initially.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Toggle } from '@leve-ui/toggle';
import { LuBold } from '@qwikest/icons/lucide';
import styles from './pressed.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Toggle.Root class="toggle-pressed" aria-label="Bold" defaultPressed>
<LuBold class="toggle-pressed-icon" aria-hidden="true" />
</Toggle.Root>
);
});.toggle-pressed {
display: inline-flex;
align-items: center;
justify-content: center;
width: 36px;
height: 36px;
padding: 0;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#475569, #c9ddf0);
font-family: inherit;
cursor: pointer;
}
.toggle-pressed:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
/* Pressed */
.toggle-pressed[data-state="on"] {
border-color: #009dfd;
background: light-dark(#daf0ff, #009dfd26);
color: light-dark(#006ce9, #4cb8ff);
}
.toggle-pressed:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.toggle-pressed[data-disabled] {
opacity: 0.5;
cursor: not-allowed;
}
.toggle-pressed-icon {
width: 16px;
height: 16px;
}API Reference#
Root#
| Prop | Default | Type |
|---|---|---|
| defaultPressed | false | boolean Whether the toggle is pressed initially (uncontrolled). |
| bind:pressed | – | Signal<boolean> A signal with the pressed state (controlled). |
| onPressedChange$ | – | QRL<(pressed: boolean) => void> Called when the user presses or releases the toggle. |
Also accepts all <button> attributes.
| Data attribute | Value |
|---|---|
data-leve-toggle | Always present |
data-state | "on" | "off" |
data-disabled | Present when disabled |
The pressed state is uncontrolled with defaultPressed, or controlled with bind:pressed. onPressedChange$ is called when the user presses or releases the toggle. disabled is the native button attribute.
Accessibility#
Complies with the Button WAI-ARIA design pattern (toggle button).
Toggle.Rootis a<button type="button">witharia-pressed("true"or"false"). When disabled, it uses the nativedisabledattribute, so it is not focusable.- Its text content is its accessible name. For an icon-only toggle, pass
aria-labeland hide the icon witharia-hidden="true". - Do not change the label with the state (for example, keep "Mute" instead of switching to "Unmute"). The state is conveyed by
aria-pressed. - Inside a Toolbar, the toggle joins the toolbar's roving tabindex: arrow keys move focus to it, and only one item of the toolbar is a tab stop.
Keyboard Interactions#
| Key | Description |
|---|---|
Space | Presses or releases the toggle. |
Enter | Presses or releases the toggle. |