Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix escape events not stopping propagation to customizer
  • Loading branch information
kevin940726 committed May 26, 2021
commit c118bd8576b7067df2d0315c86b9d6c9c831f949
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export default function useTabNav() {
const ref = useRefEffect( ( node ) => {
function onKeyDown( event ) {
if ( event.keyCode === ESCAPE && ! hasMultiSelection() ) {
event.stopPropagation();
setNavigationMode( true );
return;
}
Expand Down
30 changes: 19 additions & 11 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useRef,
useState,
useLayoutEffect,
useEffect,
forwardRef,
} from '@wordpress/element';
import { getRectangleFromRange } from '@wordpress/dom';
Expand Down Expand Up @@ -490,18 +491,25 @@ const Popover = (
] );

// Event handlers
const maybeClose = ( event ) => {
// Close on escape
if ( event.keyCode === ESCAPE && onClose ) {
event.stopPropagation();
onClose();
}
useEffect( () => {
const container = containerRef.current;

if ( container ) {
function maybeClose( event ) {
// Close on escape
if ( event.keyCode === ESCAPE && onClose ) {
event.stopPropagation();
onClose();
}
}

// Preserve original content prop behavior
if ( onKeyDown ) {
onKeyDown( event );
container.addEventListener( 'keydown', maybeClose );

return () => {
container.removeEventListener( 'keydown', maybeClose );
};
}
};
}, [ onClose ] );

/**
* Shims an onFocusOutside callback to be compatible with a deprecated
Expand Down Expand Up @@ -587,7 +595,7 @@ const Popover = (
}
) }
{ ...contentProps }
onKeyDown={ maybeClose }
onKeyDown={ onKeyDown }
{ ...focusOutsideProps }
ref={ mergedRefs }
tabIndex="-1"
Expand Down