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: Trigger onDeselect/onSelect callbacks directly #61694

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
46 changes: 10 additions & 36 deletions packages/components/src/tools-panel/tools-panel-item/hook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { usePrevious } from '@wordpress/compose';
import { useEvent, usePrevious } from '@wordpress/compose';
import {
useCallback,
useEffect,
Expand Down Expand Up @@ -31,8 +31,8 @@ export function useToolsPanelItem(
label,
panelId,
resetAllFilter = noop,
onDeselect,
onSelect,
onDeselect: onDeselectProp,
onSelect: onSelectProp,
...otherProps
} = useContextSystem( props, 'ToolsPanelItem' );

Expand All @@ -44,7 +44,6 @@ export function useToolsPanelItem(
registerPanelItem,
deregisterPanelItem,
flagItemCustomization,
isResetting,
shouldRenderPlaceholderItems: shouldRenderPlaceholder,
firstDisplayedItem,
lastDisplayedItem,
Expand All @@ -60,6 +59,8 @@ export function useToolsPanelItem(
// 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 onDeselect = useEvent( onDeselectProp );
const onSelect = useEvent( onSelectProp );
const previousPanelId = usePrevious( currentPanelId );

const hasMatchingPanel =
Expand All @@ -78,6 +79,8 @@ export function useToolsPanelItem(
isShownByDefault,
label,
panelId,
onDeselect,
onSelect,
} );
}

Expand All @@ -99,6 +102,8 @@ export function useToolsPanelItem(
previousPanelId,
registerPanelItem,
deregisterPanelItem,
onDeselect,
onSelect,
] );

useEffect( () => {
Expand All @@ -121,7 +126,6 @@ export function useToolsPanelItem(
// `ToolsPanel`.
const menuGroup = isShownByDefault ? 'default' : 'optional';
const isMenuItemChecked = menuItems?.[ menuGroup ]?.[ label ];
const wasMenuItemChecked = usePrevious( isMenuItemChecked );
const isRegistered = menuItems?.[ menuGroup ]?.[ label ] !== undefined;

const isValueSet = hasValue();
Expand All @@ -141,40 +145,10 @@ export function useToolsPanelItem(
isShownByDefault,
] );

// Determine if the panel item's corresponding menu is being toggled and
// trigger appropriate callback if it is.
useEffect( () => {
// We check whether this item is currently registered as items rendered
// via fills can persist through the parent panel being remounted.
// See: https://github.com/WordPress/gutenberg/pull/45673
if ( ! isRegistered || isResetting || ! hasMatchingPanel ) {
return;
}

if ( isMenuItemChecked && ! isValueSet && ! wasMenuItemChecked ) {
onSelect?.();
}

if ( ! isMenuItemChecked && isValueSet && wasMenuItemChecked ) {
onDeselect?.();
}
}, [
hasMatchingPanel,
isMenuItemChecked,
isRegistered,
isResetting,
isValueSet,
wasMenuItemChecked,
onSelect,
onDeselect,
] );

// The item is shown if it is a default control regardless of whether it
// has a value. Optional items are shown when they are checked or have
// a value.
const isShown = isShownByDefault
? menuItems?.[ menuGroup ]?.[ label ] !== undefined
: isMenuItemChecked;
const isShown = isShownByDefault ? isRegistered : isMenuItemChecked;
Comment on lines -176 to +151
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to the purpose of the PR, this was just to reuse isRegistered instead of reevaluating the same condition.


const cx = useCx();
const classes = useMemo( () => {
Expand Down
19 changes: 16 additions & 3 deletions packages/components/src/tools-panel/tools-panel/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,22 @@ export function useToolsPanel(

// Toggle the checked state of a menu item which is then used to determine
// display of the item within the panel.
const toggleItem = useCallback( ( label: string ) => {
panelDispatch( { type: 'TOGGLE_VALUE', label } );
}, [] );
const toggleItem = useCallback(
( label: string ) => {
panelDispatch( { type: 'TOGGLE_VALUE', label } );
const currentItem = panelItems.find(
( item ) => item.label === label
);
if ( currentItem ) {
const { isShownByDefault, onDeselect, onSelect } = currentItem;
const menuGroup = isShownByDefault ? 'default' : 'optional';
const hasValue = menuItems[ menuGroup ][ label ];
const callback = hasValue ? onDeselect : onSelect;
callback?.();
}
},
[ menuItems, panelItems ]
);

// Resets display of children and executes resetAll callback if available.
const resetAllItems = useCallback( () => {
Expand Down
14 changes: 7 additions & 7 deletions packages/components/src/tools-panel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,6 @@ export type ToolsPanelItem = {
* from a shared source.
*/
panelId?: string | null;
};

export type ToolsPanelItemProps = ToolsPanelItem & {
/**
* The child elements.
*/
children?: ReactNode;
/**
* Called when this item is deselected in the `ToolsPanel` menu. This is
* normally used to reset the panel item control's value.
Expand All @@ -150,6 +143,13 @@ export type ToolsPanelItemProps = ToolsPanelItem & {
* menu.
*/
onSelect?: () => void;
};

export type ToolsPanelItemProps = ToolsPanelItem & {
/**
* The child elements.
*/
children?: ReactNode;

/**
* A `ToolsPanel` will collect each item's `resetAllFilter` and pass an
Expand Down
Loading