GitHub

Toggle

Toggle button that can be on or off

Basic Usage

Loading...

Installation

pnpm add @corew500/ui

Usage

import { Toggle } from "@corew500/ui/toggle"

<Toggle>Content</Toggle>

Variants

toggleVariants

AxisValuesDefault
variant
defaultoutline
default
size
defaultsmlg
default

API Reference

Props

PropTypeDefaultDescription
variantenumdefaultVisual style variant. - `default`: Transparent background with muted fill when pressed - `outline`: Bordered variant with subtle shadow
sizeenumdefaultSize variant affecting height and padding. - `sm`: Compact 8px height - `default`: Standard 9px height - `lg`: Large 10px height
pressedenumWhether the toggle button is currently pressed. This is the controlled counterpart of `defaultPressed`.
defaultPressedenumfalseWhether the toggle button is currently pressed. This is the uncontrolled counterpart of `pressed`.
disabledenumfalseWhether the component should ignore user interaction.
onPressedChange(pressed: boolean, eventDetails: { reason: "none"; event: Event; cancel: () => void; allowPropagation: () => void; isCanceled: boolean; isPropagationAllowed: boolean; trigger: Element; }) => voidCallback fired when the pressed state is changed.
valuestringA unique string that identifies the toggle when used inside a toggle group.
nativeButtonenumtrueWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (e.g. `<div>`).
styleenumStyle applied to the element, or a function that returns a style object based on the component’s state.
classNameenumCSS class applied to the element, or a function that returns a class based on the component’s state.
renderenumAllows you to replace the component’s HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render.

Accessibility

Keyboard Navigation

  • Keyboard accessible: Space or Enter to toggle state
  • Focus visible ring for keyboard navigation

Screen Readers

  • - Uses `role="button"` with `aria-pressed` state (handled by Base UI)
  • `aria-pressed="true"` when active, `aria-pressed="false"` when inactive
  • For icon-only toggles, provide `aria-label` describing the action
  • Screen readers announce current pressed state and label

Additional

  • Disabled state prevents interaction and reduces opacity

Localization

Translatable Content

  • - Pass translated text as children for text toggles
  • Provide translated `aria-label` for icon-only toggles

Locale-Specific Formatting

  • Example: `aria-label={t('toolbar.bold')}` for a bold formatting toggle

Related