Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(dropdown): let Bootstrap drive the toggle's tooltip again
Elian Doran6f86216e871f
diff --git a/apps/client/src/widgets/react/Dropdown.tooltip.spec.tsx b/apps/client/src/widgets/react/Dropdown.tooltip.spec.tsxnew file mode 100644index 0000000000..9799601c83--- /dev/null+++ b/apps/client/src/widgets/react/Dropdown.tooltip.spec.tsx@@ -0,0 +1,56 @@+import { render } from "preact";+import { act } from "preact/test-utils";+import { afterEach, beforeEach, describe, expect, it } from "vitest";++import Dropdown from "./Dropdown";++// Bootstrap is left real here — unlike Dropdown.spec.tsx, which mocks it away to assert the component's+// wiring — because what is under test is which element Bootstrap ends up attached to.++class ResizeObserverStub {+ observe() {}+ unobserve() {}+ disconnect() {}+}+globalThis.ResizeObserver = globalThis.ResizeObserver ?? (ResizeObserverStub as unknown as typeof ResizeObserver);++describe("Dropdown tooltip", () => {+ let container: HTMLElement;++ beforeEach(() => {+ container = document.createElement("div");+ document.body.appendChild(container);+ });++ afterEach(() => {+ act(() => render(null, container));+ container.remove();+ for (const orphan of document.querySelectorAll(".tooltip")) {+ orphan.remove();+ }+ });++ it("hangs the tooltip off the toggle rather than off the wrapper the open menu sits in", () => {+ act(() => render(<Dropdown title="Show help" iconAction hideToggleArrow>item</Dropdown>, container));++ const wrapper = container.querySelector(".dropdown");+ const toggle = container.querySelector("button");+ const menu = container.querySelector(".dropdown-menu");+ expect(toggle).not.toBeNull();+ expect(menu).not.toBeNull();++ // Bootstrap takes the `title` of whatever it is initialised on over to `data-bs-original-title`+ // — so this says which element it is driving the hover of, without reaching into its internals.+ expect(toggle?.getAttribute("data-bs-original-title"), "driven by the toggle").toBe("Show help");+ expect(wrapper?.getAttribute("data-bs-original-title"), "not by the wrapper").toBeNull();++ // Which is the point of it: the wrapper holds the menu as well, so a tooltip driven by the+ // wrapper stayed up over the menu the pointer had moved into. The toggle never holds the menu.+ expect(wrapper?.contains(menu ?? null), "the wrapper holds the menu").toBe(true);+ expect(toggle?.contains(menu ?? null), "the toggle does not").toBe(false);++ // And with nothing left on the wrapper, the browser's own tooltip can't double up with ours.+ expect(wrapper?.getAttribute("title")).toBeNull();+ expect(toggle?.getAttribute("title"), "handed over to Bootstrap").toBeNull();+ });+});diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsxindex 7a079fd496..ac10e9153d 100644--- a/apps/client/src/widgets/react/Dropdown.tsx+++ b/apps/client/src/widgets/react/Dropdown.tsx@@ -62,10 +62,15 @@ export default function Dropdown({ id, className, buttonClassName, isStatic, chi// "" shows no tooltip).title: title ?? titleOptions?.title ?? "",placement: titlePosition ?? "bottom",- fallbackPlacements: [ titlePosition ?? "bottom" ],- trigger: "manual"+ fallbackPlacements: [ titlePosition ?? "bottom" ]}), [title, titleOptions, titlePosition]);- const { showTooltip, hideTooltip } = useTooltip(containerRef, tooltipConfig);+ // On the toggle button rather than on the wrapper, and left to Bootstrap's own hover/focus triggering.+ // The wrapper holds the open menu as well (for a non-portaled dropdown), so a tooltip bound to it stayed+ // up over the menu the pointer had moved into — which is what manual triggering off the button's own+ // mouseenter/mouseleave used to work around. Bound to the button, that can't arise: the button never+ // contains the menu. And Bootstrap's own triggering is worth having back — it keeps the hover state+ // that decides whether a tooltip stays up, which no amount of driving it from outside can stand in for.+ const { hideTooltip } = useTooltip(triggerRef, tooltipConfig);const [ shown, setShown ] = useState(false);// A portaled menu lives in `document.body`, detached from the toggle's subtree. Mounting it eagerly@@ -189,7 +194,7 @@ export default function Dropdown({ id, className, buttonClassName, isStatic, chi);return (- <div ref={containerRef} class={`dropdown ${className ?? ""}`} style={{ display: "flex" }} title={title}>+ <div ref={containerRef} class={`dropdown ${className ?? ""}`} style={{ display: "flex" }}><buttonclassName={`${iconAction ? "icon-action" : "btn"} ${!noSelectButtonStyle ? "select-button" : ""} ${buttonClassName ?? ""} ${!hideToggleArrow ? "dropdown-toggle" : ""}`}ref={triggerRef}@@ -200,8 +205,10 @@ export default function Dropdown({ id, className, buttonClassName, isStatic, chiaria-expanded="false"id={id ?? ariaId}disabled={disabled}- onMouseEnter={showTooltip}- onMouseLeave={hideTooltip}+ // Sits beside the tooltip rather than being what drives it: Bootstrap takes the attribute+ // over on init (it moves it into the tooltip and drops it), so it only stands in for the+ // moment before, and stops the browser's own tooltip doubling up with ours afterwards.+ title={title}// Mount the portaled menu just before it can open: any interaction that leads to a// Bootstrap open (pointer press, or focusing the toggle ahead of a keyboard open) is// preceded by one of these, so `_menu` is wired by the time the click/keydown fires.