Skip to content

Commit

Permalink
Fix register/deregister logic for toolspanel in inspector controls
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo committed Jun 9, 2023
1 parent 2e93f44 commit e6754b2
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/block-editor/src/components/inspector-controls/fill.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/**
* WordPress dependencies
*/
import { __experimentalStyleProvider as StyleProvider } from '@wordpress/components';
import {
__experimentalStyleProvider as StyleProvider,
__experimentalToolsPanelContext as ToolsPanelContext,
} from '@wordpress/components';
import warning from '@wordpress/warning';
import deprecated from '@wordpress/deprecated';
import { useEffect } from '@wordpress/element';
import { useEffect, useContext } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -57,19 +60,25 @@ export default function InspectorControlsFill( {
);
}

function ToolsPanelInspectorControl( { children, resetAllFilter, fillProps } ) {
const { registerResetAllFilter, deregisterResetAllFilter } = fillProps;
function RegisterResetAll( { resetAllFilter, children } ) {
const { registerResetAllFilter, deregisterResetAllFilter } =
useContext( ToolsPanelContext );
useEffect( () => {
if ( resetAllFilter && registerResetAllFilter ) {
if (
resetAllFilter &&
registerResetAllFilter &&
deregisterResetAllFilter
) {
registerResetAllFilter( resetAllFilter );
}
return () => {
if ( resetAllFilter && deregisterResetAllFilter ) {
return () => {
deregisterResetAllFilter( resetAllFilter );
}
};
};
}
}, [ resetAllFilter, registerResetAllFilter, deregisterResetAllFilter ] );
return children;
}

function ToolsPanelInspectorControl( { children, resetAllFilter, fillProps } ) {
// `fillProps.forwardedContext` is an array of context provider entries, provided by slot,
// that should wrap the fill markup.
const { forwardedContext = [] } = fillProps;
Expand All @@ -78,7 +87,11 @@ function ToolsPanelInspectorControl( { children, resetAllFilter, fillProps } ) {
// access to any React Context whose Provider is part of
// the InspectorControlsSlot tree. So we re-create the
// Provider in this subtree.
const innerMarkup = <>{ children }</>;
const innerMarkup = (
<RegisterResetAll resetAllFilter={ resetAllFilter }>
{ children }
</RegisterResetAll>
);
return forwardedContext.reduce(
( inner, [ Provider, props ] ) => (
<Provider { ...props }>{ inner }</Provider>
Expand Down

0 comments on commit e6754b2

Please sign in to comment.