# reactswitch

> View switch between two react components without remounting them

Latest version **0.1.7** (published 2016-04-13) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install reactswitch
pnpm add reactswitch
yarn add reactswitch
bun add reactswitch
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.7 |
| Published | 2016-04-13 |
| First published | 2016-03-18 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Wyde Product Team |
| Maintainers | mphasis-wyde, zongwei.wang |
| Keywords | ReactSwitch, transition, react |

## Links

- npm: https://www.npmjs.com/package/reactswitch
- Repository: https://github.com/MphasisWyde/ReactSwitch
- Homepage: https://github.com/MphasisWyde/ReactSwitch#readme
- Issues: https://github.com/MphasisWyde/ReactSwitch/issues
- npm.io page: https://npm.io/package/reactswitch

## Dependencies (2)

- [extend](https://npm.io/package/extend.md) ^3.0.0
- [classnames](https://npm.io/package/classnames.md) ^2.2.0

## 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

- 0.1.7 (latest) — 2016-04-13
- 0.1.6 — 2016-04-13
- 0.1.5 — 2016-03-22
- 0.1.4 — 2016-03-22
- 0.1.3 — 2016-03-21
- 0.1.1 — 2016-03-18
- 0.1.0 — 2016-03-18

## README

# ReactSwitch [Demo](http://mphasiswyde.github.io/ReactSwitch/demo)
Viwer switch to switch views between two react components without remounting them. 
This component can avoid performance issue when using transitions on components with large data to calculate.

## Introduction:
The ReactSwitch is concepted to have two states, 'Off' and 'On', to switch between. It accepts only two sub-components to display, which correspond to these two states.
Once the ReactSwitch is mounted, the 'Off' state will always be displayed as the default state. However, it is possible to explicitly define the correspondances between 
states and sub-components. A button with title is provided as the trigger of the switching of the state.

## Why Use ReactSwitch?
Firstly, the ReactSwitch is a react component with transitions implemented by using pure CSS properties, which does not require extra re-mounting of sub-components. This makes it easy for users to realize effects for switching without adding logics on sub-components to avoid the performance issues. Secondly, The component is functional with some basic uses of props, this saves you the works of javascript or css coding by your own. Thirdly, the switch does not require fixed size for its sub-components. It can adapt to the real sizes of the sub-components, therefore, the resulting contents will work well with responsive design.

## How-to Install?

npm install reactswitch

## How-to Use?
The ReactSwitch has default parameters for basic usage, however, it can also be configured by props and CSS files. Like many of the react components, the ReactSwitch can
take into account some properties. Apart from the default CSS styles and trainsitions, it is also possbile to achieve customized tranistions by override the default styles.

To use the component, you can at first import the component:
```js
import ReactSwitch from "reactswitch";
```

And then, you can take the default css file from **./dist/public/css/** or override it and include in the project by javascript:
```js
import "patch/to/react-switch.css";
```
or include in the page head:
```html
<link rel="stylesheet" type="text/css" href="patch/to/react-switch.css">
```

As for the icons displayed in the button, it is necessary to import the corresponding style file and then you can use the right classes such as followed (take fontawsome for 
example):
```html
<link rel="stylesheet" type="text/css" href="patch/to/icon.css">
```

```html
<ReactSwitch 
 ...
 offStateIcon="fa fa-arrow-up" onStateIcon="fa fa-arrow-down"
 ...
 />
```

## Properties on the ReactSwitch:
| Component property   | Type   | Mandatory | Default | Description                                                                                 |
| -------------------- | ------ | --------- | ------- | ------------------------------------------------------------------------------------------- |
| offStateTitle        | any    | Yes       |         | The title when the switch is 'Off', it can be a string or an even a react element ...       |
| onStateTitle         | any    | Yes       |         | The title when the switch is 'On', same as above                                            |
| offStateIcon         | string | No        |         | The icon when the switch is 'Off', this requires the icon resouce installed                 |
| onStateIcon          | string | No        |         | The icon when the switch is 'On', this requires the icon resouce installed                  |
| heightAnimationMode  | bool   | No        | false   | A predefined animation with trainsition of height, can be used together with 'fadeOutAnimationMode'. With this option on, the switch provides a transition of heights on both states.|
| fadeOutAnimationMode | bool   | No        | false   | A predefined animation with effect of fading out, can be used together with 'heightAnimationMode'. With this option on, the switch provides a transition of opacity on the next state to display.|
| buttonProps          | object | No        |         | All the properties for the button can be provided here, can be used for customization purpose|

## Properties on the sub-components:
| Component property   | Type   | Mandatory | Default | Description                                                                                 |
| -------------------- | ------ | --------- | ------- | ------------------------------------------------------------------------------------------- |
| default        | bool    | No       |         | This associates the sub-component to the 'Off' state, it can only be assigned to one of the sub-component. If neither of the sub-component uses this prop, the ReactSwitch will take the first one as the default one and associate it to the 'Off' state. |
 
## CSS classes:
| Appliable classes    | Description of applied element                                                              |
| -------------------- | ------------------------------------------------------------------------------------------- |
| react-switch-shown   | The diplayed state |
| react-switch-hidden  | The hidden state |
| react-switch-height-animation   | The animiation properties |
| react-switch-fade-out-animation   | The animiation properties |
| react-switch-height-and-fade-out-animation  | The animiation properties |
| react-switch-shown   | The button |
| react-switch-button:hover   | Hover properties for the button |
| react-switch-button-title   | Button title |
| react-switch-button-on-state-title  | Button title when in 'On' state |
| react-switch-button-off-state-title   | Button title when in 'Off' state |
| react-switch-on-state   | The 'On' state |
| react-switch-off-state  | The 'Off' state |
| react-switch-icon  | The icon |  

## Examples:
A mininum usage is as followed:
```js
    <ReactSwitch offStateTitle={"Read More"} onStateTitle={"Close"} heightAnimationMode>
        <div>
        To read hidden information, you must ...
        </div>
        <div>To read hidden information, you must click on 
        the button 'Read More'. This is a simple demonstration of the 'React Switch' component.
        with more data<br/>
        with much more data	
        with more data<br/>
        with much more data
        </div>
    </ReactSwitch>
```

A more complexed usage can be:
```js
	handleClick(){
		alert("An external action is executed");
	}
    
    <ReactSwitch onStateTitle="Read More"
    offStateTitle="Close"
    offStateIcon="fa fa-arrow-up"
    onStateIcon="fa fa-arrow-down" 
    heightAnimationMode
    fadeOutAnimationMode
    buttonProps={{className:"custom-button", onClick:this.handleClick.bind(this)}}
    >
        <p >To read hidden information, you must ...</p>
        <p default>To read hidden information, you must click on 
        the button 'Read More'. This is a simple demonstration of the 'React Switch' component.</p>
    </ReactSwitch>
```

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