-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
89 lines (85 loc) · 2.24 KB
/
index.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
86
87
88
89
/**
* WordPress dependencies
*/
import { Button } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import BlockTitle from '../block-title';
import { store as blockEditorStore } from '../../store';
/**
* Block breadcrumb component, displaying the hierarchy of the current block selection as a breadcrumb.
*
* @return {WPElement} Block Breadcrumb.
*/
function BlockBreadcrumb() {
const { selectBlock, clearSelectedBlock } = useDispatch( blockEditorStore );
const { clientId, parents, hasSelection } = useSelect( ( select ) => {
const {
getSelectionStart,
getSelectedBlockClientId,
getBlockParents,
} = select( blockEditorStore );
const selectedBlockClientId = getSelectedBlockClientId();
return {
parents: getBlockParents( selectedBlockClientId ),
clientId: selectedBlockClientId,
hasSelection: !! getSelectionStart().clientId,
};
}, [] );
/*
* Disable reason: The `list` ARIA role is redundant but
* Safari+VoiceOver won't announce the list otherwise.
*/
/* eslint-disable jsx-a11y/no-redundant-roles */
return (
<ul
className="block-editor-block-breadcrumb"
role="list"
aria-label={ __( 'Block breadcrumb' ) }
>
<li
className={
! hasSelection
? 'block-editor-block-breadcrumb__current'
: undefined
}
aria-current={ ! hasSelection ? 'true' : undefined }
>
{ hasSelection && (
<Button
className="block-editor-block-breadcrumb__button"
isTertiary
onClick={ clearSelectedBlock }
>
{ __( 'Document' ) }
</Button>
) }
{ ! hasSelection && __( 'Document' ) }
</li>
{ parents.map( ( parentClientId ) => (
<li key={ parentClientId }>
<Button
className="block-editor-block-breadcrumb__button"
isTertiary
onClick={ () => selectBlock( parentClientId ) }
>
<BlockTitle clientId={ parentClientId } />
</Button>
</li>
) ) }
{ !! clientId && (
<li
className="block-editor-block-breadcrumb__current"
aria-current="true"
>
<BlockTitle clientId={ clientId } />
</li>
) }
</ul>
/* eslint-enable jsx-a11y/no-redundant-roles */
);
}
export default BlockBreadcrumb;