Skip to content

Commit

Permalink
List View: Allow custom more menus to be passed to the component
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Mar 16, 2023
1 parent c9b83b5 commit 76679ff
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
4 changes: 4 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ _Returns_

Undocumented declaration.

### BlockSettingsDropdown

Undocumented declaration.

### BlockSettingsMenu

Undocumented declaration.
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export {
useBlockSelectionClearer as __unstableUseBlockSelectionClearer,
} from './block-selection-clearer';
export { default as BlockSettingsMenu } from './block-settings-menu';
export { default as BlockSettingsDropdown } from './block-settings-menu/block-settings-dropdown.js';
export { default as BlockSettingsMenuControls } from './block-settings-menu-controls';
export { default as BlockTitle } from './block-title';
export { default as BlockToolbar } from './block-toolbar';
Expand Down
37 changes: 20 additions & 17 deletions packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
BlockMoverDownButton,
} from '../block-mover/button';
import ListViewBlockContents from './block-contents';
import BlockSettingsDropdown from '../block-settings-menu/block-settings-dropdown';
import { useListViewContext } from './context';
import { getBlockPositionDescription } from './utils';
import { store as blockEditorStore } from '../../store';
Expand Down Expand Up @@ -135,7 +134,8 @@ function ListViewBlock( {
)
: __( 'Options' );

const { isTreeGridMounted, expand, collapse } = useListViewContext();
const { isTreeGridMounted, expand, collapse, MoreMenuComponent } =
useListViewContext();

const hasSiblings = siblingBlockCount > 0;
const hasRenderedMovers = showBlockMovers && hasSiblings;
Expand Down Expand Up @@ -326,21 +326,24 @@ function ListViewBlock( {
className={ listViewBlockSettingsClassName }
aria-selected={ !! isSelected || forceSelectionContentLock }
>
{ ( { ref, tabIndex, onFocus } ) => (
<BlockSettingsDropdown
clientIds={ dropdownClientIds }
icon={ moreVertical }
label={ settingsAriaLabel }
toggleProps={ {
ref,
className: 'block-editor-list-view-block__menu',
tabIndex,
onFocus,
} }
disableOpenOnArrowDown
__experimentalSelectBlock={ updateSelection }
/>
) }
{ MoreMenuComponent &&
( ( { ref, tabIndex, onFocus } ) => (
<MoreMenuComponent
clientIds={ dropdownClientIds }
block={ block }
icon={ moreVertical }
label={ settingsAriaLabel }
toggleProps={ {
ref,
className:
'block-editor-list-view-block__menu',
tabIndex,
onFocus,
} }
disableOpenOnArrowDown
__experimentalSelectBlock={ updateSelection }
/>
) ) }
</TreeGridCell>
) }
</ListViewLeaf>
Expand Down
31 changes: 23 additions & 8 deletions packages/block-editor/src/components/list-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,22 @@ export const BLOCK_LIST_ITEM_HEIGHT = 36;
/**
* Show a hierarchical list of blocks.
*
* @param {Object} props Components props.
* @param {string} props.id An HTML element id for the root element of ListView.
* @param {Array} props.blocks Custom subset of block client IDs to be used instead of the default hierarchy.
* @param {boolean} props.showBlockMovers Flag to enable block movers
* @param {boolean} props.isExpanded Flag to determine whether nested levels are expanded by default.
* @param {Object} ref Forwarded ref
* @param {Object} props Components props.
* @param {string} props.id An HTML element id for the root element of ListView.
* @param {Array} props.blocks Custom subset of block client IDs to be used instead of the default hierarchy.
* @param {boolean} props.showBlockMovers Flag to enable block movers
* @param {boolean} props.isExpanded Flag to determine whether nested levels are expanded by default.
* @param {Object} props.MoreMenuComponent Optional more menu substitution.
* @param {Object} ref Forwarded ref
*/
function ListView(
{ id, blocks, showBlockMovers = false, isExpanded = false },
{
id,
blocks,
showBlockMovers = false,
isExpanded = false,
MoreMenuComponent,
},
ref
) {
const { clientIdsTree, draggedClientIds, selectedClientIds } =
Expand Down Expand Up @@ -170,8 +177,16 @@ function ListView(
expandedState,
expand,
collapse,
MoreMenuComponent,
} ),
[ isMounted.current, draggedClientIds, expandedState, expand, collapse ]
[
isMounted.current,
draggedClientIds,
expandedState,
expand,
collapse,
MoreMenuComponent,
]
);

// If there are no blocks to show, do not render the list view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ function AddSubmenuItem( { block, onClose } ) {
}

export default function LeafMoreMenu( props ) {
const { clientId, block } = props;
const { block } = props;
const { clientId } = block;

const { moveBlocksDown, moveBlocksUp, removeBlocks } =
useDispatch( blockEditorStore );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* WordPress dependencies
*/
import { __experimentalListView as ListView } from '@wordpress/block-editor';
import {
__experimentalListView as ListView,
BlockSettingsDropdown,
} from '@wordpress/block-editor';
import { Button } from '@wordpress/components';
import {
useFocusOnMount,
Expand Down Expand Up @@ -59,7 +62,7 @@ export default function ListViewSidebar() {
focusOnMountRef,
] ) }
>
<ListView />
<ListView MoreMenuComponent={ BlockSettingsDropdown } />
</div>
</div>
);
Expand Down

0 comments on commit 76679ff

Please sign in to comment.