@@ -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
293342export default Switch ;
0 commit comments