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

Patterns: Don't override the rootClientID in create menu - only set if undefined #52713

Merged
merged 2 commits into from
Jul 19, 2023
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
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
Expand All @@ -10,7 +10,12 @@ import { store as blockEditorStore } from '@wordpress/block-editor';
import ReusableBlockConvertButton from './reusable-block-convert-button';
import ReusableBlocksManageButton from './reusable-blocks-manage-button';

function ReusableBlocksMenuItems( { clientIds, rootClientId } ) {
export default function ReusableBlocksMenuItems( { rootClientId } ) {
const clientIds = useSelect(
( select ) => select( blockEditorStore ).getSelectedBlockClientIds(),
[]
);
Copy link
Member

Choose a reason for hiding this comment

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

Should pass [] as the deps for useSelect here. Annoyingly it doesn't default to [].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, fixed, we should have a lint rule for that.


return (
<>
<ReusableBlockConvertButton
Expand All @@ -23,16 +28,3 @@ function ReusableBlocksMenuItems( { clientIds, rootClientId } ) {
</>
);
}

export default withSelect( ( select ) => {
const { getSelectedBlockClientIds, getBlockRootClientId } =
select( blockEditorStore );
const clientIds = getSelectedBlockClientIds();
return {
clientIds,
rootClientId:
clientIds?.length > 0
? getBlockRootClientId( clientIds[ 0 ] )
: undefined,
};
} )( ReusableBlocksMenuItems );
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,17 @@ export default function ReusableBlockConvertButton( {
const canConvert = useSelect(
( select ) => {
const { canUser } = select( coreStore );
const { getBlocksByClientId, canInsertBlockType } =
select( blockEditorStore );
const {
getBlocksByClientId,
canInsertBlockType,
getBlockRootClientId,
} = select( blockEditorStore );

const rootId =
rootClientId ||
( clientIds.length > 0
? getBlockRootClientId( clientIds[ 0 ] )
: undefined );

const blocks = getBlocksByClientId( clientIds ) ?? [];

Expand All @@ -70,7 +79,7 @@ export default function ReusableBlockConvertButton( {
// Hide when this is already a reusable block.
! isReusable &&
// Hide when reusable blocks are disabled.
canInsertBlockType( 'core/block', rootClientId ) &&
canInsertBlockType( 'core/block', rootId ) &&
blocks.every(
( block ) =>
// Guard against the case where a regular block has *just* been converted.
Expand Down
Loading