Skip to content

Build your personal knowledge base with Trilium Notes

fix(ui): dismiss an action button tooltip when the button is pressed

Elian Doran42cba69a0da1
diff --git a/apps/client/src/widgets/react/ActionButton.spec.tsx b/apps/client/src/widgets/react/ActionButton.spec.tsx
new file mode 100644
index 0000000000..ceb6f48140
--- /dev/null
+++ b/apps/client/src/widgets/react/ActionButton.spec.tsx
@@ -0,0 +1,44 @@
+import { Tooltip } from "bootstrap";
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import ActionButton from "./ActionButton";
+
+describe("ActionButton", () => {
+ let container: HTMLElement;
+
+ beforeEach(() => {
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ });
+
+ afterEach(() => {
+ render(null, container);
+ container.remove();
+ for (const orphan of document.querySelectorAll(".tooltip")) {
+ orphan.remove();
+ }
+ });
+
+ it("dismisses its tooltip when pressed, so it cannot sit on top of what the press opened", async () => {
+ const onClick = vi.fn();
+ await act(async () => render(
+ <ActionButton icon="bx bx-plus" text="Add a new attribute" onClick={onClick} />, container));
+
+ const button = container.querySelector("button");
+ expect(button).not.toBeNull();
+
+ // The tooltip is shown by hovering/focusing in the real thing; neither is reliable under
+ // happy-dom, so it is shown through the instance the hook registered.
+ act(() => {
+ if (button) Tooltip.getInstance(button)?.show();
+ });
+ expect(document.querySelector(".tooltip")).not.toBeNull();
+
+ act(() => button?.click());
+
+ expect(document.querySelector(".tooltip")).toBeNull();
+ expect(onClick).toHaveBeenCalledOnce();
+ });
+});
diff --git a/apps/client/src/widgets/react/ActionButton.tsx b/apps/client/src/widgets/react/ActionButton.tsx
index f23c7ecfb8..10a03a3477 100644
--- a/apps/client/src/widgets/react/ActionButton.tsx
+++ b/apps/client/src/widgets/react/ActionButton.tsx
@@ -59,6 +59,23 @@ export default function ActionButton({ text, icon, className, triggerCommand, ti
}
}, [title]);
+ // A "hover focus" tooltip survives the press that opened something, so it ends up sitting on top
+ // of whatever was opened — a context menu, a dropdown, a popup — until the pointer leaves the
+ // button. Dismiss it on press, as the legacy button widgets did (`onclick_button.ts`).
+ //
+ // Listened for natively rather than wrapped around the `onClick` prop, because a button can also
+ // act through `data-trigger-command` alone, with no `onClick` of its own. It has to be `click`
+ // and not `pointerdown`: the focus that follows a press would re-trigger the `focus` half of the
+ // tooltip's trigger and bring it straight back.
+ useEffect(() => {
+ const button = buttonRef.current;
+ if (!button) return;
+
+ const hideTooltip = () => Tooltip.getInstance(button)?.hide();
+ button.addEventListener("click", hideTooltip);
+ return () => button.removeEventListener("click", hideTooltip);
+ }, []);
+
useEffect(() => {
if (triggerCommand) {
keyboard_actions.getAction(triggerCommand, true).then(action => setKeyboardShortcut(action?.effectiveShortcuts));