Skip to content

Commit 200ab5a

Browse files
authored
Fix site editor region navigation (#36709)
* Implement shortcuts and labels * Use correct key for body * Fix focusing navigation sidebar when navigating regions * Only use detailed region labels when post type has loaded
1 parent 0fc1c60 commit 200ab5a

File tree

5 files changed

+139
-2
lines changed

5 files changed

+139
-2
lines changed

packages/edit-site/src/components/editor/index.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import {
2626
} from '@wordpress/editor';
2727
import { __ } from '@wordpress/i18n';
2828
import { PluginArea } from '@wordpress/plugins';
29-
import { ShortcutProvider } from '@wordpress/keyboard-shortcuts';
29+
import {
30+
ShortcutProvider,
31+
store as keyboardShortcutsStore,
32+
} from '@wordpress/keyboard-shortcuts';
3033

3134
/**
3235
* Internal dependencies
@@ -62,6 +65,8 @@ function Editor( { initialSettings, onError } ) {
6265
template,
6366
templateResolved,
6467
isNavigationOpen,
68+
previousShortcut,
69+
nextShortcut,
6570
} = useSelect( ( select ) => {
6671
const {
6772
isInserterOpened,
@@ -98,6 +103,12 @@ function Editor( { initialSettings, onError } ) {
98103
: false,
99104
entityId: postId,
100105
isNavigationOpen: isNavigationOpened(),
106+
previousShortcut: select(
107+
keyboardShortcutsStore
108+
).getAllShortcutKeyCombinations( 'core/edit-site/previous-region' ),
109+
nextShortcut: select(
110+
keyboardShortcutsStore
111+
).getAllShortcutKeyCombinations( 'core/edit-site/next-region' ),
101112
};
102113
}, [] );
103114
const { updateEditorSettings } = useDispatch( editorStore );
@@ -282,6 +293,10 @@ function Editor( { initialSettings, onError } ) {
282293
</>
283294
}
284295
footer={ <BlockBreadcrumb /> }
296+
shortcuts={ {
297+
previous: previousShortcut,
298+
next: nextShortcut,
299+
} }
285300
/>
286301
<WelcomeGuide />
287302
<Popover.Slot />

packages/edit-site/src/components/keyboard-shortcuts/index.js

+32
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,38 @@ function KeyboardShortcutsRegister() {
136136
character: ',',
137137
},
138138
} );
139+
140+
registerShortcut( {
141+
name: 'core/edit-site/next-region',
142+
category: 'global',
143+
description: __( 'Navigate to the next part of the editor.' ),
144+
keyCombination: {
145+
modifier: 'ctrl',
146+
character: '`',
147+
},
148+
aliases: [
149+
{
150+
modifier: 'access',
151+
character: 'n',
152+
},
153+
],
154+
} );
155+
156+
registerShortcut( {
157+
name: 'core/edit-site/previous-region',
158+
category: 'global',
159+
description: __( 'Navigate to the previous part of the editor.' ),
160+
keyCombination: {
161+
modifier: 'ctrlShift',
162+
character: '`',
163+
},
164+
aliases: [
165+
{
166+
modifier: 'access',
167+
character: 'p',
168+
},
169+
],
170+
} );
139171
}, [ registerShortcut ] );
140172

141173
return null;

packages/edit-site/src/components/list/index.js

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,65 @@
11
/**
22
* WordPress dependencies
33
*/
4+
import { store as coreStore } from '@wordpress/core-data';
5+
import { useSelect } from '@wordpress/data';
46
import { InterfaceSkeleton } from '@wordpress/interface';
5-
import { __ } from '@wordpress/i18n';
7+
import { __, sprintf } from '@wordpress/i18n';
68
import { useViewportMatch } from '@wordpress/compose';
9+
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
710

811
/**
912
* Internal dependencies
1013
*/
14+
import useRegisterShortcuts from './use-register-shortcuts';
1115
import Header from './header';
1216
import NavigationSidebar from '../navigation-sidebar';
1317
import Table from './table';
1418

1519
export default function List( { templateType } ) {
20+
useRegisterShortcuts();
1621
const isDesktopViewport = useViewportMatch( 'medium' );
1722

23+
const { previousShortcut, nextShortcut } = useSelect( ( select ) => {
24+
return {
25+
previousShortcut: select(
26+
keyboardShortcutsStore
27+
).getAllShortcutKeyCombinations( 'core/edit-site/previous-region' ),
28+
nextShortcut: select(
29+
keyboardShortcutsStore
30+
).getAllShortcutKeyCombinations( 'core/edit-site/next-region' ),
31+
};
32+
}, [] );
33+
34+
const postType = useSelect(
35+
( select ) => select( coreStore ).getPostType( templateType ),
36+
[ templateType ]
37+
);
38+
39+
// `postType` could load in asynchronously. Only provide the detailed region labels if
40+
// the postType has loaded, otherwise `InterfaceSkeleton` will fallback to the defaults.
41+
const itemsListLabel = postType?.labels?.items_list;
42+
const detailedRegionLabels = postType
43+
? {
44+
header: sprintf(
45+
// translators: %s - the name of the page, 'Header' as in the header area of that page.
46+
__( '%s - Header' ),
47+
itemsListLabel
48+
),
49+
body: sprintf(
50+
// translators: %s - the name of the page, 'Content' as in the content area of that page.
51+
__( '%s - Content' ),
52+
itemsListLabel
53+
),
54+
}
55+
: undefined;
56+
1857
return (
1958
<InterfaceSkeleton
2059
className="edit-site-list"
2160
labels={ {
2261
drawer: __( 'Navigation Sidebar' ),
62+
...detailedRegionLabels,
2363
} }
2464
header={ <Header templateType={ templateType } /> }
2565
drawer={
@@ -33,6 +73,10 @@ export default function List( { templateType } ) {
3373
<Table templateType={ templateType } />
3474
</main>
3575
}
76+
shortcuts={ {
77+
previous: previousShortcut,
78+
next: nextShortcut,
79+
} }
3680
/>
3781
);
3882
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
5+
import { useDispatch } from '@wordpress/data';
6+
import { useEffect } from '@wordpress/element';
7+
import { __ } from '@wordpress/i18n';
8+
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
9+
10+
export default function useRegisterShortcuts() {
11+
const { registerShortcut } = useDispatch( keyboardShortcutsStore );
12+
useEffect( () => {
13+
registerShortcut( {
14+
name: 'core/edit-site/next-region',
15+
category: 'global',
16+
description: __( 'Navigate to the next part of the editor.' ),
17+
keyCombination: {
18+
modifier: 'ctrl',
19+
character: '`',
20+
},
21+
aliases: [
22+
{
23+
modifier: 'access',
24+
character: 'n',
25+
},
26+
],
27+
} );
28+
29+
registerShortcut( {
30+
name: 'core/edit-site/previous-region',
31+
category: 'global',
32+
description: __( 'Navigate to the previous part of the editor.' ),
33+
keyCombination: {
34+
modifier: 'ctrlShift',
35+
character: '`',
36+
},
37+
aliases: [
38+
{
39+
modifier: 'access',
40+
character: 'p',
41+
},
42+
],
43+
} );
44+
}, [] );
45+
}

packages/interface/src/components/interface-skeleton/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ function InterfaceSkeleton(
8383
className="interface-interface-skeleton__drawer"
8484
role="region"
8585
aria-label={ mergedLabels.drawer }
86+
tabIndex="-1"
8687
>
8788
{ drawer }
8889
</div>

0 commit comments

Comments
 (0)