Switch

Forms

A toggle switch with label, description, 3 sizes, custom color, loading state, left/right label position, and haptic feedback.

GitHub

Installation

bash
1
npx native-mate add switch

Examples

#FAFAFA
Radius100%

Usage

tsx
1
2
3
4
5
6
7
8
9
10
import { Switch } from '~/components/ui/switch'
import { useState } from 'react'

const [enabled, setEnabled] = useState(false)

<Switch
  label="Enable notifications"
  value={enabled}
  onValueChange={setEnabled}
/>

Props

PropTypeDefaultDescription
valuebooleanThe current on/off state.
onValueChange(value: boolean) => voidCallback fired when toggled.
labelstringText label rendered beside the switch.
descriptionstringSecondary description text below the label.
size"sm" | "md" | "lg""md"Controls track and thumb dimensions.
colorstringCustom active track color. Defaults to theme primary.
loadingbooleanfalseShows a spinner inside the thumb, prevents toggling.
hapticbooleantrueFires a light haptic when toggled (requires expo-haptics).
labelPosition"left" | "right""right"Which side the label appears on.
disabledbooleanfalsePrevents toggling and reduces opacity.

Example

tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { useState } from 'react'
import { Switch } from '~/components/ui/switch'
import { View } from 'react-native'
import { Text } from '@native-mate/core'

export function SwitchExample() {
  const [notifications, setNotifications] = useState(true)
  const [darkMode, setDarkMode] = useState(false)
  const [analytics, setAnalytics] = useState(false)

  return (
    <View style={{ gap: 16, padding: 16 }}>
      <Switch label="Push notifications" value={notifications} onValueChange={setNotifications} />
      <Switch label="Dark mode" value={darkMode} onValueChange={setDarkMode} />
      <Switch label="Usage analytics" value={analytics} onValueChange={setAnalytics} disabled />
    </View>
  )
}