content logo

React Buttons:

Animating React Button with Loading Effect

As you can see, we have a button in this post. This button is made using React. By clicking on the button, the button turns into a circle and it gets bigger and smaller with this circle. This enlargement and shrinkage of the circle is like a loading effect. Then another button with the title Done is displayed. You can use this button to register a request.

#

Dynamic React Button Code

#

Free React Button Example

#

Striking React Button Code

#

Animating Button Loading Effect

<!-- This script got from frontendfreecode.com -->
<div id="i-am-root"></div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
@import url("https://fonts.googleapis.com/css?family=Quicksand");
body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #eeabab;
}
button {
  transition: width 0.5s;
  font-family: "Quicksand", sans-serif;
  font-weight: bold;
  font-size: 25px;
  background: #fff;
  border: none;
  border-radius: 100px;
  padding: 10px 25px;
  color: #eeabab;
  letter-spacing: 1px;
  cursor: pointer;
  box-shadow: 5px 5px 10px 0px rgba(0, 0, 0, 0.2), inset -5px -5px 10px 0px rgba(0, 0, 0, 0.2);
  outline: none;
  box-sizing: border-box;
  width: 253px;
  height: 51px;
}
button span {
  white-space: nowrap;
}
button.loading {
  -webkit-animation: loading 0.5s 0.75s alternate infinite;
          animation: loading 0.5s 0.75s alternate infinite;
  transition: width 0.5s 0.25s;
  width: 51px;
}
button.loading span {
  opacity: 0;
  transition: opacity 0.25s;
}
button.success {
  width: 128px;
}
button.success span {
  opacity: 0;
  -webkit-animation: success 0.5s 0.5s forwards;
          animation: success 0.5s 0.5s forwards;
}
@-webkit-keyframes loading {
  100% {
    transform: scale(1.2);
    background: #fff7ba;
  }
}
@keyframes loading {
  100% {
    transform: scale(1.2);
    background: #fff7ba;
  }
}
@-webkit-keyframes success {
  100% {
    opacity: 1;
  }
}
@keyframes success {
  100% {
    opacity: 1;
  }
}
function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}const root = document.getElementById('i-am-root');
class Button extends React.Component {
  constructor(props) {
    super(props);_defineProperty(this, "handlePush",
    e => {
      if (this.state.class === "success") return;
      const fakeDataCall = new Promise((resolve, reject) => {
        setTimeout(function () {
          resolve("success");
        }, 3000);
      });
      this.setState({ class: 'loading' }, () => {
        fakeDataCall.then(() => {
          this.setState({ class: "success", buttonText: "Did it!" });
        });
      });
    });this.state = { buttonText: 'Do Something?', class: '' };}
  render() {
    return /*#__PURE__*/(
      React.createElement("button", { onClick: this.handlePush, class: this.state.class }, /*#__PURE__*/
      React.createElement("span", null, this.state.buttonText)));
  }}
ReactDOM.render( /*#__PURE__*/React.createElement(Button, null), root);
<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/16.7.0/umd/react.production.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.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/16.7.0/umd/react.production.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js'></script>
<style>
@import url("https://fonts.googleapis.com/css?family=Quicksand");
body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #eeabab;
}
button {
  transition: width 0.5s;
  font-family: "Quicksand", sans-serif;
  font-weight: bold;
  font-size: 25px;
  background: #fff;
  border: none;
  border-radius: 100px;
  padding: 10px 25px;
  color: #eeabab;
  letter-spacing: 1px;
  cursor: pointer;
  box-shadow: 5px 5px 10px 0px rgba(0, 0, 0, 0.2), inset -5px -5px 10px 0px rgba(0, 0, 0, 0.2);
  outline: none;
  box-sizing: border-box;
  width: 253px;
  height: 51px;
}
button span {
  white-space: nowrap;
}
button.loading {
  -webkit-animation: loading 0.5s 0.75s alternate infinite;
          animation: loading 0.5s 0.75s alternate infinite;
  transition: width 0.5s 0.25s;
  width: 51px;
}
button.loading span {
  opacity: 0;
  transition: opacity 0.25s;
}
button.success {
  width: 128px;
}
button.success span {
  opacity: 0;
  -webkit-animation: success 0.5s 0.5s forwards;
          animation: success 0.5s 0.5s forwards;
}
@-webkit-keyframes loading {
  100% {
    transform: scale(1.2);
    background: #fff7ba;
  }
}
@keyframes loading {
  100% {
    transform: scale(1.2);
    background: #fff7ba;
  }
}
@-webkit-keyframes success {
  100% {
    opacity: 1;
  }
}
@keyframes success {
  100% {
    opacity: 1;
  }
}
</style>

</head>
<body>
<div id="i-am-root"></div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}const root = document.getElementById('i-am-root');
class Button extends React.Component {
  constructor(props) {
    super(props);_defineProperty(this, "handlePush",
    e => {
      if (this.state.class === "success") return;
      const fakeDataCall = new Promise((resolve, reject) => {
        setTimeout(function () {
          resolve("success");
        }, 3000);
      });
      this.setState({ class: 'loading' }, () => {
        fakeDataCall.then(() => {
          this.setState({ class: "success", buttonText: "Did it!" });
        });
      });
    });this.state = { buttonText: 'Do Something?', class: '' };}
  render() {
    return /*#__PURE__*/(
      React.createElement("button", { onClick: this.handlePush, class: this.state.class }, /*#__PURE__*/
      React.createElement("span", null, this.state.buttonText)));
  }}
ReactDOM.render( /*#__PURE__*/React.createElement(Button, null), root);
</script>

</body>
</html>
Preview