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

Add support for button elements to theme.json #40260

Merged
merged 33 commits into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
569389a
Add button component for blocks
scruffian Apr 12, 2022
0487f9f
Add button component for blocks
scruffian Apr 12, 2022
4994996
also use the blockbutton for the search block
scruffian Apr 12, 2022
533a32b
update search block icon button
scruffian Apr 12, 2022
353a149
Rename to ElementButton
scruffian Apr 22, 2022
b855667
Add content version of the component
scruffian Apr 22, 2022
a6c0ac2
refactor to remove duplicate code
scruffian Apr 22, 2022
b4faf9e
linting fix
scruffian Apr 22, 2022
0206097
Add a deprecation
scruffian Apr 22, 2022
41a5ac1
Move code to 6.1 version of theme.json
scruffian Apr 22, 2022
5f546da
also output a button rather than richtext, when children are present
scruffian Apr 22, 2022
35392df
adding missing file
scruffian Apr 22, 2022
6af1644
ignore lint
scruffian Apr 25, 2022
c03bf9d
Add the wp-block-button class to the element selector
scruffian Apr 26, 2022
3489f7d
update fixtures
scruffian May 11, 2022
0c7c100
linting fix'
scruffian May 11, 2022
fc2b78c
remove disconnected change
scruffian May 11, 2022
5abe8f7
Remove ElementButton component and replace with a const
scruffian May 11, 2022
75735f3
use a hook to supply the element button class
scruffian May 13, 2022
6c36cb3
use the same class name in the search button server side render
scruffian May 13, 2022
cf569c7
simply to just use a const
scruffian May 13, 2022
c10501d
add documentation
scruffian May 13, 2022
c1fa1f9
remove unneeded code
scruffian May 13, 2022
6ca12ed
remove unneeded code
scruffian May 13, 2022
cc917e4
also make button a selector
scruffian May 13, 2022
0587fc0
remove unneeded changes
scruffian May 13, 2022
5dd8872
Expose the classname const from block editor package
getdave May 16, 2022
b3a2480
Update packages/block-library/src/button/deprecated.js
scruffian May 18, 2022
b7c560d
update button element const
scruffian May 18, 2022
3175392
Make the const all uppercase
scruffian May 18, 2022
d076dd4
fix spacing
scruffian May 18, 2022
efbcd29
update snapshots
scruffian May 20, 2022
5bc74fa
add test
scruffian May 20, 2022
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
2 changes: 1 addition & 1 deletion lib/compat/wordpress-6.0/class-wp-theme-json-6-0.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* WP_Theme_JSON_Gutenberg class
* WP_Theme_JSON_6_0 class
Copy link
Contributor

Choose a reason for hiding this comment

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

I still have no idea what these files do and why this change is required.

Copy link
Contributor

Choose a reason for hiding this comment

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

This just fixes a typo – the class name declared below is WP_Theme_JSON_6_0

*
* @package gutenberg
*/
Expand Down
14 changes: 13 additions & 1 deletion lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* WP_Theme_JSON_Gutenberg class
* WP_Theme_JSON_6_1 class
*
* @package gutenberg
*/
Expand All @@ -15,6 +15,18 @@
* @access private
*/
class WP_Theme_JSON_6_1 extends WP_Theme_JSON_6_0 {
const __EXPERIMENTAL_ELEMENT_BUTTON_CLASS_NAME = 'wp-element-button';

const ELEMENTS = array(
'link' => 'a',
'h1' => 'h1',
'h2' => 'h2',
'h3' => 'h3',
'h4' => 'h4',
'h5' => 'h5',
'h6' => 'h6',
'button' => '.wp-element-button, .wp-block-button, button', // We have the .wp-block-button class so that this will target older buttons that have been serialized.
);
/**
* Returns the metadata for each block.
*
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/elements/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const __experimentalElementButtonClassName = 'wp-element-button';
1 change: 1 addition & 0 deletions packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
useCachedTruthy,
} from './hooks';
export * from './components';
export * from './elements';
export * from './utils';
export { storeConfig, store } from './store';
export { SETTINGS_DEFAULTS } from './store/defaults';
145 changes: 145 additions & 0 deletions packages/block-library/src/button/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,150 @@ const blockAttributes = {
},
};

const v11 = {
draganescu marked this conversation as resolved.
Show resolved Hide resolved
attributes: {
url: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'href',
},
title: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'title',
},
text: {
type: 'string',
source: 'html',
selector: 'a',
},
linkTarget: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'target',
},
rel: {
type: 'string',
source: 'attribute',
selector: 'a',
attribute: 'rel',
},
placeholder: {
type: 'string',
},
backgroundColor: {
type: 'string',
},
textColor: {
type: 'string',
},
gradient: {
type: 'string',
},
width: {
type: 'number',
},
},
supports: {
anchor: true,
align: true,
alignWide: false,
color: {
__experimentalSkipSerialization: true,
gradients: true,
__experimentalDefaultControls: {
background: true,
text: true,
},
},
typography: {
fontSize: true,
__experimentalFontFamily: true,
__experimentalDefaultControls: {
fontSize: true,
},
},
reusable: false,
spacing: {
__experimentalSkipSerialization: true,
padding: [ 'horizontal', 'vertical' ],
__experimentalDefaultControls: {
padding: true,
},
},
__experimentalBorder: {
radius: true,
__experimentalSkipSerialization: true,
__experimentalDefaultControls: {
radius: true,
},
},
__experimentalSelector: '.wp-block-button__link',
},
save( { attributes, className } ) {
const {
fontSize,
linkTarget,
rel,
style,
text,
title,
url,
width,
} = attributes;

if ( ! text ) {
return null;
}

const borderProps = getBorderClassesAndStyles( attributes );
const colorProps = getColorClassesAndStyles( attributes );
const spacingProps = getSpacingClassesAndStyles( attributes );
const buttonClasses = classnames(
'wp-block-button__link',
colorProps.className,
borderProps.className,
{
// For backwards compatibility add style that isn't provided via
// block support.
'no-border-radius': style?.border?.radius === 0,
}
);
const buttonStyle = {
...borderProps.style,
...colorProps.style,
...spacingProps.style,
};

// The use of a `title` attribute here is soft-deprecated, but still applied
// if it had already been assigned, for the sake of backward-compatibility.
// A title will no longer be assigned for new or updated button block links.

const wrapperClasses = classnames( className, {
[ `has-custom-width wp-block-button__width-${ width }` ]: width,
[ `has-custom-font-size` ]: fontSize || style?.typography?.fontSize,
} );

return (
<div { ...useBlockProps.save( { className: wrapperClasses } ) }>
<RichText.Content
tagName="a"
className={ buttonClasses }
href={ url }
title={ title }
style={ buttonStyle }
value={ text }
target={ linkTarget }
rel={ rel }
/>
</div>
);
},
};

const v10 = {
attributes: {
url: {
Expand Down Expand Up @@ -258,6 +402,7 @@ const v10 = {
};

const deprecated = [
v11,
v10,
{
supports: {
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
__experimentalUseColorProps as useColorProps,
__experimentalGetSpacingClassesAndStyles as useSpacingProps,
__experimentalLinkControl as LinkControl,
__experimentalElementButtonClassName,
} from '@wordpress/block-editor';
import { displayShortcut, isKeyboardEvent } from '@wordpress/keycodes';
import { link, linkOff } from '@wordpress/icons';
Expand Down Expand Up @@ -176,7 +177,8 @@ function ButtonEdit( props ) {
// For backwards compatibility add style that isn't
// provided via block support.
'no-border-radius': style?.border?.radius === 0,
}
},
__experimentalElementButtonClassName
) }
style={ {
...borderProps.style,
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/button/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
__experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles,
__experimentalElementButtonClassName,
} from '@wordpress/block-editor';

export default function save( { attributes, className } ) {
Expand Down Expand Up @@ -41,7 +42,8 @@ export default function save( { attributes, className } ) {
// For backwards compatibility add style that isn't provided via
// block support.
'no-border-radius': style?.border?.radius === 0,
}
},
__experimentalElementButtonClassName
);
const buttonStyle = {
...borderProps.style,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports[`Buttons block justify content sets Justify items right option 1`] = `
exports[`Buttons block when a button is shown adjusts the border radius 1`] = `
"<!-- wp:buttons -->
<div class=\\"wp-block-buttons\\"><!-- wp:button {\\"style\\":{\\"border\\":{\\"radius\\":\\"6px\\"}}} -->
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link\\" href=\\"\\" style=\\"border-radius:6px\\">Hello</a></div>
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link wp-element-button\\" href=\\"\\" style=\\"border-radius:6px\\">Hello</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->"
`;
2 changes: 1 addition & 1 deletion packages/block-library/src/buttons/test/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe( 'Buttons block', () => {
it( 'adjusts the border radius', async () => {
const initialHtml = `<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"style":{"border":{"radius":"5px"}}} -->
<div class="wp-block-button"><a class="wp-block-button__link" style="border-radius:5px" >Hello</a></div>
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" style="border-radius:5px" >Hello</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->`;
const { getByA11yLabel } = await initializeEditor( {
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/search/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
__experimentalUseBorderProps as useBorderProps,
__experimentalUseColorProps as useColorProps,
store as blockEditorStore,
__experimentalElementButtonClassName,
} from '@wordpress/block-editor';
import { useDispatch, useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
Expand Down Expand Up @@ -241,7 +242,8 @@ export default function SearchEdit( {
'wp-block-search__button',
colorProps.className,
isButtonPositionInside ? undefined : borderProps.className,
buttonUseIcon ? 'has-icon' : undefined
buttonUseIcon ? 'has-icon' : undefined,
__experimentalElementButtonClassName
);
const buttonStyles = {
...colorProps.style,
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ function render_block_core_search( $attributes ) {
</svg>';
}

// Include the button element class.
$button_classes .= ' ' . WP_Theme_JSON_GUTENBERG::__EXPERIMENTAL_ELEMENT_BUTTON_CLASS_NAME;

$button_markup = sprintf(
'<button type="submit" class="wp-block-search__button %s" %s %s>%s</button>',
esc_attr( $button_classes ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
exports[`Buttons can jump to the link editor using the keyboard shortcut 1`] = `
"<!-- wp:buttons -->
<div class=\\"wp-block-buttons\\"><!-- wp:button -->
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link\\" href=\\"https://www.wordpress.org/\\">WordPress</a></div>
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link wp-element-button\\" href=\\"https://www.wordpress.org/\\">WordPress</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->"
`;

exports[`Buttons dismisses link editor when escape is pressed 1`] = `
"<!-- wp:buttons -->
<div class=\\"wp-block-buttons\\"><!-- wp:button -->
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link\\">WordPress</a></div>
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link wp-element-button\\">WordPress</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->"
`;

exports[`Buttons has focus on button content (slash inserter) 1`] = `
"<!-- wp:buttons -->
<div class=\\"wp-block-buttons\\"><!-- wp:button -->
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link\\">Content</a></div>
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link wp-element-button\\">Content</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->"
`;

exports[`Buttons has focus on button content 1`] = `
"<!-- wp:buttons -->
<div class=\\"wp-block-buttons\\"><!-- wp:button -->
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link\\">Content</a></div>
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link wp-element-button\\">Content</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ exports[`Inserting blocks inserts a block in proper place after having clicked \
exports[`Inserting blocks inserts blocks at root level when using the root appender while selection is in an inner block 1`] = `
"<!-- wp:buttons -->
<div class=\\"wp-block-buttons\\"><!-- wp:button -->
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link\\">1.1</a></div>
<div class=\\"wp-block-button\\"><a class=\\"wp-block-button__link wp-element-button\\">1.1</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons -->

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:button {"className":"is-style-outline","style":{"border":{"radius":"10px"},"color":{"text":"#1b9b6c"}}} -->
<div class="wp-block-button is-style-outline"><a class="wp-block-button__link has-text-color" style="border-radius:10px;color:#1b9b6c">Where We Are</a></div>
<div class="wp-block-button is-style-outline"><a class="wp-block-button__link has-text-color wp-element-button" style="border-radius:10px;color:#1b9b6c">Where We Are</a></div>
<!-- /wp:button -->
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:button {"style":{"border":{"radius":"25px"}}} -->
<div class="wp-block-button"><a class="wp-block-button__link" style="border-radius:25px">My button</a></div>
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" style="border-radius:25px">My button</a></div>
<!-- /wp:button -->


Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:button {"align":"center"} -->
<div class="wp-block-button aligncenter"><a class="wp-block-button__link" href="https://github.com/WordPress/gutenberg">Help build Gutenberg</a></div>
<div class="wp-block-button aligncenter"><a class="wp-block-button__link wp-element-button" href="https://github.com/WordPress/gutenberg">Help build Gutenberg</a></div>
<!-- /wp:button -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:button {"fontFamily":"cambria-georgia"} -->
<div class="wp-block-button has-cambria-georgia-font-family"><a class="wp-block-button__link">My button</a></div>
<div class="wp-block-button has-cambria-georgia-font-family"><a class="wp-block-button__link wp-element-button">My button</a></div>
<!-- /wp:button -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:button {"style":{"border":{"radius":0},"color":{"text":"#1b9b6c","background":"#aa5a20"}}} -->
<div class="wp-block-button"><a class="wp-block-button__link no-border-radius has-text-color has-background" style="background-color:#aa5a20;color:#1b9b6c">My button</a></div>
<div class="wp-block-button"><a class="wp-block-button__link no-border-radius has-text-color has-background wp-element-button" style="background-color:#aa5a20;color:#1b9b6c">My button</a></div>
<!-- /wp:button -->
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
}
},
"innerBlocks": [],
"innerHTML": "\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link no-border-radius has-text-color has-background\" style=\"background-color:#aa5a20;color:#1b9b6c\">My button</a></div>\n",
"innerHTML": "\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link no-border-radius has-text-color has-background wp-element-button\" style=\"background-color:#aa5a20;color:#1b9b6c\">My button</a></div>\n",
"innerContent": [
"\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link no-border-radius has-text-color has-background\" style=\"background-color:#aa5a20;color:#1b9b6c\">My button</a></div>\n"
"\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link no-border-radius has-text-color has-background wp-element-button\" style=\"background-color:#aa5a20;color:#1b9b6c\">My button</a></div>\n"
]
}
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:button {"style":{"border":{"radius":0},"color":{"text":"#1b9b6c","background":"#aa5a20"}}} -->
<div class="wp-block-button"><a class="wp-block-button__link has-text-color has-background no-border-radius" style="color:#1b9b6c;background-color:#aa5a20">My button</a></div>
<div class="wp-block-button"><a class="wp-block-button__link has-text-color has-background no-border-radius wp-element-button" style="color:#1b9b6c;background-color:#aa5a20">My button</a></div>
<!-- /wp:button -->
6 changes: 3 additions & 3 deletions test/integration/fixtures/blocks/core__buttons.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<!-- wp:buttons {"align":"wide","layout":{"type":"flex","justifyContent":"center"}} -->
<div class="wp-block-buttons alignwide">
<!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link">My button 1</a></div>
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">My button 1</a></div>
<!-- /wp:button -->

<!-- wp:button -->
<div class="wp-block-button"><a class="wp-block-button__link">My button 2</a></div>
<div class="wp-block-button"><a class="wp-block-button__link wp-element-button">My button 2</a></div>
<!-- /wp:button -->
</div>
<!-- /wp:buttons -->
<!-- /wp:buttons -->
Loading