wwwwwwwwwwwwwwwwwww

Switch

Use in forms to toggle between two states.

Features

  • Accessible, easy to compose and customize.

  • Style and animate both frame and thumb.

  • Sizable & works controlled or uncontrolled.

  • Native prop that renders native Switch on mobile

Installation

Switch is already installed in tamagui, or you can install it independently:

npm install @tamagui/switch

Usage

import { Switch } from 'tamagui' // or '@tamagui/switch'
export default () => (
<Switch size="$4">
<Switch.Thumb animation="bouncy" />
</Switch>
)

Headless with createSwitch

Using the createSwitch export, you can create a fully custom switch without using any of the default styles. This is similar to unstyled, but it doesn't assume the props size or unstyled exist, and it won't automatically apply the active theme.

You must pass SwitchContext as the context option to your Frame and Thumb styled components.

If you define a checked variant, it will apply those styles.

Here's an example:

import { Stack, styled } from '@tamagui/core'
import { createSwitch, SwitchContext } from '@tamagui/switch'
const Frame = styled(Stack, {
context: SwitchContext,
variants: {
checked: {
true: {
backgroundColor: 'yellow'
},
false: {
backgroundColor: 'green',
},
},
} as const,
})
const Thumb = styled(Stack, {
context: SwitchContext,
variants: {
checked: {
true: {
opacity: 1,
},
false: {
opacity: 0.5,
},
},
} as const,
})
export const Switch = createSwitch({
Frame,
Thumb,
})

API Reference

Switch

Switch extends Stack views inheriting all the Tamagui standard props, plus:

Props

  • labeledBy

    string

    Set aria-labeled-by.

  • name

    string

    Equivalent to input name.

  • value

    string

    Give it a value (for use in HTML forms).

  • checked

    boolean

    Control the input.

  • defaultChecked

    boolean

    Uncontrolled default value.

  • required

    boolean

    Sets aria-required.

  • onCheckedChange

    (checked: boolean) => void

  • unstyled

    boolean

    Default: 

    false

    When true, remove all default tamagui styling.

  • native

    NativeValue<"mobile" | "ios" | "android">

    Render to a native switch. (Not supported on web)

  • nativeProps

    SwitchProps (from `react-native`)

    Props to pass to the native Switch;

  • Switch.Thumb

    Switch.Thumb extends Stack views inheriting all the Tamagui standard props, plus:

    Props

  • unstyled

    boolean

    Default: 

    false

    When true, remove all default tamagui styling.

  • Previous

    Slider

    Next

    ToggleGroup