All files / fx/Ripple Ripple.jsx

7.54% Statements 4/53
0% Branches 0/13
0% Functions 0/21
9.09% Lines 4/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                1x                                                                                                                                                                                                                                                                                                 1x 1x           1x            
import React, { Component } from 'react';
import PropTypes from 'prop-types';
 
import Atom from '../../atoms/Atom';
import withStyle from '../../Theme/withStyle';
 
import Wave from './Wave';
 
const getBounds = ({ target }) => {
  const { clientHeight, clientWidth } = target;
  const { top, left } = target.getBoundingClientRect();
  return {
    height: clientHeight,
    width: clientWidth,
    top,
    left,
  };
};
 
class Ripple extends Component {
  state = {
    outline: null,
    waves: [],
  };
 
  componentDidUpdate() {
    window.addEventListener('mouseup', this.release);
    window.addEventListener('pointercancel', this.release);
  }
 
  componentWillUnmount() {
    window.removeEventListener('mouseup', this.release);
    window.removeEventListener('pointercancel', this.release);
  }
 
  get backgroundColor() {
    const { color, getColor } = this.props;
    return getColor(color);
  }
 
  get outline() {
    const { outline, waves } = this.state;
    if (!outline || waves.length) return null;
    return <Wave {...outline} />;
  }
 
  get waves() {
    return this.state.waves.map(({ created, released, ...wave }) => (
      <Wave
        key={created}
        {...wave}
        onDissolve={this.makeTransitionHandler(created)}
        released={released}
      />
    ));
  }
 
  makeTransitionHandler = created => () =>
    this.setState(({ waves }) => ({
      waves: waves.filter(wave => wave.created !== created),
    }));
 
  pushEvent = event => {
    const { left, top, width, height } = getBounds(event);
    const size = (width ** 2 + height ** 2) ** 0.5 * 2;
    const halfSize = size / 2;
    this.pushWawe({
      created: performance.now(),
      css: {
        backgroundColor: this.backgroundColor,
        left: `${event.clientX - left - halfSize}px`,
        top: `${event.clientY - top - halfSize}px`,
        zIndex: 2,
      },
      released: false,
      size,
    });
  };
 
  release = () => {
    this.setState(({ waves }) => ({
      waves: waves.map(wave => ({
        ...wave,
        released: true,
      })),
    }));
  };
 
  removeFocus = () => this.setState({ outline: null });
 
  setFocus = (event, { current }) => {
    if (!current) return;
    const { width: innerWidth, height: innerHeight } = current.getBoundingClientRect();
    const { width, height } = getBounds(event);
    const size = Math.max(innerHeight, innerWidth, 32);
    const halfSize = size / 2;
 
    this.setState(({ waves }) => {
      if (waves.length) return null;
      return {
        outline: {
          css: {
            backgroundColor: this.backgroundColor,
            left: `${width / 2 - halfSize}px`,
            top: `${height / 2 - halfSize}px`,
            transform: `translate(-${size}px, -${size}px)`,
            zIndex: 2,
          },
          size,
        },
      };
    });
  };
 
  pushWawe(wawe) {
    this.setState(({ waves }) => ({
      waves: waves.concat(wawe),
    }));
  }
 
  render() {
    const { outline, waves } = this.state;
    if (!this.props.forceShow && !outline && !waves.length) return null;
    return (
      <Atom
        element="span"
        css={{
          background: 'none',
          borderRadius: 'inherit',
          display: 'block',
          overflow: 'hidden',
          pointerEvents: 'none',
          position: 'absolute',
          top: 0,
          left: 0,
          right: 0,
          bottom: 0,
        }}
      >
        <Atom
          element="span"
          css={{
            backgroundColor: this.backgroundColor,
            ...this.props.css,
          }}
        />
        {this.waves}
        {this.outline}
      </Atom>
    );
  }
}
 
Ripple.displayName = 'Ripple';
Ripple.propTypes = {
  color: PropTypes.string,
  getColor: PropTypes.func.isRequired,
  css: PropTypes.shape().isRequired,
  forceShow: PropTypes.bool,
};
Ripple.defaultProps = {
  color: null,
  forceShow: false,
};
 
export default withStyle(Ripple);