fix: Right-click object menu displays partially off-screen (#4572) (#4631)

This commit is contained in:
zsviczian 2022-01-23 17:28:38 +01:00 committed by GitHub
parent 24bf4cb5fb
commit c009e03c8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View File

@ -37,6 +37,10 @@ const ContextMenu = ({
top={top}
left={left}
fitInViewport={true}
offsetLeft={appState.offsetLeft}
offsetTop={appState.offsetTop}
viewportWidth={appState.width}
viewportHeight={appState.height}
>
<ul
className="context-menu"

View File

@ -8,6 +8,10 @@ type Props = {
children?: React.ReactNode;
onCloseRequest?(event: PointerEvent): void;
fitInViewport?: boolean;
offsetLeft?: number;
offsetTop?: number;
viewportWidth?: number;
viewportHeight?: number;
};
export const Popover = ({
@ -16,6 +20,10 @@ export const Popover = ({
top,
onCloseRequest,
fitInViewport = false,
offsetLeft = 0,
offsetTop = 0,
viewportWidth = window.innerWidth,
viewportHeight = window.innerHeight,
}: Props) => {
const popoverRef = useRef<HTMLDivElement>(null);
@ -24,17 +32,14 @@ export const Popover = ({
if (fitInViewport && popoverRef.current) {
const element = popoverRef.current;
const { x, y, width, height } = element.getBoundingClientRect();
const viewportWidth = window.innerWidth;
if (x + width > viewportWidth) {
if (x + width - offsetLeft > viewportWidth) {
element.style.left = `${viewportWidth - width}px`;
}
const viewportHeight = window.innerHeight;
if (y + height > viewportHeight) {
if (y + height - offsetTop > viewportHeight) {
element.style.top = `${viewportHeight - height}px`;
}
}
}, [fitInViewport]);
}, [fitInViewport, viewportWidth, viewportHeight, offsetLeft, offsetTop]);
useEffect(() => {
if (onCloseRequest) {