Navigated to /docs/ui/components/switch

Switch

Toggle switch control.

Preview

A live, theme-aware example rendered with the published component.

Loading preview…

Import

Import from the focused entry point below, or use the same named export from @tavojs/ui. Both are public APIs; measure the built application when comparing their bundle cost.

TSX
tsximport { Switch } from "@tavojs/ui/switch";

The component import loads compiled component CSS. Enable tavoUi() for your project theme variables, or import @tavojs/ui/theme.css once for the package default theme. The equivalent CSS-safe component alias is @tavojs/ui/css/switch.

When to use

Use it when

Use `Switch` for boolean settings

Choose something else when

Avoid Switch when wrapping controls without labels or accessible names.

Examples

These examples include imports and local state where interaction requires it. Render the exported component in a page after completing UI installation. Demo state stays in the mounted component; connect file handling, persistence, and data loading to your application as needed.

Switch example

TSXCreate: src/components/examples/SwitchExample1.tsx
tsximport { Switch } from "@tavojs/ui/switch";

export function SwitchExample1() {
  return (
    <>
      <Switch defaultChecked aria-label="Enable alerts" />
    </>
  );
}

Common props and defaults

A curated starting point. The complete TypeScript API below includes additional props and compound member contracts.

PropTypeDefaultDescription
classNamestring

Optional class hook applied to the public root element owned by the component.
checkedboolean

Configures checked for this component.
defaultCheckedboolean

Configures defaultChecked for this component.
disabledboolean

Configures disabled for this component.
sizeSize

Configures size for this component.
tone"primary" | "secondary" | "neutral"

Configures tone for this component.

Complete TypeScript API

Import from @tavojs/ui/switch. Published exports: Switch, SwitchProps.

SwitchProps

PropertyTypeRequiredDescription
childrenChild

No

Inherited children attribute accepted by Switch.

classNamestring

No

Optional class hook applied to the public root element owned by the component.

sxSx

No

Inherited sx attribute accepted by Switch.

idstring

No

Inherited id attribute accepted by Switch.

rolestring

No

Inherited role attribute accepted by Switch.

styleRecord<string, unknown>

No

Inherited style attribute accepted by Switch.

titlestring

No

Inherited title attribute accepted by Switch.

tabIndexnumber

No

Inherited tabIndex attribute accepted by Switch.

hiddenboolean

No

Inherited hidden attribute accepted by Switch.

disabledboolean

No

Configures disabled for this component.

onClickTavoEventHandler<MouseEvent>

No

Inherited onClick attribute accepted by Switch.

onChangeTavoEventHandler<Event>

No

Inherited onChange attribute accepted by Switch.

onInputTavoEventHandler<Event>

No

Inherited onInput attribute accepted by Switch.

onKeyDownTavoEventHandler<KeyboardEvent>

No

Inherited onKeyDown attribute accepted by Switch.

onFocusTavoEventHandler<FocusEvent>

No

Inherited onFocus attribute accepted by Switch.

onBlurTavoEventHandler<FocusEvent>

No

Inherited onBlur attribute accepted by Switch.

checkedboolean

No

Configures checked for this component.

defaultCheckedboolean

No

Configures defaultChecked for this component.

sizeSize

No

Configures size for this component.

tone"neutral" | "primary" | "secondary"

No

Configures tone for this component.

State ownership and DOM target

ConcernContract
StateUse checked to supply rendered state. Update it through the documented callback or native event handler. For browser-owned initial values, use defaultChecked instead.
DOM targetNative input attributes target the owned form control; wrapper-specific props stay on the documented component root.

Anatomy and related components

This component exposes one public component root rather than a compound member API.

Limitations

Avoid Switch when wrapping controls without labels or accessible names.

Use only the documented props, entry points, and compound members. Internal DOM nesting and CSS class names can change without becoming part of the public component contract.

Accessibility

Renders an input with `role="switch"`.

  • Renders an input with `role="switch"`.

Component status

ContractCurrent value
Maturitystable
Categoryforms
Component import@tavojs/ui/switch
Explicit CSS import@tavojs/ui/css/switch
PreviewInteractive preview available on this page