@wordpress/block-editor
Version:
57 lines (52 loc) • 1.78 kB
JavaScript
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { hasBlockSupport } from '@wordpress/blocks';
const ARIA_LABEL_SCHEMA = {
type: 'string',
source: 'attribute',
attribute: 'aria-label',
selector: '*'
};
/**
* Filters registered block settings, extending attributes with ariaLabel using aria-label
* of the first node.
*
* @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?.ariaLabel?.type) {
return settings;
}
if (hasBlockSupport(settings, 'ariaLabel')) {
// Gracefully handle if settings.attributes is undefined.
settings.attributes = { ...settings.attributes,
ariaLabel: ARIA_LABEL_SCHEMA
};
}
return settings;
}
/**
* Override props assigned to save component to inject aria-label, if block
* supports ariaLabel. 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, 'ariaLabel')) {
extraProps['aria-label'] = attributes.ariaLabel === '' ? null : attributes.ariaLabel;
}
return extraProps;
}
addFilter('blocks.registerBlockType', 'core/ariaLabel/attribute', addAttribute);
addFilter('blocks.getSaveContent.extraProps', 'core/ariaLabel/save-props', addSaveProps);
//# sourceMappingURL=aria-label.js.map