All files / Switch Switch.tsx

92.85% Statements 52/56
82.75% Branches 24/29
81.25% Functions 13/16
93.87% Lines 46/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 1582x   2x 2x 2x 2x   2x     8x 8x 8x 8x 8x 8x 8x 8x 8x 8x   8x 8x 8x       8x 8x 8x   8x 8x     8x 8x   8x             8x                                                                       2x   2x 64x   8x                   48x                   8x                     2x           8x               2x     8x 8x   8x         8x   8x 8x   8x         8x     8x    
import React, { forwardRef, useState, useEffect, useImperativeHandle } from "react";
import { SwitchProps, SwitchStyleProps } from "./Switch.types";
import { useTheme } from "../ThemeProvider/ThemeProvider";
import styled from "styled-components";
import colorTokens from "../../tokens/colors.json";
import { getColorPalette } from "../../helpers/helpers";
 
const Switch = forwardRef(
  (
    {
      id,
      value,
      size = "medium",
      color = colorTokens.default.primary.main,
      checked = false,
      disabled = false,
      label,
      name,
      className,
      style,
      // Events
      onChange,
      "data-testid": dataTestId,
      ...props
    }: SwitchProps,
    ref
  ) => {
    const innerRef = React.useRef<HTMLInputElement>(null);
    useImperativeHandle(ref, () => innerRef.current as HTMLInputElement);
    const [isChecked, setIsChecked] = useState(checked ?? false);
 
    useEffect(() => {
      setIsChecked(checked ?? false);
    }, [checked]);
 
    const theme = useTheme().theme;
    const colorPalette = getColorPalette(theme, color);
 
    const _onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
      setIsChecked((prev) => {
        return !prev;
      });
      onChange && onChange(event);
    };
 
    return (
      <StyledSwitch
        role="switch"
        aria-checked={isChecked}
        className={className}
        $checked={isChecked}
        $disabled={disabled}
        $size={size}
        $color={color}
        $colorGradient={colorPalette}
        style={style}
        data-testid={dataTestId}
        {...props}
      >
        <input
          ref={innerRef}
          type="checkbox"
          checked={isChecked}
          name={name}
          onChange={_onChange}
        />
        <StyledSwitchContent
          $checked={isChecked}
          $disabled={disabled}
          $size={size}
          $color={color}
          $colorGradient={colorPalette}
        >
          <span
            className={`${isChecked ? "ste-switch-content-selected" : "ste-switch-content-unselected"}`}
          ></span>
        </StyledSwitchContent>
      </StyledSwitch>
    );
  }
);
export default Switch;
 
const getSize = (size: "small" | "medium" | "large") => {
  switch (size) {
    case "small":
      return {
        height: "0.6rem",
        width: "1rem",
        contentSize: "0.4rem",
        top: "0.1rem",
        unselectedLeft: "0.1rem",
        selectedLeft: "0.5rem",
        padding: "0.2rem",
      };
    case "medium":
      return {
        height: "0.8rem",
        width: "1.4rem",
        contentSize: "0.6rem",
        top: "0.1rem",
        unselectedLeft: "0.1rem",
        selectedLeft: "0.7rem",
        padding: "0.3rem",
      };
    case "large":
      return {
        height: "1rem",
        width: "1.8rem",
        contentSize: "0.8rem",
        top: "0.1rem",
        unselectedLeft: "0.1rem",
        selectedLeft: "0.9rem",
        padding: "0.5rem",
      };
  }
};
const StyledSwitch = styled.span<SwitchStyleProps>`
  display: flex;
  align-items: center;
  justify-content: center;
  width: fit-content;
  cursor: pointer;
  padding: ${(props) => getSize(props.$size).padding};
  border-radius: 1rem;
  & input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    cursor: pointer;
  }
`;
const StyledSwitchContent = styled.span<SwitchStyleProps>`
  position: relative;
  border-radius: 1rem;
  width: ${(props) => getSize(props.$size).width};
  height: ${(props) => getSize(props.$size).height};
  background-color: ${(props) =>
    props.$checked
      ? props.$colorGradient[props.$color].accentScale[8]
      : props.$colorGradient[props.$color].grayScale[8]};
 
  & span {
    top: ${(props) => getSize(props.$size).top};
    position: absolute;
    width: ${(props) => getSize(props.$size).contentSize};
    height: ${(props) => getSize(props.$size).contentSize};
    border-radius: 50%;
    background-color: ${(props) => "#ffffff"};
    transition: left 0.2s ease-in-out;
  }
 
  & .ste-switch-content-unselected {
    left: ${(props) => getSize(props.$size).unselectedLeft};
  }
  & .ste-switch-content-selected {
    left: ${(props) => getSize(props.$size).selectedLeft};
  }
`;