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(client): keep a disabled forceShown dropdown closed
matt wilkie7f0ac86c68d0
diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsxindex 45361775f3..0cedb96998 100644--- a/apps/client/src/widgets/react/Dropdown.spec.tsx+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx@@ -140,9 +140,17 @@ describe("Dropdown", () => {void act(() => render(null, el));el.remove();instance.hide.mockClear();- renderInto(<Dropdown disabled>item</Dropdown>);+ const closedEl = renderInto(<Dropdown disabled>item</Dropdown>);expect(instance.hide).not.toHaveBeenCalled();expect(getToggle().disabled).toBe(true);++ // `forceShown` does not open a disabled dropdown, so the toggle keeps the attribute.+ void act(() => render(null, closedEl));+ closedEl.remove();+ instance.show.mockClear();+ renderInto(<Dropdown disabled forceShown>item</Dropdown>);+ expect(instance.show).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", () => {diff --git a/apps/client/src/widgets/react/Dropdown.tooltip.spec.tsx b/apps/client/src/widgets/react/Dropdown.tooltip.spec.tsxindex 28cd0c1f0b..4e2b4f3403 100644--- a/apps/client/src/widgets/react/Dropdown.tooltip.spec.tsx+++ b/apps/client/src/widgets/react/Dropdown.tooltip.spec.tsx@@ -4,8 +4,8 @@ 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 what Bootstrap does with the elements it is handed.+// This spec keeps Bootstrap real for the tooltip and for closing the menu. Dropdown.spec.tsx mocks+// Bootstrap to assert the wiring of the component.class ResizeObserverStub {observe() {}diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsxindex 7e9900c447..603dd39d42 100644--- a/apps/client/src/widgets/react/Dropdown.tsx+++ b/apps/client/src/widgets/react/Dropdown.tsx@@ -38,6 +38,10 @@ export interface DropdownProps extends Pick<HTMLProps<HTMLDivElement>, "id" | "c* working pseudo-element layer.*/noDropdownListStyle?: boolean;+ /**+ * The only supported way to disable the toggle. Bootstrap cannot close a menu whose toggle is+ * disabled through `buttonProps` or a `disabled` class.+ */disabled?: boolean;text?: ComponentChildren;forceShown?: boolean;@@ -160,7 +164,7 @@ export default function Dropdown({ id, className, buttonClassName, isStatic, chi// Show a forced-open dropdown once mounted, and dispose the instance only when the component truly// unmounts (not on every menu remount driven by the effect above).useEffect(() => {- if (forceShown) {+ if (forceShown && !disabled) {dropdownInstanceRef.current?.show();setShown(true);}@@ -188,8 +192,8 @@ 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.+ // Bootstrap's `hide()` and `clearMenus()` skip a disabled toggle, so this effect closes the+ // menu first. The toggle takes the `disabled` attribute only once `shown` is false.useLayoutEffect(() => {if (disabled && shown) {dropdownInstanceRef.current?.hide();