# react-3switch

> A simple and minimal React component for two state and three state toggle switches built with no extra dependencies.

Latest version **1.0.3** (published 2022-08-27) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install react-3switch
pnpm add react-3switch
yarn add react-3switch
bun add react-3switch
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2022-08-27 |
| First published | 2022-08-27 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Lyvan M. |
| Maintainers | m_lyv |
| Keywords | react, toggle, switch, three-state, multistate |

## Links

- npm: https://www.npmjs.com/package/react-3switch
- Repository: https://github.com/mLyvan/react-3switch
- Homepage: https://github.com/mLyvan/react-3switch#readme
- Issues: https://github.com/mLyvan/react-3switch/issues
- npm.io page: https://npm.io/package/react-3switch

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2022-08-27
- 1.0.2 — 2022-08-27

## README

# react-3switch
![CI](https://github.com/mLyvan/react-3switch/actions/workflows/ci.yaml/badge.svg)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)

A simple and minimal functional React component for toggle switches that supports simple two state input and three state input. Hence the name, react-three-state-switch. Since this is nothing more than a button, no extra dependencies are required, and it will stay that way.

Project would always be promptly updated to reflect any breaking changes in React.

## Usage
The module is available on npm.

```bash
npm install react-3switch
```

This would install the component itself, the styles and TypeScript declarations.

Its advised to import the default styling.

```javascript
// Either one depending on your requirements. scss files are also provided.
import "react-3switch/styles.css"; // esm
require("react-3switch/styles.css"); // cjs
```
If you want to override the default styling with your own custom ones, you can target the default classes `.r3s-toggleContainer` and `.r3s-toggle` or the className provided as a prop. Note that most appearance values are set in css variables which could be overriden.

| Variable             | Description                                                                           |
|----------------------|---------------------------------------------------------------------------------------|
| --widget-dimension   | The parent container dimensions. The width is dynamically calculated.                 |
| --button-dimension   | The button dimension, technically the diameter of the circular button.                |
| --animation-duration | Duration for state transition animations. The color and button movement are animated. |

The component itself behaves as a controlled input with the state being handled by the parent. So the usage is identical to normal HTML inputs.
```typescript
import {Toggle, ToggleState} from "react-3switch";

function Component() {
    const [toggleState, setToggleState] = useState<ToggleState>(ToggleState.INACTIVE);
    return (
        <Toggle value={toggleState} onChange={setToggleState} />
    );
}
```

## Available Props
NOTE: `ToggleState` is a TypeScript enum which is an incrementing numerical value. If you are using JavaScript, the values should be replaced by the numerical value, but it's recommended to use TypeScript to not lose any typing information.
```typescript
enum ToggleState {
    OFF, // 0
    INACTIVE, // 1
    ON, // 2
};
```
| Name         | Type                              | Description                                                      |
|--------------|-----------------------------------|------------------------------------------------------------------|
| `id?`        | `string`                          | OPTIONAL container id attribute.                                 |
| `className?` | `string`                          | OPTIONAL container class attribute.                              |
| `onChange`   | `(newState: ToggleState) => void` | Value change handler provided by the parent to update the state. |
| `value`      | `ToggleState`                     | The current value provided by the parent.                        |
| `isBoolean`  | `boolean`                         | `false` by default, `true` if the switch is two-state.           |

## Contributing
If you need to make changes and are interested in sharing those changes, feel free to open a pull request, which would be responded to promptly. General information can be found at [CONTRIBUTING.md](CONTRIBUTING.md). In addition, this component is safe for use in Server Side Rendered applications, so no unsafe`window` and `document` usage which can break SSR.

---
_Source: https://npm.io/package/react-3switch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
