Skip to content

Commit

Permalink
Add class name for inserter menu items to allow basic styling
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityrail committed May 18, 2018
1 parent 0774c47 commit f838d07
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions blocks/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export {
default as serialize,
getBlockContent,
getBlockDefaultClassName,
getBlockMenuDefaultClassName,
getSaveElement,
} from './serializer';
export { isValidBlock } from './validation';
Expand Down
15 changes: 15 additions & 0 deletions blocks/api/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ export function getBlockDefaultClassName( blockName ) {
return applyFilters( 'blocks.getBlockDefaultClassName', className, blockName );
}

/**
* Returns the block's default menu item classname from its name.
*
* @param {string} blockName The block name.
*
* @return {string} The block's default menu item class.
*/
export function getBlockMenuDefaultClassName( blockName ) {
// Generated HTML classes for blocks follow the `editor-block-list-item-{name}` nomenclature.
// Blocks provided by WordPress drop the prefixes 'core/' or 'core-' (used in 'core-embed/').
const className = 'editor-block-list-item-' + blockName.replace( /\//, '-' ).replace( /^core-/, '' );

return applyFilters( 'blocks.getBlockMenuDefaultClassName', className, blockName );
}

/**
* Given a block type containing a save render implementation and attributes, returns the
* enhanced element to be saved or string when raw HTML expected.
Expand Down
4 changes: 3 additions & 1 deletion editor/components/inserter/item-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
* External dependencies
*/
import { isEqual } from 'lodash';
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { NavigableMenu } from '@wordpress/components';
import { getBlockMenuDefaultClassName } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -72,7 +74,7 @@ class ItemList extends Component {
<button
role="menuitem"
key={ item.id }
className="editor-inserter__item"
className={ classnames( 'editor-inserter__item', getBlockMenuDefaultClassName( item.id ) ) }
onClick={ () => onSelect( item ) }
tabIndex={ isCurrent || item.isDisabled ? null : '-1' }
disabled={ item.isDisabled }
Expand Down

0 comments on commit f838d07

Please sign in to comment.