Toggle

A toggle lets people choose between a pair of opposing states, like on and off, using a different appearance to indicate each state.

Apple
Apple
import { Icon } from '@roninoss/icons';
import { Stack } from 'expo-router';
import * as React from 'react';
import { View, ScrollView, Pressable, Text } from 'react-native';

import { Toggle } from '~/components/nativewindui/Toggle';
import { useColorScheme } from '~/lib/useColorScheme';

export default function TextScreen() {
  const { colors } = useColorScheme();
  const [switchValue, setSwitchValue] = React.useState(true);
  return (
    <>
      <Stack.Screen
        options={{
          title: 'NativeWindUI',
          headerSearchBarOptions: {
            hideWhenScrolling: false,
          },
          headerLargeTitle: true,
          headerRight() {
            return (
              <Pressable className="opacity-80 active:opacity-40">
                <View className="opacity-90">
                  <Icon name="cog-outline" color={colors.foreground} />
                </View>
              </Pressable>
            );
          },
        }}
      />
      <ScrollView contentInsetAdjustmentBehavior="automatic" className="p-4">
        <View className="border-border bg-card gap-4 rounded-xl border p-4 pb-6 shadow-sm shadow-black/10 dark:shadow-none">
          <Text className="text-foreground text-center text-sm font-medium tracking-wider opacity-60">
            Toggle
          </Text>
          <View className="items-center">
            <Toggle value={switchValue} onValueChange={setSwitchValue} />
          </View>
        </View>
      </ScrollView>
    </>
  );
}

Installation

1

Run the following command:

npx nwui-cli@latest add toggle
🚀
Ship.
1

Copy/paste the following code to the specified file path:

~/components/nativewindui/Toggle.tsx
import { Switch } from 'react-native';

import { useColorScheme } from '~/lib/useColorScheme';
import { COLORS } from '~/theme/colors';

function Toggle(props: React.ComponentPropsWithoutRef<typeof Switch>) {
  const { colors } = useColorScheme();
  return (
    <Switch
      trackColor={{
        true: colors.primary,
        false: colors.grey,
      }}
      thumbColor={COLORS.white}
      {...props}
    />
  );
}

export { Toggle };
🛸
Ship.

Usage

import { Toggle } from '~/components/nativewindui/Toggle';
const [switchValue, setSwitchValue] = React.useState(true);

return <Toggle value={switchValue} onValueChange={setSwitchValue} />

Props

Inherits all the props from React Native's Switch component.

© Ronin Technologies LLC 2024