Tac UIv1.1.2

Switch

A toggle control that switches between on and off states, commonly used for settings and preferences.

Import

tsx
import { Switch } from '@tac-ui/native';

Playground

Interactively configure the Switch props below.

Enable notifications
tsx
<Switch label="Enable notifications" />

Default

Uncontrolled switch. The component manages its own toggle state; use defaultChecked to set the initial value.

tsx
<Switch />
<Switch defaultChecked />

With Label

Providing a label prop wraps the switch in a row container with the label on the left, suited for settings lists.

Notifications
Dark Mode
tsx
<Switch label="Notifications" />
<Switch label="Dark Mode" defaultChecked />

Disabled

Disabled switches are non-interactive and visually dimmed regardless of their current state.

tsx
<Switch disabled />
<Switch defaultChecked disabled />

Disabled with Label

Labeled switches can also be disabled, shown at reduced opacity to indicate a non-interactive state.

Disabled Off
Disabled On
tsx
<Switch label="Disabled Off" disabled />
<Switch label="Disabled On" defaultChecked disabled />

Controlled

Pass checked and onChange to fully control the switch state from a parent. The onChange callback receives the new boolean value directly.

tsx
const [checked, setChecked] = React.useState(false);

<Switch
  checked={checked}
  onChange={(val) => setChecked(val)}
/>

All States

Overview of switch states: off, on, disabled off, and disabled on.

Off
On
Disabled Off
Disabled On
tsx
<Switch label="Off" />
<Switch label="On" defaultChecked />
<Switch label="Disabled Off" disabled />
<Switch label="Disabled On" defaultChecked disabled />

API Reference

PropTypeDefaultDescription
labelstring-Label text. When provided, wraps the switch in a labeled row container.
checkedboolean-Controlled checked state. Use with onChange for a controlled component.
defaultCheckedbooleanfalseInitial checked state for uncontrolled usage.
disabledbooleanfalseDisables the switch, making it non-interactive and visually dimmed.
onChange(checked: boolean) => void-Callback fired when the toggle state changes. Receives the new boolean value.
styleViewStyle-Additional styles applied to the root container.