forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.jsx
More file actions
191 lines (153 loc) · 4.14 KB
/
button.jsx
File metadata and controls
191 lines (153 loc) · 4.14 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* MUI React button module
* @module react/button
*/
'use strict';
import React from 'react';
import * as jqLite from '../js/lib/jqLite';
import * as util from '../js/lib/util';
const btnClass = 'mui-btn',
btnAttrs = { color: 1, variant: 1, size: 1 };
/**
* Button element
* @class
*/
class Button extends React.Component {
constructor(props) {
super(props);
let cb = util.callback;
this.onMouseDownCB = cb(this, 'onMouseDown');
this.onMouseUpCB = cb(this, 'onMouseUp');
this.onMouseLeaveCB = cb(this, 'onMouseLeave');
this.onTouchStartCB = cb(this, 'onTouchStart');
this.onTouchEndCB = cb(this, 'onTouchEnd');
}
state = {
rippleStyle: {},
rippleIsVisible: false
};
static defaultProps = {
className: '',
color: 'default',
size: 'default',
variant: 'default'
};
componentDidMount() {
// disable MUI js
let el = this.buttonElRef;
el._muiDropdown = true;
el._muiRipple = true;
}
onMouseDown(ev) {
this.showRipple(ev);
// execute callback
const fn = this.props.onMouseDown;
fn && fn(ev);
}
onMouseUp(ev) {
this.hideRipple(ev);
// execute callback
const fn = this.props.onMouseUp;
fn && fn(ev);
}
onMouseLeave(ev) {
this.hideRipple(ev);
// execute callback
const fn = this.props.onMouseLeave;
fn && fn(ev);
}
onTouchStart(ev) {
this.showRipple(ev);
// execute callback
const fn = this.props.onTouchStart;
fn && fn(ev);
}
onTouchEnd(ev) {
this.hideRipple(ev);
// execute callback
const fn = this.props.onTouchEnd;
fn && fn(ev);
}
showRipple(ev) {
let buttonEl = this.buttonElRef;
// de-dupe touch events
if ('ontouchstart' in buttonEl && ev.type === 'mousedown') return;
// get (x, y) position of click
let offset = jqLite.offset(this.buttonElRef),
clickEv;
if (ev.type === 'touchstart' && ev.touches) clickEv = ev.touches[0];
else clickEv = ev;
// calculate radius
let radius = Math.sqrt(offset.width * offset.width +
offset.height * offset.height);
let diameterPx = radius * 2 + 'px';
// add ripple to state
this.setState({
rippleStyle: {
top: Math.round(clickEv.pageY - offset.top - radius) + 'px',
left: Math.round(clickEv.pageX - offset.left - radius) + 'px',
width: diameterPx,
height: diameterPx
},
rippleIsVisible: true
});
}
hideRipple(ev) {
this.setState({ rippleIsVisible: false });
}
componentDidUpdate(prevProps, prevState) {
let state = this.state,
rippleEl = this.rippleElRef;
// show ripple
if (state.rippleIsVisible && !prevState.rippleIsVisible) {
jqLite.removeClass(rippleEl, 'mui--is-animating');
jqLite.addClass(rippleEl, 'mui--is-visible');
util.requestAnimationFrame(() => {
jqLite.addClass(rippleEl, 'mui--is-animating');
});
}
// hide ripple
if (!state.rippleIsVisible && prevState.rippleIsVisible) {
// allow a repaint to occur before removing class so animation shows for
// tap events
util.requestAnimationFrame(() => {
jqLite.removeClass(rippleEl, 'mui--is-visible');
});
}
}
render() {
let cls = btnClass,
k,
v;
const { color, size, variant, ...reactProps } = this.props;
// button attributes
for (k in btnAttrs) {
v = this.props[k];
if (v !== 'default') cls += ' ' + btnClass + '--' + v;
}
return (
<button
{ ...reactProps }
ref={el => { this.buttonElRef = el }}
className={cls + ' ' + this.props.className}
onMouseUp={this.onMouseUpCB}
onMouseDown={this.onMouseDownCB}
onMouseLeave={this.onMouseLeaveCB}
onTouchStart={this.onTouchStartCB}
onTouchEnd={this.onTouchEndCB}
>
{this.props.children}
<span className="mui-btn__ripple-container">
<span
ref={el => { this.rippleElRef = el }}
className="mui-ripple"
style={this.state.rippleStyle}
>
</span>
</span>
</button>
);
}
}
/** Define module API */
export default Button;