Search docs

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>
  );
});

Installation#

Install the individual package, or use @leve-ui/core to get every component at once:

pnpm add @leve-ui/toggle

Anatomy#

import { Toggle } from '@leve-ui/toggle'; // or '@leve-ui/core'

<Toggle.Root />
PartElementDescription
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

Disabled#

disabled disables the toggle.

Pressed#

defaultPressed makes the toggle pressed initially.

API Reference#

Root#

PropDefaultType
defaultPressedfalseboolean

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 attributeValue
data-leve-toggleAlways present
data-state"on" | "off"
data-disabledPresent 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.Root is a <button type="button"> with aria-pressed ("true" or "false"). When disabled, it uses the native disabled attribute, so it is not focusable.
  • Its text content is its accessible name. For an icon-only toggle, pass aria-label and hide the icon with aria-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#

KeyDescription
SpacePresses or releases the toggle.
EnterPresses or releases the toggle.