Skip to content

Commit

Permalink
Allow selectors when registering blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Mar 13, 2023
1 parent 699dd44 commit d43c099
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ export function unstable__bootstrapServerSideBlockDefinitions( definitions ) {
serverSideBlockDefinitions[ blockName ].ancestor =
definitions[ blockName ].ancestor;
}
// The `selectors` prop is not yet included in the server provided
// definitions. Polyfill it as well.
if (
serverSideBlockDefinitions[ blockName ].selectors ===
undefined &&
definitions[ blockName ].selectors
) {
serverSideBlockDefinitions[ blockName ].selectors =
definitions[ blockName ].selectors;
}
continue;
}

Expand Down Expand Up @@ -203,6 +213,7 @@ function getBlockSettingsFromMetadata( { textdomain, ...metadata } ) {
'attributes',
'providesContext',
'usesContext',
'selectors',
'supports',
'styles',
'example',
Expand Down
36 changes: 36 additions & 0 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,42 @@ describe( 'blocks', () => {
} );
} );

// This can be removed once polyfill adding selectors has been removed.
it( 'should apply selectors on the client when not set on the server', () => {
const blockName = 'core/test-block-with-selectors';
unstable__bootstrapServerSideBlockDefinitions( {
[ blockName ]: {
category: 'widgets',
},
} );
unstable__bootstrapServerSideBlockDefinitions( {
[ blockName ]: {
selectors: { root: '.wp-block-custom-selector' },
category: 'ignored',
},
} );

const blockType = {
title: 'block title',
};
registerBlockType( blockName, blockType );
expect( getBlockType( blockName ) ).toEqual( {
name: blockName,
save: expect.any( Function ),
title: 'block title',
category: 'widgets',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
providesContext: {},
usesContext: [],
keywords: [],
selectors: { root: '.wp-block-custom-selector' },
supports: {},
styles: [],
variations: [],
} );
} );

it( 'should validate the icon', () => {
const blockType = {
save: noop,
Expand Down

0 comments on commit d43c099

Please sign in to comment.