Skip to content

Commit 0968696

Browse files
committed
Merge pull request mui#1485 from hai-cea/enhanced-button-tabpresses
[EnhancedButton] Listen to tab presses on the window
2 parents 365559f + ae90552 commit 0968696

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/enhanced-button.jsx

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,41 @@ const PureRenderMixin = React.addons.PureRenderMixin;
33
const StylePropable = require('./mixins/style-propable');
44
const Colors = require('./styles/colors');
55
const Children = require('./utils/children');
6+
const Events = require('./utils/events');
67
const KeyCode = require('./utils/key-code');
78
const FocusRipple = require('./ripples/focus-ripple');
89
const TouchRipple = require('./ripples/touch-ripple');
910

10-
1111
let styleInjected = false;
12+
let listening = false;
1213
let tabPressed = false;
1314

15+
function injectStyle() {
16+
if (!styleInjected) {
17+
// Remove inner padding and border in Firefox 4+.
18+
let style = document.createElement("style");
19+
style.innerHTML = `
20+
button::-moz-focus-inner,
21+
input::-moz-focus-inner {
22+
border: 0;
23+
padding: 0;
24+
}
25+
`;
26+
27+
document.body.appendChild(style);
28+
styleInjected = true;
29+
}
30+
}
31+
32+
function listenForTabPresses() {
33+
if (!listening) {
34+
Events.on(window, 'keydown', (e) =>{
35+
tabPressed = e.keyCode === KeyCode.TAB;
36+
});
37+
listening = true;
38+
}
39+
}
40+
1441
const EnhancedButton = React.createClass({
1542

1643
mixins: [PureRenderMixin, StylePropable],
@@ -77,20 +104,8 @@ const EnhancedButton = React.createClass({
77104
},
78105

79106
componentDidMount() {
80-
if (!styleInjected) {
81-
// Remove inner padding and border in Firefox 4+.
82-
let style = document.createElement("style");
83-
style.innerHTML = `
84-
button::-moz-focus-inner,
85-
input::-moz-focus-inner {
86-
border: 0;
87-
padding: 0;
88-
}
89-
`;
90-
91-
document.body.appendChild(style);
92-
styleInjected = true;
93-
}
107+
injectStyle();
108+
listenForTabPresses();
94109
},
95110

96111
render() {
@@ -230,9 +245,6 @@ const EnhancedButton = React.createClass({
230245

231246
_handleKeyDown(e) {
232247
if (!this.props.disabled && !this.props.disableKeyboardFocus) {
233-
if (e.keyCode === KeyCode.TAB) {
234-
tabPressed = true;
235-
}
236248
if (e.keyCode === KeyCode.ENTER && this.state.isKeyboardFocused) {
237249
this._handleTouchTap(e);
238250
}

0 commit comments

Comments
 (0)