React Tooltip and Timer

In this example below you will see how to do a React Tooltip and Timer with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by naturalclar, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright naturalclar ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>React Tooltip and Timer</title>
  
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  <link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css'>

      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div id="root"></div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.6.1/react-dom.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.2/moment.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/naturalclar/react-tooltip-and-timer-NwyXzy */
body {
  background: deepskyblue;
}

.container{
  display: flex;
  justify-content: center;
  padding-top: 112px;
  padding-top: 7rem;
}

.hover {
  position: relative;
  padding: 16px;
  padding: 1rem;
  border-radius: 5px;
  background: #ddd;
}

.tooltip {
  position: absolute;
  right: 110%;
  margin-top: 24px;
  margin-top: 1.5rem;
  top: -112px;
  top: -7rem;
  width: 224px;
  width: 14rem;
  height: 224px;
  height: 14rem;
  color: white;
  z-index: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.tooltip_header{
    padding: 8px;
    padding: 0.5rem;
    background: #2d2f38;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

.tooltip_header_icon {
      font-family: 'FontAwesome';
      display: flex;
      justify-content: center;
      align-items: center;
    }

.tooltip_header_text {
      padding-left: 4.8px;
      padding-left: 0.3rem;
      flex: 1;
    }

.tooltip_body{
    margin: 0;
    list-style-type:none;
    flex: 1;
    opacity: 0.8;
    background: #2d2f38;
    padding: 8px;
    padding: 0.5rem;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }

.tooltip_body > li {
      white-space: nowrap;
    }

.tooltip_footer {
    padding: 8px;
    padding: 0.5rem;
    background: #2d2f38;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

.tooltip_footer_text {
      flex: 1;
      display: flex;
      justify-content: center;
      cursor: pointer;
    }

.tooltip_footer_icon {
      padding: 0 8px;
      padding: 0 0.5rem;
      font-family: 'FontAwesome';
      cursor: pointer;
      display: flex;
      justify-content: center;
      align-items: center;
    }

.tooltip_arrow {
    position: absolute;
    left: 100%;
    top: 50%;
    opacity: 0.8;
    margin-top: -4.8px;
    margin-top: -0.3rem;
    border-left: 5px solid #2d2f38;
    border-top: 5px solid transparent;
    border-right: 5px solid transparent;
    border-bottom: 5px solid transparent;    
  }

.tooltip span {
    font-weight: bold;
    color: #53a0a4;
  }



/*Downloaded from https://www.codeseek.co/naturalclar/react-tooltip-and-timer-NwyXzy */
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Tooltip = function (_React$Component) {
  _inherits(Tooltip, _React$Component);

  function Tooltip(props) {
    _classCallCheck(this, Tooltip);

    var _this = _possibleConstructorReturn(this, (Tooltip.__proto__ || Object.getPrototypeOf(Tooltip)).call(this, props));

    _this.state = {
      time: moment(),
      ellapsed: moment()
    };
    return _this;
  }

  _createClass(Tooltip, [{
    key: 'setTime',
    value: function setTime() {
      this.setState({ ellapsed: moment() });
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _this2 = this;

      this.time = setInterval(function () {
        return _this2.setTime();
      }, 1000);
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      clearInterval(this.time);
    }
  }, {
    key: 'getDuration',
    value: function getDuration() {
      return moment.duration(this.state.ellapsed.diff(this.state.time));
    }
  }, {
    key: 'getEllapsedHours',
    value: function getEllapsedHours() {
      return parseInt(this.getDuration().asHours(), 10);
    }
  }, {
    key: 'getEllapsedMinutes',
    value: function getEllapsedMinutes() {
      var hours = this.getEllapsedHours();
      return parseInt(this.getDuration().asMinutes() - 60 * hours, 10);
    }
  }, {
    key: 'getRemaining',
    value: function getRemaining() {
      return moment(moment(this.props.duration, 'hour').diff(this.state.ellapsed.diff(this.state.time)));
    }
  }, {
    key: 'getRemainingHours',
    value: function getRemainingHours() {
      return this.getRemaining().format('h');
    }
  }, {
    key: 'getRemainingMinutes',
    value: function getRemainingMinutes() {
      return this.getRemaining().format('mm');
    }
  }, {
    key: 'render',
    value: function render() {
      var visibility = {
        visibility: this.props.show ? 'visible' : 'hidden'
      };
      return React.createElement(
        'div',
        { className: 'tooltip', style: visibility },
        React.createElement(
          'div',
          { className: 'tooltip_header' },
          React.createElement(
            'div',
            { className: 'tooltip_header_icon' },
            '\uF017'
          ),
          React.createElement(
            'div',
            { className: 'tooltip_header_text' },
            this.state.time.format('h:mm')
          ),
          React.createElement(
            'div',
            null,
            React.createElement(
              'span',
              null,
              this.getEllapsedHours()
            ),
            'hr\xA0',
            React.createElement(
              'span',
              null,
              this.getEllapsedMinutes()
            ),
            'min'
          )
        ),
        React.createElement(
          'ul',
          { className: 'tooltip_body' },
          React.createElement(
            'li',
            null,
            'Duration:\xA0',
            React.createElement(
              'span',
              null,
              this.props.duration
            ),
            'hrs'
          ),
          React.createElement(
            'li',
            null,
            'Rate:\xA0$',
            React.createElement(
              'span',
              null,
              this.props.rate
            ),
            '/hr'
          ),
          React.createElement(
            'li',
            null,
            'Remaining time:\xA0',
            React.createElement(
              'span',
              null,
              this.getRemainingHours()
            ),
            'hr\xA0',
            React.createElement(
              'span',
              null,
              this.getRemainingMinutes()
            ),
            'min'
          )
        ),
        React.createElement(
          'div',
          { className: 'tooltip_footer' },
          React.createElement(
            'div',
            { className: 'tooltip_footer_text' },
            React.createElement(
              'span',
              null,
              'Alert'
            )
          ),
          React.createElement(
            'div',
            { className: 'tooltip_footer_icon' },
            '\uF15C'
          ),
          React.createElement(
            'div',
            { className: 'tooltip_footer_icon' },
            '\uF013'
          )
        ),
        React.createElement('span', { className: 'tooltip_arrow' })
      );
    }
  }]);

  return Tooltip;
}(React.Component);

var Main = function (_React$Component2) {
  _inherits(Main, _React$Component2);

  function Main(props) {
    _classCallCheck(this, Main);

    var _this3 = _possibleConstructorReturn(this, (Main.__proto__ || Object.getPrototypeOf(Main)).call(this, props));

    _this3.state = {
      hover: false
    };
    return _this3;
  }

  _createClass(Main, [{
    key: 'handleOnMouseEnter',
    value: function handleOnMouseEnter() {
      this.setState({ hover: true });
    }
  }, {
    key: 'handleOnMouseLeave',
    value: function handleOnMouseLeave() {
      this.setState({ hover: false });
    }
  }, {
    key: 'render',
    value: function render() {
      var _this4 = this;

      return React.createElement(
        'div',
        { className: 'container' },
        React.createElement(
          'div',
          {
            className: 'hover',
            onMouseEnter: function onMouseEnter() {
              return _this4.handleOnMouseEnter();
            },
            onMouseLeave: function onMouseLeave() {
              return _this4.handleOnMouseLeave();
            }
          },
          'Hover Me!',
          React.createElement(Tooltip, { duration: 3, rate: 2, show: this.state.hover })
        )
      );
    }
  }]);

  return Main;
}(React.Component);

ReactDOM.render(React.createElement(Main, null), document.querySelector('#root'));

Comments