-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Copy pathtouch-ripple.jsx
166 lines (139 loc) · 4.07 KB
/
touch-ripple.jsx
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
const React = require('react/addons');
const PureRenderMixin = React.addons.PureRenderMixin;
const ReactTransitionGroup = React.addons.TransitionGroup;
const StylePropable = require('../mixins/style-propable');
const Dom = require('../utils/dom');
const ImmutabilityHelper = require('../utils/immutability-helper');
const CircleRipple = require('./circle-ripple');
const TouchRipple = React.createClass({
mixins: [PureRenderMixin, StylePropable],
propTypes: {
centerRipple: React.PropTypes.bool,
color: React.PropTypes.string,
opacity: React.PropTypes.number,
},
getInitialState() {
return {
//This prop allows us to only render the ReactTransitionGroup
//on the first click of the component, making the inital
//render faster
hasRipples: false,
nextKey: 0,
ripples: [],
};
},
render() {
const {
children,
style,
} = this.props;
const {
hasRipples,
ripples,
} = this.state;
let rippleGroup;
if (hasRipples) {
const mergedStyles = this.mergeAndPrefix({
height: '100%',
width: '100%',
position: 'absolute',
top: 0,
left: 0,
overflow: 'hidden',
}, style);
rippleGroup = (
<ReactTransitionGroup style={mergedStyles}>
{ripples}
</ReactTransitionGroup>
);
}
return (
<div
onMouseUp={this._handleMouseUp}
onMouseDown={this._handleMouseDown}
onMouseLeave={this._handleMouseLeave}
onTouchStart={this._handleTouchStart}
onTouchEnd={this._handleTouchEnd}>
{rippleGroup}
{children}
</div>
);
},
start(e, isRippleTouchGenerated) {
let ripples = this.state.ripples;
//Do nothing if we're starting a click-event-generated ripple
//while having touch-generated ripples
if (!isRippleTouchGenerated) {
for (let i = 0; i < ripples.length; i++) {
if (ripples[i].props.touchGenerated) return;
}
}
//Add a ripple to the ripples array
ripples = ImmutabilityHelper.push(ripples, (
<CircleRipple
key={this.state.nextKey}
style={!this.props.centerRipple ? this._getRippleStyle(e) : {}}
color={this.props.color}
opacity={this.props.opacity}
touchGenerated={isRippleTouchGenerated} />
));
this.setState({
hasRipples: true,
nextKey: this.state.nextKey + 1,
ripples: ripples,
});
},
end() {
const currentRipples = this.state.ripples;
this.setState({
ripples: ImmutabilityHelper.shift(currentRipples),
});
},
_handleMouseDown(e) {
//only listen to left clicks
if (e.button === 0) this.start(e, false);
},
_handleMouseUp() {
this.end();
},
_handleMouseLeave() {
this.end();
},
_handleTouchStart(e) {
this.start(e, true);
},
_handleTouchEnd() {
this.end();
},
_getRippleStyle(e) {
let style = {};
const el = React.findDOMNode(this);
const elHeight = el.offsetHeight;
const elWidth = el.offsetWidth;
const offset = Dom.offset(el);
const isTouchEvent = e.touches && e.touches.length;
const pageX = isTouchEvent ? e.touches[0].pageX : e.pageX;
const pageY = isTouchEvent ? e.touches[0].pageY : e.pageY;
const pointerX = pageX - offset.left;
const pointerY = pageY - offset.top;
const topLeftDiag = this._calcDiag(pointerX, pointerY);
const topRightDiag = this._calcDiag(elWidth - pointerX, pointerY);
const botRightDiag = this._calcDiag(elWidth - pointerX, elHeight - pointerY);
const botLeftDiag = this._calcDiag(pointerX, elHeight - pointerY);
const rippleRadius = Math.max(
topLeftDiag, topRightDiag, botRightDiag, botLeftDiag
);
const rippleSize = rippleRadius * 2;
const left = pointerX - rippleRadius;
const top = pointerY - rippleRadius;
style.height = rippleSize + 'px';
style.width = rippleSize + 'px';
style.top = top + 'px';
style.left = left + 'px';
return style;
},
_calcDiag(a, b) {
return Math.sqrt((a * a) + (b * b));
},
});
module.exports = TouchRipple;