Skip to content

Commit

Permalink
Prevent deregistration removing matching controls
Browse files Browse the repository at this point in the history
When the ToolsPanel is used for block supports via a SlotFill, the same control can be injected e.g. Padding. The current approach to deregistration removes any menu item with a matching label. Unfortunately, that clean up for the hook runs after the the new control is registered.
  • Loading branch information
aaronrobertshaw committed Aug 25, 2021
1 parent 336d423 commit b9c995a
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/components/src/tools-panel/tools-panel/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ export function useToolsPanel( props ) {
// Panels need to deregister on unmount to avoid orphans in menu state.
// This is an issue when panel items are being injected via SlotFills.
const deregisterPanelItem = ( label ) => {
setPanelItems( ( items ) =>
items.filter( ( item ) => item.label !== label )
);
// When switching selections between components injecting matching
// controls, e.g. both panels have a "padding" control, the
// deregistration of the first panel doesn't occur until after the
// registration of the next.
const index = panelItems.findIndex( ( item ) => item.label === label );

if ( index !== -1 ) {
setPanelItems( ( items ) => items.splice( index, 1 ) );
}
};

// Manage and share display state of menu items representing child controls.
Expand Down

0 comments on commit b9c995a

Please sign in to comment.