-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbutton.jsx
118 lines (103 loc) · 3.02 KB
/
button.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
import React from 'react';
import { Set as ImmutableSet, Map as ImmutableMap } from 'immutable';
import classnames from 'classnames';
import { MDCRipple, MDCRippleFoundation } from '@material/ripple';
import { getMatchesProperty } from '@material/ripple/util';
import '@material/button/dist/mdc.button.css';
const MATCHES = getMatchesProperty(HTMLElement.prototype);
export default class Button extends React.PureComponent {
static propTypes = {
dense: React.PropTypes.bool,
raised: React.PropTypes.bool,
compact: React.PropTypes.bool,
primary: React.PropTypes.bool,
accent: React.PropTypes.bool,
disabled: React.PropTypes.bool,
}
static defaultProps = {
dense: false,
raised: true,
compact: false,
primary: true,
accent: false,
disabled: false,
}
state = {
classes: new ImmutableSet(),
rippleCss: new ImmutableMap(),
}
componentDidMount() {
this.rippleFoundation.init();
}
componentDidUpdate() {
this.state.rippleCss.forEach((v, k) => {
this.refs.root.style.setProperty(k, v);
});
}
componentWillUnmount() {
this.rippleFoundation.destroy();
}
render() {
return (
<button
ref="root"
className={
classnames(this.state.classes.toJS(), 'mdc-button', {
'mdc-button--dense': this.props.dense,
'mdc-button--raised': this.props.raised,
'mdc-button--compact': this.props.compact,
'mdc-button--primary': this.props.primary,
'mdc-button--accent': this.props.accent,
})
}
disabled={this.props.disabled}
>
{this.props.children}
</button>
);
}
rippleFoundation = new MDCRippleFoundation(
Object.assign(MDCRipple.createAdapter(this), {
isUnbounded: () => false,
isSurfaceActive: () => this.refs.root[MATCHES](':active'),
addClass: (className) => {
this.setState(prevState => ({
classes: prevState.classes.add(className),
}));
},
removeClass: (className) => {
this.setState(prevState => ({
classes: prevState.classes.remove(className),
}));
},
registerInteractionHandler: (type, handler) => {
if (this.refs.root) {
this.refs.root.addEventListener(type, handler);
}
},
deregisterInteractionHandler: (type, handler) => {
if (this.refs.root) {
this.refs.root.removeEventListener(type, handler);
}
},
updateCssVariable: (varName, value) => {
this.setState(prevState => ({
rippleCss: prevState.rippleCss.set(varName, value),
}));
},
computeBoundingRect: () => this.refs.root.getBoundingClientRect(),
}));
}
function filterUnusedProps(props, usedProps) {
const empty = {};
for (const key in props) {
if (!props.hasOwnProperty(key)) {
continue;
}
if (usedProps.hasOwnProperty(key)) {
continue;
}
empty[key] = props[key];
}
return empty;
}