@@ -27,45 +27,31 @@ export const Switch = ({
2727 size = 24 ,
2828 skipSystem,
2929 children,
30- ...props
30+ ...tagProps
3131} : SwitchProps ) => {
32- const [ state , setState ] = useStore ( ) ;
32+ const [ { m, s } , setState ] = useStore ( ) ;
33+ const n = modes . length - ( skipSystem ? 1 : 0 ) ;
3334 /** toggle mode */
34- const handleModeSwitch = ( ) => {
35- let index = modes . indexOf ( state . m ) ;
36- const n = modes . length ;
37- if ( skipSystem && index === n - 1 ) index = 0 ;
35+ tagProps . onClick = ( ) =>
3836 setState ( {
39- ... state ,
40- m : modes [ ( index + 1 ) % n ] ,
37+ s ,
38+ m : modes [ ( modes . indexOf ( m ) + 1 ) % n ] ,
4139 } ) ;
42- } ;
43- if ( children ) {
44- return (
45- // @ts -expect-error -- too complex types
46- < Tag
47- suppressHydrationWarning
48- { ...props }
49- data-testid = "switch"
50- // skipcq: JS-0417
51- onClick = { handleModeSwitch } >
52- { /* @ts -expect-error -> we are setting the CSS variable */ }
53- < div className = { styles . switch } style = { { "--size" : `${ size } px` } } />
54- { children }
55- </ Tag >
56- ) ;
40+
41+ const className = styles . switch ;
42+ const style = { "--size" : `${ size } px` } ;
43+
44+ if ( ! children ) {
45+ tagProps . className += " " + className ;
46+ tagProps . style = { ...tagProps . style , ...style } ;
5747 }
48+
5849 return (
59- < Tag
60- // Hydration warning is caused when the data from localStorage differs from the default data provided while rendering on server
61- suppressHydrationWarning
62- { ...props }
63- className = { [ props . className , styles . switch ] . join ( " " ) }
64- // @ts -expect-error -> we are setting the CSS variable
65- style = { { "--size" : `${ size } px` } }
66- data-testid = "switch"
67- // skipcq: JS-0417 -> tradeoff between size and best practices
68- onClick = { handleModeSwitch }
69- />
50+ // @ts -expect-error -- too complex types
51+ < Tag suppressHydrationWarning { ...tagProps } data-testid = "switch" >
52+ { /* @ts -expect-error -> we are setting the CSS variable */ }
53+ { children && < div { ...{ className, style } } /> }
54+ { children }
55+ </ Tag >
7056 ) ;
7157} ;
0 commit comments