Skip to main content

Switch

npm version npm downloads GitHub source code Contributions welcome npm unpacked size language npm last update npm license

A highly customizable and universal switch component for React Native and Web, part of the FlexNative design system.

Features​

  • 🌐 Universal: Works seamlessly on Android, iOS, and Web.
  • 🎨 Themed: Integrated with @flexnative/theme-context for consistent styling.
  • ⚡ Smooth Animations: Powered by React Native's Animated API for high-performance transitions.
  • 🔧 Customizable: Control colors, dimensions, and animation duration with ease.
  • 📝 TypeScript: Fully typed for a great developer experience.

Installation​

Install the package via npm or yarn:

npm install @flexnative/switch
# or
yarn add @flexnative/switch

Peer Dependencies​

This component requires react-native-svg and @flexnative/theme-context. Ensure they are installed in your project:

npm install react-native-svg @flexnative/theme-context

Props​

PropTypeDefaultDescription
valuebooleanfalseThe value of the switch.
onValueChange(value: boolean) => voidundefinedCallback called when the value changes.
activeTrackColorColorValuetheme.colors.primaryBackground color when the switch is ON.
inactiveTrackColor`ColorValuetheme.colors.defaultBackground color when the switch is OFF.
thumbColorColorValuetheme.colors.cardColor of the sliding thumb.
widthnumber50Total width of the switch component.
heightnumber28Total height of the switch component.
durationnumber200Duration of the toggle animation in milliseconds.
disabledbooleanundefinedIf true, prevents user interaction.

Theming​

The component automatically consumes colors from the @flexnative/theme-context. If you don't provide explicit colors via props, it will fallback to your theme's primary, default, and card colors.

Usage​

import React, { useState } from 'react';
import Switch from '@flexnative/switch';

const Example = () => {
const [isEnabled, setIsEnabled] = useState(false);

return (
<Switch
value={isEnabled}
onValueChange={(value) => setIsEnabled(value)}
activeTrackColor="#2ecc71"
/>
);
};

Interactive Playground​

You can experiment with the Switch component in real-time below.

Live Editor
function SwitchPlayground() {
  const [isNotificationsEnabled, setIsNotificationsEnabled] = useState(false);

  return (
    <Switch
        value={isNotificationsEnabled}
        onValueChange={setIsNotificationsEnabled}
        activeTrackColor="#007AFF" // iOS blue
        width={60}
        height={32}
      />
  );
}
Result
Loading...