Skip to content

Commit

Permalink
Try anchoring the position of the settings dropdown to the right clic…
Browse files Browse the repository at this point in the history
…k position
  • Loading branch information
andrewserong committed May 27, 2022
1 parent d2bc061 commit 3b48415
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function BlockSettingsDropdown( {
clientIds,
__experimentalSelectBlock,
children,
popoverProps,
...props
} ) {
const blockClientIds = castArray( clientIds );
Expand Down Expand Up @@ -195,7 +196,7 @@ export function BlockSettingsDropdown( {
icon={ moreVertical }
label={ __( 'Options' ) }
className="block-editor-block-settings-menu"
popoverProps={ POPOVER_PROPS }
popoverProps={ { ...POPOVER_PROPS, ...popoverProps } }
noIcons
{ ...props }
>
Expand Down
17 changes: 16 additions & 1 deletion packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function ListViewBlock( {
const cellRef = useRef( null );
const settingsRef = useRef( null );
const [ isHovered, setIsHovered ] = useState( false );
const [ settingsAnchorRect, setSettingsAnchorRect ] = useState();
const { clientId } = block;
const isFirstSelectedBlock =
isSelected && selectedClientIds[ 0 ] === clientId;
Expand Down Expand Up @@ -183,11 +184,21 @@ function ListViewBlock( {
// Allow right-clicking an item in the List View to open up the block settings dropdown.
const onContextMenu = useCallback( ( event ) => {
if ( showBlockActions && allowRightClickOverrides ) {
event.preventDefault();
settingsRef.current?.click();
// Ensure the position of the settings dropdown is at the cursor.
setSettingsAnchorRect(
new window.DOMRect( event.clientX, event.clientY, 0, 0 )
);
event.preventDefault();
}
} );

const clearSettingsAnchorRect = useCallback( () => {
// Clear the custom position for the settings dropdown so that it is restored back
// to being anchored to the DropdownMenu toggle button.
setSettingsAnchorRect( undefined );
} );

let colSpan;
if ( hasRenderedMovers ) {
colSpan = 2;
Expand Down Expand Up @@ -307,10 +318,14 @@ function ListViewBlock( {
clientIds={ dropdownClientIds }
icon={ moreVertical }
label={ settingsAriaLabel }
popoverProps={ {
anchorRect: settingsAnchorRect, // Used to position the settings at the cursor on right-click.
} }
toggleProps={ {
ref,
className: 'block-editor-list-view-block__menu',
tabIndex,
onClick: clearSettingsAnchorRect,
onFocus,
} }
disableOpenOnArrowDown
Expand Down

0 comments on commit 3b48415

Please sign in to comment.