Watch
0
0
Fork
You've already forked hyperhive
0

swarm ui: a popover stays on the page, and the ssh popover folds its explanations

A popover hung from its anchor and ran off the foot of the window when the anchor sat low. It now goes over the anchor when it fits neither way better below, and otherwise takes the roomier side with its height cut so it scrolls inside itself; the placement is a pure function with a test, and the box places itself again when its content changes size. The ssh popover showed three long paragraphs above its switch: the explainer and the host-key text now sit in a folded how it works section, and the warning and the switch stay visible. Its sections can no longer give the popover a sideways scroll.
This commit is contained in:
iris 2026-11-20 11:47:08 +01:00 • committed by mara
commit da187f8368
2 changed files with 136 additions and 32 deletions

View file

@ -28,7 +28,7 @@ import {
import { createPortal } from "preact/compat";
import type { ComponentChildren, RefObject } from "preact";
import { useDismiss } from "@hive/shared/use-dismiss.js";
import { clampLeft } from "../table/popover-position.js";
import { clampLeft, placeVertically } from "../table/popover-position.js";
import { focusInto, restoreFocus, triggerOf } from "./focus.js";
import "./Popover.css";
@ -52,7 +52,11 @@ export function Popover({
children: ComponentChildren;
}) {
const ref = useRef<HTMLDivElement>(null);
const [pos, setPos] = useState<{ top: number; left: number } | null>(null);
const [pos, setPos] = useState<{
top: number;
left: number;
maxHeight: number;
} | null>(null);
const id = useId();
const wasOpen = useRef(false);
@ -97,20 +101,42 @@ export function Popover({
if (!anchor) return;
const r = anchor.getBoundingClientRect();
const width = ref.current?.offsetWidth ?? 0;
const top = r.bottom + GAP_PX;
// The content's own height, not the box's: the box is already cut to the
// room it was given, and measuring that would keep it cut.
const content = (ref.current?.scrollHeight ?? 0) + 2;
const { top, maxHeight } = placeVertically(
r.top,
r.bottom,
content,
window.innerHeight,
GAP_PX,
);
const left = clampLeft(r.left, width, window.innerWidth);
// Scrolling anywhere (the streaming terminal included) calls this; keep
// the old object when nothing moved so the subtree does not re-render.
setPos((prev) =>
prev && prev.top === top && prev.left === left ? prev : { top, left },
prev &&
prev.top === top &&
prev.left === left &&
prev.maxHeight === maxHeight
? prev
: { top, left, maxHeight },
);
}
place();
window.addEventListener("scroll", place, true);
window.addEventListener("resize", place);
// The content can change size while the box is open (a folded section
// opens), and the box then needs another place.
const watch =
typeof ResizeObserver === "undefined" || !ref.current
? null
: new ResizeObserver(place);
if (watch && ref.current) watch.observe(ref.current);
return () => {
window.removeEventListener("scroll", place, true);
window.removeEventListener("resize", place);
watch?.disconnect();
};
}, [open, anchorRef]);
@ -129,7 +155,11 @@ export function Popover({
ref={ref}
style={
pos
? { top: `${pos.top}px`, left: `${pos.left}px` }
? {
top: `${pos.top}px`,
left: `${pos.left}px`,
maxHeight: `${pos.maxHeight}px`,
}
: { visibility: "hidden" }
}
>

View file

@ -1,4 +1,4 @@
// Where a table's filter popover sits, without a DOM.
// Where a floating popover sits, without a DOM.
// Space kept between a floating box and the viewport's edge.
const POPOVER_EDGE_PX = 8;
@ -17,3 +17,27 @@ export function clampLeft(
const maxLeft = viewportWidth - width - POPOVER_EDGE_PX;
return Math.max(POPOVER_EDGE_PX, Math.min(left, maxLeft));
}
/**
* The top edge and the tallest height for a popover of `height` that hangs
* from an anchor spanning `anchorTop` to `anchorBottom`: under the anchor when
* it fits there, over it when it does not and there is more room above, and
* otherwise under it again with the height cut to the room left, so it scrolls
* inside itself. A popover under a badge near the foot of the window would
* otherwise run off the page.
*/
export function placeVertically(
anchorTop: number,
anchorBottom: number,
height: number,
viewportHeight: number,
gap: number,
): { top: number; maxHeight: number } {
const below = viewportHeight - anchorBottom - gap - POPOVER_EDGE_PX;
const above = anchorTop - gap - POPOVER_EDGE_PX;
if (height <= below || above <= below) {
return { top: anchorBottom + gap, maxHeight: Math.max(below, 0) };
}
const h = Math.min(height, above);
return { top: anchorTop - gap - h, maxHeight: above };
}