Skip to content

Commit

Permalink
Prevent re-rendering the editor header when the selected block changes (
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Dec 17, 2023
1 parent cdeca67 commit 2c79c9e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
10 changes: 3 additions & 7 deletions packages/block-editor/src/components/navigable-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,7 @@ function useToolbarFocus( {
};
}, [ initialIndex, initialFocusOnMount, onIndexChange, toolbarRef ] );

const { lastFocus } = useSelect( ( select ) => {
const { getLastFocus } = select( blockEditorStore );
return {
lastFocus: getLastFocus(),
};
}, [] );
const { getLastFocus } = useSelect( blockEditorStore );
/**
* Handles returning focus to the block editor canvas when pressing escape.
*/
Expand All @@ -178,6 +173,7 @@ function useToolbarFocus( {

if ( focusEditorOnEscape ) {
const handleKeyDown = ( event ) => {
const lastFocus = getLastFocus();
if ( event.keyCode === ESCAPE && lastFocus?.current ) {
// Focus the last focused element when pressing escape.
event.preventDefault();
Expand All @@ -192,7 +188,7 @@ function useToolbarFocus( {
);
};
}
}, [ focusEditorOnEscape, lastFocus, toolbarRef ] );
}, [ focusEditorOnEscape, getLastFocus, toolbarRef ] );
}

export default function NavigableToolbar( {
Expand Down
16 changes: 7 additions & 9 deletions packages/edit-post/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function Header( {
const isLargeViewport = useViewportMatch( 'medium' );
const blockToolbarRef = useRef();
const {
blockSelectionStart,
hasBlockSelection,
hasActiveMetaboxes,
hasFixedToolbar,
isEditingTemplate,
Expand All @@ -74,8 +74,8 @@ function Header( {
const { get: getPreference } = select( preferencesStore );

return {
blockSelectionStart:
select( blockEditorStore ).getBlockSelectionStart(),
hasBlockSelection:
!! select( blockEditorStore ).getBlockSelectionStart(),
hasActiveMetaboxes: select( editPostStore ).hasMetaBoxes(),
isEditingTemplate:
select( editorStore ).getRenderingMode() === 'template-only',
Expand All @@ -90,14 +90,12 @@ function Header( {
const [ isBlockToolsCollapsed, setIsBlockToolsCollapsed ] =
useState( true );

const hasBlockSelected = !! blockSelectionStart;

useEffect( () => {
// If we have a new block selection, show the block tools
if ( blockSelectionStart ) {
if ( hasBlockSelection ) {
setIsBlockToolsCollapsed( false );
}
}, [ blockSelectionStart ] );
}, [ hasBlockSelection ] );

return (
<div className="edit-post-header">
Expand Down Expand Up @@ -136,7 +134,7 @@ function Header( {
ref={ blockToolbarRef }
name="block-toolbar"
/>
{ isEditingTemplate && hasBlockSelected && (
{ isEditingTemplate && hasBlockSelection && (
<Button
className="edit-post-header__block-tools-toggle"
icon={ isBlockToolsCollapsed ? next : previous }
Expand All @@ -158,7 +156,7 @@ function Header( {
className={ classnames( 'edit-post-header__center', {
'is-collapsed':
isEditingTemplate &&
hasBlockSelected &&
hasBlockSelection &&
! isBlockToolsCollapsed &&
hasFixedToolbar &&
isLargeViewport,
Expand Down

1 comment on commit 2c79c9e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 2c79c9e.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7239792166
📝 Reported issues:

Please sign in to comment.