Skip to content

Build your personal knowledge base with Trilium Notes

fix(dropdown): suspend dialog focus traps before a bottom sheet takes focus

Elian Dorand2b1548f8922
diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsx
index 85a25465ea..0e45ab8b85 100644
--- a/apps/client/src/widgets/react/Dropdown.spec.tsx
+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx
@@ -386,6 +386,26 @@ describe("Dropdown", () => {
await vi.waitFor(() => expect(popup()).toBeNull());
expect(restore).toHaveBeenCalledTimes(1);
});
+
+ it("suspends the focus traps before a bottom sheet, placed at once, takes focus", async () => {
+ layout.onMobile = true;
+ const order: string[] = [];
+ focusTraps.suspend.mockImplementation(() => {
+ order.push("suspend");
+ return () => {};
+ });
+ const focus = HTMLElement.prototype.focus;
+ vi.spyOn(HTMLElement.prototype, "focus").mockImplementation(function (this: HTMLElement, options) {
+ if (this.closest(".tn-popup")) order.push("popup focus");
+ focus.call(this, options);
+ });
+ const { toggle } = renderDropdown({ mobileBottomSheet: true });
+
+ click(toggle);
+ await vi.waitFor(() => expect(popup()?.contains(document.activeElement)).toBe(true));
+ expect(order.at(0)).toBe("suspend");
+ expect(order).toContain("popup focus");
+ });
});
describe("as a menu", () => {
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsx
index fa51950394..205bcaf9a6 100644
--- a/apps/client/src/widgets/react/Dropdown.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tsx
@@ -210,12 +210,22 @@ function DropdownToggle({ id, className, buttonClassName, title, text, dropdownC
const shownRef = useRef(shown);
const callbacks = useRef({ onShown, onHidden, hideTooltip });
callbacks.current = { onShown, onHidden, hideTooltip };
+ const restoreFocusTraps = useRef<(() => void) | null>(null);
const setOpen = useCallback((open: boolean) => {
if (shownRef.current === open) return;
shownRef.current = open;
+ // A dialog's focus trap would pull focus out of a popup that stands outside the dialog. The
+ // traps go before the render, since a bottom sheet takes focus in the effect that places it.
+ if (open) {
+ restoreFocusTraps.current = suspendModalFocusTraps();
+ }
// Focus that the popup holds goes back to the toggle, as a native menu button's does; a
// command that moved it elsewhere keeps it there.
if (!open && popupRef.current?.contains(document.activeElement)) triggerRef.current?.focus();
+ if (!open) {
+ restoreFocusTraps.current?.();
+ restoreFocusTraps.current = null;
+ }
if (!open) setStartAt(undefined);
setShown(open);
if (open) {
@@ -260,11 +270,7 @@ function DropdownToggle({ id, className, buttonClassName, title, text, dropdownC
return () => cover?.classList.remove("show", "global-menu-cover");
}, [ shown, mobileBackdrop ]);
- // A dialog's focus trap would pull focus out of a popup that stands outside the dialog.
- useLayoutEffect(() => {
- if (!shown) return;
- return suspendModalFocusTraps();
- }, [ shown ]);
+ useEffect(() => () => restoreFocusTraps.current?.(), []);
const ariaId = useUniqueName("button");
const toggleId = id ?? ariaId;