Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

web-ui: use native CSS anchors for scrolling tooltips

16francej60ba79195dc8
diff --git a/docs/images/tooltip-scroll.png b/docs/images/tooltip-scroll.png
new file mode 100644
index 000000000..b460d9fc7
Binary files /dev/null and b/docs/images/tooltip-scroll.png differ
diff --git a/plugins/web-ui/src/shell.css b/plugins/web-ui/src/shell.css
index 20f48e702..a56145f30 100644
--- a/plugins/web-ui/src/shell.css
+++ b/plugins/web-ui/src/shell.css
@@ -7437,11 +7437,22 @@ h3.ambient-field-label {
background: color-mix(in srgb, var(--primary, #6366f1) 8%, var(--background));
}
-/* Floating tooltip (ours — never the browser's native title bubble). */
+[data-qm-tooltip-anchor] {
+ anchor-name: --qm-tooltip-anchor;
+}
+
.qm-tooltip {
position: fixed;
+ position-anchor: --qm-tooltip-anchor;
+ position-area: top span-all;
+ justify-self: anchor-center;
+ position-try-fallbacks: flip-block;
+ position-visibility: anchors-visible;
+ inset: auto;
+ margin: 7px;
+ width: max-content;
z-index: 4000;
- max-width: 320px;
+ max-width: min(320px, calc(100vw - 14px));
padding: 4px 8px;
border-radius: 6px;
font-size: 11.5px;
@@ -7449,12 +7460,27 @@ h3.ambient-field-label {
color: var(--background, #fff);
background: color-mix(in srgb, var(--foreground) 92%, transparent);
pointer-events: none;
- opacity: 0;
- transition: opacity 0.1s ease;
- white-space: nowrap;
+ display: none;
+ overflow-wrap: anywhere;
}
.qm-tooltip.visible {
- opacity: 1;
+ display: block;
+}
+.qm-tooltip.beside {
+ position-area: right span-all;
+ justify-self: normal;
+ align-self: anchor-center;
+ position-try-fallbacks: --qm-tooltip-above, --qm-tooltip-below;
+}
+@position-try --qm-tooltip-above {
+ position-area: top span-all;
+ justify-self: anchor-center;
+ align-self: normal;
+}
+@position-try --qm-tooltip-below {
+ position-area: bottom span-all;
+ justify-self: anchor-center;
+ align-self: normal;
}
/* ===== Chat search palette (⌘K) ===== */
diff --git a/plugins/web-ui/src/tooltip.ts b/plugins/web-ui/src/tooltip.ts
index 83da89321..bdbc57a8b 100644
--- a/plugins/web-ui/src/tooltip.ts
+++ b/plugins/web-ui/src/tooltip.ts
@@ -19,31 +19,21 @@ function hoverless(): boolean {
}
function showTooltip(target: Element, text: string): void {
+ hideTooltip();
if (!text || hoverless()) return;
anchor = target;
+ target.setAttribute("data-qm-tooltip-anchor", "");
const el = ensureEl();
el.textContent = text;
+ el.classList.toggle("beside", !!target.closest('[data-tip-placement="right"]'));
el.classList.add("visible");
- // Measure after content is set.
- const r = target.getBoundingClientRect();
- const tr = el.getBoundingClientRect();
- const clamp = (value: number, extent: number) => Math.max(6, Math.min(value, extent - 6));
- const beside = target.closest('[data-tip-placement="right"]') && r.right + 7 + tr.width <= window.innerWidth - 6;
- if (beside) {
- el.style.left = `${Math.round(r.right + 7)}px`;
- el.style.top = `${Math.round(clamp(r.top + r.height / 2 - tr.height / 2, window.innerHeight - tr.height))}px`;
- return;
- }
- let top = r.top - tr.height - 7;
- if (top < 6) top = r.bottom + 7; // no room above — flip below
- el.style.left = `${Math.round(clamp(r.left + r.width / 2 - tr.width / 2, window.innerWidth - tr.width))}px`;
- el.style.top = `${Math.round(top)}px`;
}
export function hideTooltip(target?: Element): void {
if (target && anchor && target !== anchor) return;
- anchor = null;
tipEl?.classList.remove("visible");
+ anchor?.removeAttribute("data-qm-tooltip-anchor");
+ anchor = null;
}
const attached = new WeakMap<Element, TooltipBinding>();
diff --git a/plugins/web-ui/test/tooltip-placement.test.ts b/plugins/web-ui/test/tooltip-placement.test.ts
index 41ce76a78..b6ac582be 100644
--- a/plugins/web-ui/test/tooltip-placement.test.ts
+++ b/plugins/web-ui/test/tooltip-placement.test.ts
@@ -4,50 +4,94 @@ import { JSDOM } from "jsdom";
const dom = new JSDOM(
`<div class="sidebar" data-tip-placement="right"><button id="rail"></button></div>
- <button id="pane"></button><button id="edge"></button>`,
+ <button id="pane"></button>`,
);
Object.assign(globalThis, { document: dom.window.document, window: dom.window });
const { attachTooltip, hideTooltip } = await import("../src/tooltip.ts");
-function rect(left: number, top: number, width = 34, height = 34) {
- return { left, top, right: left + width, bottom: top + height, width, height, x: left, y: top, toJSON: () => ({}) };
-}
-
-function hover(id: string, label: string): HTMLElement {
+function hover(id: string, label = "Tooltip"): HTMLElement {
const target = dom.window.document.querySelector<HTMLElement>(`#${id}`)!;
attachTooltip(target, label);
target.dispatchEvent(new dom.window.MouseEvent("mouseenter"));
- const tip = dom.window.document.querySelector<HTMLElement>(".qm-tooltip")!;
- tip.getBoundingClientRect = () => rect(0, 0, 56, 24) as DOMRect;
- target.dispatchEvent(new dom.window.MouseEvent("mouseenter"));
- return tip;
+ return dom.window.document.querySelector<HTMLElement>(".qm-tooltip")!;
}
-test("a tooltip inside a right-placement container sits beside the target, vertically centred", () => {
- const target = dom.window.document.querySelector<HTMLElement>("#rail")!;
- target.getBoundingClientRect = () => rect(8, 180) as DOMRect;
- const tip = hover("rail", "Browse");
- assert.equal(tip.style.left, "49px");
- assert.equal(tip.style.top, "185px");
- hideTooltip(target);
+const pane = dom.window.document.querySelector<HTMLElement>("#pane")!;
+const rail = dom.window.document.querySelector<HTMLElement>("#rail")!;
+
+test("placement delegates to CSS without measuring or writing viewport coordinates", () => {
+ pane.getBoundingClientRect = () => {
+ throw new Error("JavaScript must not measure tooltip anchors");
+ };
+ const tip = hover("pane");
+ assert.equal(pane.hasAttribute("data-qm-tooltip-anchor"), true);
+ assert.equal(tip.classList.contains("beside"), false);
+ assert.equal(tip.style.left, "");
+ assert.equal(tip.style.top, "");
+ assert.equal(tip.parentElement, document.body);
+ hideTooltip();
+});
+
+test("right placement resolves on show and updates when the rail expands", () => {
+ const sidebar = rail.parentElement!;
+ const tip = hover("rail");
+ assert.equal(tip.classList.contains("beside"), true);
+ sidebar.removeAttribute("data-tip-placement");
+ hover("rail");
+ assert.equal(tip.classList.contains("beside"), false);
+ sidebar.setAttribute("data-tip-placement", "right");
+ hideTooltip();
+});
+
+test("switching targets removes the old anchor and ignores its late leave", () => {
+ const tip = hover("rail");
+ hover("pane", "Attachment");
+ assert.equal(rail.hasAttribute("data-qm-tooltip-anchor"), false);
+ assert.equal(pane.hasAttribute("data-qm-tooltip-anchor"), true);
+ assert.equal(document.querySelectorAll("[data-qm-tooltip-anchor]").length, 1);
+ hideTooltip(rail);
+ assert.equal(tip.classList.contains("visible"), true);
+ assert.equal(tip.textContent, "Attachment");
+ hideTooltip(pane);
+ assert.equal(tip.classList.contains("visible"), false);
+ assert.equal(pane.hasAttribute("data-qm-tooltip-anchor"), false);
+});
+
+test("scrolling does not dismiss the anchored tooltip", () => {
+ const tip = hover("pane");
+ pane.parentElement!.dispatchEvent(new dom.window.Event("scroll", { bubbles: false }));
+ dom.window.dispatchEvent(new dom.window.Event("scroll"));
+ assert.equal(tip.classList.contains("visible"), true);
+ assert.equal(pane.hasAttribute("data-qm-tooltip-anchor"), true);
+ hideTooltip();
+});
+
+test("updating text preserves the active target and empty text clears it", () => {
+ const tip = hover("pane", "Before");
+ attachTooltip(pane, "After");
+ assert.equal(tip.textContent, "After");
+ assert.equal(pane.hasAttribute("data-qm-tooltip-anchor"), true);
+ attachTooltip(pane, "");
+ assert.equal(tip.classList.contains("visible"), false);
+ assert.equal(pane.hasAttribute("data-qm-tooltip-anchor"), false);
});
-test("tooltips outside that container keep the default placement above the target", () => {
- const target = dom.window.document.querySelector<HTMLElement>("#pane")!;
- target.getBoundingClientRect = () => rect(500, 300) as DOMRect;
- const tip = hover("pane", "Refresh");
- assert.equal(tip.style.left, "489px");
- assert.equal(tip.style.top, "269px");
- hideTooltip(target);
+test("focus shows and blur or click removes the tooltip and anchor", () => {
+ attachTooltip(pane, "Keyboard label");
+ for (const event of ["blur", "click"]) {
+ pane.dispatchEvent(new dom.window.Event("focus"));
+ assert.equal(pane.hasAttribute("data-qm-tooltip-anchor"), true);
+ pane.dispatchEvent(new dom.window.Event(event));
+ assert.equal(pane.hasAttribute("data-qm-tooltip-anchor"), false);
+ assert.equal(document.querySelector(".qm-tooltip")!.classList.contains("visible"), false);
+ }
});
-test("a right-placed tooltip with no room beside it falls back to the default placement", () => {
- const target = dom.window.document.querySelector<HTMLElement>("#edge")!;
- target.getBoundingClientRect = () => rect(dom.window.innerWidth - 40, 300) as DOMRect;
- dom.window.document.querySelector(".sidebar")!.appendChild(target);
- const tip = hover("edge", "Cramped");
- assert.equal(tip.style.top, "269px");
- assert.equal(tip.style.left, `${dom.window.innerWidth - 56 - 6}px`);
- hideTooltip(target);
+test("hoverless devices do not create a visible tooltip or anchor", () => {
+ Object.defineProperty(dom.window, "matchMedia", { configurable: true, value: () => ({ matches: true }) });
+ const tip = hover("pane");
+ assert.equal(tip.classList.contains("visible"), false);
+ assert.equal(pane.hasAttribute("data-qm-tooltip-anchor"), false);
+ Reflect.deleteProperty(dom.window, "matchMedia");
});