@wordpress/edit-post 7.3.3 → 7.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,10 +14,12 @@ import {
14
14
  useMergeRefs,
15
15
  } from '@wordpress/compose';
16
16
  import { useDispatch } from '@wordpress/data';
17
+ import { focus } from '@wordpress/dom';
18
+ import { useRef, useState } from '@wordpress/element';
17
19
  import { __ } from '@wordpress/i18n';
18
20
  import { closeSmall } from '@wordpress/icons';
21
+ import { useShortcut } from '@wordpress/keyboard-shortcuts';
19
22
  import { ESCAPE } from '@wordpress/keycodes';
20
- import { useState } from '@wordpress/element';
21
23
 
22
24
  /**
23
25
  * Internal dependencies
@@ -31,6 +33,7 @@ export default function ListViewSidebar() {
31
33
  const focusOnMountRef = useFocusOnMount( 'firstElement' );
32
34
  const headerFocusReturnRef = useFocusReturn();
33
35
  const contentFocusReturnRef = useFocusReturn();
36
+
34
37
  function closeOnEscape( event ) {
35
38
  if ( event.keyCode === ESCAPE && ! event.defaultPrevented ) {
36
39
  event.preventDefault();
@@ -40,12 +43,63 @@ export default function ListViewSidebar() {
40
43
 
41
44
  const [ tab, setTab ] = useState( 'list-view' );
42
45
 
46
+ // This ref refers to the sidebar as a whole.
47
+ const sidebarRef = useRef();
48
+ // This ref refers to the list view tab button.
49
+ const listViewTabRef = useRef();
50
+ // This ref refers to the outline tab button.
51
+ const outlineTabRef = useRef();
52
+ // This ref refers to the list view application area.
53
+ const listViewRef = useRef();
54
+
55
+ /*
56
+ * Callback function to handle list view or outline focus.
57
+ *
58
+ * @param {string} currentTab The current tab. Either list view or outline.
59
+ *
60
+ * @return void
61
+ */
62
+ function handleSidebarFocus( currentTab ) {
63
+ // List view tab is selected.
64
+ if ( currentTab === 'list-view' ) {
65
+ // Either focus the list view or the list view tab button. Must have a fallback because the list view does not render when there are no blocks.
66
+ const listViewApplicationFocus = focus.tabbable.find(
67
+ listViewRef.current
68
+ )[ 0 ];
69
+ const listViewFocusArea = sidebarRef.current.contains(
70
+ listViewApplicationFocus
71
+ )
72
+ ? listViewApplicationFocus
73
+ : listViewTabRef.current;
74
+ listViewFocusArea.focus();
75
+ // Outline tab is selected.
76
+ } else {
77
+ outlineTabRef.current.focus();
78
+ }
79
+ }
80
+
81
+ // This only fires when the sidebar is open because of the conditional rendering. It is the same shortcut to open but that is defined as a global shortcut and only fires when the sidebar is closed.
82
+ useShortcut( 'core/edit-post/toggle-list-view', () => {
83
+ // If the sidebar has focus, it is safe to close.
84
+ if (
85
+ sidebarRef.current.contains(
86
+ sidebarRef.current.ownerDocument.activeElement
87
+ )
88
+ ) {
89
+ setIsListViewOpened( false );
90
+ // If the list view or outline does not have focus, focus should be moved to it.
91
+ } else {
92
+ handleSidebarFocus( tab );
93
+ }
94
+ } );
95
+
43
96
  return (
44
97
  // eslint-disable-next-line jsx-a11y/no-static-element-interactions
45
98
  <div
46
99
  aria-label={ __( 'Document Overview' ) }
47
100
  className="edit-post-editor__document-overview-panel"
48
101
  onKeyDown={ closeOnEscape }
102
+ ref={ sidebarRef }
49
103
  >
50
104
  <div
51
105
  className="edit-post-editor__document-overview-panel-header components-panel__header edit-post-sidebar__panel-tabs"
@@ -59,6 +113,7 @@ export default function ListViewSidebar() {
59
113
  <ul>
60
114
  <li>
61
115
  <Button
116
+ ref={ listViewTabRef }
62
117
  onClick={ () => {
63
118
  setTab( 'list-view' );
64
119
  } }
@@ -73,6 +128,7 @@ export default function ListViewSidebar() {
73
128
  </li>
74
129
  <li>
75
130
  <Button
131
+ ref={ outlineTabRef }
76
132
  onClick={ () => {
77
133
  setTab( 'outline' );
78
134
  } }
@@ -91,6 +147,7 @@ export default function ListViewSidebar() {
91
147
  ref={ useMergeRefs( [
92
148
  contentFocusReturnRef,
93
149
  focusOnMountRef,
150
+ listViewRef,
94
151
  ] ) }
95
152
  className="edit-post-editor__list-view-container"
96
153
  >
package/src/editor.js CHANGED
@@ -139,6 +139,7 @@ function Editor( { postId, postType, settings, initialEdits, ...props } ) {
139
139
  }, [
140
140
  settings,
141
141
  hasFixedToolbar,
142
+ hasInlineToolbar,
142
143
  focusMode,
143
144
  isDistractionFree,
144
145
  hiddenBlockTypes,