본문 바로가기

A.개발관련자료

[0]react toggle 버튼 만들기

import React, { useState } from "react";
const ToggleBtn = () => {
  const [isOn, setisOn] = useState(false);

  const toggleHandler = () => {
    setisOn(!isOn);
  };

  return (
    
    <>
      <div className="toggle" onClick={toggleHandler}>
        <div className={`toggle-container ${isOn ? "toggle--checked" : ""}`} />
        <div className={`toggle-circle ${isOn ? "toggle--checked" : ""}`} />
      </div>
    </>
  );
};

export default ToggleBtn;
.toggle {
  position: relative;
  cursor: pointer;
}

.toggle .toggle-container{
  width: 44px;
  height: 24px;
  border-radius: 30px;
  background-color: #d4d3d3;
  transition: all 0.2s ease;
}

.toggle .toggle-container.toggle--checked{
  background-color: #61BE82;
}

.toggle .toggle-circle{
  position: absolute;
  top: 4px;
  left: 4px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background-color: #fafafa;
  transition: all 0.25s ease;
}

.toggle .toggle-circle.toggle--checked{
  left: 24px;
}