Skip to content

Build your personal knowledge base with Trilium Notes

fix(dropdown): show the popup of a dropdown forced open on mount

Elian Doranc5f3002d213f
diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsx
index 5d3e181fba..073fb21271 100644
--- a/apps/client/src/widgets/react/Dropdown.spec.tsx
+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx
@@ -119,6 +119,16 @@ describe("Dropdown", () => {
await vi.waitFor(() => expect(popup()).toBeNull());
});
+ it("shows its popup from the start when forced open, and says so once", async () => {
+ const onShown = vi.fn();
+ const { toggle } = renderDropdown({ forceShown: true, onShown });
+
+ await vi.waitFor(() => expect(popup()?.style.visibility).toBe("visible"));
+ expect(popup()?.textContent).toBe("By title");
+ expect(toggle.getAttribute("aria-expanded")).toBe("true");
+ expect(onShown).toHaveBeenCalledTimes(1);
+ });
+
it("closes an open popup once it is disabled, and does not open while it is", async () => {
const { toggle } = renderDropdown();
click(toggle);
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsx
index 5500a1cdf7..2803918bef 100644
--- a/apps/client/src/widgets/react/Dropdown.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tsx
@@ -225,6 +225,13 @@ function DropdownToggle({ id, className, buttonClassName, isStatic, title, text,
if (disabled) setOpen(false);
}, [ disabled ]);
+ // The popup anchors to `triggerRef`, which is empty during the first render, so a dropdown shown
+ // from the start renders again once the toggle is mounted, before the browser paints.
+ const [ , setTriggerMounted ] = useState(false);
+ useLayoutEffect(() => {
+ if (shownRef.current) setTriggerMounted(true);
+ }, []);
+
// One shown from the start says so once it is mounted. `shown` is the first render's here.
useEffect(() => {
if (shown) callbacks.current.onShown?.();