Socket
Socket
Sign inDemoInstall

react-switch

Package Overview
Dependencies
8
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-switch

Draggable toggle-switch component for react


Version published
Weekly downloads
231K
increased by5.72%
Maintainers
1
Install size
285 kB
Created
Weekly downloads
 

Changelog

Source

[7.0.0 - 2022-06-09]

Changed

  • Export package as ESM, fixes incompatibility with Rollup/Vite and makes output slightly smaller (@Niputi)

Readme

Source

A draggable toggle-switch component for React.

npm npm GitHub stars gzip size

  • Draggable with the mouse or with a touch screen.
  • Customizable - Easy to customize size, color and more.
  • Accessible to visually impaired users and those who can't use a mouse.
  • Reasonable package size - <2.5 kB gzipped.
  • It Just Works - Sensible default styling. Uses inline styles, so no need to import a separate css file.

Demo

Take a look at the demo

Installation

npm install react-switch

Usage

import React, { Component } from "react";
import Switch from "react-switch";

class SwitchExample extends Component {
  constructor() {
    super();
    this.state = { checked: false };
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(checked) {
    this.setState({ checked });
  }

  render() {
    return (
      <label>
        <span>Switch with default style</span>
        <Switch onChange={this.handleChange} checked={this.state.checked} />
      </label>
    );
  }
}

What's the deal with the label tag?

The Switch component in the above example is nested inside a label tag. This makes sure that the label text is read out to people with reduced sight who use screen readers and enables users to click on the text to toggle the switch. If you would only put some text next to the switch but not inside a label element, the screen reader will just read out "switch off" and the user will have no idea what it is for.

If you don't want to nest the switch inside a label, you can use the htmlFor attribute on the label-element and set it to the same value as the id of the switch. Alternatively, you can use the aria-labelledby or aria-label props to give the switch a label. You can see examples of this at the bottom of the demo page.

API

PropTypeDefaultDescription
checkedboolRequiredIf true, the switch is set to checked. If false, it is not checked.
onChange ([checked], [event], [id])funcRequiredInvoked when the user clicks or drags the switch. It is passed three arguments: checked, 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'#888'The switch will take on this color when it is not checked. Only accepts hex-colors.
onColorstring'#080'The switch will take on this color when it is checked. Only accepts hex-colors.
offHandleColorstring'#fff'The handle of the switch will take on this color when it is not checked. Only accepts hex-colors.
onHandleColorstring'#fff'The handle of the switch will take on this color when it is checked. Only accepts hex-colors.
handleDiameternumberundefinedThe diameter of the handle, measured in pixels. By default it will be 2 pixels smaller than the height of the switch.
uncheckedIconelement or boolDefault valueAn icon that will be shown on the switch when it is not checked. Pass in false if you don't want any icon.
checkedIconelement or boolDefault valueAn icon that will be shown on the switch when it is checked. Pass in false if you don't want any icon.
uncheckedHandleIconelementundefinedAn icon that will be shown on the handle of the switch when it is not checked.
checkedHandleIconelementundefinedAn icon that will be shown on the handle of the switch when it is checked.
boxShadowstringundefinedThe default box-shadow of the handle. You can read up on the box-shadow syntax on MDN.
activeBoxShadowstring'0 0 2px 3px #3bf'The box-shadow of the handle when it is active or focused. Do not set this to null, since it is important for accessibility.
heightnumber28The height of the background of the switch, measured in pixels.
widthnumber56The width of the background of the switch, measured in pixels.
classNamestringundefinedSet as the className of the outer shell of the switch. Useful for positioning the switch.
borderRadiusnumberundefinedBorder radius of the switch and the handle.
idstringundefinedSet as an attribute to the embedded checkbox. This is useful for the associated label, which can point to the id in its htmlFor attribute.

NOTE: All additional props will be passed to the nested input element.

The following props have to be either 3-digit or 6-digit hex-colors: offColor, onColor, offHandleColor, and onHandleColor. This is because this library calculates intermediate color values based on the hex-color strings.

Examples of valid colors: '#abc', '#123456'

Examples of invalid colors: 'red', 'rgb(0,0,0)'

Development

You're welcome to contribute to react-switch. Keep in mind that big changes have to be thoroughly tested on lots of different browsers and devices before they can be merged.

To set up the project:

  1. Fork and clone the repository
  2. $ npm install
  3. $ npm run dev

The demo page will then be served on http://localhost:8000/ in watch mode, meaning you don't have refresh the page to see your changes.

Contributors


Markus Englund

Timothy McLane

License

MIT

Keywords

FAQs

Last updated on 09 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc