-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (162 loc) · 5.38 KB
/
index.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
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
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import {MDCRippleFoundation, util} from '@material/ripple';
const withRipple = (WrappedComponent) => {
class RippledComponent extends Component {
foundation_ = null;
state = {
classList: new Set(),
style: {},
};
componentDidMount() {
if (!this.foundation_) {
throw new Error('You must call initRipple from the element\'s ' +
'ref prop to initialize the adapter for withRipple');
}
}
componentWillUnmount() {
if (this.foundation_) {
this.foundation_.destroy();
}
}
initializeFoundation_ = (instance) => {
const adapter = this.createAdapter_(instance);
this.foundation_ = new MDCRippleFoundation(adapter);
this.foundation_.init();
}
createAdapter_ = (instance) => {
const MATCHES = util.getMatchesProperty(HTMLElement.prototype);
return {
browserSupportsCssVars: () => util.supportsCssVariables(window),
isUnbounded: () => this.props.unbounded,
isSurfaceActive: () => instance[MATCHES](':active'),
isSurfaceDisabled: () => this.props.disabled,
addClass: (className) =>
this.setState({classList: this.state.classList.add(className)}),
removeClass: (className) => {
const {classList} = this.state;
classList.delete(className);
this.setState({classList});
},
registerDocumentInteractionHandler: (evtType, handler) =>
document.documentElement.addEventListener(evtType, handler, util.applyPassive()),
deregisterDocumentInteractionHandler: (evtType, handler) =>
document.documentElement.removeEventListener(evtType, handler, util.applyPassive()),
registerResizeHandler: (handler) => window.addEventListener('resize', handler),
deregisterResizeHandler: (handler) => window.removeEventListener('resize', handler),
updateCssVariable: this.updateCssVariable,
computeBoundingRect: () => instance.getBoundingClientRect(),
getWindowPageOffset: () => ({x: window.pageXOffset, y: window.pageYOffset}),
};
}
get classes() {
const {className: wrappedCompClasses} = this.props;
const {classList} = this.state;
return classnames(Array.from(classList), wrappedCompClasses);
}
handleMouseDown = (e) => {
this.props.onMouseDown(e);
this.activateRipple(e);
}
handleMouseUp = (e) => {
this.props.onMouseUp(e);
this.deactivateRipple(e);
}
handleTouchStart = (e) => {
this.props.onTouchStart(e);
this.activateRipple(e);
}
handleTouchEnd = (e) => {
this.props.onTouchEnd(e);
this.deactivateRipple(e);
}
handleKeyDown = (e) => {
this.props.onKeyDown(e);
this.activateRipple(e);
}
handleKeyUp = (e) => {
this.props.onKeyUp(e);
this.deactivateRipple(e);
}
activateRipple = (e) => {
// https://reactjs.org/docs/events.html#event-pooling
e.persist();
requestAnimationFrame(() => {
this.foundation_.activate(e);
});
}
deactivateRipple = (e) => {
this.foundation_.deactivate(e);
}
updateCssVariable = (varName, value) => {
const updatedStyle = Object.assign({}, this.state.style);
updatedStyle[varName] = value;
this.setState({style: updatedStyle});
}
getMergedStyles = () => {
const {style: wrappedStyle} = this.props;
const {style} = this.state;
return Object.assign({}, style, wrappedStyle);
}
render() {
const {
/* start black list of otherprops */
/* eslint-disable */
unbounded,
style,
className,
onMouseDown,
onMouseUp,
onTouchStart,
onTouchEnd,
onKeyDown,
onKeyUp,
/* eslint-enable */
/* end black list of otherprops */
...otherProps
} = this.props;
const updatedProps = Object.assign(otherProps, {
onMouseDown: this.handleMouseDown,
onMouseUp: this.handleMouseUp,
onTouchStart: this.handleTouchStart,
onTouchEnd: this.handleTouchEnd,
onKeyDown: this.handleKeyDown,
onKeyUp: this.handleKeyUp,
// call initRipple on ref on root element that needs ripple
initRipple: this.initializeFoundation_,
className: this.classes,
style: this.getMergedStyles(),
});
return <WrappedComponent {...updatedProps} />;
}
}
WrappedComponent.propTypes = Object.assign({
unbounded: PropTypes.bool,
disabled: PropTypes.bool,
style: PropTypes.object,
className: PropTypes.string,
onMouseDown: PropTypes.func,
onMouseUp: PropTypes.func,
onTouchStart: PropTypes.func,
onTouchEnd: PropTypes.func,
onKeyDown: PropTypes.func,
onKeyUp: PropTypes.func,
}, WrappedComponent.propTypes);
WrappedComponent.defaultProps = Object.assign({
unbounded: false,
disabled: false,
style: {},
className: '',
onMouseDown: () => {},
onMouseUp: () => {},
onTouchStart: () => {},
onTouchEnd: () => {},
onKeyDown: () => {},
onKeyUp: () => {},
}, WrappedComponent.defaultProps);
RippledComponent.propTypes = WrappedComponent.propTypes;
RippledComponent.defaultProps = WrappedComponent.defaultProps;
return RippledComponent;
};
export default withRipple;