-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
list-view-sidebar.js
68 lines (61 loc) · 2.08 KB
/
list-view-sidebar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* WordPress dependencies
*/
import { __experimentalListView as ListView } from '@wordpress/block-editor';
import { Button } from '@wordpress/components';
import { useFocusOnMount, useMergeRefs } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { useCallback, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { closeSmall } from '@wordpress/icons';
import { ESCAPE } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import { store as editWidgetsStore } from '../../store';
import { unlock } from '../../lock-unlock';
export default function ListViewSidebar() {
const { setIsListViewOpened } = useDispatch( editWidgetsStore );
const { getListViewToggleRef } = unlock( useSelect( editWidgetsStore ) );
// Use internal state instead of a ref to make sure that the component
// re-renders when the dropZoneElement updates.
const [ dropZoneElement, setDropZoneElement ] = useState( null );
const focusOnMountRef = useFocusOnMount( 'firstElement' );
// When closing the list view, focus should return to the toggle button.
const closeListView = useCallback( () => {
setIsListViewOpened( false );
getListViewToggleRef().current?.focus();
}, [ getListViewToggleRef, setIsListViewOpened ] );
const closeOnEscape = useCallback(
( event ) => {
if ( event.keyCode === ESCAPE && ! event.defaultPrevented ) {
event.preventDefault();
closeListView();
}
},
[ closeListView ]
);
return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div
className="edit-widgets-editor__list-view-panel"
onKeyDown={ closeOnEscape }
>
<div className="edit-widgets-editor__list-view-panel-header">
<strong>{ __( 'List View' ) }</strong>
<Button
icon={ closeSmall }
label={ __( 'Close' ) }
onClick={ closeListView }
size="compact"
/>
</div>
<div
className="edit-widgets-editor__list-view-panel-content"
ref={ useMergeRefs( [ focusOnMountRef, setDropZoneElement ] ) }
>
<ListView dropZoneElement={ dropZoneElement } />
</div>
</div>
);
}