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

Allow blocks to use the role attribute #47868

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
80b1dc3
Allow button and group blocks to use role attribute
carolinan Feb 8, 2023
ada53f5
Use a block attribute instead of block support
carolinan Feb 28, 2023
911fc4a
Add the role attribute to the group block
carolinan Mar 1, 2023
a5ef6b6
Revert button block changes
carolinan Mar 1, 2023
9445b0b
Merge branch 'trunk' into add/role-attribute
carolinan Mar 1, 2023
08f9476
Revert the attribute again and use a block support
carolinan Mar 2, 2023
ca6b098
Merge branch 'trunk' into add/role-attribute
carolinan Mar 2, 2023
085b9f7
Update block-supports.md
carolinan Mar 2, 2023
0b8a93d
register the new block support
carolinan Mar 3, 2023
dace32b
Fix CS spacing issues
carolinan Mar 3, 2023
ef9a0c0
Merge branch 'trunk' into add/role-attribute
carolinan Mar 27, 2023
719530b
Merge branch 'trunk' into add/role-attribute
carolinan Apr 12, 2023
85b5757
Merge branch 'trunk' into add/role-attribute
carolinan Apr 19, 2023
9fe6536
Merge branch 'trunk' into add/role-attribute
carolinan Apr 21, 2023
e0190b5
Merge branch 'trunk' into add/role-attribute
carolinan May 11, 2023
129a074
Merge branch 'trunk' into add/role-attribute
carolinan Jun 26, 2023
4894c23
Merge branch 'trunk' into add/role-attribute
carolinan Aug 7, 2023
be52745
Merge branch 'trunk' into add/role-attribute
carolinan Sep 26, 2023
79044f1
Merge branch 'trunk' into add/role-attribute
carolinan Nov 23, 2023
186f209
Merge branch 'trunk' into add/role-attribute
carolinan Jan 8, 2024
5a77651
Merge branch 'trunk' into add/role-attribute
carolinan Jan 25, 2024
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 docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Gather blocks in a layout container. ([Source](https://github.com/WordPress/gute

- **Name:** core/group
- **Category:** design
- **Supports:** align (full, wide), anchor, ariaLabel, color (background, gradients, link, text), dimensions (minHeight), position (sticky), spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Supports:** align (full, wide), anchor, ariaLabel, color (background, gradients, link, text), dimensions (minHeight), position (sticky), role, spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** tagName, templateLock

## Heading
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import './layout';
import './content-lock-ui';
import './metadata';
import './metadata-name';
import './role';

export { useCustomSides } from './dimensions';
export { useLayoutClasses, useLayoutStyles } from './layout';
Expand Down
61 changes: 61 additions & 0 deletions packages/block-editor/src/hooks/role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { hasBlockSupport } from '@wordpress/blocks';

const ROLE_SCHEMA = {
type: 'string',
source: 'attribute',
attribute: 'role',
selector: '*',
};

/**
* Filters registered block settings, extending attributes with role.
*
* @param {Object} settings Original block settings.
*
* @return {Object} Filtered block settings.
*/
export function addAttribute( settings ) {
// Allow blocks to specify their own attribute definition with default values if needed.
if ( settings?.attributes?.role?.type ) {
return settings;
}
if ( hasBlockSupport( settings, 'role' ) ) {
// Gracefully handle if settings.attributes is undefined.
settings.attributes = {
...settings.attributes,
role: ROLE_SCHEMA,
};
}

return settings;
}

/**
* Override props assigned to save component to inject the role attribute, if block
* supports roles. This is only applied if the block's save result is an
* element and not a markup string.
*
* @param {Object} extraProps Additional props applied to save element.
* @param {Object} blockType Block type.
* @param {Object} attributes Current block attributes.
*
* @return {Object} Filtered props applied to save element.
*/
export function addSaveProps( extraProps, blockType, attributes ) {
if ( hasBlockSupport( blockType, 'role' ) ) {
extraProps.role = attributes.role === '' ? null : attributes.role;
}

return extraProps;
}

addFilter( 'blocks.registerBlockType', 'core/role/attribute', addAttribute );
addFilter(
'blocks.getSaveContent.extraProps',
'core/role/save-props',
addSaveProps
);
1 change: 1 addition & 0 deletions packages/block-library/src/group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"position": {
"sticky": true
},
"role": true,
"typography": {
"fontSize": true,
"lineHeight": true,
Expand Down
2 changes: 2 additions & 0 deletions schemas/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add new block support for the `role` attribute. ([#47868](https://github.com/WordPress/gutenberg/pull/47868/))

- Add new properties `settings.typography.fluid` and `settings.typography.fontSizes[n].fluidSize` to theme.json to enable fluid typography ([#39529](https://github.com/WordPress/gutenberg/pull/39529)).


carolinan marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
5 changes: 5 additions & 0 deletions schemas/json/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@
"description": "A block may want to disable the ability of being converted into a reusable block. By default all blocks can be converted to a reusable block. If supports reusable is set to false, the option to convert the block into a reusable block will not appear.",
"default": true
},
"role": {
"type": "boolean",
"description": "Role attributes let you add semantic meaning to non-semantic HTML elements. This property allows enabling the definition of a role for the block, without exposing a UI field.",
"default": false
},
"lock": {
"type": "boolean",
"description": "A block may want to disable the ability to toggle the lock state. It can be locked/unlocked by a user from the block 'Options' dropdown by default. To disable this behavior, set lock to false.",
Expand Down