From 0246a6b73f74a63e75cc5de9ead99b647cfa2b0a Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Mon, 27 Nov 2023 13:29:48 +1000 Subject: [PATCH] Design Tools: Fix last ToolsPanelItem styling (#56536) --- packages/components/CHANGELOG.md | 4 ++++ .../src/tools-panel/tools-panel-item/hook.ts | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index 0f60b1aaee51a..89dfdf8cdfc13 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -6,6 +6,10 @@ - `Button`: Add focus rings to focusable disabled buttons ([#56383](https://github.com/WordPress/gutenberg/pull/56383)). +### Bug Fix + +- `ToolsPanelItem`: Use useLayoutEffect to prevent rendering glitch for last panel item styling. ([#56536](https://github.com/WordPress/gutenberg/pull/56536)). + ### Experimental - `Tabs`: Memoize and expose the component context ([#56224](https://github.com/WordPress/gutenberg/pull/56224)). diff --git a/packages/components/src/tools-panel/tools-panel-item/hook.ts b/packages/components/src/tools-panel/tools-panel-item/hook.ts index 23701afdfcfd0..244349b6379ea 100644 --- a/packages/components/src/tools-panel/tools-panel-item/hook.ts +++ b/packages/components/src/tools-panel/tools-panel-item/hook.ts @@ -2,7 +2,12 @@ * WordPress dependencies */ import { usePrevious } from '@wordpress/compose'; -import { useCallback, useEffect, useMemo } from '@wordpress/element'; +import { + useCallback, + useEffect, + useLayoutEffect, + useMemo, +} from '@wordpress/element'; /** * Internal dependencies @@ -59,7 +64,11 @@ export function useToolsPanelItem( // Registering the panel item allows the panel to include it in its // automatically generated menu and determine its initial checked status. - useEffect( () => { + // + // This is performed in a layout effect to ensure that the panel item + // is registered before it is rendered preventing a rendering glitch. + // See: https://github.com/WordPress/gutenberg/issues/56470 + useLayoutEffect( () => { if ( hasMatchingPanel && previousPanelId !== null ) { registerPanelItem( { hasValue: hasValueCallback,