Skip to content

Commit 3af05de

Browse files
author
Nicolas Gallagher
authored
[react-ui] usePress from useKeyboard and useTap (#16772)
This implements 'usePress' in user-space as a combination of 'useKeyboard' and 'useTap'. The existing 'usePress' API is preserved for now. The previous 'usePress' implementation is moved to 'PressLegacy'.
1 parent 494300b commit 3af05de

26 files changed

Lines changed: 2364 additions & 1438 deletions

‎packages/react-ui/accessibility/src/FocusGrid.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
* @flow
88
*/
99

10-
importtype{KeyboardEvent}from'react-ui/events/src/dom/Keyboard';
10+
importtype{KeyboardEvent}from'react-ui/events/keyboard';
1111

1212
importReactfrom'react';
1313
import{tabFocusableImpl}from'./TabbableScope';
14-
import{useKeyboard}from'../../events/keyboard';
14+
import{useKeyboard}from'react-ui/events/keyboard';
1515

1616
typeGridComponentProps={
1717
children: React.Node,

‎packages/react-ui/accessibility/src/ReactTabFocus.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
*/
99

1010
importtype{ReactScopeMethods}from'shared/ReactTypes';
11-
importtype{KeyboardEvent}from'react-ui/events/src/dom/Keyboard';
11+
importtype{KeyboardEvent}from'react-ui/events/keyboard';
1212

1313
importReactfrom'react';
1414
import{TabbableScope}from'./TabbableScope';
15-
import{useKeyboard}from'../../events/keyboard';
15+
import{useKeyboard}from'react-ui/events/keyboard';
1616

1717
typeTabFocusControllerProps={
1818
children: React.Node,
1919
contain?: boolean,
2020
};
21+
2122
const{useRef}=React;
2223

2324
functiongetTabbableNodes(scope: ReactScopeMethods){
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
'use strict';
11+
12+
module.exports=require('./src/dom/PressLegacy');

‎packages/react-ui/events/src/dom/Keyboard.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type KeyboardProps = {|
2727
onClick?: (e: KeyboardEvent)=> ?boolean,
2828
onKeyDown?: (e: KeyboardEvent)=> ?boolean,
2929
onKeyUp?: (e: KeyboardEvent)=> ?boolean,
30+
preventClick?: boolean,
3031
preventKeys?: PreventKeysArray,
3132
|};
3233

@@ -256,6 +257,12 @@ const keyboardResponderImpl = {
256257
);
257258
}
258259
}elseif(type==='click'&&isVirtualClick(event)){
260+
if(props.preventClick!==false){
261+
// 'click' occurs before or after 'keyup', and may need native
262+
// behavior prevented
263+
nativeEvent.preventDefault();
264+
state.defaultPrevented=true;
265+
}
259266
constonClick=props.onClick;
260267
if(onClick!=null){
261268
dispatchKeyboardEvent(
@@ -266,10 +273,6 @@ const keyboardResponderImpl = {
266273
state.defaultPrevented,
267274
);
268275
}
269-
if(state.defaultPrevented&&!nativeEvent.defaultPrevented){
270-
// 'click' occurs before 'keyup' and may need native behavior prevented
271-
nativeEvent.preventDefault();
272-
}
273276
}elseif(type==='keyup'){
274277
state.isActive=false;
275278
constonKeyUp=props.onKeyUp;

0 commit comments

Comments
 (0)