This commit is contained in:
parent
24bf4cb5fb
commit
c009e03c8e
@ -37,6 +37,10 @@ const ContextMenu = ({
|
|||||||
top={top}
|
top={top}
|
||||||
left={left}
|
left={left}
|
||||||
fitInViewport={true}
|
fitInViewport={true}
|
||||||
|
offsetLeft={appState.offsetLeft}
|
||||||
|
offsetTop={appState.offsetTop}
|
||||||
|
viewportWidth={appState.width}
|
||||||
|
viewportHeight={appState.height}
|
||||||
>
|
>
|
||||||
<ul
|
<ul
|
||||||
className="context-menu"
|
className="context-menu"
|
||||||
|
@ -8,6 +8,10 @@ type Props = {
|
|||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
onCloseRequest?(event: PointerEvent): void;
|
onCloseRequest?(event: PointerEvent): void;
|
||||||
fitInViewport?: boolean;
|
fitInViewport?: boolean;
|
||||||
|
offsetLeft?: number;
|
||||||
|
offsetTop?: number;
|
||||||
|
viewportWidth?: number;
|
||||||
|
viewportHeight?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Popover = ({
|
export const Popover = ({
|
||||||
@ -16,6 +20,10 @@ export const Popover = ({
|
|||||||
top,
|
top,
|
||||||
onCloseRequest,
|
onCloseRequest,
|
||||||
fitInViewport = false,
|
fitInViewport = false,
|
||||||
|
offsetLeft = 0,
|
||||||
|
offsetTop = 0,
|
||||||
|
viewportWidth = window.innerWidth,
|
||||||
|
viewportHeight = window.innerHeight,
|
||||||
}: Props) => {
|
}: Props) => {
|
||||||
const popoverRef = useRef<HTMLDivElement>(null);
|
const popoverRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
@ -24,17 +32,14 @@ export const Popover = ({
|
|||||||
if (fitInViewport && popoverRef.current) {
|
if (fitInViewport && popoverRef.current) {
|
||||||
const element = popoverRef.current;
|
const element = popoverRef.current;
|
||||||
const { x, y, width, height } = element.getBoundingClientRect();
|
const { x, y, width, height } = element.getBoundingClientRect();
|
||||||
|
if (x + width - offsetLeft > viewportWidth) {
|
||||||
const viewportWidth = window.innerWidth;
|
|
||||||
if (x + width > viewportWidth) {
|
|
||||||
element.style.left = `${viewportWidth - width}px`;
|
element.style.left = `${viewportWidth - width}px`;
|
||||||
}
|
}
|
||||||
const viewportHeight = window.innerHeight;
|
if (y + height - offsetTop > viewportHeight) {
|
||||||
if (y + height > viewportHeight) {
|
|
||||||
element.style.top = `${viewportHeight - height}px`;
|
element.style.top = `${viewportHeight - height}px`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [fitInViewport]);
|
}, [fitInViewport, viewportWidth, viewportHeight, offsetLeft, offsetTop]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (onCloseRequest) {
|
if (onCloseRequest) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user