Skip to content

Commit

Permalink
Use the block title within aria description
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Dwyer authored and getdave committed Jan 12, 2023
1 parent 2b2bd76 commit d0c2283
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import { speak } from '@wordpress/a11y';
import { __, _x, sprintf } from '@wordpress/i18n';
import { Dropdown, Button } from '@wordpress/components';
import { Component } from '@wordpress/element';
import { withDispatch, withSelect } from '@wordpress/data';
import { useSelect, withDispatch, withSelect } from '@wordpress/data';
import { compose, ifCondition } from '@wordpress/compose';
import { createBlock, store as blocksStore } from '@wordpress/blocks';
import {
createBlock,
store as blocksStore,
__experimentalGetBlockLabel as getBlockLabel,
getBlockType,
} from '@wordpress/blocks';
import { plus } from '@wordpress/icons';

/**
Expand All @@ -22,6 +27,15 @@ import InserterMenu from './menu';
import QuickInserter from './quick-inserter';
import { store as blockEditorStore } from '../../store';

// Duplicated from elsewhere.
function getBlockDisplayText( block ) {
if ( block ) {
const blockType = getBlockType( block.name );
return blockType ? getBlockLabel( blockType, block.attributes ) : null;
}
return null;
}

const defaultRenderToggle = ( {
onToggle,
disabled,
Expand All @@ -31,10 +45,19 @@ const defaultRenderToggle = ( {
toggleProps = {},
prioritizePatterns,
label,
rootClientId,
} ) => {
let buttonLabel;
const {
getBlock,
} = useSelect( blockEditorStore );
const rootBlock = getBlock( rootClientId );
const rootBlockTitle = getBlockDisplayText( rootBlock );

if ( label ) {
let buttonLabel;
if ( rootBlockTitle ) {
/* translators: %s is a block name, e.g. Navigation */
buttonLabel = sprintf( __('Add block to %s' ), rootBlockTitle );
} else if ( label ) {
buttonLabel = label;
} else if ( hasSingleBlockType ) {
buttonLabel = sprintf(
Expand Down Expand Up @@ -117,6 +140,8 @@ class Inserter extends Component {
renderToggle = defaultRenderToggle,
prioritizePatterns,
toggleLabel,
clientId,
rootClientId,
} = this.props;

return renderToggle( {
Expand All @@ -129,6 +154,8 @@ class Inserter extends Component {
toggleProps,
prioritizePatterns,
label: toggleLabel,
clientId,
rootClientId,
} );
}

Expand Down

0 comments on commit d0c2283

Please sign in to comment.