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

Buttons block: Support default button variation in template and default block #53786

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
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
30 changes: 17 additions & 13 deletions packages/block-library/src/buttons/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import {
useInnerBlocksProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { store as blocksStore } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { name as buttonBlockName } from '../button';
const buttonBlockName = 'core/button';

const ALLOWED_BLOCKS = [ buttonBlockName ];

Expand All @@ -42,23 +40,29 @@ function ButtonsEdit( { attributes, className } ) {
'has-custom-font-size': fontSize || style?.typography?.fontSize,
} ),
} );
const preferredStyle = useSelect( ( select ) => {
const defaultButtonAttributes = useSelect( ( select ) => {
const preferredStyleVariations =
select( blockEditorStore ).getSettings()
.__experimentalPreferredStyleVariations;
return preferredStyleVariations?.value?.[ buttonBlockName ];
const preferredStyle =
preferredStyleVariations?.value?.[ buttonBlockName ];
const defaultButton = select( blocksStore ).getDefaultBlockVariation(
buttonBlockName
) || { attributes: {} };
const buttonClasses = classnames( defaultButton.attributes.className, {
[ `is-style-${ preferredStyle }` ]: !! preferredStyle,
} );
return {
...defaultButton.attributes,
className: buttonClasses === '' ? undefined : buttonClasses,
};
}, [] );

const innerBlocksProps = useInnerBlocksProps( blockProps, {
allowedBlocks: ALLOWED_BLOCKS,
defaultBlock: DEFAULT_BLOCK,
defaultBlock: { ...DEFAULT_BLOCK, attributes: defaultButtonAttributes },
directInsert: true,
template: [
[
buttonBlockName,
{ className: preferredStyle && `is-style-${ preferredStyle }` },
],
],
template: [ [ buttonBlockName, defaultButtonAttributes ] ],
templateInsertUpdatesSelection: true,
orientation: layout?.orientation ?? 'horizontal',
} );
Expand Down