Code Monkey home page Code Monkey logo

react-on-off's Introduction

react-on-off

react-on-off

Flexible React components to manage on/off states

   

Table of contents

Motivation

Many UI components either have a single or multiple on/off states and require you to write the same type of stateful React components over and over again. It's not hard, but it takes time and duplicates code. Instead, we can extract common state requirements into generic, flexible and well-tested components and reuse them. This is what react-on-off is.

Inspiration

All credit for the design and API should go to react-toggled. If you only need to render a toggle component, go with react-toggled since it comes with functions that help with accessibility. If you're handling accessibility by yourself or you need to manage multiple on/off states, react-on-off is for you.

Installation

npm install --save react-on-off
# or
yarn add react-on-off

You can also use UMD builds from unpkg:

<script src="https://unpkg.com/react-on-off/lib/index.umd.js"></script>
<script src="https://unpkg.com/react-on-off/lib/index.umd.min.js"></script>

Components

OnOff

Manages a single, independent on/off state. Useful whenever you need to render something with two states.

Usage

import React from "react";
import { render } from "react-dom";
import { OnOff } from "react-on-off";

render(
  <OnOff>
    {({ on, toggle }) => (
      <>
        <h1>{on ? "Red" : "Blue"}</h1>
        <button onClick={toggle}>Switch pill</button>
      </>
    )}
  </OnOff>,
  document.getElementById("root")
);

Props

Prop Type Default Description
defaultOn
optional
boolean false The initial on state.
on
optional
boolean Control prop if you want to control the state by yourself.
onChange
optional
function Called whenever the state changes with the new on state.
children
required
function A render prop. This is where you render whatever you want based on the state of OnOff.

Render object

OnOff expects the children prop to be a function. It is called with a single argument, an object with the following properties:

Property Type Description
on boolean true if the state is on, false otherwise.
off boolean Convenience property if you need !on.
setOn function Sets the state to on.
setOff function Sets the state to off.
toggle function Toggles the state (i.e. when it's on, will set to off and vice versa).

OnOffCollection

Manages multiple on/off states where only one state can be on at all times. To render the indivial on/off states, use OnOffItem anywhere inside an OnOffCollection parent.

Usage

import React from "react";
import { render } from "react-dom";
import { OnOffCollection, OnOffItem } from "react-on-off";

render(
  <OnOffCollection defaultOn="Home">
    <ul>
      {["Home", "About", "Contact"].map(stateId => (
        <OnOffItem id={stateId} key={stateId}>
          {({ id, on, setOn }) => (
            <li onClick={setOn} className={on ? "active" : null}>
              {id}
            </li>
          )}
        </OnOffItem>
      ))}
    </ul>
  </OnOffCollection>,
  document.getElementById("root")
);

Props

Prop Type Default Description
defaultOn
optional
string The item id that should be on initially.
on
optional
string Control prop if you want to control which item should be on. Either an id or null if no item should be on.
onChange
optional
function Called whenever the state changes with the item id that is on.

OnOffItem

Represents a single on/off state that is coupled to other on/off states. Doesn't do anything without an OnOffCollection parent.

Usage

See OnOffCollection Usage

Props

Prop Type Default Description
id
optional
string a unique id Only useful in combination with the defaultOn or onChange prop from OnOffCollection. Defaults to a unique id.
children
required
function A render prop. This is where you render whatever you want based on the state of the OnOffItem.

Render object

OnOffItem expects the children prop to be a function. It is called with a single argument, an object with the following properties:

Property Type Description
id string The state id you set yourself or a generated unique id.
on boolean true if the state is on, false otherwise.
off boolean Convenience property if you need !on.
setOn function Sets the state of the item to on.
setOff function Sets the state of the item to off.
toggle function Toggles the state of the item (i.e. when it's on, will set to off and vice versa).

Related

License

MIT

react-on-off's People

Contributors

cangoektas avatar greenkeeper[bot] avatar greenkeeperio-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-on-off's Issues

An in-range update of react is breaking the build 🚨

There have been updates to the react monorepo:

    • The devDependency react was updated from 16.8.6 to 16.9.0.
  • The devDependency react-dom was updated from 16.8.6 to 16.9.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

This monorepo update includes releases of one or more dependencies which all belong to the react group definition.

react is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details

Release Notes for 16.9.0 (August 8, 2019)

React

  • Add <React.Profiler> API for gathering performance measurements programmatically. (@bvaughn in #15172)
  • Remove unstable_ConcurrentMode in favor of unstable_createRoot. (@acdlite in #15532)

React DOM

React DOM Server

  • Fix incorrect output for camelCase custom CSS property names. (@bedakb in #16167)

React Test Utilities and Test Renderer

Artifacts

• react: https://unpkg.com/[email protected]/umd/
• react-art: https://unpkg.com/[email protected]/umd/
• react-dom: https://unpkg.com/[email protected]/umd/
• react-is: https://unpkg.com/[email protected]/umd/
• react-test-renderer: https://unpkg.com/[email protected]/umd/
• scheduler: https://unpkg.com/[email protected]/umd/

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of prettier is breaking the build 🚨

The devDependency prettier was updated from 1.17.0 to 1.17.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

prettier is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of codecov is breaking the build 🚨

The devDependency codecov was updated from 3.3.0 to 3.4.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

codecov is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details

Commits

The new version differs by 4 commits.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of babel7 is breaking the build 🚨

There have been updates to the babel7 monorepo:

    • The devDependency @babel/core was updated from 7.4.3 to 7.4.4.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

This monorepo update includes releases of one or more dependencies which all belong to the babel7 group definition.

babel7 is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of rimraf is breaking the build 🚨

The devDependency rimraf was updated from 2.6.3 to 2.7.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

rimraf is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details

Commits

The new version differs by 2 commits.

  • 250ee15 2.7.0
  • dc1682d feat: make it possible to omit glob dependency

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of rollup-plugin-babel is breaking the build 🚨

The devDependency rollup-plugin-babel was updated from 4.2.0 to 4.3.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

rollup-plugin-babel is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details

Commits

The new version differs by 26 commits.

  • bf9f1a2 4.3.0
  • 97ac95b Merge branch 'custom-builder'
  • f250dea Add docs about .custom builder
  • 542c143 Merge branch 'master' into custom-builder
  • ba2559a Fail build when trying plugin tries to add non-existent helper (#290)
  • 0a764c0 Remove unnecessary guard against missing plugins in preflight check
  • 31a6f88 Tweak .custom API, validate options hook - it cant be async
  • 54d3cee Merge branch 'master' into custom-builder
  • 0752385 Update devDep to rollup@1
  • 7fc31d9 Fix issue with option plugins/presets being added twice to the loaded config
  • c8f6d69 Add Andarist as contributor in package.json
  • 348fec6 Tweak npm scripts
  • 717ce79 Merge branch 'master' into custom-builder
  • 5a425f9 Fixed .custom tests
  • e31c298 loadPartialConfig and perform preflightCheck later

There are 26 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

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.