UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 2.48 kB
{ "version": 3, "sources": ["../../src/hooks/aria-label.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { addFilter } from '@wordpress/hooks';\nimport { hasBlockSupport } from '@wordpress/blocks';\n\n/**\n * Filters registered block settings, extending attributes with ariaLabel using aria-label\n * of the first node.\n *\n * @param {Object} settings Original block settings.\n *\n * @return {Object} Filtered block settings.\n */\nexport function addAttribute( settings ) {\n\t// Allow blocks to specify their own attribute definition with default values if needed.\n\tif ( settings?.attributes?.ariaLabel?.type ) {\n\t\treturn settings;\n\t}\n\tif ( hasBlockSupport( settings, 'ariaLabel' ) ) {\n\t\t// Gracefully handle if settings.attributes is undefined.\n\t\tsettings.attributes = {\n\t\t\t...settings.attributes,\n\t\t\tariaLabel: {\n\t\t\t\ttype: 'string',\n\t\t\t},\n\t\t};\n\t}\n\n\treturn settings;\n}\n\n/**\n * Override props assigned to save component to inject aria-label, if block\n * supports ariaLabel. This is only applied if the block's save result is an\n * element and not a markup string.\n *\n * @param {Object} extraProps Additional props applied to save element.\n * @param {Object} blockType Block type.\n * @param {Object} attributes Current block attributes.\n *\n * @return {Object} Filtered props applied to save element.\n */\nexport function addSaveProps( extraProps, blockType, attributes ) {\n\tif ( hasBlockSupport( blockType, 'ariaLabel' ) ) {\n\t\textraProps[ 'aria-label' ] =\n\t\t\tattributes.ariaLabel === '' ? null : attributes.ariaLabel;\n\t}\n\n\treturn extraProps;\n}\n\nexport default {\n\taddSaveProps,\n\tattributeKeys: [ 'ariaLabel' ],\n\thasSupport( name ) {\n\t\treturn hasBlockSupport( name, 'ariaLabel' );\n\t},\n};\n\naddFilter(\n\t'blocks.registerBlockType',\n\t'core/ariaLabel/attribute',\n\taddAttribute\n);\n"], "mappings": ";AAGA,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAUzB,SAAS,aAAc,UAAW;AAExC,MAAK,UAAU,YAAY,WAAW,MAAO;AAC5C,WAAO;AAAA,EACR;AACA,MAAK,gBAAiB,UAAU,WAAY,GAAI;AAE/C,aAAS,aAAa;AAAA,MACrB,GAAG,SAAS;AAAA,MACZ,WAAW;AAAA,QACV,MAAM;AAAA,MACP;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AACR;AAaO,SAAS,aAAc,YAAY,WAAW,YAAa;AACjE,MAAK,gBAAiB,WAAW,WAAY,GAAI;AAChD,eAAY,YAAa,IACxB,WAAW,cAAc,KAAK,OAAO,WAAW;AAAA,EAClD;AAEA,SAAO;AACR;AAEA,IAAO,qBAAQ;AAAA,EACd;AAAA,EACA,eAAe,CAAE,WAAY;AAAA,EAC7B,WAAY,MAAO;AAClB,WAAO,gBAAiB,MAAM,WAAY;AAAA,EAC3C;AACD;AAEA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AACD;", "names": [] }