Coder Social home page Coder Social logo

custom-react-toggle's Introduction

React Toggle Switch

Accessible Custom Toggle Switch Component for React.

Examples

Here is a simple example of react toggle-switch being used in an app with some custom styles.

import React from 'react';
import styled from 'styled-components';

const StyledLable = styled.label`
  cursor: pointer;
  text-indent: 10px;
  width: 70px;
  height: 33px;
  background: #2b3039;
  display: block;
  border-radius: 100px;
  position: relative;
  &:after {
    content: '';
    position: absolute;
    top: 5px;
    left: 5px;
    width: 27px;
    height: 23px;
    background: #dc3545;
    border-radius: 90px;
    transition: 0.3s;
  }
  &:active:after{
    width: 130px;
  }
`;

const StyledInput = styled.input`
  width: 37px;
  height: 39px;
  margin-right: 10px;
  &:checked+${StyledLable} {
    background: #2b3039;
  }
  &:checked+${StyledLable}:after {
    left: calc(100% - 5px);
    transform: translateX(-100%);
    background-color: #347129 !important;
  }
  &[type=checkbox] {
    height: 0;
    width: 0;
    visibility: hidden;
  }
`;

class ToggleBtn extends React.Component {
  render() {
    return (
      <div>
        <StyledInput type="checkbox" id="switch" />
        <StyledLable htmlFor="switch" />
      </div>
    );
  }
}

export default ToggleBtn;

This works great if all we ever need is the exact same button throughout our entire application, has different colors in it ( ‘success’ or ‘danger’ )!

We’ve created a button component that is entirely self contained and can be easily used in a variety of different states throughout the application.

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.