Skip to content

Commit 3af05de

Browse files
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

+2364
-1438
lines changed

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-
import type {KeyboardEvent} from 'react-ui/events/src/dom/Keyboard';
10+
import type {KeyboardEvent} from 'react-ui/events/keyboard';
1111

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

1616
type GridComponentProps = {
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
import type {ReactScopeMethods} from 'shared/ReactTypes';
11-
import type {KeyboardEvent} from 'react-ui/events/src/dom/Keyboard';
11+
import type {KeyboardEvent} from 'react-ui/events/keyboard';
1212

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

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

2324
function getTabbableNodes(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
} else if (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
const onClick = 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
} else if (type === 'keyup') {
274277
state.isActive = false;
275278
const onKeyUp = props.onKeyUp;

0 commit comments

Comments
 (0)