Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToolsPanel: Add tools panel item deregistration #34085

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export function useToolsPanelItem( props ) {
return cx( styles.ToolsPanelItem, className );
} );

const { menuItems, registerPanelItem } = useToolsPanelContext();
const {
menuItems,
registerPanelItem,
deregisterPanelItem,
} = useToolsPanelContext();

// Registering the panel item allows the panel to include it in its
// automatically generated menu and determine its initial checked status.
Expand All @@ -38,6 +42,8 @@ export function useToolsPanelItem( props ) {
isShownByDefault,
label,
} );

return () => deregisterPanelItem( label );
}, [] );

const isValueSet = hasValue();
Expand Down
10 changes: 9 additions & 1 deletion packages/components/src/tools-panel/tools-panel/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export function useToolsPanel( props ) {
setPanelItems( ( items ) => [ ...items, item ] );
};

// 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 )
);
};

// Manage and share display state of menu items representing child controls.
const [ menuItems, setMenuItems ] = useState( {} );

Expand Down Expand Up @@ -67,7 +75,7 @@ export function useToolsPanel( props ) {
setMenuItems( resetMenuItems );
};

const panelContext = { menuItems, registerPanelItem };
const panelContext = { menuItems, registerPanelItem, deregisterPanelItem };

return {
...otherProps,
Expand Down