Toggle Switch

혜진 조·2022년 5월 30일
0

리액트

목록 보기
6/31
import { useState } from "react";
import "./ToggleSwitch.scss";
export const ToggleSwitch = () => {
  const [switchOn, setSwitchOn] = useState(false);
  return (
    <div id="ToggleSwitch">
      <label className={switchOn ? "switch_button active" : "switch_button"}>
        <input
          type={"checkbox"}
          checked={switchOn}
          onChange={() => setSwitchOn(!switchOn)}
        />
        <span
          className={switchOn ? "on_off_switch active" : "on_off_switch"}
        ></span>
      </label>
    </div>
  );
};
#ToggleSwitch {
  .switch_button {
    position: relative;
    width: 40px;
    height: 22px;
    border-radius: 18px;
    background-color: var(--inline-color-gray15);
    &.active {
      background-color: var(--color-brand-toneup-red);
    }
    & > input {
      opacity: 0;
    }
    span {
      position: absolute;
      display: inline-block;
      width: 18px;
      height: 18px;
      border-radius: 50%;
      background-color: white;
      top: 2px;
      left: 2px;
      transition: all 0.2s ease;
      &.active {
        transform: translateX(18px);
      }
    }
  }
}
profile
나를 믿고 한 걸음 한 걸음 내딛기! 🍏

0개의 댓글