Switch A
Switch that have two states: on and off.
Preview
Color: default
Color: green
Color: ios
Color: m3
Code
import React from "react"; import styles from "./switch-a.module.css"; type Props = { className?: string; children?: React.ReactNode; rounded?: "none" | "small" | "medium" | "large" | "full"; initialChecked?: boolean; // Put additional variants here, then define them in the CSS variant?: "default" | "m3" | "blue" | "green" | "ios"; // Optional event handler onToggle?: (checked: boolean) => void; }; export default function SwitchA({ className, children, initialChecked = false, rounded = "full", variant = "default", onToggle, }: Props) { const [checked, setChecked] = React.useState(initialChecked || false); return ( <label className={[ styles["wrapper"], styles[`r-${rounded}`], styles[`variant-${variant}`], className, ].join(" ")} > {children && <div className={styles["before-element"]}>{children}</div>} <div className={styles["input-checkbox-wrapper"]}> <input className={styles["input-checkbox"]} type="checkbox" checked={checked} onChange={(e) => { setChecked(!checked); onToggle && onToggle(!checked); }} /> <span className={styles["slider"]}></span> </div> </label> ); }
Design
Figma design file:
Documentation
Properties
Props of the component:
-
className
(string): Specifies the CSS class of the component. -
children
(ReactNode): React children of the component. Specifies the ReactNode placed as label of the component. -
rounded
("none" | "small" | "medium" | "large" | "full"): Specifies the border radius of the frame. -
initialChecked
(boolean): Specifies if it is initially checked. -
variant
("default" | "m3" | "blue" | "green" | "ios" or a customized value): Specifies the color or theme variant of the component. See the "Sample CSS customization" below for an example of usage. -
onToggle
(function (checked: boolean) => void ): Fires when the component checked status change.
Sample CSS customization
.variant-ios { --width: 3.25rem; --height: 2rem; --switch-width: 1.75rem; --on-bg-color: #00d85b; }