All files / Rindu/components/FormToggle FormToggle.tsx

11.11% Statements 1/9
0% Branches 0/8
0% Functions 0/5
11.11% Lines 1/9

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          19x                                                                                                                                                                                                                                                                                                      
import {
  DetailedHTMLProps,
  InputHTMLAttributes,
  ReactElement,
  useState,
} from "react";
 
export default function FormToggle(
  props: DetailedHTMLProps<
    InputHTMLAttributes<HTMLInputElement>,
    HTMLInputElement
  > & { popupText?: string }
): ReactElement {
  const [isPopupOpen, setIsPopupOpen] = useState(false);
  const { popupText = "", ...restProps } = props;
 
  return (
    <div className="form-toggle">
      <input
        type="checkbox"
        className="toggle-input"
        id={props.id}
        {...restProps}
      />
      {/* eslint-disable-next-line  jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/label-has-associated-control*/}
      <label
        htmlFor={props.id}
        className="toggle-text"
        onMouseEnter={() => {
          Iif (props.disabled) {
            setIsPopupOpen(true);
          }
        }}
        onMouseLeave={() => {
          setIsPopupOpen(false);
        }}
      >
        <span></span>
      </label>
      {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */}
      <div
        className={`helpText ${isPopupOpen ? "visible" : ""}`}
        onMouseEnter={() => {
          setIsPopupOpen(true);
        }}
        onMouseLeave={() => {
          setIsPopupOpen(false);
        }}
      >
        <div>{popupText}</div>
      </div>
      <style jsx>{`
        .form-toggle {
          display: inline-block;
          position: relative;
          width: 50px;
          height: 24px;
        }
        .toggle-input {
          position: absolute;
          opacity: 0;
          width: 0;
          height: 0;
        }
        .toggle-text {
          display: block;
          position: absolute;
          width: 100%;
          height: 100%;
          background-color: #666;
          border-radius: 50px;
          transition: background-color 0.2s ease;
        }
        .toggle-input:hover + .toggle-text,
        .toggle-text:hover {
          background-color: #9f9f9f;
        }
        .toggle-text::before {
          content: "";
          display: block;
          width: 20px;
          height: 20px;
          background-color: #fff;
          border-radius: 50%;
          position: absolute;
          top: 2px;
          left: 2px;
          transition: transform 0.2s ease;
        }
        .toggle-input:checked + .toggle-text {
          background-color: #1ed760;
        }
        .toggle-input:checked:focus:not(:hover) + .toggle-text {
          background-color: #169c46;
        }
        .toggle-input:focus:not(:hover) + .toggle-text {
          background-color: #9f9f9f;
        }
        .toggle-input:checked:hover + .toggle-text,
        .toggle-input:checked:focus:active + .toggle-text {
          background-color: #1fdf64;
        }
        .toggle-input:disabled + .toggle-text {
          background: #3a3a3a;
        }
        .toggle-input:disabled + .toggle-text:before {
          background: #999999;
        }
        .toggle-input:active:checked + .toggle-text,
        .toggle-input:checked:active + .toggle-text {
          background-color: #169c46;
        }
        .toggle-input:checked + .toggle-text::before {
          transform: translateX(26px);
        }
        .toggle-input:checked:focus + .toggle-text::before {
          outline: none;
        }
        .helpText {
          position: absolute;
          top: -4rem;
          transform: translateX(-50%);
          left: 50%;
          visibility: hidden;
          width: 240px;
          z-index: 2;
        }
        .helpText div {
          background: #282828;
          box-shadow: 0 4px 4px #00000040;
          font-size: 12px;
          color: #f0f0f0;
          box-sizing: border-box;
          border-radius: 8px;
          text-align: start;
          cursor: default;
          display: inline-block;
          max-inline-size: 240px;
          position: relative;
          font-weight: 400;
          padding: 8px 12px;
          text-transform: initial;
          overflow-wrap: break-word;
          line-height: 1.5;
        }
        .visible {
          visibility: visible;
        }
      `}</style>
    </div>
  );
}