1.0.1 • Published 5 years ago

simple-react-switch v1.0.1

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

simple-react-switch

npm

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.

Installation

npm install simple-react-switch

Usage

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>
    );
  }
  
}

API

PropTypeDefaultDescription
defaultCheckedboolfalseIf true, the switch is set to checked. If false, it is not checked.
idstringRequiredSet 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)funcRequiredInvoked 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).
disabledboolfalseWhen disabled, the switch will no longer be interactive and its colors will be greyed out.
offColorstring'#FF4651'The switch will take on this color when it is not checked. Only accepts hex-colors.
onColorstring'#48EA8B'The switch will take on this color when it is checked. Only accepts hex-colors.
foregroundColorstring'#fff'The toggle icon with animation will take on this color. Only accepts hex-colors.
disabledColorstring'#D3D3D3'The switch will take on this color when disabled. Only accepts hex-colors.

Available Scripts

In the project directory, you can run:

npm start

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.

npm test

Launches the test runner in the interactive watch mode. See the section about running tests for more information.

npm run build

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.

1.0.1

5 years ago

1.0.0

5 years ago

0.1.8

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago