Skip to content

Build your personal knowledge base with Trilium Notes

fix(client/tooltips): hide tooltips when a context menu opens (#10705)

matt wilkie2ffc1e455c1b
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index def6d108ed..64a2e2bcab 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -170,6 +170,44 @@ describe("contextMenu", () => {
expect(contextMenu.isShown()).toBe(true);
});
+ it("hides a Bootstrap tooltip that is up when the menu opens", async () => {
+ buildPage();
+ const contextMenu = await buildContextMenu();
+ // Imported after `vi.resetModules()` so that the spec and `context_menu` share one
+ // Bootstrap instance registry.
+ const { Tooltip } = await import("bootstrap");
+
+ const button = document.createElement("button");
+ document.body.append(button);
+ const tooltip = new Tooltip(button, {
+ title: "Calendar",
+ animation: false,
+ trigger: "hover focus"
+ });
+ const showMenu = () =>
+ contextMenu.show({ x: 10, y: 10, items, selectMenuItemHandler: () => {} });
+
+ tooltip.show();
+ expect(document.querySelector(".tooltip")).not.toBeNull();
+ expect(button.hasAttribute("aria-describedby")).toBe(true);
+
+ await showMenu();
+ expect(document.querySelector(".tooltip")).toBeNull();
+ expect(button.hasAttribute("aria-describedby")).toBe(false);
+
+ // A right-click also focuses the trigger, which keeps the focus trigger active. Bootstrap
+ // shows the tooltip from a timer, so the spec waits for it.
+ button.dispatchEvent(new FocusEvent("focusin", { bubbles: true }));
+ await vi.waitFor(() => expect(document.querySelector(".tooltip")).not.toBeNull());
+
+ await showMenu();
+ expect(document.querySelector(".tooltip")).toBeNull();
+ expect(button.hasAttribute("aria-describedby")).toBe(false);
+
+ tooltip.dispose();
+ button.remove();
+ });
+
it("says whether it is up, for a host whose own press would otherwise not know", async () => {
buildPage();
const contextMenu = await buildContextMenu();
diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.ts
index e5b4e0291a..69f93a07b8 100644
--- a/apps/client/src/menus/context_menu.ts
+++ b/apps/client/src/menus/context_menu.ts
@@ -1,4 +1,5 @@
import { KeyboardActionNames } from "@triliumnext/commons";
+import { Tooltip } from "bootstrap";
import { h, JSX, render } from "preact";
import keyboardActionService, { getActionSync } from "../services/keyboard_actions.js";
@@ -104,6 +105,7 @@ class ContextMenu {
this.options = options;
note_tooltip.dismissAllTooltips();
+ hideShownTooltips();
if (this.$widget.hasClass("show")) {
// The menu is already visible. Hide the menu then open it again
@@ -463,3 +465,11 @@ class ContextMenu {
const contextMenu = new ContextMenu();
export default contextMenu;
+
+// Bootstrap sets `aria-describedby` on a trigger while its tooltip is shown. `hide()` also clears
+// the hover and focus triggers that keep the tooltip up.
+function hideShownTooltips() {
+ for (const trigger of document.querySelectorAll("[aria-describedby]")) {
+ Tooltip.getInstance(trigger)?.hide();
+ }
+}