Virtual whiteboard for sketching hand-drawn like diagrams
ensure contextMenu doesn't overflow viewport (#364)
David Luzar8104c8525d6f
diff --git a/src/components/ContextMenu.tsx b/src/components/ContextMenu.tsxindex c367d82461..8053e226a1 100644--- a/src/components/ContextMenu.tsx+++ b/src/components/ContextMenu.tsx@@ -18,7 +18,12 @@ type Props = {function ContextMenu({ options, onCloseRequest, top, left }: Props) {return (- <Popover onCloseRequest={onCloseRequest} top={top} left={left}>+ <Popover+ onCloseRequest={onCloseRequest}+ top={top}+ left={left}+ fitInViewport={true}+ ><ul className="context-menu" onContextMenu={e => e.preventDefault()}>{options.map((option, idx) => (<lidiff --git a/src/components/Popover.tsx b/src/components/Popover.tsxindex 522f7b3512..613a9d6a3a 100644--- a/src/components/Popover.tsx+++ b/src/components/Popover.tsx@@ -1,15 +1,41 @@-import React from "react";+import React, { useLayoutEffect, useRef } from "react";type Props = {top?: number;left?: number;children?: React.ReactNode;onCloseRequest?(): void;+ fitInViewport?: boolean;};-export function Popover({ children, left, onCloseRequest, top }: Props) {+export function Popover({+ children,+ left,+ top,+ onCloseRequest,+ fitInViewport = false+}: Props) {+ const popoverRef = useRef<HTMLDivElement>(null);++ // ensure the popover doesn't overflow the viewport+ useLayoutEffect(() => {+ if (fitInViewport && popoverRef.current) {+ const element = popoverRef.current;+ const { x, y, width, height } = element.getBoundingClientRect();++ const viewportWidth = window.innerWidth;+ if (x + width > viewportWidth) {+ element.style.left = viewportWidth - width + "px";+ }+ const viewportHeight = window.innerHeight;+ if (y + height > viewportHeight) {+ element.style.top = viewportHeight - height + "px";+ }+ }+ }, [fitInViewport]);+return (- <div className="popover" style={{ top: top, left: left }}>+ <div className="popover" style={{ top: top, left: left }} ref={popoverRef}><divclassName="cover"onClick={onCloseRequest}