Skip to content

Commit

Permalink
List View: Allow the component to show a custom "more" menu. (#48097)
Browse files Browse the repository at this point in the history
* List View: Allow custom more menus to be passed to the component

* Update packages/edit-site/src/components/secondary-sidebar/list-view-sidebar.js

Co-authored-by: Alex Lende <alex@lende.xyz>

* fix prop name

* Set a default for the prop

* lock the blockSettingsMenu prop

* unconnected change

* Improve jsdoc

---------

Co-authored-by: Alex Lende <alex@lende.xyz>
Co-authored-by: Daniel Richards <daniel.richards@automattic.com>
  • Loading branch information
3 people committed Mar 28, 2023
1 parent 113e858 commit 65cd782
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
9 changes: 5 additions & 4 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, BlockSettingsMenu } =
useListViewContext();

const hasSiblings = siblingBlockCount > 0;
const hasRenderedMovers = showBlockMovers && hasSiblings;
Expand Down Expand Up @@ -321,14 +321,15 @@ function ListViewBlock( {
</>
) }

{ showBlockActions && (
{ showBlockActions && BlockSettingsMenu && (
<TreeGridCell
className={ listViewBlockSettingsClassName }
aria-selected={ !! isSelected || forceSelectionContentLock }
>
{ ( { ref, tabIndex, onFocus } ) => (
<BlockSettingsDropdown
<BlockSettingsMenu
clientIds={ dropdownClientIds }
block={ block }
icon={ moreVertical }
label={ settingsAriaLabel }
toggleProps={ {
Expand Down
39 changes: 30 additions & 9 deletions packages/block-editor/src/components/list-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import useListViewClientIds from './use-list-view-client-ids';
import useListViewDropZone from './use-list-view-drop-zone';
import useListViewExpandSelectedItem from './use-list-view-expand-selected-item';
import { store as blockEditorStore } from '../../store';
import { BlockSettingsDropdown } from '../block-settings-menu/block-settings-dropdown';

const expanded = ( state, action ) => {
if ( Array.isArray( action.clientIds ) ) {
Expand All @@ -47,16 +48,20 @@ const expanded = ( state, action ) => {

export const BLOCK_LIST_ITEM_HEIGHT = 36;

/** @typedef {import('react').ComponentType} ComponentType */
/** @typedef {import('react').Ref<HTMLElement>} Ref */

/**
* 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 {boolean} props.showAppender Flag to show or hide the block appender.
* @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. Defaults to `false`.
* @param {?boolean} props.isExpanded Flag to determine whether nested levels are expanded by default. Defaults to `false`.
* @param {?boolean} props.showAppender Flag to show or hide the block appender. Defaults to `false`.
* @param {?ComponentType} props.blockSettingsMenu Optional more menu substitution. Defaults to the standard `BlockSettingsDropdown` component.
* @param {Ref} ref Forwarded ref
*/
function ListViewComponent(
{
Expand All @@ -65,6 +70,7 @@ function ListViewComponent(
showBlockMovers = false,
isExpanded = false,
showAppender = false,
blockSettingsMenu: BlockSettingsMenu = BlockSettingsDropdown,
},
ref
) {
Expand Down Expand Up @@ -177,8 +183,16 @@ function ListViewComponent(
expandedState,
expand,
collapse,
BlockSettingsMenu,
} ),
[ isMounted.current, draggedClientIds, expandedState, expand, collapse ]
[
isMounted.current,
draggedClientIds,
expandedState,
expand,
collapse,
BlockSettingsMenu,
]
);

// If there are no blocks to show, do not render the list view.
Expand Down Expand Up @@ -221,5 +235,12 @@ function ListViewComponent(
export const PrivateListView = forwardRef( ListViewComponent );

export default forwardRef( ( props, ref ) => {
return <PrivateListView ref={ ref } { ...props } showAppender={ false } />;
return (
<PrivateListView
ref={ ref }
{ ...props }
showAppender={ false }
blockSettingsMenu={ BlockSettingsDropdown }
/>
);
} );

0 comments on commit 65cd782

Please sign in to comment.