Skip to content

Commit

Permalink
[react-ui] usePress from useKeyboard and useTap (#16772)
Browse files Browse the repository at this point in the history
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'.
  • Loading branch information
necolas authored Sep 16, 2019
1 parent 494300b commit 3af05de
Show file tree
Hide file tree
Showing 26 changed files with 2,364 additions and 1,438 deletions.
4 changes: 2 additions & 2 deletions packages/react-ui/accessibility/src/FocusGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* @flow
*/

import type {KeyboardEvent} from 'react-ui/events/src/dom/Keyboard';
import type {KeyboardEvent} from 'react-ui/events/keyboard';

import React from 'react';
import {tabFocusableImpl} from './TabbableScope';
import {useKeyboard} from '../../events/keyboard';
import {useKeyboard} from 'react-ui/events/keyboard';

type GridComponentProps = {
children: React.Node,
Expand Down
5 changes: 3 additions & 2 deletions packages/react-ui/accessibility/src/ReactTabFocus.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
*/

import type {ReactScopeMethods} from 'shared/ReactTypes';
import type {KeyboardEvent} from 'react-ui/events/src/dom/Keyboard';
import type {KeyboardEvent} from 'react-ui/events/keyboard';

import React from 'react';
import {TabbableScope} from './TabbableScope';
import {useKeyboard} from '../../events/keyboard';
import {useKeyboard} from 'react-ui/events/keyboard';

type TabFocusControllerProps = {
children: React.Node,
contain?: boolean,
};

const {useRef} = React;

function getTabbableNodes(scope: ReactScopeMethods) {
Expand Down
12 changes: 12 additions & 0 deletions packages/react-ui/events/press-legacy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

module.exports = require('./src/dom/PressLegacy');
11 changes: 7 additions & 4 deletions packages/react-ui/events/src/dom/Keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type KeyboardProps = {|
onClick?: (e: KeyboardEvent) => ?boolean,
onKeyDown?: (e: KeyboardEvent) => ?boolean,
onKeyUp?: (e: KeyboardEvent) => ?boolean,
preventClick?: boolean,
preventKeys?: PreventKeysArray,
|};

Expand Down Expand Up @@ -256,6 +257,12 @@ const keyboardResponderImpl = {
);
}
} else if (type === 'click' && isVirtualClick(event)) {
if (props.preventClick !== false) {
// 'click' occurs before or after 'keyup', and may need native
// behavior prevented
nativeEvent.preventDefault();
state.defaultPrevented = true;
}
const onClick = props.onClick;
if (onClick != null) {
dispatchKeyboardEvent(
Expand All @@ -266,10 +273,6 @@ const keyboardResponderImpl = {
state.defaultPrevented,
);
}
if (state.defaultPrevented && !nativeEvent.defaultPrevented) {
// 'click' occurs before 'keyup' and may need native behavior prevented
nativeEvent.preventDefault();
}
} else if (type === 'keyup') {
state.isActive = false;
const onKeyUp = props.onKeyUp;
Expand Down
Loading

0 comments on commit 3af05de

Please sign in to comment.