Skip to content

Commit

Permalink
Interface: Use plugin context hook instead of HoC (#66362)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: tyxla <[email protected]>
  • Loading branch information
4 people authored Oct 23, 2024
1 parent 0433d90 commit 2dbd0a6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
*/
import { Button } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { usePluginContext } from '@wordpress/plugins';

/**
* Internal dependencies
*/
import { store as interfaceStore } from '../../store';
import complementaryAreaContext from '../complementary-area-context';

/**
* Whether the role supports checked state.
Expand All @@ -29,17 +29,20 @@ function roleSupportsCheckedState( role ) {
].includes( role );
}

function ComplementaryAreaToggle( {
export default function ComplementaryAreaToggle( {
as = Button,
scope,
identifier,
icon,
identifier: identifierProp,
icon: iconProp,
selectedIcon,
name,
shortcut,
...props
} ) {
const ComponentToUse = as;
const context = usePluginContext();
const icon = iconProp || context.icon;
const identifier = identifierProp || `${ context.name }/${ name }`;
const isSelected = useSelect(
( select ) =>
select( interfaceStore ).getActiveComplementaryArea( scope ) ===
Expand Down Expand Up @@ -70,5 +73,3 @@ function ComplementaryAreaToggle( {
/>
);
}

export default complementaryAreaContext( ComplementaryAreaToggle );
17 changes: 9 additions & 8 deletions packages/interface/src/components/complementary-area/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import {
useViewportMatch,
usePrevious,
} from '@wordpress/compose';
import { usePluginContext } from '@wordpress/plugins';

/**
* Internal dependencies
*/
import ComplementaryAreaHeader from '../complementary-area-header';
import ComplementaryAreaMoreMenuItem from '../complementary-area-more-menu-item';
import ComplementaryAreaToggle from '../complementary-area-toggle';
import withComplementaryAreaContext from '../complementary-area-context';
import PinnedItems from '../pinned-items';
import { store as interfaceStore } from '../../store';

Expand Down Expand Up @@ -171,10 +171,10 @@ function ComplementaryArea( {
children,
className,
closeLabel = __( 'Close plugin' ),
identifier,
identifier: identifierProp,
header,
headerClassName,
icon,
icon: iconProp,
isPinnable = true,
panelClassName,
scope,
Expand All @@ -183,6 +183,10 @@ function ComplementaryArea( {
toggleShortcut,
isActiveByDefault,
} ) {
const context = usePluginContext();
const icon = iconProp || context.icon;
const identifier = identifierProp || `${ context.name }/${ name }`;

// This state is used to delay the rendering of the Fill
// until the initial effect runs.
// This prevents the animation from running on mount if
Expand Down Expand Up @@ -344,9 +348,6 @@ function ComplementaryArea( {
);
}

const ComplementaryAreaWrapped =
withComplementaryAreaContext( ComplementaryArea );

ComplementaryAreaWrapped.Slot = ComplementaryAreaSlot;
ComplementaryArea.Slot = ComplementaryAreaSlot;

export default ComplementaryAreaWrapped;
export default ComplementaryArea;

1 comment on commit 2dbd0a6

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 2dbd0a6.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11478639096
📝 Reported issues:

Please sign in to comment.