-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathlist-view-sidebar.js
85 lines (79 loc) · 2.38 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* WordPress dependencies
*/
import {
__experimentalBlockNavigationTree as BlockNavigationTree,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { Button } from '@wordpress/components';
import {
useFocusOnMount,
useFocusReturn,
useInstanceId,
useMergeRefs,
} from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { closeSmall } from '@wordpress/icons';
import { ESCAPE } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
export default function ListViewSidebar() {
const { clientIdsTree, selectedBlockClientIds } = useSelect( ( select ) => {
const {
__unstableGetClientIdsTree,
getSelectedBlockClientIds,
} = select( blockEditorStore );
return {
clientIdsTree: __unstableGetClientIdsTree(),
selectedBlockClientIds: getSelectedBlockClientIds(),
};
}, [] );
const { setIsListViewOpened } = useDispatch( editSiteStore );
const { clearSelectedBlock, selectBlock } = useDispatch( blockEditorStore );
async function selectEditorBlock( clientId ) {
await clearSelectedBlock();
selectBlock( clientId, -1 );
}
const focusOnMountRef = useFocusOnMount( 'firstElement' );
const focusReturnRef = useFocusReturn();
function closeOnEscape( event ) {
if ( event.keyCode === ESCAPE ) {
event.stopPropagation();
setIsListViewOpened( false );
}
}
const instanceId = useInstanceId( ListViewSidebar );
const labelId = `edit-site-editor__list-view-panel-label-${ instanceId }`;
return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div
aria-labelledby={ labelId }
className="edit-site-editor__list-view-panel"
onKeyDown={ closeOnEscape }
>
<div className="edit-site-editor__list-view-panel-header">
<strong id={ labelId }>{ __( 'List view' ) }</strong>
<Button
icon={ closeSmall }
label={ __( 'Close list view sidebar' ) }
onClick={ () => setIsListViewOpened( false ) }
/>
</div>
<div
className="edit-site-editor__list-view-panel-content"
ref={ useMergeRefs( [ focusReturnRef, focusOnMountRef ] ) }
>
<BlockNavigationTree
blocks={ clientIdsTree }
selectBlock={ selectEditorBlock }
selectedBlockClientIds={ selectedBlockClientIds }
showNestedBlocks
__experimentalPersistentListViewFeatures
/>
</div>
</div>
);
}