@@ -20,58 +20,40 @@ export default function Toggle({
2020 setIsChecked ( checked )
2121 } , [ checked ] )
2222
23- const toggle = ( ) => {
24- if ( disabled ) return
25-
26- const next = ! isChecked
27- setIsChecked ( next )
28- onChange ( next )
29- }
30-
31- const handleKeyDown = ( event : React . KeyboardEvent < HTMLInputElement > ) => {
32- if ( disabled ) return
33-
34- if ( event . key === 'Enter' ) {
35- event . preventDefault ( )
36- toggle ( )
23+ const handleChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
24+ if ( ! disabled ) {
25+ const newIsChecked = event . target . checked
26+ setIsChecked ( newIsChecked )
27+ onChange ?.( newIsChecked )
3728 }
3829 }
3930
4031 return (
41- < div className = "group inline- flex items-center" >
32+ < div className = "group flex items-center gap-2 " >
4233 < label
4334 className = { clsx (
44- 'relative inline-block h-6 w-12' ,
45- disabled ? 'cursor-not-allowed opacity-50' : 'cursor-pointer ',
35+ 'relative inline-block h-6 w-12 cursor-pointer ' ,
36+ disabled && 'cursor-not-allowed opacity-50' ,
4637 className ,
4738 ) }
4839 >
4940 < input
5041 type = "checkbox"
5142 checked = { isChecked }
52- onChange = { toggle }
53- onKeyDown = { handleKeyDown }
43+ onChange = { handleChange }
5444 disabled = { disabled }
55- className = "peer sr-only "
45+ className = "absolute h-0 w-0 opacity-0 "
5646 { ...properties }
5747 />
58-
59- < span
60- className = { clsx (
61- 'absolute inset-0 rounded-full transition-colors duration-200 ease-in-out' ,
62- isChecked ? 'bg-brand' : 'bg-border' ,
63- 'peer-focus-visible:ring-brand peer-focus-visible:ring-offset-background peer-focus-visible:ring-2 peer-focus-visible:ring-offset-2' ,
64- ) }
65- />
66-
67- < span
68- className = { clsx (
69- 'bg-backdrop absolute top-1/2 h-5 w-5 rounded-full shadow-md' ,
70- 'transition-transform duration-200 ease-in-out' ,
71- 'group-hover:bg-hover' ,
72- isChecked ? 'translate-x-6 -translate-y-1/2' : 'translate-x-1 -translate-y-1/2' ,
73- ) }
74- />
48+ < span className = { clsx ( 'absolute inset-0 rounded-full' , isChecked ? 'bg-brand' : 'bg-border' ) } >
49+ < span
50+ className = { clsx (
51+ 'bg-background group-hover:bg-hover absolute top-[50%] h-5 w-5 -translate-y-1/2 transform rounded-full' ,
52+ 'bg-backdrop group-hover:bg-hover absolute top-[50%] h-5 w-5 -translate-y-1/2 transform rounded-full' ,
53+ isChecked ? 'right-0.5' : 'left-0.5' ,
54+ ) }
55+ />
56+ </ span >
7557 </ label >
7658 </ div >
7759 )
0 commit comments