npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mshafiqyajid/react-switch

v0.3.0

Published

Headless toggle switch hook and styled component for React. Accessible, keyboard-friendly, animated, fully typed.

Readme

@mshafiqyajid/react-switch

Headless toggle switch hook and styled component for React. Accessible, keyboard-friendly, animated, fully typed.

Full docs →

Install

npm install @mshafiqyajid/react-switch

Headless usage

import { useSwitch } from "@mshafiqyajid/react-switch";

function MySwitch() {
  const { switchProps, isChecked } = useSwitch({ defaultChecked: false });
  return <button {...switchProps}>{isChecked ? "On" : "Off"}</button>;
}

Styled usage

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

function App() {
  return (
    <SwitchStyled
      label="Enable notifications"
      defaultChecked
      tone="primary"
      size="md"
    />
  );
}

Async toggle

Return a Promise from onChange to drive the pending state automatically. The switch shows a spinner during the promise, blocks further clicks, and reverts the optimistic value on rejection.

<SwitchStyled
  label="Email notifications"
  defaultChecked
  onChange={async (next) => {
    await fetch("/api/prefs/email", {
      method: "POST",
      body: JSON.stringify({ enabled: next }),
    });
  }}
/>

The hook also exposes isPending for headless consumers. The rendered switch lands aria-busy="true" + data-pending="true" while the promise is in flight.

What's new in 0.3.0

confirm guard

Pass confirm to intercept the toggle before it is committed. If it returns false or a Promise that resolves to false (or rejects), the switch reverts. While the Promise is pending, the track crossfades to a neutral grey and aria-busy="true" is set.

<SwitchStyled
  label="Delete account"
  confirm={async (next) => {
    const ok = await showConfirmDialog(`Turn ${next ? "on" : "off"}?`);
    return ok;
  }}
  onChange={async (next) => { /* only called if confirm resolves true */ }}
/>

Track labels — onLabel / offLabel

Render text or icons inside the track halves. Opacity-transition hides the label behind the thumb's shadow zone and reveals it in the opposite half.

<SwitchStyled onLabel="ON" offLabel="OFF" />
<SwitchStyled onLabel={<CheckIcon />} offLabel={<XIcon />} />

CSS classes: rsw-track-label rsw-track-label--on / rsw-track-label--off.

Thumb icon slots — thumbIconOn / thumbIconOff

Render an icon inside the thumb circle that cross-fades (180 ms ease) between on and off states.

<SwitchStyled thumbIconOn={<SunIcon />} thumbIconOff={<MoonIcon />} />

CSS classes: rsw-thumb-icon, rsw-thumb-icon__on, rsw-thumb-icon__off.

Both spinner and thumb icons respect prefers-reduced-motion.

Props

| Prop | Type | Default | Description | |------|------|---------|-------------| | checked | boolean | — | Controlled checked state | | defaultChecked | boolean | false | Uncontrolled initial state | | onChange | (v: boolean) => void \| Promise<void> | — | Called on toggle. Return a Promise to drive a pending state automatically; reverts on rejection. | | confirm | (next: boolean) => boolean \| Promise<boolean> | — | Guard called before committing. Return/resolve false to cancel. A Promise triggers pending state while awaiting. | | size | "sm" \| "md" \| "lg" | "md" | Size | | tone | "neutral" \| "primary" \| "success" \| "danger" | "primary" | Color when on | | label | ReactNode | — | Label text | | labelPosition | "left" \| "right" | "right" | Label side | | loading | boolean | false | Force spinner in thumb. Auto-set when onChange returns a Promise. | | disabled | boolean | false | Disable the switch | | onLabel | ReactNode | — | Content rendered inside track on the "on" side | | offLabel | ReactNode | — | Content rendered inside track on the "off" side | | thumbIconOn | ReactNode | — | Icon inside thumb when on; cross-fades with thumbIconOff | | thumbIconOff | ReactNode | — | Icon inside thumb when off; cross-fades with thumbIconOn |

License

MIT