Skip to content

Commit 3fc70ff

Browse files
committed
add circleChildrenActive & circleChildrenInActive
1 parent 6500d61 commit 3fc70ff

10 files changed

Lines changed: 139 additions & 42 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ const styles = StyleSheet.create({
8181
| circleInActiveColor | string | #fff |
8282
| textStyle | TextStyle | undefined |
8383
| switchStyle | ViewStyle | undefined |
84+
| circleChildrenActive| ReactElement | undefined |
85+
| circleChildrenInActive| ReactElement | undefined |
8486

8587
## Contributing
8688

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ PODS:
365365
- React-jsi (= 0.68.2)
366366
- React-logger (= 0.68.2)
367367
- React-perflogger (= 0.68.2)
368-
- RNReanimated (2.10.0):
368+
- RNReanimated (2.17.0):
369369
- DoubleConversion
370370
- FBLazyVector
371371
- FBReactNativeSpec
@@ -591,7 +591,7 @@ SPEC CHECKSUMS:
591591
React-RCTVibration: 79040b92bfa9c3c2d2cb4f57e981164ec7ab9374
592592
React-runtimeexecutor: b960b687d2dfef0d3761fbb187e01812ebab8b23
593593
ReactCommon: 095366164a276d91ea704ce53cb03825c487a3f2
594-
RNReanimated: 5bdcbcc3a72aedeee7bb099604262403aa75a1e5
594+
RNReanimated: 1f117be601c59177816351a965bc57320b766c15
595595
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
596596
Yoga: 99652481fcd320aefa4a7ef90095b95acd181952
597597
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
"dependencies": {
1414
"react": "17.0.2",
1515
"react-native": "0.68.2",
16-
"react-native-reanimated": "^2.13.0"
16+
"react-native-reanimated": "^2.17.0"
1717
},
1818
"devDependencies": {
1919
"@babel/core": "^7.12.10",
2020
"@babel/runtime": "^7.12.5",
2121
"babel-plugin-module-resolver": "^4.1.0",
2222
"metro-react-native-babel-preset": "^0.67.0",
23-
"patch-package": "^6.5.0",
23+
"patch-package": "^6.5.1",
2424
"postinstall-postinstall": "^2.1.0"
2525
}
2626
}

example/src/App.tsx

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import * as React from 'react';
2-
import { StyleSheet, View } from 'react-native';
2+
import { Image, StyleSheet, View } from 'react-native';
33
import Switch from '@meksiabdou/react-native-switch';
44

55
export default function App() {
6-
const [value, setValue] = React.useState({ 0: true, 1: false, 2: false });
6+
const [value, setValue] = React.useState({
7+
0: true,
8+
1: false,
9+
2: false,
10+
3: true,
11+
});
712

813
return (
914
<View style={styles.container}>
@@ -14,8 +19,8 @@ export default function App() {
1419
onValueChange={(_value) => setValue({ ...value, 0: _value })}
1520
activeText="حلال"
1621
inActiveText="حرام"
17-
backgroundInActive={"#ff0000"}
18-
circleInActiveColor={"#fff"}
22+
backgroundInActive={'#ff0000'}
23+
circleInActiveColor={'#fff'}
1924
circleSize={25}
2025
/>
2126
</View>
@@ -29,10 +34,11 @@ export default function App() {
2934
switchBorderRadius={30}
3035
switchPaddingLeft={2}
3136
switchPaddingRight={2}
32-
switchStyle={{paddingVertical: 4}}
37+
circleInActiveColor='#ff5454'
38+
switchStyle={{ paddingVertical: 4 }}
3339
/>
3440
</View>
35-
<View>
41+
<View style={{ marginVertical: 5 }}>
3642
<Switch
3743
value={value['2']}
3844
width={70}
@@ -44,6 +50,34 @@ export default function App() {
4450
disabled={false}
4551
/>
4652
</View>
53+
<View style={{ marginVertical: 5 }}>
54+
<Switch
55+
value={value['3']}
56+
width={80}
57+
onValueChange={(_value) => setValue({ ...value, 3: _value })}
58+
activeText=""
59+
inActiveText=""
60+
circleSize={30}
61+
textStyle={{ fontSize: 13 }}
62+
disabled={false}
63+
circleChildrenInActive={
64+
<View>
65+
<Image
66+
source={require('./assets/images/dark.png')}
67+
style={styles.icon}
68+
/>
69+
</View>
70+
}
71+
circleChildrenActive={
72+
<View>
73+
<Image
74+
source={require('./assets/images/light.png')}
75+
style={styles.icon}
76+
/>
77+
</View>
78+
}
79+
/>
80+
</View>
4781
</View>
4882
);
4983
}
@@ -53,6 +87,11 @@ const styles = StyleSheet.create({
5387
flex: 1,
5488
alignItems: 'center',
5589
justifyContent: 'center',
56-
backgroundColor: "#ccc",
90+
backgroundColor: '#ccc',
91+
},
92+
icon: {
93+
width: 28,
94+
height: 28,
95+
resizeMode: 'center',
5796
},
5897
});

example/src/assets/images/dark.png

909 Bytes
Loading
1 KB
Loading

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@meksiabdou/react-native-switch",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Customisable switch component for React Native",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",
@@ -49,24 +49,24 @@
4949
"@arkweid/lefthook": "^0.7.7",
5050
"@commitlint/config-conventional": "^17.0.2",
5151
"@react-native-community/eslint-config": "^3.0.2",
52-
"@release-it/conventional-changelog": "^5.0.0",
52+
"@release-it/conventional-changelog": "^5.1.1",
5353
"@types/jest": "^28.1.8",
54-
"@types/react": "~18.0.21",
54+
"@types/react": "~18.0.38",
5555
"@types/react-native": "0.70.7",
5656
"commitlint": "^17.0.2",
5757
"del-cli": "^5.0.0",
5858
"eslint": "^8.4.1",
5959
"eslint-config-prettier": "^8.5.0",
60-
"eslint-plugin-prettier": "^4.0.0",
61-
"jest": "^28.1.1",
60+
"eslint-plugin-prettier": "^4.2.1",
61+
"jest": "^28.1.3",
6262
"pod-install": "^0.1.0",
6363
"prettier": "^2.0.5",
6464
"react": "18.1.0",
6565
"react-native": "0.70.6",
66-
"react-native-builder-bob": "^0.20.1",
67-
"react-native-reanimated": "^2.13.0",
68-
"release-it": "^15.0.0",
69-
"typescript": "^4.9.3"
66+
"react-native-builder-bob": "^0.20.4",
67+
"react-native-reanimated": "^2.17.0",
68+
"release-it": "^15.11.0",
69+
"typescript": "^5.1.6"
7070
},
7171
"resolutions": {
7272
"@types/react": "18.0.21"

src/components/switch.tsx

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
4343
switchPaddingRight,
4444
switchPaddingLeft,
4545
switchStyle,
46+
circleChildrenActive,
47+
circleChildrenInActive,
4648
} = IProps;
4749

4850
const { isRTL } = I18nManager;
4951
const circleTranslateX = useSharedValue<any>(0);
5052
const textTranslateXInActive = useSharedValue<any>(0);
5153
const textTranslateXActive = useSharedValue<any>(0);
5254
const opacity = useSharedValue<number>(1);
55+
const circleChildrenActiveOpacity = useSharedValue<number>(1);
56+
const circleChildrenInActiveOpacity = useSharedValue<number>(0);
5357
const circleColor = useSharedValue<string | undefined>(circleInActiveColor);
5458

5559
const [defaultWidth, setDefaultWidth] = useState<number>(
@@ -103,6 +107,18 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
103107
};
104108
});
105109

110+
const circleChildrenInActiveStyle = useAnimatedStyle(() => {
111+
return {
112+
opacity: circleChildrenInActiveOpacity.value,
113+
};
114+
});
115+
116+
const circleChildrenActiveStyle = useAnimatedStyle(() => {
117+
return {
118+
opacity: circleChildrenActiveOpacity.value,
119+
};
120+
});
121+
106122
useEffect(() => {
107123
setDefaultWidth(isNumbre(width, 100));
108124
}, [width]);
@@ -135,6 +151,8 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
135151
stiffness: 100,
136152
});
137153
}
154+
circleChildrenActiveOpacity.value = spring(1);
155+
circleChildrenInActiveOpacity.value = spring(0);
138156
} else {
139157
circleTranslateX.value = spring(0, { damping: 15, stiffness: 120 });
140158
textTranslateXActive.value = spring(-(defaultWidth * factory));
@@ -145,6 +163,8 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
145163
stiffness: 100,
146164
});
147165
}
166+
circleChildrenActiveOpacity.value = spring(0);
167+
circleChildrenInActiveOpacity.value = spring(1);
148168
}
149169
}, [value, defaultWidth, defaultCircleSize, defaultPadding, isRTL]);
150170

@@ -184,15 +204,9 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
184204
>
185205
<Reanimated.View
186206
style={[
207+
styles.switchTextView,
208+
styles.center,
187209
{
188-
position: 'absolute',
189-
display: 'flex',
190-
right: 0,
191-
left: 0,
192-
top: 0,
193-
bottom: 0,
194-
justifyContent: 'center',
195-
alignItems: 'center',
196210
width:
197211
defaultWidth +
198212
(defaultPadding.paddingLeft + defaultPadding.paddingRight) / 2,
@@ -213,15 +227,9 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
213227
</Reanimated.View>
214228
<Reanimated.View
215229
style={[
230+
styles.switchTextView,
231+
styles.center,
216232
{
217-
position: 'absolute',
218-
display: 'flex',
219-
left: 0,
220-
right: 0,
221-
top: 0,
222-
bottom: 0,
223-
justifyContent: 'center',
224-
alignItems: 'center',
225233
width: defaultWidth,
226234
backgroundColor: backgroundInActive,
227235
},
@@ -240,16 +248,34 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
240248
</Reanimated.View>
241249
<Reanimated.View
242250
style={[
251+
styles.circleStyle,
243252
{
244-
position: 'relative',
245-
zIndex: 99,
246253
width: defaultCircleSize,
247254
height: defaultCircleSize,
248255
borderRadius: isNumbre(switchBorderRadius, 30),
249256
},
250257
circleStyle,
251258
]}
252-
/>
259+
>
260+
<Reanimated.View
261+
style={[
262+
styles.circleChildren,
263+
styles.center,
264+
circleChildrenActiveStyle,
265+
]}
266+
>
267+
{circleChildrenActive}
268+
</Reanimated.View>
269+
<Reanimated.View
270+
style={[
271+
styles.circleChildren,
272+
styles.center,
273+
circleChildrenInActiveStyle,
274+
]}
275+
>
276+
{circleChildrenInActive}
277+
</Reanimated.View>
278+
</Reanimated.View>
253279
</Reanimated.View>
254280
</Button>
255281
);
@@ -283,11 +309,34 @@ const styles = StyleSheet.create({
283309
backgroundColor: '#fff',
284310
overflow: 'hidden',
285311
},
312+
switchTextView: {
313+
position: 'absolute',
314+
left: 0,
315+
right: 0,
316+
top: 0,
317+
bottom: 0,
318+
},
286319
textStyle: {
287320
fontSize: 14,
288321
color: '#fff',
289322
marginHorizontal: 2,
290323
},
324+
circleStyle: {
325+
position: 'relative',
326+
zIndex: 99,
327+
},
328+
center: {
329+
display: 'flex',
330+
justifyContent: 'center',
331+
alignItems: 'center',
332+
},
333+
circleChildren: {
334+
position: 'absolute',
335+
left: 0,
336+
right: 0,
337+
top: 0,
338+
bottom: 0,
339+
},
291340
});
292341

293342
export default Switch;

src/types/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ReactElement } from 'react';
12
import type { TextStyle, ViewStyle } from 'react-native';
23

34
export interface SwitchProps {
@@ -17,4 +18,6 @@ export interface SwitchProps {
1718
switchStyle?: ViewStyle;
1819
switchPaddingRight?: number;
1920
switchPaddingLeft?: number;
21+
circleChildrenActive?: ReactElement;
22+
circleChildrenInActive?: ReactElement;
2023
}

tsconfig.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
"compilerOptions": {
33
"baseUrl": "./",
44
"paths": {
5-
"@meksiabdou/react-native-switch": ["./src/index"]
5+
"@meksiabdou/react-native-switch": [
6+
"./src/index"
7+
]
68
},
79
"allowUnreachableCode": false,
810
"allowUnusedLabels": false,
911
"esModuleInterop": true,
10-
"importsNotUsedAsValues": "error",
12+
// "importsNotUsedAsValues": "error",
1113
"forceConsistentCasingInFileNames": true,
1214
"jsx": "react",
13-
"lib": ["esnext"],
15+
"lib": [
16+
"esnext"
17+
],
1418
"module": "esnext",
1519
"moduleResolution": "node",
1620
"noFallthroughCasesInSwitch": true,

0 commit comments

Comments
 (0)