/
FocusRipple.js
127 lines (106 loc) · 3.11 KB
/
FocusRipple.js
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
import React, {Component, PropTypes} from 'react';
import ReactDOM from 'react-dom';
import shallowEqual from 'recompose/shallowEqual';
import autoPrefix from '../utils/autoPrefix';
import transitions from '../styles/transitions';
import ScaleInTransitionGroup from './ScaleIn';
const pulsateDuration = 750;
class FocusRipple extends Component {
static propTypes = {
color: PropTypes.string,
innerStyle: PropTypes.object,
opacity: PropTypes.number,
show: PropTypes.bool,
style: PropTypes.object,
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
componentDidMount() {
if (this.props.show) {
this.setRippleSize();
this.pulsate();
}
}
shouldComponentUpdate(nextProps, nextState) {
return (
!shallowEqual(this.props, nextProps) ||
!shallowEqual(this.state, nextState)
);
}
componentDidUpdate() {
if (this.props.show) {
this.setRippleSize();
this.pulsate();
} else {
if (this.timeout) clearTimeout(this.timeout);
}
}
componentWillUnmount() {
clearTimeout(this.timeout);
}
getRippleElement(props) {
const {
color,
innerStyle,
opacity,
} = props;
const {prepareStyles, ripple} = this.context.muiTheme;
const innerStyles = Object.assign({
position: 'absolute',
height: '100%',
width: '100%',
borderRadius: '50%',
opacity: opacity ? opacity : 0.16,
backgroundColor: color || ripple.color,
transition: transitions.easeOut(`${pulsateDuration}ms`, 'transform', null, transitions.easeInOutFunction),
}, innerStyle);
return <div ref="innerCircle" style={prepareStyles(Object.assign({}, innerStyles))} />;
}
pulsate = () => {
const innerCircle = ReactDOM.findDOMNode(this.refs.innerCircle);
if (!innerCircle) return;
const startScale = 'scale(1)';
const endScale = 'scale(0.85)';
const currentScale = innerCircle.style.transform || startScale;
const nextScale = currentScale === startScale ? endScale : startScale;
autoPrefix.set(innerCircle.style, 'transform', nextScale);
this.timeout = setTimeout(this.pulsate, pulsateDuration);
};
setRippleSize() {
const el = ReactDOM.findDOMNode(this.refs.innerCircle);
const height = el.offsetHeight;
const width = el.offsetWidth;
const size = Math.max(height, width);
let oldTop = 0;
// For browsers that don't support endsWith()
if (el.style.top.indexOf('px', el.style.top.length - 2) !== -1) {
oldTop = parseInt(el.style.top);
}
el.style.height = `${size}px`;
el.style.top = `${(height / 2) - (size / 2 ) + oldTop}px`;
}
render() {
const {
show,
style,
} = this.props;
const mergedRootStyles = Object.assign({
height: '100%',
width: '100%',
position: 'absolute',
top: 0,
left: 0,
}, style);
const ripple = show ? this.getRippleElement(this.props) : null;
return (
<ScaleInTransitionGroup
maxScale={0.85}
style={mergedRootStyles}
>
{ripple}
</ScaleInTransitionGroup>
);
}
}
export default FocusRipple;