react ~5 KB 0 deps v0.1.0 ↗ GitHub ↗

@mshafiqyajid/react-switch

Headless toggle switch hook and styled component. Controlled and uncontrolled, sizes, tones, label positioning, loading spinner.

Playground #

Enable notifications
Props
TSX
import { SwitchStyled } from "@mshafiqyajid/react-switch/styled";
import "@mshafiqyajid/react-switch/styles.css";

<SwitchStyled
  checked={checked}
  onChange={setChecked}
  label="Enable notifications"
/>

Install #

npm install @mshafiqyajid/react-switch

Quick start #

import { SwitchStyled } from "@mshafiqyajid/react-switch/styled";
import "@mshafiqyajid/react-switch/styles.css";

const [on, setOn] = useState(false);
<SwitchStyled checked={on} onChange={setOn} label="Enable notifications" />

API #

PropTypeDefaultDescription
checkedbooleanControlled checked state
defaultCheckedbooleanfalseUncontrolled initial state
onChange(v: boolean) => voidCalled on toggle
size"sm" | "md" | "lg""md"Size
tone"neutral" | "primary" | "success" | "danger""primary"Color when on
labelReactNodeLabel text
labelPosition"left" | "right""right"Label side
loadingbooleanfalseShow spinner in thumb
disabledbooleanfalseDisable the switch
Edit this page on GitHub