content logo

React Buttons:

React Toggle Switch Button with Color Change Effect

In this post, we have prepared a button that is made using React. This button is a toggle switch with color change effect. By clicking on this switch, the color of the button changes from green to blue and its scroll bar goes to the right. The rolling circle inside this switch is white.

#

Free React Switch Button

#

React Toggle Switche Code

#

HTML React Button Code

#

Color Change Effect Code

<!-- This script got from frontendfreecode.com -->
<div id="page"></div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
.switch-container {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate3d(-50%, -50%, 0);
  transform: translate3d(-50%, -50%, 0);
}
input[type=checkbox].switch {
  position: absolute;
  opacity: 0;
}
input[type=checkbox].switch + div {
  vertical-align: middle;
  width: 40px;
  height: 20px;
  border-radius: 999px;
  background-color: #00B0F2;
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
  -webkit-transition-property: background-color, box-shadow;
  transition-property: background-color, box-shadow;
  cursor: pointer;
}
input[type=checkbox].switch + div span {
  position: absolute;
  font-size: 1.6rem;
  color: white;
  margin-top: 12px;
}
input[type=checkbox].switch + div span:nth-child(1) {
  margin-left: 15px;
}
input[type=checkbox].switch + div span:nth-child(2) {
  margin-left: 55px;
}
input[type=checkbox].switch:checked + div {
  width: 94px;
  background-position: 0 0;
  background-color: #3b89ec;
}
input[type=checkbox].switch + div {
  width: 94px;
  height: 48px;
}
input[type=checkbox].switch:checked + div {
  background-color: #57BB00;
}
input[type=checkbox].switch + div > div {
  float: left;
  width: 46px;
  height: 46px;
  border-radius: inherit;
  background: #ffffff;
  -webkit-transition-timing-function: cubic-bezier(1, 0, 0, 1);
  transition-timing-function: cubic-bezier(1, 0, 0, 1);
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
  -webkit-transition-property: transform, background-color;
  transition-property: transform, background-color;
  pointer-events: none;
  margin-top: 1px;
  margin-left: 1px;
}
input[type=checkbox].switch:checked + div > div {
  -webkit-transform: translate3d(20px, 0, 0);
  transform: translate3d(20px, 0, 0);
  background-color: #ffffff;
}
input[type=checkbox].bigswitch.switch + div > div {
  width: 44px;
  height: 44px;
  margin-top: 1px;
}
input[type=checkbox].switch:checked + div > div {
  -webkit-transform: translate3d(46px, 0, 0);
  transform: translate3d(46px, 0, 0);
}
class Switch extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isChecked: null };
  }
  componentWillMount() {
    this.setState({ isChecked: this.props.isChecked });
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("div", { className: "switch-container" }, /*#__PURE__*/
      React.createElement("label", null, /*#__PURE__*/
      React.createElement("input", { ref: "switch", checked: this.state.isChecked, onChange: this._handleChange, className: "switch", type: "checkbox" }), /*#__PURE__*/
      React.createElement("div", null, /*#__PURE__*/
      React.createElement("div", null)))));
  }
  _handleChange() {
    this.setState({ isChecked: !this.state.isChecked });
  }}
React.render( /*#__PURE__*/React.createElement(Switch, { isChecked: true }), document.getElementById("page"));
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react.min.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/0.14.3/react.min.js'></script>
<style>
.switch-container {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate3d(-50%, -50%, 0);
  transform: translate3d(-50%, -50%, 0);
}
input[type=checkbox].switch {
  position: absolute;
  opacity: 0;
}
input[type=checkbox].switch + div {
  vertical-align: middle;
  width: 40px;
  height: 20px;
  border-radius: 999px;
  background-color: #00B0F2;
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
  -webkit-transition-property: background-color, box-shadow;
  transition-property: background-color, box-shadow;
  cursor: pointer;
}
input[type=checkbox].switch + div span {
  position: absolute;
  font-size: 1.6rem;
  color: white;
  margin-top: 12px;
}
input[type=checkbox].switch + div span:nth-child(1) {
  margin-left: 15px;
}
input[type=checkbox].switch + div span:nth-child(2) {
  margin-left: 55px;
}
input[type=checkbox].switch:checked + div {
  width: 94px;
  background-position: 0 0;
  background-color: #3b89ec;
}
input[type=checkbox].switch + div {
  width: 94px;
  height: 48px;
}
input[type=checkbox].switch:checked + div {
  background-color: #57BB00;
}
input[type=checkbox].switch + div > div {
  float: left;
  width: 46px;
  height: 46px;
  border-radius: inherit;
  background: #ffffff;
  -webkit-transition-timing-function: cubic-bezier(1, 0, 0, 1);
  transition-timing-function: cubic-bezier(1, 0, 0, 1);
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
  -webkit-transition-property: transform, background-color;
  transition-property: transform, background-color;
  pointer-events: none;
  margin-top: 1px;
  margin-left: 1px;
}
input[type=checkbox].switch:checked + div > div {
  -webkit-transform: translate3d(20px, 0, 0);
  transform: translate3d(20px, 0, 0);
  background-color: #ffffff;
}
input[type=checkbox].bigswitch.switch + div > div {
  width: 44px;
  height: 44px;
  margin-top: 1px;
}
input[type=checkbox].switch:checked + div > div {
  -webkit-transform: translate3d(46px, 0, 0);
  transform: translate3d(46px, 0, 0);
}
</style>

</head>
<body>
<div id="page"></div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
class Switch extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isChecked: null };
  }
  componentWillMount() {
    this.setState({ isChecked: this.props.isChecked });
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("div", { className: "switch-container" }, /*#__PURE__*/
      React.createElement("label", null, /*#__PURE__*/
      React.createElement("input", { ref: "switch", checked: this.state.isChecked, onChange: this._handleChange, className: "switch", type: "checkbox" }), /*#__PURE__*/
      React.createElement("div", null, /*#__PURE__*/
      React.createElement("div", null)))));
  }
  _handleChange() {
    this.setState({ isChecked: !this.state.isChecked });
  }}
React.render( /*#__PURE__*/React.createElement(Switch, { isChecked: true }), document.getElementById("page"));
</script>

</body>
</html>
Preview