Skip to content

Commit

Permalink
Fix sticking “Reset” option in ToolsPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed Apr 9, 2024
1 parent 7c25dc4 commit 29b7dc9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
16 changes: 5 additions & 11 deletions packages/components/src/tools-panel/tools-panel-item/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,11 @@ export function useToolsPanelItem(
const isRegistered = menuItems?.[ menuGroup ]?.[ label ] !== undefined;

const isValueSet = hasValue();
const wasValueSet = usePrevious( isValueSet );
const newValueSet = isValueSet && ! wasValueSet;

// Notify the panel when an item's value has been set.
useEffect( () => {
if ( ! newValueSet ) {
return;
}

flagItemCustomization( label, menuGroup );
}, [ newValueSet, menuGroup, label, flagItemCustomization ] );
// Notify the panel when an item's value has changed.
useEffect(
() => flagItemCustomization( isValueSet, label, menuGroup ),
[ isValueSet, menuGroup, label, flagItemCustomization ]
);

// Determine if the panel item's corresponding menu is being toggled and
// trigger appropriate callback if it is.
Expand Down
8 changes: 6 additions & 2 deletions packages/components/src/tools-panel/tools-panel/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,17 @@ export function useToolsPanel(
// separately to optional items and have different display states,
// we need to update that when their value is customized.
const flagItemCustomization = useCallback(
( label: string, group: ToolsPanelMenuItemKey = 'default' ) => {
(
value: boolean,
label: string,
group: ToolsPanelMenuItemKey = 'default'
) => {
setMenuItems( ( items ) => {
const newState = {
...items,
[ group ]: {
...items[ group ],
[ label ]: true,
[ label ]: value,
},
};
return newState;
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/tools-panel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export type ToolsPanelContext = {
registerResetAllFilter: ( filter: ResetAllFilter ) => void;
deregisterResetAllFilter: ( filter: ResetAllFilter ) => void;
flagItemCustomization: (
value: boolean,
label: string,
group?: ToolsPanelMenuItemKey
) => void;
Expand Down

0 comments on commit 29b7dc9

Please sign in to comment.