-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
raised-button.jsx
218 lines (193 loc) · 6.85 KB
/
raised-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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
var React = require('react');
var StylePropable = require('./mixins/style-propable');
var Transitions = require('./styles/transitions');
var ColorManipulator = require('./utils/color-manipulator');
var Typography = require('./styles/typography');
var EnhancedButton = require('./enhanced-button');
var Paper = require('./paper');
var RaisedButton = React.createClass({
mixins: [StylePropable],
contextTypes: {
muiTheme: React.PropTypes.object
},
propTypes: {
className: React.PropTypes.string,
disabled: React.PropTypes.bool,
label: function(props, propName, componentName){
if (!props.children && !props.label) {
return new Error('Warning: Required prop `label` or `children` was not specified in `'+ componentName + '`.')
}
},
onMouseDown: React.PropTypes.func,
onMouseUp: React.PropTypes.func,
onMouseOut: React.PropTypes.func,
onTouchEnd: React.PropTypes.func,
onTouchStart: React.PropTypes.func,
primary: React.PropTypes.bool,
secondary: React.PropTypes.bool,
labelStyle: React.PropTypes.object,
},
getInitialState: function() {
var zDepth = this.props.disabled ? 0 : 1;
return {
zDepth: zDepth,
initialZDepth: zDepth,
hovered: false
};
},
componentWillReceiveProps: function(nextProps) {
var zDepth = nextProps.disabled ? 0 : 1;
this.setState({
zDepth: zDepth,
initialZDepth: zDepth,
});
},
_getBackgroundColor: function() {
return this.props.disabled ? this.getTheme().disabledColor :
this.props.primary ? this.getTheme().primaryColor :
this.props.secondary ? this.getTheme().secondaryColor :
this.getTheme().color;
},
_getLabelColor: function() {
return this.props.disabled ? this.getTheme().disabledTextColor :
this.props.primary ? this.getTheme().primaryTextColor :
this.props.secondary ? this.getTheme().secondaryTextColor :
this.getTheme().textColor;
},
getThemeButton: function() {
return this.context.muiTheme.component.button;
},
getTheme: function() {
return this.context.muiTheme.component.raisedButton;
},
getStyles: function() {
var amount = (this.props.primary || this.props.secondary) ? 0.4 : 0.08;
var styles = {
root: {
display: 'inline-block',
minWidth: this.getThemeButton().minWidth,
height: this.getThemeButton().height,
transition: Transitions.easeOut()
},
container: {
position: 'relative',
height: '100%',
width: '100%',
padding: 0,
overflow: 'hidden',
borderRadius: 2,
transition: Transitions.easeOut(),
backgroundColor: this._getBackgroundColor(),
//This is need so that ripples do not bleed
//past border radius.
//See: http://stackoverflow.com/questions/17298739/css-overflow-hidden-not-working-in-chrome-when-parent-has-border-radius-and-chil
transform: 'translate3d(0, 0, 0)'
},
label: {
position: 'relative',
opacity: 1,
fontSize: '14px',
letterSpacing: 0,
textTransform: 'uppercase',
fontWeight: Typography.fontWeightMedium,
margin: 0,
padding: '0px ' + this.context.muiTheme.spacing.desktopGutterLess + 'px',
userSelect: 'none',
lineHeight: this.getThemeButton().height + 'px',
color: this._getLabelColor(),
},
overlay: {
transition: Transitions.easeOut(),
top: 0
},
overlayWhenHovered: {
backgroundColor: ColorManipulator.fade(this._getLabelColor(), amount)
}
};
return styles;
},
render: function() {
var {
label,
primary,
secondary,
...other } = this.props;
var styles = this.getStyles();
var labelElement;
if (label) {
labelElement = (
<span style={this.mergeAndPrefix(styles.label, this.props.labelStyle)}>
{label}
</span>
);
}
var rippleColor = styles.label.color;
var rippleOpacity = !(primary || secondary) ? 0.1 : 0.16;
return (
<Paper
style={this.mergeAndPrefix(styles.root, this.props.style)}
zDepth={this.state.zDepth}>
<EnhancedButton {...other}
ref="container"
style={this.mergeAndPrefix(styles.container)}
onMouseUp={this._handleMouseUp}
onMouseDown={this._handleMouseDown}
onMouseOut={this._handleMouseOut}
onMouseOver={this._handleMouseOver}
onTouchStart={this._handleTouchStart}
onTouchEnd={this._handleTouchEnd}
focusRippleColor={rippleColor}
touchRippleColor={rippleColor}
focusRippleOpacity={rippleOpacity}
touchRippleOpacity={rippleOpacity}
onKeyboardFocus={this._handleKeyboardFocus}>
<div ref="overlay" style={this.mergeAndPrefix(
styles.overlay,
(this.state.hovered && !this.props.disabled) && styles.overlayWhenHovered
)}>
{labelElement}
{this.props.children}
</div>
</EnhancedButton>
</Paper>
);
},
_handleMouseDown: function(e) {
//only listen to left clicks
if (e.button === 0) {
this.setState({ zDepth: this.state.initialZDepth + 1 });
}
if (this.props.onMouseDown) this.props.onMouseDown(e);
},
_handleMouseUp: function(e) {
this.setState({ zDepth: this.state.initialZDepth });
if (this.props.onMouseUp) this.props.onMouseUp(e);
},
_handleMouseOut: function(e) {
if (!this.refs.container.isKeyboardFocused()) this.setState({ zDepth: this.state.initialZDepth, hovered: false });
if (this.props.onMouseOut) this.props.onMouseOut(e);
},
_handleMouseOver: function(e) {
if (!this.refs.container.isKeyboardFocused()) this.setState({hovered: true});
if (this.props.onMouseOver) this.props.onMouseOver(e);
},
_handleTouchStart: function(e) {
this.setState({ zDepth: this.state.initialZDepth + 1 });
if (this.props.onTouchStart) this.props.onTouchStart(e);
},
_handleTouchEnd: function(e) {
this.setState({ zDepth: this.state.initialZDepth });
if (this.props.onTouchEnd) this.props.onTouchEnd(e);
},
_handleKeyboardFocus: function(e, keyboardFocused) {
if (keyboardFocused && !this.props.disabled) {
this.setState({ zDepth: this.state.initialZDepth + 1 });
var amount = (this.props.primary || this.props.secondary) ? 0.4 : 0.08;
React.findDOMNode(this.refs.overlay).style.backgroundColor = ColorManipulator.fade(this.mergeAndPrefix(this.getStyles().label, this.props.labelStyle).color, amount);
} else if (!this.state.hovered) {
this.setState({ zDepth: this.state.initialZDepth });
React.findDOMNode(this.refs.overlay).style.backgroundColor = 'transparent';
}
},
});
module.exports = RaisedButton;