Skip to content

Commit

Permalink
Components: ToolsPanel: fix deregister/register on type (#56770)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Dec 6, 2023
1 parent d1d0b2a commit a82ae41
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Bug Fix

- `ToggleGroupControl`: react correctly to external controlled updates ([#56678](https://github.com/WordPress/gutenberg/pull/56678)).
- `ToolsPanel`: fix a performance issue ([#56770](https://github.com/WordPress/gutenberg/pull/56770)).

## 25.13.0 (2023-11-29)

Expand Down
31 changes: 10 additions & 21 deletions packages/components/src/tools-panel/tools-panel-item/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ export function useToolsPanelItem(
__experimentalLastVisibleItemClass,
} = useToolsPanelContext();

const hasValueCallback = useCallback( hasValue, [ panelId, hasValue ] );
const resetAllFilterCallback = useCallback( resetAllFilter, [
panelId,
resetAllFilter,
] );
// hasValue is a new function on every render, so do not add it as a
// dependency to the useCallback hook! If needed, we should use a ref.
// eslint-disable-next-line react-hooks/exhaustive-deps
const hasValueCallback = useCallback( hasValue, [ panelId ] );
// resetAllFilter is a new function on every render, so do not add it as a
// dependency to the useCallback hook! If needed, we should use a ref.
// eslint-disable-next-line react-hooks/exhaustive-deps
const resetAllFilterCallback = useCallback( resetAllFilter, [ panelId ] );
const previousPanelId = usePrevious( currentPanelId );

const hasMatchingPanel =
Expand Down Expand Up @@ -126,27 +129,13 @@ export function useToolsPanelItem(
const newValueSet = isValueSet && ! wasValueSet;

// Notify the panel when an item's value has been set.
//
// 1. For default controls, this is so "reset" appears beside its menu item.
// 2. For optional controls, when the panel ID is `null`, it allows the
// panel to ensure the item is toggled on for display in the menu, given the
// value has been set external to the control.
useEffect( () => {
if ( ! newValueSet ) {
return;
}

if ( isShownByDefault || currentPanelId === null ) {
flagItemCustomization( label, menuGroup );
}
}, [
currentPanelId,
newValueSet,
isShownByDefault,
menuGroup,
label,
flagItemCustomization,
] );
flagItemCustomization( label, menuGroup );
}, [ newValueSet, menuGroup, label, flagItemCustomization ] );

// Determine if the panel item's corresponding menu is being toggled and
// trigger appropriate callback if it is.
Expand Down

1 comment on commit a82ae41

@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 a82ae41.
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/7111248588
📝 Reported issues:

Please sign in to comment.