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

Blocks: Adds check for parent before showing convert to pattern button #66158

Merged
merged 1 commit into from
Nov 20, 2024
Merged
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
12 changes: 11 additions & 1 deletion packages/patterns/src/components/pattern-convert-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isReusableBlock,
createBlock,
serialize,
getBlockType,
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
} from '@wordpress/blocks';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useState, useCallback } from '@wordpress/element';
Expand Down Expand Up @@ -60,6 +61,15 @@ export default function PatternConvertButton( {

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

// Check if the block has reusable support defined.
const hasReusableBlockSupport = ( blockName ) => {
const blockType = getBlockType( blockName );
const hasParent = blockType && 'parent' in blockType;

// If the block has a parent, check with false as default, otherwise with true.
return hasBlockSupport( blockName, 'reusable', ! hasParent );
};

const isReusable =
blocks.length === 1 &&
blocks[ 0 ] &&
Expand All @@ -82,7 +92,7 @@ export default function PatternConvertButton( {
// Hide on invalid blocks.
block.isValid &&
// Hide when block doesn't support being made into a pattern.
hasBlockSupport( block.name, 'reusable', true )
hasReusableBlockSupport( block.name )
) &&
// Hide when current doesn't have permission to do that.
// Blocks refers to the wp_block post type, this checks the ability to create a post of that type.
Expand Down
Loading