15
15
// NOTICE file in the root directory of this source tree.
16
16
// See https://github.com/facebook/react/tree/cc7c1aece46a6b69b41958d731e0fd27c94bfc6c/packages/react-interactions
17
17
18
+ import { chain , focusWithoutScrolling , getOwnerDocument , getOwnerWindow , isMac , isVirtualClick , isVirtualPointerEvent , mergeProps , openLink , useEffectEvent , useGlobalListeners , useSyncRef } from '@react-aria/utils' ;
18
19
import { disableTextSelection , restoreTextSelection } from './textSelection' ;
19
20
import { DOMAttributes , FocusableElement , PressEvent as IPressEvent , PointerType , PressEvents } from '@react-types/shared' ;
20
- import { focusWithoutScrolling , getOwnerDocument , getOwnerWindow , isMac , isVirtualClick , isVirtualPointerEvent , mergeProps , openLink , useEffectEvent , useGlobalListeners , useSyncRef } from '@react-aria/utils' ;
21
21
import { PressResponderContext } from './context' ;
22
22
import { RefObject , useContext , useEffect , useMemo , useRef , useState } from 'react' ;
23
23
@@ -270,8 +270,16 @@ export function usePress(props: PressHookProps): PressResult {
270
270
shouldStopPropagation = triggerPressStart ( e , 'keyboard' ) ;
271
271
272
272
// Focus may move before the key up event, so register the event on the document
273
- // instead of the same element where the key down event occurred.
274
- addGlobalListener ( getOwnerDocument ( e . currentTarget ) , 'keyup' , onKeyUp , false ) ;
273
+ // instead of the same element where the key down event occurred. Make it capturing so that it will trigger
274
+ // before stopPropagation from useKeyboard on a child element may happen and thus we can still call triggerPress for the parent element.
275
+ let originalTarget = e . currentTarget ;
276
+ let pressUp = ( e ) => {
277
+ if ( isValidKeyboardEvent ( e , originalTarget ) && ! e . repeat && originalTarget . contains ( e . target as Element ) && state . target ) {
278
+ triggerPressUp ( createEvent ( state . target , e ) , 'keyboard' ) ;
279
+ }
280
+ } ;
281
+
282
+ addGlobalListener ( getOwnerDocument ( e . currentTarget ) , 'keyup' , chain ( pressUp , onKeyUp ) , true ) ;
275
283
}
276
284
277
285
if ( shouldStopPropagation ) {
@@ -292,11 +300,6 @@ export function usePress(props: PressHookProps): PressResult {
292
300
state . metaKeyEvents = new Map ( ) ;
293
301
}
294
302
} ,
295
- onKeyUp ( e ) {
296
- if ( isValidKeyboardEvent ( e . nativeEvent , e . currentTarget ) && ! e . repeat && e . currentTarget . contains ( e . target as Element ) && state . target ) {
297
- triggerPressUp ( createEvent ( state . target , e ) , 'keyboard' ) ;
298
- }
299
- } ,
300
303
onClick ( e ) {
301
304
if ( e && ! e . currentTarget . contains ( e . target as Element ) ) {
302
305
return ;
@@ -338,13 +341,9 @@ export function usePress(props: PressHookProps): PressResult {
338
341
}
339
342
340
343
let target = e . target as Element ;
341
- let shouldStopPropagation = triggerPressEnd ( createEvent ( state . target , e ) , 'keyboard' , state . target . contains ( target ) ) ;
344
+ triggerPressEnd ( createEvent ( state . target , e ) , 'keyboard' , state . target . contains ( target ) ) ;
342
345
removeAllGlobalListeners ( ) ;
343
346
344
- if ( shouldStopPropagation ) {
345
- e . stopPropagation ( ) ;
346
- }
347
-
348
347
// If a link was triggered with a key other than Enter, open the URL ourselves.
349
348
// This means the link has a role override, and the default browser behavior
350
349
// only applies when using the Enter key.
0 commit comments