(null);
const openPopover = useCallback((): void => {
if (!isOpen) {
onOpen?.();
}
setIsOpen(true);
}, [isOpen, onOpen]);
const closePopover = useCallback((): void => {
if (!isOpen) return;
setIsOpen(false);
onClose?.();
}, [isOpen, onClose]);
const closeIfPointerLeavesPopover = useCallback(
(event: MouseEvent): void => {
const nextTarget = event.relatedTarget;
if (
nextTarget instanceof Node &&
(anchorRef.current?.contains(nextTarget) ||
popoverRef.current?.contains(nextTarget))
) {
return;
}
closePopover();
},
[closePopover]
);
const updatePopoverPosition = useCallback((): void => {
const anchor = anchorRef.current;
if (!anchor) {
return;
}
const rect = anchor.getBoundingClientRect();
const availableWidth = Math.max(
0,
window.innerWidth - POPOVER_MIN_VIEWPORT_INSET_PX * 2
);
const currentWidth = popoverRef.current?.offsetWidth;
const measuredWidth = Math.min(
currentWidth && currentWidth > 0
? currentWidth
: POPOVER_DEFAULT_WIDTH_PX,
availableWidth
);
const centeredLeft = rect.left + rect.width / 2 - measuredWidth / 2;
const maxLeft =
window.innerWidth - measuredWidth - POPOVER_MIN_VIEWPORT_INSET_PX;
setPopoverStyle({
top: rect.bottom + 8,
left: Math.max(
POPOVER_MIN_VIEWPORT_INSET_PX,
Math.min(centeredLeft, maxLeft)
),
maxWidth: availableWidth
});
}, []);
useEffect(() => {
if (!isOpen) {
return;
}
updatePopoverPosition();
}, [isOpen, lines, updatePopoverPosition]);
useLayoutEffect(() => {
if (!isOpen) {
return;
}
updatePopoverPosition();
window.addEventListener("resize", updatePopoverPosition);
window.addEventListener("scroll", updatePopoverPosition, true);
return () => {
window.removeEventListener("resize", updatePopoverPosition);
window.removeEventListener("scroll", updatePopoverPosition, true);
};
}, [isOpen, updatePopoverPosition]);
if (lines.length === 0) {
return null;
}
const popover = (
{lines.map((line) => {
const label = getDockAgentProbeLineLabel(line);
const valueText = formatDockAgentProbeLineText(line);
return (
-
{label ? (
{label}
) : null}
{renderDockAgentProbeLine(line)}
);
})}
);
return (
{
updatePopoverPosition();
openPopover();
}}
onMouseLeave={closeIfPointerLeavesPopover}
onFocus={() => {
updatePopoverPosition();
openPopover();
}}
onBlur={closePopover}
>
{isOpen ? createPortal(popover, document.body) : null}
);
}