|
1 | | -# react-native-multiswitch-controller |
| 1 | +# React Native Multi-Switch Controller |
2 | 2 |
|
3 | | -Smooth animated multiswitch component with dynamic width |
| 3 | +A flexible and performant multi-switch controller for React Native with support for segmented controls and tabs. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 🎯 **Zero Re-renders**: Access state outside provider without causing re-renders |
| 8 | +- 🎨 **Customizable**: Full control over colors, sizes, and styling |
| 9 | +- ⚡ **Performant**: Optimized animations and state management |
| 10 | +- 🔄 **Flexible**: Support for segmented controls and tabs |
| 11 | +- 📱 **Accessible**: Built-in accessibility support |
4 | 12 |
|
5 | 13 | ## Installation |
6 | 14 |
|
7 | | -```sh |
| 15 | +```bash |
8 | 16 | npm install react-native-multiswitch-controller |
9 | 17 | ``` |
10 | 18 |
|
11 | | -## Usage |
| 19 | +## Basic Usage |
| 20 | + |
| 21 | +```tsx |
| 22 | +import { |
| 23 | + ControlListProvider, |
| 24 | + PillSwitch, |
| 25 | +} from 'react-native-multiswitch-controller'; |
| 26 | + |
| 27 | +function MyComponent() { |
| 28 | + return ( |
| 29 | + <ControlListProvider |
| 30 | + controlListProps={{ |
| 31 | + options: [ |
| 32 | + { value: 'morning', label: '🌅' }, |
| 33 | + { value: 'afternoon', label: '☀️' }, |
| 34 | + { value: 'evening', label: '🌇' }, |
| 35 | + { value: 'night', label: '🌙' }, |
| 36 | + ], |
| 37 | + defaultOption: 'morning', |
| 38 | + variant: 'segmentedControl', |
| 39 | + }} |
| 40 | + > |
| 41 | + <PillSwitch |
| 42 | + inactiveBackgroundColor="rgba(59, 130, 246, 0.08)" |
| 43 | + activeBackgroundColor="rgb(37, 99, 235)" |
| 44 | + inactiveTextColor="rgb(37, 99, 235)" |
| 45 | + activeTextColor="rgb(253, 230, 138)" |
| 46 | + /> |
| 47 | + </ControlListProvider> |
| 48 | + ); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## Accessing State Outside Provider |
| 53 | + |
| 54 | +To avoid unnecessary re-renders while still accessing the control state, use one of these approaches: |
| 55 | + |
| 56 | +### Option 1: Ref-based Access (Recommended) |
12 | 57 |
|
| 58 | +```tsx |
| 59 | +import { useControlListStateRef } from 'react-native-multiswitch-controller'; |
13 | 60 |
|
14 | | -```js |
15 | | -import { multiply } from 'react-native-multiswitch-controller'; |
| 61 | +function StateReader() { |
| 62 | + const stateRef = useControlListStateRef<string>(); |
16 | 63 |
|
17 | | -// ... |
| 64 | + const handleGetCurrentValue = () => { |
| 65 | + const currentState = stateRef.current; |
| 66 | + if (currentState) { |
| 67 | + console.log('Current active option:', currentState.activeOption); |
| 68 | + console.log('All options:', currentState.options); |
| 69 | + } |
| 70 | + }; |
18 | 71 |
|
19 | | -const result = await multiply(3, 7); |
| 72 | + return ( |
| 73 | + <View> |
| 74 | + <Text> |
| 75 | + Current value: {stateRef.current?.activeOption || 'Loading...'} |
| 76 | + </Text> |
| 77 | + <Button title="Log State" onPress={handleGetCurrentValue} /> |
| 78 | + </View> |
| 79 | + ); |
| 80 | +} |
20 | 81 | ``` |
21 | 82 |
|
| 83 | +### Option 2: Event-based Subscription |
22 | 84 |
|
23 | | -## Contributing |
| 85 | +```tsx |
| 86 | +import { useControlListStateSubscription } from 'react-native-multiswitch-controller'; |
24 | 87 |
|
25 | | -See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow. |
| 88 | +function StateSubscriber() { |
| 89 | + const [lastChange, setLastChange] = useState<string>('None'); |
26 | 90 |
|
27 | | -## License |
| 91 | + useControlListStateSubscription<string>((state) => { |
| 92 | + setLastChange(state.activeOption); |
| 93 | + console.log('State changed to:', state.activeOption); |
| 94 | + }); |
28 | 95 |
|
29 | | -MIT |
| 96 | + return ( |
| 97 | + <View> |
| 98 | + <Text>Last change: {lastChange}</Text> |
| 99 | + </View> |
| 100 | + ); |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## API Reference |
| 105 | + |
| 106 | +### ControlListProvider |
| 107 | + |
| 108 | +The provider component that manages the control list state. |
| 109 | + |
| 110 | +```tsx |
| 111 | +<ControlListProvider |
| 112 | + controlListProps={{ |
| 113 | + options: ControlOption<TValue>[]; |
| 114 | + defaultOption: TValue; |
| 115 | + variant?: 'segmentedControl' | 'tabs'; |
| 116 | + onPressCallback?: (value: TValue) => void; |
| 117 | + tabConfig?: { gap: number; padding: number }; |
| 118 | + }} |
| 119 | +> |
| 120 | + {children} |
| 121 | +</ControlListProvider> |
| 122 | +``` |
| 123 | + |
| 124 | +### PillSwitch |
| 125 | + |
| 126 | +The main switch component. |
| 127 | + |
| 128 | +```tsx |
| 129 | +<PillSwitch |
| 130 | + align?: 'left' | 'right' | 'center'; |
| 131 | + onPressCallback?: (value: TValue) => void; |
| 132 | + customItemStyle?: ViewStyle; |
| 133 | + containerHeight?: number; |
| 134 | + itemHeight?: number; |
| 135 | + inactiveBackgroundColor?: string; |
| 136 | + activeBackgroundColor?: string; |
| 137 | + inactiveTextColor?: string; |
| 138 | + activeTextColor?: string; |
| 139 | + customTextStyle?: TextStyle; |
| 140 | +/> |
| 141 | +``` |
30 | 142 |
|
31 | | ---- |
| 143 | +### Hooks |
| 144 | + |
| 145 | +#### useControlListStateRef |
| 146 | + |
| 147 | +Returns a ref to the current state without causing re-renders. |
| 148 | + |
| 149 | +```tsx |
| 150 | +const stateRef = useControlListStateRef<TValue>(); |
| 151 | +// Access state via stateRef.current |
| 152 | +``` |
| 153 | + |
| 154 | +#### useControlListStateSubscription |
| 155 | + |
| 156 | +Subscribe to state changes without re-renders. |
| 157 | + |
| 158 | +```tsx |
| 159 | +useControlListStateSubscription<TValue>((state) => { |
| 160 | + // Handle state changes |
| 161 | +}); |
| 162 | +``` |
| 163 | + |
| 164 | +## Performance Benefits |
| 165 | + |
| 166 | +- **Ref-based access**: No re-renders when reading state |
| 167 | +- **Event-based subscription**: Only re-renders when you explicitly handle changes |
| 168 | +- **Optimized animations**: Smooth transitions with minimal performance impact |
| 169 | +- **Memoized callbacks**: Prevents unnecessary re-renders in child components |
| 170 | + |
| 171 | +## Types |
| 172 | + |
| 173 | +```tsx |
| 174 | +type ControlOption<TValue> = { |
| 175 | + value: TValue; |
| 176 | + label: string; |
| 177 | +}; |
| 178 | + |
| 179 | +type ControlListState<TValue> = { |
| 180 | + options: ControlOption<TValue>[]; |
| 181 | + activeOption: TValue; |
| 182 | + onChange: (value: TValue, callback?: () => void) => void; |
| 183 | + // ... other properties |
| 184 | +}; |
| 185 | +``` |
| 186 | + |
| 187 | +## Contributing |
| 188 | + |
| 189 | +1. Fork the repository |
| 190 | +2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
| 191 | +3. Commit your changes (`git commit -m 'Add some amazing feature'`) |
| 192 | +4. Push to the branch (`git push origin feature/amazing-feature`) |
| 193 | +5. Open a Pull Request |
| 194 | + |
| 195 | +## License |
32 | 196 |
|
33 | | -Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob) |
| 197 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments