이번 토글 구현은
🥲 CSS가 복병이었다!
position을 이해했다고 생각했는데, 완전 반대로 알고있었다는...
🥰 이번에 처음으로 transition도 써봤당 ㅋ.ㅋ 뿌-듯
import { useState } from "react";
import styled from "styled-components";
const toggleHandler = () => {
setisOn(!isOn);
};
<ToggleContainer onClick = {toggleHandler}>
<div
className=
{isOn ? "toggle-container toggle--checked" :toggle-container"} />
<div
className=
{isOn ? "toggle-circle circle--checked" : "toggle-circle"} />
> .toggle-container {
width: 60px;
height: 25px;
border-radius: 30px;
background-color: lightgray;
transition: all 1s;
&.toggle--checked {
background-color: skyblue;
}
}
> .toggle-circle {
position: absolute;
top: 1px;
left: 1px;
width: 22px;
height: 22px;
border-radius: 50%;
background-color: white;
transition: all 1s;
&.circle--checked {
left: 36px;
}
}
import { useState } from "react";
import styled from "styled-components";
const ToggleContainer = styled.div`
position: relative;
left: 47%;
margin-top: 8rem;
> .toggle-container {
width: 60px;
height: 25px;
border-radius: 30px;
background-color: lightgray;
transition: all 1s;
&.toggle--checked {
background-color: skyblue;
}
}
> .toggle-circle {
position: absolute;
top: 1px;
left: 1px;
width: 22px;
height: 22px;
border-radius: 50%;
background-color: white;
transition: all 1s;
&.circle--checked {
left: 36px;
}
}
`;
export const Toggle = () => {
const [isOn, setisOn] = useState(false);
const toggleHandler = () => {
setisOn(!isOn);
};
return (
<>
<ToggleContainer onClick={toggleHandler}>
<div className=
{ isOn ? "toggle-container toggle--checked" : "toggle-container"
}/>
<div className=
{ isOn ? "toggle-circle circle--checked" : "toggle-circle"}
/>
</ToggleContainer>
</>
);
};