Giter Club home page Giter Club logo

react-switch's Introduction

React Switch

npm version

license

React Switch is a customizable switch component for React applications.

Installation

Install the package using npm:

npm install @usmangurowa/react-switch

Install the package using yarn:

yarn add @usmangurowa/react-switch

Usage

Here's a basic example of how to use @usmangurowa/react-switch:

import React from "react";
import { Switch, Case } from "@usmangurowa/react-switch";

const App = () => {
  const condition = true;
  return (
    <Switch case={condition.toString()}>
      <Case when="true">
        <h1>This is displayed when the condition is true</h1>
      </Case>
      <Case when="false">
        <h1>This is displayed when the condition is false</h1>
      </Case>
    </Switch>
  );
};

export default App;

API

Switch

The Switch component renders the first matching Case component based on the provided condition.

Props:

  • case (optional): The condition to match against. Accepts a string.
  • children: The Case components to render.

Case

The Case component represents a case within the Switch component.

Props:

  • when (optional): The condition to match against. Accepts a string.
  • case (optional): Same thing as when.
  • component (optional): A custom component to render when the condition matches.
  • children: The content to render when the condition matches.

Examples

Example 1: Toggle Switch

import React, { useState } from "react";
import { Switch, Case } from "@usmangurowa/react-switch";

const ToggleSwitch = () => {
  const [isOn, setIsOn] = useState(false);

  const handleToggle = () => {
    setIsOn(!isOn);
  };

  return (
    <Switch case={isOn}>
      <Case when={true}>
        <button onClick={handleToggle}>ON</button>
      </Case>
      <Case when={false}>
        <button onClick={handleToggle}>OFF</button>
      </Case>
    </Switch>
  );
};

export default ToggleSwitch;

Example 2: User Role Switch

import React, { useState } from "react";
import { Switch, Case } from "@usmangurowa/react-switch";

const UserRoleSwitch = () => {
  const [userRole, setUserRole] = useState("user");

  const handleChangeUserRole = (role) => {
    setUserRole(role);
  };

  return (
    <Switch case={userRole}>
      <Case when="admin">
        <button onClick={() => handleChangeUserRole("user")}>
          Switch to User
        </button>
      </Case>
      <Case when="user">
        <button onClick={() => handleChangeUserRole("admin")}>
          Switch to Admin
        </button>
      </Case>
    </Switch>
  );
};

export default UserRoleSwitch;

Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.

react-switch's People

Contributors

usmangurowa avatar

Stargazers

Feranmi avatar Charles avatar Edet Noah avatar Adebisi Tobiloba  avatar Abraham avatar Abubakar Abdullahi avatar Gustave Anthony Zoumedor avatar Chisom avatar Ayodeji O. avatar  avatar

Watchers

 avatar

Forkers

stavio12

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.