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

Performance: run block variation hook only for matches #62617

Merged
merged 1 commit into from
Jun 18, 2024
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
25 changes: 21 additions & 4 deletions packages/block-editor/src/hooks/block-style-variation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ import { useStyleOverride } from './utils';
import { store as blockEditorStore } from '../store';
import { globalStylesDataKey } from '../store/private-keys';

const VARIATION_PREFIX = 'is-style-';

function getVariationMatches( className ) {
if ( ! className ) {
return [];
}
return className.split( /\s+/ ).reduce( ( matches, name ) => {
if ( name.startsWith( VARIATION_PREFIX ) ) {
const match = name.slice( VARIATION_PREFIX.length );
if ( match !== 'default' ) {
matches.push( match );
}
}
return matches;
}, [] );
}

/**
* Get the first block style variation that has been registered from the class string.
*
Expand All @@ -28,14 +45,13 @@ import { globalStylesDataKey } from '../store/private-keys';
function getVariationNameFromClass( className, registeredStyles = [] ) {
// The global flag affects how capturing groups work in JS. So the regex
// below will only return full CSS classes not just the variation name.
const matches = className?.match( /\bis-style-(?!default)(\S+)\b/g );
const matches = getVariationMatches( className );

if ( ! matches ) {
return null;
}

for ( const variationClass of matches ) {
const variation = variationClass.substring( 9 ); // Remove 'is-style-' prefix.
for ( const variation of matches ) {
if ( registeredStyles.some( ( style ) => style.name === variation ) ) {
return variation;
}
Expand Down Expand Up @@ -94,7 +110,7 @@ function useBlockProps( { name, className, clientId } ) {

const registeredStyles = getBlockStyles( name );
const variation = getVariationNameFromClass( className, registeredStyles );
const variationClass = `is-style-${ variation }-${ clientId }`;
const variationClass = `${ VARIATION_PREFIX }${ variation }-${ clientId }`;

const { settings, styles } = useBlockStyleVariation(
name,
Expand Down Expand Up @@ -152,5 +168,6 @@ function useBlockProps( { name, className, clientId } ) {
export default {
hasSupport: () => true,
attributeKeys: [ 'className' ],
isMatch: ( { className } ) => getVariationMatches( className ).length > 0,
useBlockProps,
};
4 changes: 3 additions & 1 deletion packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ export function createBlockListBlockFilter( features ) {
hasSupport,
attributeKeys = [],
useBlockProps,
isMatch,
} = feature;

const neededProps = {};
Expand All @@ -595,7 +596,8 @@ export function createBlockListBlockFilter( features ) {
// Skip rendering if none of the needed attributes are
// set.
! Object.keys( neededProps ).length ||
! hasSupport( props.name )
! hasSupport( props.name ) ||
( isMatch && ! isMatch( neededProps ) )
) {
return null;
}
Expand Down
Loading