Code Monkey home page Code Monkey logo

react-flexible-switch's Introduction

React Flexible Switch

Easy and Flexible React switches with support for custom styles.

Demo & Examples

switch

Live demo: http://nettofarah.github.io/react-flexible-switch To build the examples locally, run:

npm install
npm start

Then open localhost:8000 in a browser.

Installation

The easiest way to use react-switch is to install it from NPM and include it in your own React build process (using Browserify, Webpack, etc).

You can also use the standalone build by including dist/react-switch.js in your page. If you use this, make sure you have already included React, and it is available as a global variable.

npm install react-flexible-switch --save

Usage

Just require 'react-flexible-switch' in your app and include it in your components.

const Switch = require('react-flexible-switch');
<Switch />

Properties

Switch.propTypes = {
  value: React.PropTypes.bool,

  circleStyles: React.PropTypes.shape({
    onColor: React.PropTypes.string,
    offColor: React.PropTypes.string,
    diameter: React.PropTypes.number
  }),

  labels: React.PropTypes.shape({
    on: React.PropTypes.string,
    off: React.PropTypes.string
  }),

  locked: React.PropTypes.bool,

  onChange: React.PropTypes.func,

  switchStyles: React.PropTypes.shape({
    width: React.PropTypes.number
  })
};

value

Allows you to start a switch either turned on or off.

//On by default
<Switch value={true} />

//Off by default
<Switch value={false} />

onChange

Allows you to pass in callback for when state changes. This will allow you to make the switch a controlled component.

const onChange = (active) => {
  if (active) {
    console.log('active!')
  } else {
    console.log('inactive!')
  }

  // update your state here
  this.setState({ value: active })
}

<Switch onChange={onChange} />

Custom Styles

You can style both the circle and switch styles with any css property, with the addition of onColor, offColor and diameter.

// Custom circle colors and size
<Switch circleStyles={{ onColor: 'blue', offColor: 'red' }} />
<Switch circleStyles={{ diameter: 55 }} />
<Switch circleStyles={{ diameter: 20 }} />

// Custom Switch width
<Switch switchStyles={{ width: 50 }} />
<Switch switchStyles={{ width: 200 }} />

Customzing with CSS classes

You can also style the components using the following css classes:

  • react-flexible-switch: the main component
  • react-flexible-switch--active: the main component, when active
  • react-flexible-switch--inactive: the main component, when inactive
  • react-flexible-switch--sliding: the main component, during the transition
  • react-flexible-switch-label: the label component
  • react-flexible-switch-circle: the circle component

Labels

Labels for the on and off states can be set by using the labels property.

<Switch labels={{ on: 'Turned On', off: 'Turned Off' }} />
<Switch labels={{ on: 'On', off: 'Off' }} />

Blocking User Interaction

In case you need to lock the switch and block user interaction for some reason.

<Switch locked />
<Switch active locked />

Development (src, lib and the build process)

NOTE: The source code for the component is in src. A transpiled CommonJS version (generated with Babel) is available in lib for use with node.js, browserify and webpack. A UMD bundle is also built to dist, which can be included without the need for any build system.

To build, watch and serve the examples (which will also watch the component source), run npm start. If you just want to watch changes to src and rebuild lib, run npm run watch (this is useful if you are working with npm link).

License

The module is available as open source under the terms of the MIT License. Copyright (c) 2016 Netto Farah.

react-flexible-switch's People

Contributors

latal avatar nettofarah avatar rf- avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-flexible-switch's Issues

Class names

Because of class names, when using with bootstrap, labels get color:#fff and become invisible.
To avoid this you should use bem style classes for example:

    .switch
    .switch--active
    .switch__label
    .switch__cirlce

or something else.

Server Side Rendering Support

When using this component with server side rendering, this error occured:

ReferenceError: window is not defined

This is when you check about touch device:

this.isTouchDevice = window['ontouchstart'] !== undefined;

Currently, i just add this:

if (window !== undefined)
    this.isTouchDevice = window['ontouchstart'] !== undefined;
else
    this.isTouchDevice = false;

Not sure this is the good solution, but if it can help...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.