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

Commit 85dc58a

Browse files
authored
Try harder to keep context menus inside the window (#7863)
* Try harder to keep context menus inside the window Signed-off-by: Robin Townsend <[email protected]> * Use UIStore for window dimensions Signed-off-by: Robin Townsend <[email protected]> * Test ContextMenu positioning Signed-off-by: Robin Townsend <[email protected]>
1 parent 6e143c3 commit 85dc58a

File tree

2 files changed

+101
-13
lines changed

2 files changed

+101
-13
lines changed

src/components/structures/ContextMenu.tsx

+42-13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { checkInputableElement, RovingTabIndexProvider } from "../../accessibili
3131
// of doing reusable widgets like dialog boxes & menus where we go and
3232
// pass in a custom control as the actual body.
3333

34+
const WINDOW_PADDING = 10;
3435
const ContextualMenuContainerId = "mx_ContextualMenu_Container";
3536

3637
function getOrCreateContainer(): HTMLDivElement {
@@ -247,21 +248,49 @@ export default class ContextMenu extends React.PureComponent<IProps, IState> {
247248

248249
if (chevronFace === ChevronFace.Top || chevronFace === ChevronFace.Bottom) {
249250
chevronOffset.left = props.chevronOffset;
250-
} else if (position.top !== undefined) {
251-
const target = position.top;
252-
253-
// By default, no adjustment is made
254-
let adjusted = target;
251+
} else {
252+
chevronOffset.top = props.chevronOffset;
253+
}
255254

256-
// If we know the dimensions of the context menu, adjust its position
257-
// such that it does not leave the (padded) window.
258-
if (contextMenuRect) {
259-
const padding = 10;
260-
adjusted = Math.min(position.top, document.body.clientHeight - contextMenuRect.height - padding);
255+
// If we know the dimensions of the context menu, adjust its position to
256+
// keep it within the bounds of the (padded) window
257+
const { windowWidth, windowHeight } = UIStore.instance;
258+
if (contextMenuRect) {
259+
if (position.top !== undefined) {
260+
position.top = Math.min(
261+
position.top,
262+
windowHeight - contextMenuRect.height - WINDOW_PADDING,
263+
);
264+
// Adjust the chevron if necessary
265+
if (chevronOffset.top !== undefined) {
266+
chevronOffset.top = props.chevronOffset + props.top - position.top;
267+
}
268+
} else if (position.bottom !== undefined) {
269+
position.bottom = Math.min(
270+
position.bottom,
271+
windowHeight - contextMenuRect.height - WINDOW_PADDING,
272+
);
273+
if (chevronOffset.top !== undefined) {
274+
chevronOffset.top = props.chevronOffset + props.bottom - position.bottom;
275+
}
276+
}
277+
if (position.left !== undefined) {
278+
position.left = Math.min(
279+
position.left,
280+
windowWidth - contextMenuRect.width - WINDOW_PADDING,
281+
);
282+
if (chevronOffset.left !== undefined) {
283+
chevronOffset.left = props.chevronOffset + props.left - position.left;
284+
}
285+
} else if (position.right !== undefined) {
286+
position.right = Math.min(
287+
position.right,
288+
windowWidth - contextMenuRect.width - WINDOW_PADDING,
289+
);
290+
if (chevronOffset.left !== undefined) {
291+
chevronOffset.left = props.chevronOffset + props.right - position.right;
292+
}
261293
}
262-
263-
position.top = adjusted;
264-
chevronOffset.top = Math.max(props.chevronOffset, props.chevronOffset + target - adjusted);
265294
}
266295

267296
let chevron;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React from "react";
18+
import { mount } from "enzyme";
19+
20+
import "../../../skinned-sdk";
21+
import ContextMenu, { ChevronFace } from "../../../../src/components/structures/ContextMenu.tsx";
22+
import UIStore from "../../../../src/stores/UIStore.ts";
23+
24+
describe("<ContextMenu />", () => {
25+
// Hardcode window and menu dimensions
26+
const windowSize = 300;
27+
const menuSize = 200;
28+
jest.spyOn(UIStore, "instance", "get").mockImplementation(() => ({
29+
windowWidth: windowSize,
30+
windowHeight: windowSize,
31+
}));
32+
window.Element.prototype.getBoundingClientRect = jest.fn().mockReturnValue({
33+
width: menuSize,
34+
height: menuSize,
35+
});
36+
37+
const targetY = windowSize - menuSize + 50;
38+
const targetChevronOffset = 25;
39+
40+
const wrapper = mount(
41+
<ContextMenu
42+
top={targetY}
43+
left={0}
44+
chevronFace={ChevronFace.Right}
45+
chevronOffset={targetChevronOffset}
46+
/>,
47+
);
48+
const chevron = wrapper.find(".mx_ContextualMenu_chevron_right");
49+
50+
const actualY = parseInt(wrapper.getDOMNode().style.getPropertyValue("top"));
51+
const actualChevronOffset = parseInt(chevron.getDOMNode().style.getPropertyValue("top"));
52+
53+
it("stays within the window", () => {
54+
expect(actualY + menuSize).toBeLessThanOrEqual(windowSize);
55+
});
56+
it("positions the chevron correctly", () => {
57+
expect(actualChevronOffset).toEqual(targetChevronOffset + targetY - actualY);
58+
});
59+
});

0 commit comments

Comments
 (0)