Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 7c41b86

Browse files
Put the right-click message context menu to the right (#8339)
* Improve `alwaysAboveRightOf()` typing Signed-off-by: Šimon Brandner <[email protected]> * Improve typing of `alwaysAboveLeftOf()` Signed-off-by: Šimon Brandner <[email protected]> * Add `aboveRightOf()` Signed-off-by: Šimon Brandner <[email protected]> * Use `aboveRightOf()` Signed-off-by: Šimon Brandner <[email protected]> * Fix typo Signed-off-by: Šimon Brandner <[email protected]>
1 parent 741b13a commit 7c41b86

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/components/structures/ContextMenu.tsx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,37 @@ export const aboveLeftOf = (
450450
return menuOptions;
451451
};
452452

453+
// Placement method for <ContextMenu /> to position context menu right-aligned and flowing to the right of elementRect,
454+
// and either above or below: wherever there is more space (maybe this should be aboveOrBelowRightOf?)
455+
export const aboveRightOf = (
456+
elementRect: Pick<DOMRect, "left" | "top" | "bottom">,
457+
chevronFace = ChevronFace.None,
458+
vPadding = 0,
459+
): AboveLeftOf => {
460+
const menuOptions: IPosition & { chevronFace: ChevronFace } = { chevronFace };
461+
462+
const buttonLeft = elementRect.left + window.pageXOffset;
463+
const buttonBottom = elementRect.bottom + window.pageYOffset;
464+
const buttonTop = elementRect.top + window.pageYOffset;
465+
// Align the left edge of the menu to the left edge of the button
466+
menuOptions.left = buttonLeft;
467+
// Align the menu vertically on whichever side of the button has more space available.
468+
if (buttonBottom < UIStore.instance.windowHeight / 2) {
469+
menuOptions.top = buttonBottom + vPadding;
470+
} else {
471+
menuOptions.bottom = (UIStore.instance.windowHeight - buttonTop) + vPadding;
472+
}
473+
474+
return menuOptions;
475+
};
476+
453477
// Placement method for <ContextMenu /> to position context menu right-aligned and flowing to the left of elementRect
454478
// and always above elementRect
455-
export const alwaysAboveLeftOf = (elementRect: DOMRect, chevronFace = ChevronFace.None, vPadding = 0) => {
479+
export const alwaysAboveLeftOf = (
480+
elementRect: Pick<DOMRect, "right" | "bottom" | "top">,
481+
chevronFace = ChevronFace.None,
482+
vPadding = 0,
483+
) => {
456484
const menuOptions: IPosition & { chevronFace: ChevronFace } = { chevronFace };
457485

458486
const buttonRight = elementRect.right + window.pageXOffset;
@@ -472,7 +500,11 @@ export const alwaysAboveLeftOf = (elementRect: DOMRect, chevronFace = ChevronFac
472500

473501
// Placement method for <ContextMenu /> to position context menu right-aligned and flowing to the right of elementRect
474502
// and always above elementRect
475-
export const alwaysAboveRightOf = (elementRect: DOMRect, chevronFace = ChevronFace.None, vPadding = 0) => {
503+
export const alwaysAboveRightOf = (
504+
elementRect: Pick<DOMRect, "left" | "top">,
505+
chevronFace = ChevronFace.None,
506+
vPadding = 0,
507+
) => {
476508
const menuOptions: IPosition & { chevronFace: ChevronFace } = { chevronFace };
477509

478510
const buttonLeft = elementRect.left + window.pageXOffset;

src/components/views/rooms/EventTile.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { E2EState } from "./E2EIcon";
3939
import { toRem } from "../../../utils/units";
4040
import RoomAvatar from "../avatars/RoomAvatar";
4141
import MessageContextMenu, { IEventTileOps } from "../context_menus/MessageContextMenu";
42-
import { aboveLeftOf } from '../../structures/ContextMenu';
42+
import { aboveRightOf } from '../../structures/ContextMenu';
4343
import { objectHasDiff } from "../../../utils/objects";
4444
import Tooltip from "../elements/Tooltip";
4545
import EditorStateTransfer from "../../../utils/EditorStateTransfer";
@@ -230,7 +230,7 @@ interface IState {
230230

231231
// Position of the context menu
232232
contextMenu?: {
233-
position: Pick<DOMRect, "right" | "top" | "bottom">;
233+
position: Pick<DOMRect, "top" | "left" | "bottom">;
234234
showPermalink?: boolean;
235235
};
236236

@@ -972,7 +972,7 @@ export class UnwrappedEventTile extends React.Component<IProps, IState> {
972972
this.setState({
973973
contextMenu: {
974974
position: {
975-
right: ev.clientX,
975+
left: ev.clientX,
976976
top: ev.clientY,
977977
bottom: ev.clientY,
978978
},
@@ -1017,7 +1017,7 @@ export class UnwrappedEventTile extends React.Component<IProps, IState> {
10171017

10181018
return (
10191019
<MessageContextMenu
1020-
{...aboveLeftOf(this.state.contextMenu.position)}
1020+
{...aboveRightOf(this.state.contextMenu.position)}
10211021
mxEvent={this.props.mxEvent}
10221022
permalinkCreator={this.props.permalinkCreator}
10231023
eventTileOps={eventTileOps}

0 commit comments

Comments
 (0)