@@ -116,11 +116,15 @@ const Transitions = Object.freeze({
116
116
}
117
117
} ) ;
118
118
119
+ const getElementRole = ( element ) = > element . getAttribute ( 'role' ) ;
120
+
121
+ const getElementType = ( element ) => element . tagName . toLowerCase ( ) ;
122
+
119
123
const isActiveSignal = ( signal ) =>
120
124
signal === RESPONDER_ACTIVE_PRESS_START ||
121
125
signal === RESPONDER_ACTIVE_LONG_PRESS_START ;
122
126
123
- const isButtonRole = ( element ) => element . getAttribute ( 'role' ) === 'button' ;
127
+ const isButtonRole = ( element ) => getElementRole ( element ) === 'button' ;
124
128
125
129
const isPressStartSignal = ( signal ) =>
126
130
signal === RESPONDER_INACTIVE_PRESS_START ||
@@ -132,10 +136,10 @@ const isTerminalSignal = (signal) =>
132
136
133
137
const isValidKeyPress = ( event ) => {
134
138
const { key, target } = event ;
135
- const role = target . getAttribute ( 'role' ) ;
136
139
const isSpacebar = key === ' ' || key === 'Spacebar' ;
137
-
138
- return key === 'Enter' || ( isSpacebar && role === 'button' ) ;
140
+ const isButtonish =
141
+ getElementType ( target ) === 'button' || isButtonRole ( target ) ;
142
+ return key === 'Enter' || ( isSpacebar && isButtonish ) ;
139
143
} ;
140
144
141
145
const DEFAULT_LONG_PRESS_DELAY_MS = 450 ; // 500 - 50
@@ -307,7 +311,7 @@ export default class PressResponder {
307
311
document . removeEventListener ( 'keyup' , keyupHandler ) ;
308
312
309
313
const role = target . getAttribute ( 'role' ) ;
310
- const elementType = target . tagName . toLowerCase ( ) ;
314
+ const elementType = getElementType ( target ) ;
311
315
312
316
const isNativeInteractiveElement =
313
317
role === 'link' ||
@@ -345,11 +349,15 @@ export default class PressResponder {
345
349
// focus is moved to another element during 'keydown'.
346
350
document . addEventListener ( 'keyup' , keyupHandler ) ;
347
351
}
348
- const role = target . getAttribute ( 'role' ) ;
349
352
const isSpacebarKey = key === ' ' || key === 'Spacebar' ;
350
- const isButtonRole = role === 'button' || role === 'menuitem' ;
351
- if ( isSpacebarKey && isButtonRole ) {
352
- // Prevent spacebar scrolling the window
353
+ const role = getElementRole ( target ) ;
354
+ const isButtonLikeRole = role === 'button' || role === 'menuitem' ;
355
+ if (
356
+ isSpacebarKey &&
357
+ isButtonLikeRole &&
358
+ getElementType ( target ) !== 'button'
359
+ ) {
360
+ // Prevent spacebar scrolling the window if using non-native button
353
361
event . preventDefault ( ) ;
354
362
}
355
363
event . stopPropagation ( ) ;
0 commit comments