Skip to content

Commit

Permalink
Refactor BlockSwitcher - make function component and new specific sel…
Browse files Browse the repository at this point in the history
…ector `getBlockTransformItems` (#27674)

* Refactor BlockSwitcher - function component and break into more files

* new selector for block transforms

* jsdoc selectors

* fix BlockSwitcher tests

* getBlockTransformItems tests

* make BlockStylesMenu named function

* move icon logic in `useSelect`

* fix unit tests
  • Loading branch information
ntsekouras authored Dec 21, 2020
1 parent 35bd1cb commit 2db6a54
Show file tree
Hide file tree
Showing 8 changed files with 654 additions and 343 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,40 @@ _Returns_

- `?string`: Client ID of block selection start.

<a name="getBlockTransformItems" href="#getBlockTransformItems">#</a> **getBlockTransformItems**

Determines the items that appear in the available block transforms list.

Each item object contains what's necessary to display a menu item in the
transform list and handle its selection.

The 'frecency' property is a heuristic (<https://en.wikipedia.org/wiki/Frecency>)
that combines block usage frequenty and recency.

Items are returned ordered descendingly by their 'frecency'.

_Parameters_

- _state_ `Object`: Editor state.
- _rootClientId_ `?string`: Optional root client ID of block list.

_Returns_

- `Array<WPEditorTransformItem>`: Items that appear in inserter.

_Type Definition_

- _WPEditorTransformItem_ `Object`

_Properties_

- _id_ `string`: Unique identifier for the item.
- _name_ `string`: The type of block to create.
- _title_ `string`: Title of the item, as it appears in the inserter.
- _icon_ `string`: Dashicon for the item, as it appears in the inserter.
- _isDisabled_ `boolean`: Whether or not the user should be prevented from inserting this item.
- _frecency_ `number`: Heuristic that combines frequency and recency.

<a name="getClientIdsOfDescendants" href="#getClientIdsOfDescendants">#</a> **getClientIdsOfDescendants**

Returns an array containing the clientIds of all descendants
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { MenuGroup } from '@wordpress/components';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import BlockStyles from '../block-styles';
import PreviewBlockPopover from './preview-block-popover';

export default function BlockStylesMenu( { hoveredBlock, onSwitch } ) {
const [ hoveredClassName, setHoveredClassName ] = useState();
return (
<MenuGroup
label={ __( 'Styles' ) }
className="block-editor-block-switcher__styles__menugroup"
>
{ hoveredClassName && (
<PreviewBlockPopover
hoveredBlock={ hoveredBlock }
hoveredClassName={ hoveredClassName }
/>
) }
<BlockStyles
clientId={ hoveredBlock.clientId }
onSwitch={ onSwitch }
onHoverClassName={ setHoveredClassName }
itemRole="menuitem"
/>
</MenuGroup>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const BlockTransformationsMenu = ( {
return (
<MenuGroup label={ __( 'Transform to' ) } className={ className }>
{ possibleBlockTransformations.map( ( item ) => {
const { name, icon, title } = item;
const { name, icon, title, isDisabled } = item;
return (
<MenuItem
key={ name }
Expand All @@ -27,6 +27,7 @@ const BlockTransformationsMenu = ( {
event.preventDefault();
onSelect( name );
} }
disabled={ isDisabled }
>
<BlockIcon icon={ icon } showColors />
{ title }
Expand Down
Loading

0 comments on commit 2db6a54

Please sign in to comment.