A simple toggle switch component for ReactJS inspired from this cool switch animation by Oleg Frolov.
Referred most of the styling from this codepen.io project by Aaron Iker.
Demo is available here.
npm install simple-react-switch
import React, { Component } from "react";
import { HorizontalSwitch } from "simple-react-switch";
class App extends Component {
handleChange = (isChecked, key, event) => {
// Take action...
};
render() {
return (
<div>
<HorizontalSwitch onStateChange={this.handleChange} id={"123456"} />
</div>
);
}
}
Prop | Type | Default | Description |
---|---|---|---|
defaultChecked | bool | false | If true, the switch is set to checked. If false, it is not checked. |
id | string | Required | Set as an attribute to the embedded checkbox. This is useful for the associated label, which can point to the id in its htmlFor attribute. |
onStateChange ([isChecked], [id], [event]) | func | Required | Invoked when the user toggles the switch. It is passed three arguments: isChecked, which is a boolean that describes the presumed future state of the checked prop (1), the event object (2) and the id prop (3). |
disabled | bool | false | When disabled, the switch will no longer be interactive and its colors will be greyed out. |
offColor | string | '#FF4651' | The switch will take on this color when it is not checked. Only accepts hex-colors. |
onColor | string | '#48EA8B' | The switch will take on this color when it is checked. Only accepts hex-colors. |
foregroundColor | string | '#fff' | The toggle icon with animation will take on this color. Only accepts hex-colors. |
disabledColor | string | '#D3D3D3' | The switch will take on this color when disabled. Only accepts hex-colors. |
In the project directory, you can run:
Runs the app in the development mode.
Open http://localhost:3000 to view it in the browser.
The page will reload if you make edits.
You will also see any lint errors in the console.
Launches the test runner in the interactive watch mode.
See the section about running tests for more information.
Builds the app for production to the build
folder.
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.
Your app is ready to be deployed!
See the section about deployment for more information.