xonecas.io
53129
Pa

PAT / files

PAT / src / components / Switch.tsx

1.3 KB
1"use client";
2
3import { forwardRef } from "react";
4
5type SwitchProps = {
6  checked: boolean;
7  onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
8  disabled?: boolean;
9  id?: string;
10  name?: string;
11  className?: string;
12  "aria-label"?: string;
13};
14
15export const Switch = forwardRef<HTMLInputElement, SwitchProps>(function Switch(
16  { checked, onChange, disabled = false, id, name, className = "", "aria-label": ariaLabel },
17  ref
18) {
19  return (
20    <label
21      className={`relative inline-flex h-6 w-11 cursor-pointer items-center ${disabled ? "pointer-events-none opacity-50" : ""} ${className}`}
22      htmlFor={id}
23    >
24      <input
25        type="checkbox"
26        className="peer sr-only"
27        checked={checked}
28        onChange={onChange}
29        disabled={disabled}
30        id={id}
31        name={name}
32        ref={ref}
33        aria-label={ariaLabel}
34      />
35      <div
36        className="h-6 w-11 rounded-full bg-neutral-300 transition-colors duration-200 peer-checked:bg-neutral-600
37          dark:bg-neutral-700 dark:peer-checked:bg-neutral-500"
38      />
39      <div
40        className="absolute left-0.5 top-0.5 h-5 w-5 rounded-full bg-white shadow
41          transition-all duration-200 peer-checked:translate-x-5
42          dark:bg-neutral-900"
43      />
44    </label>
45  );
46});