# Switch (/docs/native/components/switch)

<Preview id="switch/tap-or-drag" title={null} />

## Installation [#installation]

<ComponentInstall name="switch" />

## Usage [#usage]

```tsx title="app/index.tsx"
import { Switch } from "@/components/ui/switch";
import { useState } from "react";
import { View } from "react-native";

export default function Example() {
  const [notify, setNotify] = useState(true);

  return (
    <View className="flex-1 items-center justify-center p-4">
      <Switch isSelected={notify} onSelectedChange={setNotify} />
    </View>
  );
}
```

## Tap or drag [#tap-or-drag]

A tap toggles; a drag carries the thumb and settles on release by position, or by the flick's
velocity if you let go fast.

<Preview id="switch/tap-or-drag" />

## Colours [#colours]

`color` says what being *on* means — an off switch is the same chrome at all six.

<Preview id="switch/colours" />

```tsx
<Switch color="default" />
<Switch color="primary" />
<Switch color="success" />
<Switch color="warning" />
<Switch color="destructive" />
<Switch color="info" />
```

## Sizes [#sizes]

<Preview id="switch/sizes" />

```tsx
<Switch size="sm" />
<Switch size="md" />
<Switch size="lg" />
```

## Start and end content [#start-and-end-content]

Both layers are written once with no conditionals — `StartContent` is revealed as the switch turns
on and `EndContent` as it turns off, each fading with the thumb's travel.

<Preview id="switch/start-and-end-content" />

```tsx
import { Icon } from "@delacour/react-native-ui/icon";
import { IconCheckmark1Small, IconX } from "@delacour/react-native-ui/icons/central";

<Switch color="primary" defaultSelected>
  <Switch.StartContent>
    <Icon icon={IconCheckmark1Small} />
  </Switch.StartContent>
  <Switch.EndContent>
    <Icon icon={IconX} />
  </Switch.EndContent>
</Switch>
```

The glyphs take their size step and colour from the switch.

## States [#states]

<Preview id="switch/disabled-and-invalid" />

```tsx
<Switch isDisabled />
<Switch isInvalid />
```

## In a settings list [#in-a-settings-list]

The row's own press toggles the switch, in a `ListGroup.Item` or in an enclosing
[`Field`](/docs/native/components/field) — so a form switch can be a bare `<Switch />` with
`Field.Label` naming it.

```tsx
<ListGroup.Item onPress={toggle}>
  <ListGroup.ItemPrefix>
    <Icon icon={IconBell} />
  </ListGroup.ItemPrefix>
  <ListGroup.ItemContent>
    <ListGroup.ItemTitle>Notifications</ListGroup.ItemTitle>
  </ListGroup.ItemContent>
  <ListGroup.ItemSuffix>
    <Switch accessibilityLabel="Notifications" color="success" isSelected={on} onSelectedChange={setOn} size="sm" />
  </ListGroup.ItemSuffix>
</ListGroup.Item>
```

## API Reference [#api-reference]

### Anatomy [#anatomy]

| Part | What it is |
| --- | --- |
| `Switch` | The track. Owns the gesture, the spring and the state |
| `Switch.Thumb` | The knob. Composed in automatically; write it out to restyle it or fill it |
| `Switch.StartContent` | Behind the thumb at the leading edge — revealed as the switch turns on |
| `Switch.EndContent` | Behind the thumb at the trailing edge — revealed as the switch turns off |

### Switch [#switch]

`ViewProps`, minus `children` and `style` — the root writes its own animated style.

<TypeTable
  type={{
    isSelected: { type: "boolean", description: "Controlled state. Pass nothing and the switch holds its own." },
    defaultSelected: { type: "boolean", description: "Starting state while uncontrolled." },
    onSelectedChange: { type: "(isSelected: boolean) => void" },
    color: {
      type: '"default" | "primary" | "success" | "warning" | "destructive" | "info"',
      default: '"default"',
      description: "What a switch that is on means.",
    },
    size: { type: '"sm" | "md" | "lg"', default: '"md"' },
    isDisabled: {
      type: "boolean",
      description: "Blocks the gesture and fades the control. Inherited from an enclosing Field.",
    },
    isInvalid: {
      type: "boolean",
      description: "Reports an invalid value. Inherited from an enclosing Field.",
    },
    haptic: {
      type: 'false | "selection" | "light" | "medium" | "heavy" | "success" | "warning" | "error"',
      description: "Played when a press or a drag actually flips it.",
    },
    className: { type: "string" },
    children: { type: "ReactNode" },
  }}
/>

<Callout type="warn">
There is no `Switch.Label` and no `Switch.Group` — use `Field`. And **RTL is not handled**: the
thumb travels on `translateX`, which does not flip.
</Callout>
