11import "./styles.scss" ;
22import Ref from "html-tag-js/ref" ;
3+ import { animate } from "motion" ;
34
45/**
56 * @typedef {Object } Checkbox
@@ -23,35 +24,81 @@ import Ref from "html-tag-js/ref";
2324 * @param {string } [size] Size of checkbox
2425 * @returns {Checkbox & HTMLLabelElement }
2526 */
26- function Checkbox ( text , checked , name , id , type , ref , size ) {
27+ function Checkbox ( text , checked , name , id , type , ref , size , isSwitch ) {
2728 if ( typeof text === "object" ) {
28- ( { text, checked, name, id, type, ref, size } = text ) ;
29+ ( { text, checked, name, id, type, ref, size, isSwitch } = text ) ;
2930 }
3031
3132 size = size || "1rem" ;
3233
3334 const $input = ref || Ref ( ) ;
35+ const $handle = Ref ( ) ;
3436 const $checkbox = (
35- < label className = " input-checkbox" >
37+ < label className = { ` input-checkbox ${ isSwitch ? "switch" : "" } ` } >
3638 < input
3739 ref = { $input }
3840 checked = { checked }
3941 type = { type || "checkbox" }
4042 name = { name }
4143 id = { id }
44+ onchange = { handleChange }
4245 />
43- < span style = { { height : size , width : size } } className = "box" > </ span >
46+ < span style = { { height : size , width : size } } className = "box" >
47+ < span ref = { $handle } className = "handle" > </ span >
48+ </ span >
4449 < span > { text } </ span >
4550 </ label >
4651 ) ;
4752
53+ function updateToggle ( animateToggle = true ) {
54+ const isSwitch =
55+ $checkbox . classList . contains ( "switch" ) ||
56+ $checkbox . closest (
57+ ".detail-settings-list, .main-settings-list, .settings-search-section" ,
58+ ) !== null ;
59+
60+ if ( isSwitch && $handle . el ) {
61+ const isChecked = ! ! $input . el . checked ;
62+ const targetTransform = isChecked
63+ ? "translate3d(1.12rem, 0, 0)"
64+ : "translate3d(0, 0, 0)" ;
65+
66+ if ( animateToggle && ! document . body . classList . contains ( "no-animation" ) ) {
67+ animate (
68+ $handle . el ,
69+ {
70+ transform : targetTransform ,
71+ } ,
72+ {
73+ type : "spring" ,
74+ stiffness : 500 ,
75+ damping : 28 ,
76+ } ,
77+ ) . then ( ( ) => {
78+ $handle . el . style . transform = targetTransform ;
79+ } ) ;
80+ } else {
81+ $handle . el . style . transform = targetTransform ;
82+ }
83+ }
84+ }
85+
86+ function handleChange ( ) {
87+ updateToggle ( true ) ;
88+ }
89+
90+ requestAnimationFrame ( ( ) => {
91+ updateToggle ( false ) ;
92+ } ) ;
93+
4894 Object . defineProperties ( $checkbox , {
4995 checked : {
5096 get ( ) {
5197 return ! ! $input . el . checked ;
5298 } ,
5399 set ( value ) {
54100 $input . el . checked = value ;
101+ updateToggle ( true ) ;
55102 } ,
56103 } ,
57104 onclick : {
0 commit comments