react-native-directional-toggle 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.en.md CHANGED
@@ -1,65 +1,265 @@
1
- # Description
1
+ # react-native-directional-toggle
2
2
 
3
- This is a React Native / Expo component library.
3
+ <p align="center">
4
+ <a href="https://www.npmjs.com/package/react-native-directional-toggle">
5
+ <img src="https://img.shields.io/npm/v/react-native-directional-toggle.svg?style=for-the-badge" alt="NPM Version" />
6
+ </a>
7
+ <a href="https://github.com/alansuhe/react-native-directional-toggle/actions/workflows/ci.yml">
8
+ <img src="https://img.shields.io/github/actions/workflow/status/alansuhe/react-native-directional-toggle/ci.yml?branch=main&style=for-the-badge" alt="CI Status" />
9
+ </a>
10
+ <a href="https://github.com/alansuhe/react-native-directional-toggle/blob/main/LICENSE">
11
+ <img src="https://img.shields.io/npm/l/react-native-directional-toggle.svg?style=for-the-badge" alt="License" />
12
+ </a>
13
+ <a href="https://www.npmjs.com/package/react-native-directional-toggle">
14
+ <img src="https://img.shields.io/npm/dm/react-native-directional-toggle.svg?style=for-the-badge" alt="NPM Downloads" />
15
+ </a>
16
+ </p>
4
17
 
5
- For Chinese version, please click: [中文](README.md).
18
+ <p align="center">
19
+ <b>Multi-Element Toggle Component</b><br/>
20
+ A multi-option toggle switch component for React Native and Expo with support for horizontal and vertical layouts and smooth animations.
21
+ </p>
6
22
 
7
- ## Installation
23
+ <p align="center">
24
+ English | <a href="./README.md">中文</a>
25
+ </p>
26
+
27
+ ---
28
+
29
+ ## ✨ Features
30
+
31
+ - 🎯 **Multi-Option Support** - Toggle between two or more options
32
+ - 📱 **Bidirectional Layout** - Support for horizontal and vertical layouts
33
+ - 🎨 **Highly Customizable** - Customize styles, colors, and text styles
34
+ - ⚡ **Smooth Animations** - High-performance animations powered by Reanimated
35
+ - 👆 **Gesture Support** - Support for tap and swipe gestures
36
+ - 🔧 **TypeScript** - Complete TypeScript type support
37
+ - 📦 **Lightweight** - Zero dependencies (except peer dependencies)
38
+ - 🚀 **Expo Compatible** - Works with Expo projects
39
+
40
+ ## 📦 Installation
41
+
42
+ ### Using npm
8
43
 
9
44
  ```bash
10
- pnpm add react-native-directional-toggle
45
+ npm install react-native-directional-toggle
46
+ ```
11
47
 
48
+ ### Using yarn
49
+
50
+ ```bash
12
51
  yarn add react-native-directional-toggle
13
52
  ```
14
53
 
15
- ## Peer Dependencies
54
+ ### Using pnpm
16
55
 
17
56
  ```bash
18
- pnpm add react-native-gesture-handler react-native-reanimated react-native-worklets --save-peer
57
+ pnpm add react-native-directional-toggle
19
58
  ```
20
59
 
21
- Dependencies required:
22
- - react-native-gesture-handler
23
- - react-native-reanimated
24
- - react-native-worklets
60
+ ### Install Peer Dependencies
25
61
 
26
- Note: Only works with `expo run` after prebuild, not supported by `expo go`.
62
+ This component relies on the following peer dependencies. Please ensure they are installed:
27
63
 
28
- ## Usage
64
+ ```bash
65
+ # npm
66
+ npm install react-native-gesture-handler react-native-reanimated react-native-worklets
67
+
68
+ # yarn
69
+ yarn add react-native-gesture-handler react-native-reanimated react-native-worklets
70
+
71
+ # pnpm
72
+ pnpm add react-native-gesture-handler react-native-reanimated react-native-worklets
73
+ ```
29
74
 
30
- > Refer to the [example project](example/).
75
+ **Note:**
31
76
 
32
- ### Import and use
77
+ - Expo projects require `expo prebuild` and `expo run`, not supported in Expo Go
78
+ - You need to wrap your app with `GestureHandlerRootView`
79
+
80
+ ## 🚀 Usage
81
+
82
+ ### Basic Usage
33
83
 
34
84
  ```tsx
35
85
  import { GestureHandlerRootView } from 'react-native-gesture-handler';
36
86
  import Switcher from 'react-native-directional-toggle';
37
87
 
38
88
  const options = [
39
- {
40
- label: 'Option 1',
41
- value: 'Option 1',
42
- },
43
- {
44
- label: 'Option 2',
45
- value: 'Option 2',
46
- },
47
- {
48
- label: 'Option 3',
49
- value: 'Option 3',
50
- },
89
+ { label: 'Option 1', value: 'Option 1' },
90
+ { label: 'Option 2', value: 'Option 2' },
91
+ { label: 'Option 3', value: 'Option 3' },
51
92
  ];
52
93
 
53
- ...
94
+ function App() {
95
+ const [value, setValue] = useState('Option 2');
96
+
97
+ return (
98
+ <GestureHandlerRootView style={{ flex: 1 }}>
99
+ {/* Horizontal mode: specify width and height */}
100
+ <Switcher
101
+ options={options}
102
+ value={value}
103
+ onChange={setValue}
104
+ style={{ width: 300, height: 40, backgroundColor: '#f2f2f2' }}
105
+ thumbStyle={{ backgroundColor: '#fff', elevation: 3 }}
106
+ />
107
+ </GestureHandlerRootView>
108
+ );
109
+ }
110
+ ```
54
111
 
55
- <GestureHandlerRootView>
56
- <Switcher
57
- options={options}
58
- value={'Option 2'}
59
- height={36}
60
- onChange={value => console.log(value)}
61
- />
62
- </GestureHandlerRootView>
112
+ ### Vertical Mode
63
113
 
114
+ ```tsx
115
+ import { View } from 'react-native';
116
+
117
+ // Vertical mode: adapts to parent container height
118
+ <View style={{ height: 200, width: 60 }}>
119
+ <Switcher
120
+ vertical
121
+ options={options}
122
+ value={value}
123
+ onChange={setValue}
124
+ style={{ flex: 1 }}
125
+ />
126
+ </View>;
64
127
  ```
65
- Note: You need to wrap the outer layer of your App (such as _layout.tsx, etc.) with `GestureHandlerRootView`.
128
+
129
+ ### Custom Styles
130
+
131
+ ```tsx
132
+ <Switcher
133
+ options={[
134
+ { label: 'Day', value: 'day' },
135
+ { label: 'Week', value: 'week' },
136
+ { label: 'Month', value: 'month' },
137
+ ]}
138
+ value={value}
139
+ onChange={setValue}
140
+ style={{
141
+ width: 240,
142
+ height: 44,
143
+ backgroundColor: '#e8e8e8',
144
+ borderRadius: 22,
145
+ padding: 3,
146
+ }}
147
+ thumbStyle={{
148
+ backgroundColor: '#fff',
149
+ borderRadius: 20,
150
+ shadowColor: '#000',
151
+ shadowOffset: { width: 0, height: 2 },
152
+ shadowOpacity: 0.1,
153
+ shadowRadius: 4,
154
+ elevation: 3,
155
+ }}
156
+ textStyle={{
157
+ fontSize: 14,
158
+ fontWeight: '600',
159
+ }}
160
+ activeTextStyle={{
161
+ color: '#007AFF',
162
+ }}
163
+ inactiveTextStyle={{
164
+ color: '#666',
165
+ }}
166
+ />
167
+ ```
168
+
169
+ ## 📖 API Documentation
170
+
171
+ ### Props
172
+
173
+ | Prop | Type | Required | Default | Description |
174
+ | ------------------- | -------------------------------------- | -------- | ------------------- | ------------------------------------------------------ |
175
+ | `options` | `Option[]` | ✅ | - | List of options, each with `label` and `value` |
176
+ | `value` | `string \| number` | ✅ | - | Currently selected value |
177
+ | `onChange` | `(value: string \| number) => void` | ✅ | - | Callback when value changes |
178
+ | `vertical` | `boolean` | ❌ | `false` | Whether to use vertical layout |
179
+ | `style` | `StyleProp<ViewStyle>` | ❌ | - | Container style for width, height, background, etc. |
180
+ | `thumbStyle` | `StyleProp<ViewStyle>` | ❌ | - | Style for the sliding thumb (selected item background) |
181
+ | `textStyle` | `StyleProp<TextStyle>` | ❌ | - | Default style for option text |
182
+ | `activeTextStyle` | `StyleProp<TextStyle>` | ❌ | - | Text style when active/selected |
183
+ | `inactiveTextStyle` | `StyleProp<TextStyle>` | ❌ | - | Text style when inactive |
184
+ | `animationConfig` | `WithTimingConfig \| WithSpringConfig` | ❌ | `{ duration: 150 }` | Animation configuration |
185
+
186
+ ### Type Definitions
187
+
188
+ ```typescript
189
+ type Option = {
190
+ label: string;
191
+ value: string | number;
192
+ };
193
+
194
+ type AnimatedSwitchProps = {
195
+ options: Option[];
196
+ value: string | number;
197
+ onChange: (value: string | number) => void;
198
+ vertical?: boolean;
199
+ style?: StyleProp<ViewStyle>;
200
+ thumbStyle?: StyleProp<ViewStyle>;
201
+ textStyle?: StyleProp<TextStyle>;
202
+ activeTextStyle?: StyleProp<TextStyle>;
203
+ inactiveTextStyle?: StyleProp<TextStyle>;
204
+ animationConfig?: WithTimingConfig | WithSpringConfig;
205
+ };
206
+ ```
207
+
208
+ ### Animation Configuration
209
+
210
+ Customize animations via `animationConfig`:
211
+
212
+ ```tsx
213
+ // Using timing animation
214
+ <Switcher
215
+ animationConfig={{
216
+ duration: 200, // Animation duration in milliseconds
217
+ }}
218
+ />
219
+
220
+ // Using spring animation
221
+ <Switcher
222
+ animationConfig={{
223
+ damping: 15, // Damping coefficient
224
+ stiffness: 150, // Stiffness
225
+ mass: 1, // Mass
226
+ }}
227
+ />
228
+ ```
229
+
230
+ ## 📱 Example
231
+
232
+ Check the [example](example/) directory for a complete example project.
233
+
234
+ ```bash
235
+ # Clone the repository
236
+ git clone https://github.com/alansuhe/react-native-directional-toggle.git
237
+
238
+ # Navigate to the example directory
239
+ cd react-native-directional-toggle/example
240
+
241
+ # Install dependencies
242
+ pnpm install
243
+
244
+ # Run on iOS
245
+ pnpm ios
246
+
247
+ # Run on Android
248
+ pnpm android
249
+ ```
250
+
251
+ ## 🤝 Contributing
252
+
253
+ Issues and Pull Requests are welcome!
254
+
255
+ Please read our [Contributing Guide](./CONTRIBUTING.md) for more information.
256
+
257
+ ## 📄 License
258
+
259
+ [MIT](./LICENSE) © [Alan Suhe](https://github.com/alansuhe)
260
+
261
+ ---
262
+
263
+ <p align="center">
264
+ If this project helped you, please give it a ⭐️
265
+ </p>
package/README.md CHANGED
@@ -1,65 +1,265 @@
1
- # 说明
1
+ # react-native-directional-toggle
2
2
 
3
- 这是一个 React Native / Expo 组件库。
3
+ <p align="center">
4
+ <a href="https://www.npmjs.com/package/react-native-directional-toggle">
5
+ <img src="https://img.shields.io/npm/v/react-native-directional-toggle.svg?style=for-the-badge" alt="NPM Version" />
6
+ </a>
7
+ <a href="https://github.com/alansuhe/react-native-directional-toggle/actions/workflows/ci.yml">
8
+ <img src="https://img.shields.io/github/actions/workflow/status/alansuhe/react-native-directional-toggle/ci.yml?branch=main&style=for-the-badge" alt="CI Status" />
9
+ </a>
10
+ <a href="https://github.com/alansuhe/react-native-directional-toggle/blob/main/LICENSE">
11
+ <img src="https://img.shields.io/npm/l/react-native-directional-toggle.svg?style=for-the-badge" alt="License" />
12
+ </a>
13
+ <a href="https://www.npmjs.com/package/react-native-directional-toggle">
14
+ <img src="https://img.shields.io/npm/dm/react-native-directional-toggle.svg?style=for-the-badge" alt="NPM Downloads" />
15
+ </a>
16
+ </p>
4
17
 
5
- For English version, please click: [English](README.en.md)。
18
+ <p align="center">
19
+ <b>多元素切换组件</b><br/>
20
+ 支持 React Native 和 Expo 的多选项切换开关组件,支持横向和纵向布局及流畅动画。
21
+ </p>
6
22
 
7
- ## 安装
23
+ <p align="center">
24
+ <a href="./README.en.md">English</a> | 中文
25
+ </p>
26
+
27
+ ---
28
+
29
+ ## ✨ 特性
30
+
31
+ - 🎯 **多选项支持** - 支持两个或更多选项的切换
32
+ - 📱 **双方向布局** - 支持横向(horizontal)和纵向(vertical)布局
33
+ - 🎨 **高度可定制** - 自定义样式、颜色、文字样式
34
+ - ⚡ **流畅动画** - 基于 Reanimated 的高性能动画
35
+ - 👆 **手势支持** - 支持点击和滑动手势
36
+ - 🔧 **TypeScript** - 完整的 TypeScript 类型支持
37
+ - 📦 **轻量级** - 零依赖(peer dependencies 除外)
38
+ - 🚀 **Expo 支持** - 兼容 Expo 项目
39
+
40
+ ## 📦 安装
41
+
42
+ ### 使用 npm
8
43
 
9
44
  ```bash
10
- pnpm add react-native-directional-toggle
45
+ npm install react-native-directional-toggle
46
+ ```
11
47
 
48
+ ### 使用 yarn
49
+
50
+ ```bash
12
51
  yarn add react-native-directional-toggle
13
52
  ```
14
53
 
15
- ## 依赖包
54
+ ### 使用 pnpm
16
55
 
17
56
  ```bash
18
- pnpm add react-native-gesture-handler react-native-reanimated react-native-worklets --save-peer
57
+ pnpm add react-native-directional-toggle
19
58
  ```
20
59
 
21
- 需要安装的依赖包:
22
- - react-native-gesture-handler
23
- - react-native-reanimated
24
- - react-native-worklets
60
+ ### 安装 peer dependencies
25
61
 
26
- 注意:只能在prebuild后正常用`expo run`的方式运行,不支持expo go。
62
+ 本组件依赖以下 peer dependencies,请确保已安装:
27
63
 
28
- ## 使用
64
+ ```bash
65
+ # npm
66
+ npm install react-native-gesture-handler react-native-reanimated react-native-worklets
67
+
68
+ # yarn
69
+ yarn add react-native-gesture-handler react-native-reanimated react-native-worklets
70
+
71
+ # pnpm
72
+ pnpm add react-native-gesture-handler react-native-reanimated react-native-worklets
73
+ ```
29
74
 
30
- > 参考[示例项目](example/)。
75
+ **注意:**
31
76
 
32
- ### 导入组件使用
77
+ - Expo 项目需要使用 `expo prebuild` 后通过 `expo run` 运行,不支持 Expo Go
78
+ - 需要在应用外层包裹 `GestureHandlerRootView`
79
+
80
+ ## 🚀 使用
81
+
82
+ ### 基础用法
33
83
 
34
84
  ```tsx
35
85
  import { GestureHandlerRootView } from 'react-native-gesture-handler';
36
86
  import Switcher from 'react-native-directional-toggle';
37
87
 
38
88
  const options = [
39
- {
40
- label: 'Option 1',
41
- value: 'Option 1',
42
- },
43
- {
44
- label: 'Option 2',
45
- value: 'Option 2',
46
- },
47
- {
48
- label: 'Option 3',
49
- value: 'Option 3',
50
- },
89
+ { label: 'Option 1', value: 'Option 1' },
90
+ { label: 'Option 2', value: 'Option 2' },
91
+ { label: 'Option 3', value: 'Option 3' },
51
92
  ];
52
93
 
53
- ...
94
+ function App() {
95
+ const [value, setValue] = useState('Option 2');
96
+
97
+ return (
98
+ <GestureHandlerRootView style={{ flex: 1 }}>
99
+ {/* 横向模式:指定宽度和高度 */}
100
+ <Switcher
101
+ options={options}
102
+ value={value}
103
+ onChange={setValue}
104
+ style={{ width: 300, height: 40, backgroundColor: '#f2f2f2' }}
105
+ thumbStyle={{ backgroundColor: '#fff', elevation: 3 }}
106
+ />
107
+ </GestureHandlerRootView>
108
+ );
109
+ }
110
+ ```
54
111
 
55
- <GestureHandlerRootView>
56
- <Switcher
57
- options={options}
58
- value={'Option 2'}
59
- height={36}
60
- onChange={value => console.log(value)}
61
- />
62
- </GestureHandlerRootView>
112
+ ### 纵向模式
63
113
 
114
+ ```tsx
115
+ import { View } from 'react-native';
116
+
117
+ // 纵向模式:自适应父容器高度
118
+ <View style={{ height: 200, width: 60 }}>
119
+ <Switcher
120
+ vertical
121
+ options={options}
122
+ value={value}
123
+ onChange={setValue}
124
+ style={{ flex: 1 }}
125
+ />
126
+ </View>;
64
127
  ```
65
- 注意: App中需要在外层(如_layout.tsx等)包裹GestureHandlerRootView。
128
+
129
+ ### 自定义样式
130
+
131
+ ```tsx
132
+ <Switcher
133
+ options={[
134
+ { label: '日', value: 'day' },
135
+ { label: '周', value: 'week' },
136
+ { label: '月', value: 'month' },
137
+ ]}
138
+ value={value}
139
+ onChange={setValue}
140
+ style={{
141
+ width: 240,
142
+ height: 44,
143
+ backgroundColor: '#e8e8e8',
144
+ borderRadius: 22,
145
+ padding: 3,
146
+ }}
147
+ thumbStyle={{
148
+ backgroundColor: '#fff',
149
+ borderRadius: 20,
150
+ shadowColor: '#000',
151
+ shadowOffset: { width: 0, height: 2 },
152
+ shadowOpacity: 0.1,
153
+ shadowRadius: 4,
154
+ elevation: 3,
155
+ }}
156
+ textStyle={{
157
+ fontSize: 14,
158
+ fontWeight: '600',
159
+ }}
160
+ activeTextStyle={{
161
+ color: '#007AFF',
162
+ }}
163
+ inactiveTextStyle={{
164
+ color: '#666',
165
+ }}
166
+ />
167
+ ```
168
+
169
+ ## 📖 API 文档
170
+
171
+ ### Props
172
+
173
+ | 属性 | 类型 | 必填 | 默认值 | 描述 |
174
+ | ------------------- | -------------------------------------- | ---- | ------------------- | ----------------------------------------- |
175
+ | `options` | `Option[]` | ✅ | - | 选项列表,每个选项包含 `label` 和 `value` |
176
+ | `value` | `string \| number` | ✅ | - | 当前选中的值 |
177
+ | `onChange` | `(value: string \| number) => void` | ✅ | - | 值改变时的回调函数 |
178
+ | `vertical` | `boolean` | ❌ | `false` | 是否为纵向布局 |
179
+ | `style` | `StyleProp<ViewStyle>` | ❌ | - | 容器样式,用于设置宽度、高度、背景色等 |
180
+ | `thumbStyle` | `StyleProp<ViewStyle>` | ❌ | - | 滑块(选中项背景)样式 |
181
+ | `textStyle` | `StyleProp<TextStyle>` | ❌ | - | 选项文字的默认样式 |
182
+ | `activeTextStyle` | `StyleProp<TextStyle>` | ❌ | - | 选中状态的文字样式 |
183
+ | `inactiveTextStyle` | `StyleProp<TextStyle>` | ❌ | - | 未选中状态的文字样式 |
184
+ | `animationConfig` | `WithTimingConfig \| WithSpringConfig` | ❌ | `{ duration: 150 }` | 动画配置 |
185
+
186
+ ### 类型定义
187
+
188
+ ```typescript
189
+ type Option = {
190
+ label: string;
191
+ value: string | number;
192
+ };
193
+
194
+ type AnimatedSwitchProps = {
195
+ options: Option[];
196
+ value: string | number;
197
+ onChange: (value: string | number) => void;
198
+ vertical?: boolean;
199
+ style?: StyleProp<ViewStyle>;
200
+ thumbStyle?: StyleProp<ViewStyle>;
201
+ textStyle?: StyleProp<TextStyle>;
202
+ activeTextStyle?: StyleProp<TextStyle>;
203
+ inactiveTextStyle?: StyleProp<TextStyle>;
204
+ animationConfig?: WithTimingConfig | WithSpringConfig;
205
+ };
206
+ ```
207
+
208
+ ### 动画配置
209
+
210
+ 可以通过 `animationConfig` 自定义动画效果:
211
+
212
+ ```tsx
213
+ // 使用 timing 动画
214
+ <Switcher
215
+ animationConfig={{
216
+ duration: 200, // 动画时长(毫秒)
217
+ }}
218
+ />
219
+
220
+ // 使用 spring 动画
221
+ <Switcher
222
+ animationConfig={{
223
+ damping: 15, // 阻尼系数
224
+ stiffness: 150, // 刚度
225
+ mass: 1, // 质量
226
+ }}
227
+ />
228
+ ```
229
+
230
+ ## 📱 示例
231
+
232
+ 查看 [example](example/) 目录获取完整的示例项目。
233
+
234
+ ```bash
235
+ # 克隆仓库
236
+ git clone https://github.com/alansuhe/react-native-directional-toggle.git
237
+
238
+ # 进入示例目录
239
+ cd react-native-directional-toggle/example
240
+
241
+ # 安装依赖
242
+ pnpm install
243
+
244
+ # 运行 iOS
245
+ pnpm ios
246
+
247
+ # 运行 Android
248
+ pnpm android
249
+ ```
250
+
251
+ ## 🤝 贡献
252
+
253
+ 欢迎提交 Issue 和 Pull Request!
254
+
255
+ 请阅读我们的[贡献指南](./CONTRIBUTING.md)了解详细信息。
256
+
257
+ ## 📄 许可证
258
+
259
+ [MIT](./LICENSE) © [Alan Suhe](https://github.com/alansuhe)
260
+
261
+ ---
262
+
263
+ <p align="center">
264
+ 如果这个项目对您有帮助,请给它一个 ⭐️
265
+ </p>