Skip to content

Build your personal knowledge base with Trilium Notes

fix(shortcut_hints): keep a button's hints pane inside the window

Elian Doran875412ecf4da
diff --git a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.css b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.css
index fa2c8aceb0..4ed15f1c59 100644
--- a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.css
+++ b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.css
@@ -15,6 +15,15 @@
border-radius: 6px;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
+ /* Beside a button, where `placeFloating()` sets `left` and `top` and shows it once placed. */
+ &.anchored {
+ top: 0;
+ left: 0;
+ right: auto;
+ bottom: auto;
+ visibility: hidden;
+ }
+
.shortcut-hints-section {
&:not(:last-child) {
margin-bottom: 0.75rem;
diff --git a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.spec.tsx b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.spec.tsx
index 57d97f5549..960f5a50a2 100644
--- a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.spec.tsx
+++ b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.spec.tsx
@@ -113,29 +113,36 @@ describe("ShortcutHintsPanel", () => {
expect(document.querySelector(".shortcut-hints-panel")).toBeNull();
});
- /** A button pinned to the foot of what it sits over, as the board's is. */
- it("opens the dropdown upwards when the anchor stands low in the window", () => {
+ it("stands beside its anchor, lined up with its end, and keeps inside the window", async () => {
+ // happy-dom lays nothing out: a 1000×800 window and a 300×200 pane, as Floating UI measures them.
+ vi.spyOn(document.documentElement, "clientWidth", "get").mockReturnValue(1000);
+ vi.spyOn(document.documentElement, "clientHeight", "get").mockReturnValue(800);
+ vi.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockReturnValue(300);
+ vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(200);
const host = mountPanel();
const anchor = document.createElement("button");
document.body.appendChild(anchor);
- // happy-dom lays nothing out, so the anchor is put near the foot of the window by hand.
- anchor.getBoundingClientRect = () => ({
- top: window.innerHeight - 40,
- bottom: window.innerHeight - 10,
- right: 300
- }) as DOMRect;
+ const placedAt = async (x: number, y: number) => {
+ vi.spyOn(anchor, "getBoundingClientRect").mockReturnValue(DOMRect.fromRect({ x, y, width: 80, height: 30 }));
+ act(() => { host.handleEvent("shortcutHintsRequested", { sections: SECTIONS, anchor }); });
+ const panel = document.querySelector<HTMLElement>(".shortcut-hints-panel");
+ await vi.waitFor(() => expect(panel?.style.visibility).toBe("visible"));
+ const placed = [ panel?.style.left, panel?.style.top ];
+ // The same request closes it again.
+ act(() => { host.handleEvent("shortcutHintsRequested", { sections: SECTIONS, anchor }); });
+ return placed;
+ };
try {
- act(() => {
- host.handleEvent("shortcutHintsRequested", { sections: SECTIONS, anchor });
- });
-
- const panel = document.querySelector<HTMLElement>(".shortcut-hints-panel");
- // Its foot sits a gap above the anchor's head, and the corner offset is cleared.
- expect(panel?.style.bottom).toBe("46px");
- expect(panel?.style.top).toBe("auto");
+ // Under the anchor with the right edges lined up, as a button at the end of a toolbar has it.
+ expect(await placedAt(700, 50)).toEqual([ "480px", "86px" ]);
+ // Near the window's left edge, lined up with the anchor's start instead of running off it.
+ expect(await placedAt(20, 50)).toEqual([ "20px", "86px" ]);
+ // Near its foot, above the anchor.
+ expect(await placedAt(700, 760)).toEqual([ "480px", "554px" ]);
} finally {
anchor.remove();
+ vi.restoreAllMocks();
}
});
@@ -146,10 +153,7 @@ describe("ShortcutHintsPanel", () => {
try {
act(() => { host.handleEvent("shortcutHintsRequested", { sections: SECTIONS, anchor }); });
- const panel = document.querySelector<HTMLElement>(".shortcut-hints-panel");
- // Anchored positioning uses top/bottom-auto instead of the corner's bottom offset.
- expect(panel?.style.top).toBe("6px");
- expect(panel?.style.bottom).toBe("auto");
+ expect(document.querySelector(".shortcut-hints-panel")).not.toBeNull();
// No Esc footer when opened via the button (mouse users click away).
expect(document.querySelector(".shortcut-hints-footer")).toBeNull();
diff --git a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsx b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsx
index 6fd02af0e3..06837fe917 100644
--- a/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsx
+++ b/apps/client/src/widgets/shortcut_hints/shortcut_hints_panel.tsx
@@ -1,13 +1,16 @@
import "./shortcut_hints_kbd.css";
import "./shortcut_hints_panel.css";
+import { autoUpdate } from "@floating-ui/dom";
+import clsx from "clsx";
import { createPortal } from "preact/compat";
-import { useCallback, useEffect, useRef, useState } from "preact/hooks";
+import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";
import { t } from "../../services/i18n.js";
import keyboard_actions from "../../services/keyboard_actions.js";
import type { ShortcutHint, ShortcutHintSection } from "../../services/shortcut_hints.js";
import { useTriliumEvent } from "../react/hooks.js";
+import { placeFloating } from "../react/Popup.js";
import { renderShortcutKbds } from "../react/shortcut_kbd.js";
/** How long the panel stays up when left untouched. Paused while hovered; other signals dismiss it sooner. */
@@ -74,19 +77,31 @@ export default function ShortcutHintsPanel() {
};
}, [ isOpen, startTimer, clearTimer, close ]);
+ // Under the anchor with the right edges lined up, flipped and shifted as the window requires,
+ // and following the anchor while it moves.
+ const anchor = state?.anchor;
+ useLayoutEffect(() => {
+ const panel = panelRef.current;
+ if (!panel || !anchor) return;
+ return autoUpdate(anchor, panel, () => void placeFloating(panel, anchor, {
+ placement: "bottom-end",
+ offset: ANCHOR_GAP
+ }));
+ }, [ anchor ]);
+
if (!state) {
return null;
}
- // Anchored: position the dropdown beside the anchor, right edges aligned. The rect is a
- // genuinely dynamic value, so it belongs in an inline style rather than CSS.
- const anchorRect = state.anchor?.getBoundingClientRect();
- const style = anchorRect ? anchoredStyle(anchorRect) : undefined;
-
// Portal to <body> so no transformed / contained / overflow-clipped ancestor breaks the fixed
// positioning or hides it behind content.
return createPortal(
- <div ref={panelRef} className="shortcut-hints-panel tn-shortcut-hints-kbd" style={style} onMouseEnter={clearTimer} onMouseLeave={startTimer}>
+ <div
+ ref={panelRef}
+ className={clsx("shortcut-hints-panel tn-shortcut-hints-kbd", state.anchor && "anchored")}
+ onMouseEnter={clearTimer}
+ onMouseLeave={startTimer}
+ >
<ShortcutHintsSections sections={state.sections} />
{/* Keyboard users get the Esc reminder; mouse users (opened via the button) click away. */}
{!state.anchor && (
@@ -99,23 +114,6 @@ export default function ShortcutHintsPanel() {
);
}
-/**
- * Where an anchored panel stands: under the anchor, or over it where there is more room that way.
- *
- * Which side it takes is read off the room around the anchor rather than told to it, so a button
- * pinned to the foot of what it sits over drops its panel upwards instead of off the window. The
- * side not used is cleared, the corner placement the stylesheet gives otherwise being the one that
- * would fight it.
- */
-function anchoredStyle(anchor: DOMRect) {
- const right = `${Math.max(ANCHOR_GAP, window.innerWidth - anchor.right)}px`;
- const opensUp = window.innerHeight - anchor.bottom < anchor.top;
-
- return opensUp
- ? { bottom: `${window.innerHeight - anchor.top + ANCHOR_GAP}px`, top: "auto", right }
- : { top: `${anchor.bottom + ANCHOR_GAP}px`, bottom: "auto", right };
-}
-
export function ShortcutHintsSections({ sections }: { sections: ShortcutHintSection[] }) {
return (
<>