Skip to content

Build your personal knowledge base with Trilium Notes

fix(tooltips): put a tooltip away on the press that opened something

Elian Doran5d99e3b02dad
diff --git a/apps/client/src/widgets/react/ActionButton.tsx b/apps/client/src/widgets/react/ActionButton.tsx
index 10a03a3477..f23c7ecfb8 100644
--- a/apps/client/src/widgets/react/ActionButton.tsx
+++ b/apps/client/src/widgets/react/ActionButton.tsx
@@ -59,23 +59,6 @@ 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));
diff --git a/apps/client/src/widgets/react/hooks.spec.tsx b/apps/client/src/widgets/react/hooks.spec.tsx
index 3dbb40a78f..10468eff39 100644
--- a/apps/client/src/widgets/react/hooks.spec.tsx
+++ b/apps/client/src/widgets/react/hooks.spec.tsx
@@ -154,6 +154,22 @@ describe("useStaticTooltip", () => {
expect(document.querySelector(".tooltip")).toBeNull();
});
+
+ it("puts the tooltip away on a press, which would otherwise leave it standing over what the press opened", async () => {
+ await act(async () => render(<TooltipHarness generation={1} />, container));
+
+ const trigger = container.querySelector("span");
+ expect(trigger).not.toBeNull();
+ act(() => {
+ if (trigger) Tooltip.getInstance(trigger)?.show();
+ });
+ expect(document.querySelector(".tooltip"), "shown").not.toBeNull();
+
+ // The press leaves the trigger focused, and Bootstrap declines to put a tooltip away while a
+ // trigger of its own is still active — so nothing else takes it down, pointer gone or not.
+ await act(async () => { trigger?.dispatchEvent(new MouseEvent("click", { bubbles: true })); });
+ expect(document.querySelector(".tooltip"), "gone with the press").toBeNull();
+ });
});
describe("useTooltip", () => {
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 09ee14f210..605fa08d91 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -1104,7 +1104,24 @@ export function useStaticTooltip(elRef: RefObject<Element>, config?: Partial<Too
});
tooltips.add(tooltip);
+ // A tooltip triggered by hover *and focus* — Bootstrap's default — outstays the press that
+ // opened something: the press leaves the trigger focused, and while any trigger is still active
+ // Bootstrap declines to put the tooltip away, pointer gone or not. So it sits over whatever the
+ // press brought up rather than beside it, until focus moves on. The status bar's connection
+ // badges are where that shows worst: the tooltip is left standing over the sidebar the press
+ // peeked. Dismissed on press, as the legacy button widgets did (`onclick_button.ts`).
+ const dismissOnPress = (event: Event) => {
+ // A delegated (`selector:`) config spawns an instance per hovered child, so the one showing
+ // belongs to the child the press landed on rather than to the container the config sits on.
+ const delegate = config?.selector && event.target instanceof Element
+ ? event.target.closest(config.selector)
+ : null;
+ (delegate ? Tooltip.getInstance(delegate) : tooltip)?.hide();
+ };
+ element.addEventListener("click", dismissOnPress);
+
return () => {
+ element.removeEventListener("click", dismissOnPress);
tooltips.delete(tooltip);
// Dispose even when the trigger element is already detached (e.g. a keyed remount
// replaced it before this cleanup ran) — dispose() also removes a currently-shown