switch.tsx 900 B

123456789101112131415161718192021222324252627
  1. "use client";
  2. import { useState } from "react";
  3. import type { ComponentRenderProps } from "./types";
  4. import { baseClass, getCustomClass } from "./utils";
  5. export function Switch({ element }: ComponentRenderProps) {
  6. const { props } = element;
  7. const customClass = getCustomClass(props);
  8. const [checked, setChecked] = useState(!!props.checked);
  9. return (
  10. <label
  11. className={`flex items-center justify-between gap-2 text-xs cursor-pointer ${baseClass} ${customClass}`}
  12. onClick={() => setChecked((prev) => !prev)}
  13. >
  14. <span>{props.label as string}</span>
  15. <div
  16. className={`w-8 h-4 rounded-full relative transition-colors ${checked ? "bg-foreground" : "bg-border"}`}
  17. >
  18. <div
  19. className={`absolute w-3 h-3 rounded-full bg-background top-0.5 transition-all ${checked ? "right-0.5" : "left-0.5"}`}
  20. />
  21. </div>
  22. </label>
  23. );
  24. }