Skip to content

Commit

Permalink
Add behaviour to widgets editor, too
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong committed Mar 8, 2023
1 parent f464168 commit 1d4c96f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/edit-widgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"react-native": "src/index",
"dependencies": {
"@babel/runtime": "^7.16.0",
"@wordpress/a11y": "file:../a11y",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/block-editor": "file:../block-editor",
"@wordpress/block-library": "file:../block-library",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
/**
* WordPress dependencies
*/
import { __experimentalListView as ListView } from '@wordpress/block-editor';
import {
__experimentalListView as ListView,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { Button } from '@wordpress/components';
import {
useFocusOnMount,
useFocusReturn,
useInstanceId,
useMergeRefs,
} from '@wordpress/compose';
import { useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { speak } from '@wordpress/a11y';
import { __ } from '@wordpress/i18n';
import { closeSmall } from '@wordpress/icons';
import { ESCAPE } from '@wordpress/keycodes';
Expand All @@ -21,11 +25,32 @@ import { store as editWidgetsStore } from '../../store';

export default function ListViewSidebar() {
const { setIsListViewOpened } = useDispatch( editWidgetsStore );
const { clearSelectedBlock } = useDispatch( blockEditorStore );
const { hasBlockSelection } = useSelect(
( select ) => ( {
hasBlockSelection:
!! select( blockEditorStore ).getBlockSelectionStart(),
} ),
[]
);

const focusOnMountRef = useFocusOnMount( 'firstElement' );
const headerFocusReturnRef = useFocusReturn();
const contentFocusReturnRef = useFocusReturn();
function closeOnEscape( event ) {
// If there is a block selection, then skip closing the list view
// and clear out the block selection instead.
if (
event.keyCode === ESCAPE &&
! event.defaultPrevented &&
hasBlockSelection
) {
event.preventDefault();
clearSelectedBlock();
speak( __( 'All blocks deselected.' ), 'assertive' );
return;
}

if ( event.keyCode === ESCAPE && ! event.defaultPrevented ) {
event.preventDefault();
setIsListViewOpened( false );
Expand Down

0 comments on commit 1d4c96f

Please sign in to comment.