2020-03-15 13:26:52 -04:00
|
|
|
import React, { useLayoutEffect, useRef, useEffect } from "react";
|
2020-09-26 02:48:45 +05:30
|
|
|
import "./Popover.scss";
|
2020-03-16 19:07:47 -07:00
|
|
|
import { unstable_batchedUpdates } from "react-dom";
|
2022-06-23 17:33:50 +02:00
|
|
|
import { queryFocusableElements } from "../utils";
|
|
|
|
import { KEYS } from "../keys";
|
2020-01-07 07:50:59 +05:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
top?: number;
|
|
|
|
left?: number;
|
|
|
|
children?: React.ReactNode;
|
2020-04-10 18:09:29 -04:00
|
|
|
onCloseRequest?(event: PointerEvent): void;
|
2020-01-14 09:44:18 +01:00
|
|
|
fitInViewport?: boolean;
|
2022-01-23 17:28:38 +01:00
|
|
|
offsetLeft?: number;
|
|
|
|
offsetTop?: number;
|
|
|
|
viewportWidth?: number;
|
|
|
|
viewportHeight?: number;
|
2020-01-07 07:50:59 +05:00
|
|
|
};
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const Popover = ({
|
2020-01-14 09:44:18 +01:00
|
|
|
children,
|
|
|
|
left,
|
|
|
|
top,
|
|
|
|
onCloseRequest,
|
2020-01-24 12:04:54 +02:00
|
|
|
fitInViewport = false,
|
2022-01-23 17:28:38 +01:00
|
|
|
offsetLeft = 0,
|
|
|
|
offsetTop = 0,
|
|
|
|
viewportWidth = window.innerWidth,
|
|
|
|
viewportHeight = window.innerHeight,
|
2020-05-20 16:21:37 +03:00
|
|
|
}: Props) => {
|
2020-01-14 09:44:18 +01:00
|
|
|
const popoverRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
2022-06-23 17:33:50 +02:00
|
|
|
useEffect(() => {
|
2023-04-12 02:53:36 +05:30
|
|
|
const container = popoverRef.current;
|
|
|
|
|
2022-06-23 17:33:50 +02:00
|
|
|
if (!container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-16 15:33:30 +02:00
|
|
|
// focus popover only if the caller didn't focus on something else nested
|
|
|
|
// within the popover, which should take precedence. Fixes cases
|
|
|
|
// like color picker listening to keydown events on containers nested
|
|
|
|
// in the popover.
|
|
|
|
if (!container.contains(document.activeElement)) {
|
|
|
|
container.focus();
|
|
|
|
}
|
2023-04-12 02:53:36 +05:30
|
|
|
|
2022-06-23 17:33:50 +02:00
|
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
|
|
if (event.key === KEYS.TAB) {
|
|
|
|
const focusableElements = queryFocusableElements(container);
|
|
|
|
const { activeElement } = document;
|
|
|
|
const currentIndex = focusableElements.findIndex(
|
|
|
|
(element) => element === activeElement,
|
|
|
|
);
|
|
|
|
|
2023-04-12 02:53:36 +05:30
|
|
|
if (activeElement === container) {
|
|
|
|
if (event.shiftKey) {
|
|
|
|
focusableElements[focusableElements.length - 1]?.focus();
|
|
|
|
} else {
|
|
|
|
focusableElements[0].focus();
|
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
} else if (currentIndex === 0 && event.shiftKey) {
|
|
|
|
focusableElements[focusableElements.length - 1]?.focus();
|
2022-06-23 17:33:50 +02:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
} else if (
|
|
|
|
currentIndex === focusableElements.length - 1 &&
|
|
|
|
!event.shiftKey
|
|
|
|
) {
|
2023-04-12 02:53:36 +05:30
|
|
|
focusableElements[0]?.focus();
|
2022-06-23 17:33:50 +02:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopImmediatePropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
container.addEventListener("keydown", handleKeyDown);
|
|
|
|
|
|
|
|
return () => container.removeEventListener("keydown", handleKeyDown);
|
2023-04-12 02:53:36 +05:30
|
|
|
}, []);
|
|
|
|
|
|
|
|
const lastInitializedPosRef = useRef<{ top: number; left: number } | null>(
|
|
|
|
null,
|
|
|
|
);
|
2022-06-23 17:33:50 +02:00
|
|
|
|
2020-01-14 09:44:18 +01:00
|
|
|
// ensure the popover doesn't overflow the viewport
|
|
|
|
useLayoutEffect(() => {
|
2023-04-12 02:53:36 +05:30
|
|
|
if (fitInViewport && popoverRef.current && top != null && left != null) {
|
|
|
|
const container = popoverRef.current;
|
|
|
|
const { width, height } = container.getBoundingClientRect();
|
2022-07-21 18:04:49 +05:30
|
|
|
|
2023-04-12 02:53:36 +05:30
|
|
|
// hack for StrictMode so this effect only runs once for
|
|
|
|
// the same top/left position, otherwise
|
|
|
|
// we'd potentically reposition twice (once for viewport overflow)
|
|
|
|
// and once for top/left position afterwards
|
|
|
|
if (
|
|
|
|
lastInitializedPosRef.current?.top === top &&
|
|
|
|
lastInitializedPosRef.current?.left === left
|
|
|
|
) {
|
|
|
|
return;
|
2020-01-14 09:44:18 +01:00
|
|
|
}
|
2023-04-12 02:53:36 +05:30
|
|
|
lastInitializedPosRef.current = { top, left };
|
|
|
|
|
|
|
|
if (width >= viewportWidth) {
|
|
|
|
container.style.width = `${viewportWidth}px`;
|
|
|
|
container.style.left = "0px";
|
|
|
|
container.style.overflowX = "scroll";
|
|
|
|
} else if (left + width - offsetLeft > viewportWidth) {
|
|
|
|
container.style.left = `${viewportWidth - width - 10}px`;
|
|
|
|
} else {
|
|
|
|
container.style.left = `${left}px`;
|
2020-01-14 09:44:18 +01:00
|
|
|
}
|
2022-07-21 18:04:49 +05:30
|
|
|
|
|
|
|
if (height >= viewportHeight) {
|
2023-04-12 02:53:36 +05:30
|
|
|
container.style.height = `${viewportHeight - 20}px`;
|
|
|
|
container.style.top = "10px";
|
|
|
|
container.style.overflowY = "scroll";
|
|
|
|
} else if (top + height - offsetTop > viewportHeight) {
|
|
|
|
container.style.top = `${viewportHeight - height}px`;
|
|
|
|
} else {
|
|
|
|
container.style.top = `${top}px`;
|
2022-07-21 18:04:49 +05:30
|
|
|
}
|
2020-01-14 09:44:18 +01:00
|
|
|
}
|
2023-04-12 02:53:36 +05:30
|
|
|
}, [
|
|
|
|
top,
|
|
|
|
left,
|
|
|
|
fitInViewport,
|
|
|
|
viewportWidth,
|
|
|
|
viewportHeight,
|
|
|
|
offsetLeft,
|
|
|
|
offsetTop,
|
|
|
|
]);
|
2020-01-14 09:44:18 +01:00
|
|
|
|
2020-03-15 13:26:52 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (onCloseRequest) {
|
2020-04-10 18:09:29 -04:00
|
|
|
const handler = (event: PointerEvent) => {
|
|
|
|
if (!popoverRef.current?.contains(event.target as Node)) {
|
|
|
|
unstable_batchedUpdates(() => onCloseRequest(event));
|
2020-03-15 13:26:52 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
document.addEventListener("pointerdown", handler, false);
|
|
|
|
return () => document.removeEventListener("pointerdown", handler, false);
|
|
|
|
}
|
|
|
|
}, [onCloseRequest]);
|
|
|
|
|
2020-01-07 07:50:59 +05:00
|
|
|
return (
|
2023-04-12 02:53:36 +05:30
|
|
|
<div className="popover" ref={popoverRef} tabIndex={-1}>
|
2020-01-07 07:50:59 +05:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|