Skip to content

Commit b0821db

Browse files
committed
Improved performance of raised button touch ripple
1 parent 8416732 commit b0821db

File tree

2 files changed

+55
-41
lines changed

2 files changed

+55
-41
lines changed

src/js/raised-button.jsx

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
var React = require('react');
2-
var CssEvent = require('./utils/css-event.js');
32
var Classable = require('./mixins/classable.js');
43
var EnhancedButton = require('./enhanced-button.jsx');
54
var Paper = require('./paper.jsx');
65
var Ripple = require('./ripple.jsx');
6+
var TouchRipple = require('./ripples/touch-ripple.jsx');
77

88
var RaisedButton = React.createClass({
99

@@ -12,7 +12,10 @@ var RaisedButton = React.createClass({
1212
propTypes: {
1313
className: React.PropTypes.string,
1414
label: React.PropTypes.string.isRequired,
15-
onTouchTap: React.PropTypes.func,
15+
onMouseDown: React.PropTypes.func,
16+
onMouseUp: React.PropTypes.func,
17+
onTouchEnd: React.PropTypes.func,
18+
onTouchStart: React.PropTypes.func,
1619
primary: React.PropTypes.bool,
1720
secondary: React.PropTypes.bool
1821
},
@@ -27,22 +30,28 @@ var RaisedButton = React.createClass({
2730

2831
render: function() {
2932
var {
30-
className,
31-
onTouchTap,
32-
...other } = this.props,
33-
classes = this.getClasses('mui-raised-button', {
34-
'mui-is-primary': this.props.primary,
35-
'mui-is-secondary': !this.props.primary && this.props.secondary
36-
});
33+
label,
34+
primary,
35+
secondary,
36+
...other } = this.props;
37+
var classes = this.getClasses('mui-raised-button', {
38+
'mui-is-primary': this.props.primary,
39+
'mui-is-secondary': !this.props.primary && this.props.secondary
40+
});
3741

3842
return (
3943
<Paper className={classes} zDepth={this.state.zDepth}>
40-
<EnhancedButton
41-
{...other}
44+
<EnhancedButton {...other}
4245
className="mui-raised-button-container"
43-
onTouchTap={this._onTouchTap}>
46+
onMouseUp={this._handleMouseUp}
47+
onMouseDown={this._handleMouseDown}
48+
onTouchStart={this._handleTouchStart}
49+
onTouchEnd={this._handleTouchEnd}>
50+
51+
<TouchRipple
52+
className="mui-raised-button-ripple"
53+
ref="touchRipple" />
4454

45-
<Ripple ref="ripple" className="mui-raised-button-ripple" />
4655
<Ripple className="mui-raised-button-focus-ripple" />
4756
<span className="mui-raised-button-label">{this.props.label}</span>
4857

@@ -51,22 +60,31 @@ var RaisedButton = React.createClass({
5160
);
5261
},
5362

54-
_onTouchTap: function(e) {
55-
if (!this.props.disabled) this._animateButtonClick(e);
56-
if (this.props.onTouchTap) this.props.onTouchTap(e);
63+
_handleMouseDown: function(e) {
64+
//only listen to left clicks
65+
if (e.button === 0) {
66+
this.refs.touchRipple.start(e);
67+
this.setState({ zDepth: this.state.initialZDepth + 1 });
68+
}
69+
if (this.props.onMouseDown) this.props.onMouseDown(e);
5770
},
5871

59-
_animateButtonClick: function(e) {
60-
var el = this.getDOMNode();
61-
62-
//animate the ripple
63-
this.refs.ripple.animate(e);
72+
_handleMouseUp: function(e) {
73+
this.refs.touchRipple.end();
74+
this.setState({ zDepth: this.state.initialZDepth });
75+
if (this.props.onMouseUp) this.props.onMouseUp(e);
76+
},
6477

65-
//animate the zdepth change
78+
_handleTouchStart: function(e) {
79+
this.refs.touchRipple.start(e);
6680
this.setState({ zDepth: this.state.initialZDepth + 1 });
67-
setTimeout(function() {
68-
this.setState({ zDepth: this.state.initialZDepth });
69-
}.bind(this), 450);
81+
if (this.props.onTouchStart) this.props.onTouchStart(e);
82+
},
83+
84+
_handleTouchEnd: function(e) {
85+
this.refs.touchRipple.end();
86+
this.setState({ zDepth: this.state.initialZDepth });
87+
if (this.props.onTouchEnd) this.props.onTouchEnd(e);
7088
}
7189

7290
});

src/less/components/raised-button.less

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
min-width: @button-min-width;
55
height: @button-height;
66

7-
* { .ease-out(); }
7+
&, * { .ease-out(); }
88

99
.mui-raised-button-container {
1010
border-radius: 2px;
@@ -14,6 +14,11 @@
1414
position: relative;
1515
width: 100%;
1616

17+
//This is need so that ripples do not bleed
18+
//past border radius.
19+
//See: http://stackoverflow.com/questions/17298739/css-overflow-hidden-not-working-in-chrome-when-parent-has-border-radius-and-chil
20+
transform: translate3d(0, 0, 0);
21+
1722
.mui-raised-button-focus-ripple {
1823
background-color: @transparent;
1924
}
@@ -29,11 +34,8 @@
2934
}
3035

3136
.mui-raised-button-ripple {
32-
&.mui-is-visible {
33-
.ripple-click-alt(
34-
@animation-name: raised-button-ripple-click;
35-
@color: @raised-button-ripple-color;
36-
)
37+
.mui-ripple-circle-inner {
38+
background-color: @raised-button-ripple-color;
3739
}
3840
}
3941

@@ -72,11 +74,8 @@
7274
background-color: @raised-button-primary-color;
7375

7476
.mui-raised-button-ripple {
75-
&.mui-is-visible {
76-
.ripple-click-alt(
77-
@animation-name: raised-button-primary-ripple-click;
78-
@color: @raised-button-primary-ripple-color;
79-
)
77+
.mui-ripple-circle-inner {
78+
background-color: @raised-button-primary-ripple-color;
8079
}
8180
}
8281

@@ -106,11 +105,8 @@
106105
background-color: @raised-button-secondary-color;
107106

108107
.mui-raised-button-ripple {
109-
&.mui-is-visible {
110-
.ripple-click-alt(
111-
@animation-name: raised-button-secondary-ripple-click;
112-
@color: @raised-button-secondary-ripple-color;
113-
)
108+
.mui-ripple-circle-inner {
109+
background-color: @raised-button-secondary-ripple-color;
114110
}
115111
}
116112

0 commit comments

Comments
 (0)