Skip to content

Commit b62b6f4

Browse files
committed
Header toolbar: useCallback to avoid unnecessary rerenders
1 parent 19017d7 commit b62b6f4

File tree

1 file changed

+19
-14
lines changed
  • packages/edit-post/src/components/header/header-toolbar

1 file changed

+19
-14
lines changed

packages/edit-post/src/components/header/header-toolbar/index.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from '@wordpress/editor';
1818
import { Button, ToolbarItem } from '@wordpress/components';
1919
import { listView, plus } from '@wordpress/icons';
20-
import { useRef } from '@wordpress/element';
20+
import { useRef, useCallback } from '@wordpress/element';
2121
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
2222

2323
/**
@@ -26,6 +26,9 @@ import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
2626
import TemplateTitle from '../template-title';
2727
import { store as editPostStore } from '../../../store';
2828

29+
const preventDefault = ( event ) => {
30+
event.preventDefault();
31+
};
2932
function HeaderToolbar() {
3033
const inserterButton = useRef();
3134
const { setIsInserterOpened, setIsListViewOpened } = useDispatch(
@@ -73,6 +76,10 @@ function HeaderToolbar() {
7376
/* translators: accessibility text for the editor toolbar */
7477
const toolbarAriaLabel = __( 'Document tools' );
7578

79+
const toggleListView = useCallback(
80+
() => setIsListViewOpened( ! isListViewOpen ),
81+
[ setIsListViewOpened, isListViewOpen ]
82+
);
7683
const overflowItems = (
7784
<>
7885
<ToolbarItem
@@ -90,13 +97,20 @@ function HeaderToolbar() {
9097
isPressed={ isListViewOpen }
9198
/* translators: button label text should, if possible, be under 16 characters. */
9299
label={ __( 'List View' ) }
93-
onClick={ () => setIsListViewOpened( ! isListViewOpen ) }
100+
onClick={ toggleListView }
94101
shortcut={ listViewShortcut }
95102
showTooltip={ ! showIconLabels }
96103
/>
97104
</>
98105
);
99-
106+
const openInserter = useCallback( () => {
107+
if ( isInserterOpened ) {
108+
// Focusing the inserter button closes the inserter popover
109+
inserterButton.current.focus();
110+
} else {
111+
setIsInserterOpened( true );
112+
}
113+
}, [ isInserterOpened, setIsInserterOpened ] );
100114
return (
101115
<NavigableToolbar
102116
className="edit-post-header-toolbar"
@@ -109,17 +123,8 @@ function HeaderToolbar() {
109123
className="edit-post-header-toolbar__inserter-toggle"
110124
variant="primary"
111125
isPressed={ isInserterOpened }
112-
onMouseDown={ ( event ) => {
113-
event.preventDefault();
114-
} }
115-
onClick={ () => {
116-
if ( isInserterOpened ) {
117-
// Focusing the inserter button closes the inserter popover
118-
inserterButton.current.focus();
119-
} else {
120-
setIsInserterOpened( true );
121-
}
122-
} }
126+
onMouseDown={ preventDefault }
127+
onClick={ openInserter }
123128
disabled={ ! isInserterEnabled }
124129
icon={ plus }
125130
/* translators: button label text should, if possible, be under 16

0 commit comments

Comments
 (0)