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

@hbi-developer/react-switch-component

v1.1.0

Published

A Simple and Native Switch Component for React. Work like javascript switch but with React Components.

Downloads

197

Readme

@hbi-developer/react-switch-component

A zero-dependency, simple, and native-like Switch component for React. It works like the JavaScript switch statement but uses declarative React components, allowing for cleaner and more readable conditional rendering.

Features

  • 🚀 Zero Dependencies: Lightweight and fast.
  • 🧩 Declarative API: Read your logic as if it were code.
  • 🛠 Flexible Layouts: Wrap matched cases in a custom container with Switch.Layout.
  • ⏱️ Delayed Switches: Add delays before changing the rendered component to handle transitions seamlessly.
  • ⌨️ TypeScript Support: Full type definitions included.
  • 📦 Modern: Works with React 16.8+ (including React 19).

Installation

# Using pnpm (Recommended)
pnpm add @hbi-developer/react-switch-component

# Using npm
npm install @hbi-developer/react-switch-component

# Using yarn
yarn add @hbi-developer/react-switch-component

Quick Start

import { Switch } from "@hbi-developer/react-switch-component";

const MyComponent = ({
  status,
}: {
  status: "loading" | "success" | "error";
}) => {
  return (
    <Switch.Root>
      <Switch.Case condition={status === "loading"}>
        <p>Loading...</p>
      </Switch.Case>

      <Switch.Case condition={status === "success"}>
        <p>Data loaded successfully!</p>
      </Switch.Case>

      <Switch.Default>
        {" "}
        {/* Optional Component */}
        <p>Something went wrong.</p>
      </Switch.Default>
    </Switch.Root>
  );
};

Advanced Usage: Custom Layouts

You can use Switch.Layout to wrap the matched content in a specific component (like a Motion div or a styled container).

<Switch.Root>
  <Switch.Layout withDefault={true}>
    <div className="card-container" />
  </Switch.Layout>

  <Switch.Case condition={isAdmin}>
    <AdminPanel />
  </Switch.Case>

  <Switch.Default>
    <UserPanel />
  </Switch.Default>
</Switch.Root>

[!NOTE] The children of Switch.Layout should be a single React element that will receive the matched case content as its own children.

Advanced Usage: Delaying Switches

You can add a delay prop to Switch to delay the rendering of components when conditions change. This is especially useful for coordinating exit and enter animations.

<Switch.Root delay={300}>
  {" "}
  {/* 300ms delay before switching to any matched case */}
  <Switch.Case condition={status === "loading"}>
    <LoadingSpinner />
  </Switch.Case>
  <Switch.Case condition={status === "success"}>
    <DataView />
  </Switch.Case>
</Switch.Root>

You can also pass an object to define specific delays for each case, where 0 targets the Switch.Default and 1, 2, etc., target the Switch.Case components based on their order.

<Switch.Root delay={{ 0: 0, 1: 200, 2: 500 }}>
  <Switch.Case condition={status === "success"}>
    {" "}
    {/* index 1 */}
    <SuccessMessage />
  </Switch.Case>

  <Switch.Case condition={status === "error"}>
    {" "}
    {/* index 2 */}
    <ErrorMessage />
  </Switch.Case>

  <Switch.Default>
    {" "}
    {/* index 0 */}
    <IdleState />
  </Switch.Default>
</Switch.Root>

API Reference

Switch (or Switch.Root)

The main container for your switch logic.

| Prop | Type | Description | | :--------- | :--------------------------------- | :------------------------------------------------------------------------------------ | | children | ReactNode | Should contain Switch.Case, Switch.Default, and optionally Switch.Layout. | | delay | number \| Record<number, number> | (Optional) Adds a delay (in ms) before switching between components. Defaults to 0. |

Switch.Case

Defines a specific condition and the content to render if it's met.

| Prop | Type | Description | | :---------- | :---------- | :------------------------------------------------------------------------- | | condition | boolean | If true, this case is rendered (unless a previous case already matched). | | children | ReactNode | The content to render. |

Switch.Default

The fallback content if no Switch.Case matches.

| Prop | Type | Description | | :--------- | :---------- | :-------------------- | | children | ReactNode | The fallback content. |

Switch.Layout

A special component to define a wrapper for the matched case.

| Prop | Type | Description | | :------------ | :------------- | :------------------------------------------------------------------------------------------------- | | children | ReactElement | The wrapper element. | | withDefault | boolean | (Optional) If true, the layout will also wrap the Switch.Default content. Defaults to false. |

License

MIT © HBI Developer