-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the inserter behave as a popover
- Loading branch information
1 parent
d09ff88
commit faf22d8
Showing
3 changed files
with
95 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/edit-post/src/components/layout/popover-wrapper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
withConstrainedTabbing, | ||
withFocusReturn, | ||
withFocusOutside, | ||
} from '@wordpress/components'; | ||
import { Component } from '@wordpress/element'; | ||
import { ESCAPE } from '@wordpress/keycodes'; | ||
|
||
function stopPropagation( event ) { | ||
event.stopPropagation(); | ||
} | ||
|
||
const DetectOutside = withFocusOutside( | ||
class extends Component { | ||
handleFocusOutside( event ) { | ||
this.props.onFocusOutside( event ); | ||
} | ||
|
||
render() { | ||
return this.props.children; | ||
} | ||
} | ||
); | ||
|
||
const FocusManaged = withConstrainedTabbing( | ||
withFocusReturn( ( { children } ) => children ) | ||
); | ||
|
||
export default function PopoverWrapper( { onClose, children } ) { | ||
// Event handlers | ||
const maybeClose = ( event ) => { | ||
// Close on escape | ||
if ( event.keyCode === ESCAPE && onClose ) { | ||
event.stopPropagation(); | ||
onClose(); | ||
} | ||
}; | ||
|
||
// Disable reason: this stops certain events from propagating outside of the component. | ||
// - onMouseDown is disabled as this can cause interactions with other DOM elements | ||
/* eslint-disable jsx-a11y/no-static-element-interactions */ | ||
return ( | ||
<div onKeyDown={ maybeClose } onMouseDown={ stopPropagation }> | ||
<DetectOutside onFocusOutside={ onClose }> | ||
<FocusManaged>{ children }</FocusManaged> | ||
</DetectOutside> | ||
</div> | ||
); | ||
/* eslint-enable jsx-a11y/no-static-element-interactions */ | ||
} |