Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): close an open dropdown when its toggle becomes disabled

matt wilkie8d2c97fdb306
diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsx
index 2767187585..45361775f3 100644
--- a/apps/client/src/widgets/react/Dropdown.spec.tsx
+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx
@@ -113,6 +113,38 @@ describe("Dropdown", () => {
expect(getToggle().getAttribute("aria-expanded")).toBe("true");
});
+ it("closes an open menu before disabling the toggle", () => {
+ const el = renderInto(<Dropdown>item</Dropdown>);
+ const dropdownEl = el.querySelector(".dropdown");
+ expect(dropdownEl).toBeTruthy();
+ void act(() => {
+ $(dropdownEl as HTMLElement).trigger("show.bs.dropdown");
+ });
+
+ // Bootstrap's hide() returns early on a disabled toggle, so the state at call time matters.
+ let disabledAtHide: boolean | undefined;
+ instance.hide.mockImplementationOnce(() => {
+ disabledAtHide = getToggle().disabled;
+ });
+ void act(() => render(<Dropdown disabled>item</Dropdown>, el));
+ expect(instance.hide).toHaveBeenCalledTimes(1);
+ expect(disabledAtHide).toBe(false);
+
+ void act(() => {
+ $(dropdownEl as HTMLElement).trigger("hide.bs.dropdown");
+ });
+ expect(getToggle().disabled).toBe(true);
+ expect(getToggle().getAttribute("aria-expanded")).toBe("false");
+
+ // A closed dropdown takes the attribute at once and has nothing to hide.
+ void act(() => render(null, el));
+ el.remove();
+ instance.hide.mockClear();
+ renderInto(<Dropdown disabled>item</Dropdown>);
+ expect(instance.hide).not.toHaveBeenCalled();
+ expect(getToggle().disabled).toBe(true);
+ });
+
it("mounts the portaled menu on arm (pointerdown/focus), wires _menu, and tears down on blur without open", () => {
renderInto(<Dropdown portalToBody className="my-scope" text="btn">item</Dropdown>);
diff --git a/apps/client/src/widgets/react/Dropdown.tooltip.spec.tsx b/apps/client/src/widgets/react/Dropdown.tooltip.spec.tsx
index dcc3de163b..28cd0c1f0b 100644
--- a/apps/client/src/widgets/react/Dropdown.tooltip.spec.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tooltip.spec.tsx
@@ -73,3 +73,30 @@ describe("Dropdown tooltip", () => {
expect(shownTooltips(), "gone with the dropdown").toBe(0);
});
});
+
+describe("Dropdown with a disabled toggle", () => {
+ let container: HTMLElement;
+
+ beforeEach(() => {
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ });
+
+ afterEach(() => {
+ act(() => render(null, container));
+ container.remove();
+ });
+
+ it("closes an open menu when the toggle becomes disabled", () => {
+ act(() => render(<Dropdown>item</Dropdown>, container));
+ const toggle = container.querySelector("button");
+ const menu = container.querySelector(".dropdown-menu");
+ act(() => toggle?.click());
+ expect(menu?.classList.contains("show"), "opened by the click").toBe(true);
+
+ act(() => render(<Dropdown disabled>item</Dropdown>, container));
+ expect(menu?.classList.contains("show"), "closed by the disable").toBe(false);
+ expect(toggle?.disabled).toBe(true);
+ expect(toggle?.getAttribute("aria-expanded")).toBe("false");
+ });
+});
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsx
index 3692dd5074..7e9900c447 100644
--- a/apps/client/src/widgets/react/Dropdown.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tsx
@@ -188,6 +188,14 @@ export default function Dropdown({ id, className, buttonClassName, isStatic, chi
}
}, [ mobileBackdrop ]);
+ // Bootstrap's `hide()` and `clearMenus()` skip a disabled toggle, so the menu closes before the
+ // toggle takes the `disabled` attribute.
+ useLayoutEffect(() => {
+ if (disabled && shown) {
+ dropdownInstanceRef.current?.hide();
+ }
+ }, [ disabled, shown ]);
+
// A portaled menu lives in `document.body`, outside any modal that opened it. That modal's focus-trap
// would keep yanking focus back into the modal, so an input in the menu (e.g. the note-icon picker's
// search box) could never hold focus. Suspend the shown modals' traps while the menu is open.
@@ -259,7 +267,7 @@ export default function Dropdown({ id, className, buttonClassName, isStatic, chi
aria-haspopup="true"
aria-expanded={shown}
id={id ?? ariaId}
- disabled={disabled}
+ disabled={disabled && !shown}
// 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.