Skip to content

Build your personal knowledge base with Trilium Notes

fix(dropdown): cap launcher bar menus above the bar on phones

Elian Doran0b24691db813
diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsx
index 0e45ab8b85..5e73a34b2d 100644
--- a/apps/client/src/widgets/react/Dropdown.spec.tsx
+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx
@@ -541,6 +541,25 @@ describe("Dropdown", () => {
cover.remove();
});
+ it("leaves a launcher bar menu to the stylesheet on a phone, and beside its toggle on a tablet", async () => {
+ layout.onMobile = true;
+ const { toggle } = renderDropdown({ mobileBackdrop: true });
+
+ click(toggle);
+ await vi.waitFor(() => expect(popup()?.style.visibility).toBe("visible"));
+ expect(popup()?.classList.contains("mobile-bottom-menu")).toBe(false);
+ const { left, top, maxHeight } = popup()?.style ?? {};
+ expect([ left, top, maxHeight ]).toEqual([ "", "", "" ]);
+
+ click(toggle);
+ await vi.waitFor(() => expect(popup()).toBeNull());
+ layout.narrow = false;
+ for (const listener of layout.onChange) listener();
+ click(toggle);
+ await vi.waitFor(() => expect(popup()?.style.top).toBe("82px"));
+ expect(popup()?.style.maxHeight).not.toBe("");
+ });
+
it("turns into a placed menu and back as a tablet turns while it is open", async () => {
layout.onMobile = true;
const turn = (narrow: boolean) => {
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsx
index 205bcaf9a6..5335fe1a74 100644
--- a/apps/client/src/widgets/react/Dropdown.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tsx
@@ -53,6 +53,11 @@ export interface DropdownProps extends Pick<HTMLAttributes<HTMLDivElement>, "id"
dropdownRef?: RefObject<DropdownHandle | null>;
titlePosition?: "top" | "right" | "bottom" | "left";
titleOptions?: Partial<Tooltip.Options>;
+ /**
+ * For a launcher bar menu: on a phone, dims the page above the bar and leaves the popup to the
+ * stylesheet, which pins it above the bar. On a tablet's wider mobile layout, the popup opens
+ * beside its toggle over the dimmed page. No effect on a desktop layout.
+ */
mobileBackdrop?: boolean;
/**
* On a phone, shows the popup as a sheet rising from the bottom of the screen over a dimmed
@@ -134,7 +139,7 @@ export function DropdownPanel({ scrollable, ...props }: DropdownPanelProps) {
{...popupProps}
// Otherwise it grows with its content, as Bootstrap's dropdowns did.
capHeight={!!scrollable}
- placedByStylesheet={bottomSheet}
+ placedByStylesheet={popupProps.placedByStylesheet}
className={clsx("dropdown-menu show", className, bottomSheet && "mobile-bottom-menu")}
startAt={startAt}
onDismiss={dismiss}
@@ -152,7 +157,7 @@ export function DropdownPanel({ scrollable, ...props }: DropdownPanelProps) {
interface PopupSlot {
/** Placement, portal, backdrop and click handling, for the `Popup` or `Menu`. */
popupProps: Pick<PopupProps, "anchor" | "placement" | "offset" | "portalClassName" | "backdropClassName"
- | "elementRef" | "style" | "aria-labelledby" | "onClick">;
+ | "elementRef" | "style" | "aria-labelledby" | "onClick" | "placedByStylesheet">;
className: string;
bottomSheet: boolean;
/** The item to start at, for a popup a key on the toggle opened. */
@@ -175,14 +180,16 @@ function DropdownToggle({ id, className, buttonClassName, title, text, dropdownC
}) {
// The sheet is placed by the app's own rule and dims what is behind it, so it is asked for as
// one thing and unpacked here. It follows the layout, as a tablet can turn while it is open.
- const followsLayout = !!mobileBottomSheet && isMobile();
+ const followsLayout = (!!mobileBottomSheet || !!mobileBackdropProp) && isMobile();
const [ narrow, setNarrow ] = useState(isNarrowLayout);
useEffect(() => {
if (!followsLayout) return;
setNarrow(isNarrowLayout());
return onNarrowLayoutChange(() => setNarrow(isNarrowLayout()));
}, [ followsLayout ]);
- const bottomSheet = followsLayout && narrow;
+ const bottomSheet = followsLayout && narrow && !!mobileBottomSheet;
+ // A launcher bar menu on a phone is pinned above the bar by `style.css`.
+ const pinnedAboveBar = followsLayout && narrow && !mobileBottomSheet;
const mobileBackdrop = (!!mobileBackdropProp || !!mobileBottomSheet) && isMobile();
const containerRef = useRef<HTMLDivElement | null>(null);
const triggerRef = useRef<HTMLButtonElement | null>(null);
@@ -333,6 +340,7 @@ function DropdownToggle({ id, className, buttonClassName, title, text, dropdownC
// In a wrapper with the dropdown's classes, so CSS scoped under them still
// applies to the popup in the page's body. style.css stacks a portaled popup by
// `tn-dropdown-portal`, and its backdrop just under it.
+ placedByStylesheet: bottomSheet || pinnedAboveBar,
portalClassName: clsx("tn-dropdown-portal", className),
backdropClassName: backdrop ? "tn-dropdown-backdrop" : undefined,
elementRef: popupRef,
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index 0ccb2d57fc..6afa424fa2 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -24,7 +24,7 @@ import Popup, { type PopupProps } from "./Popup";
* `FormDropdownSubmenu`, `FormDropdownDivider`, `FormListHeader` and `FormListCustomItem`.
*/
export interface MenuProps<T> extends Pick<PopupProps, "anchor" | "offset" | "container" | "portalClassName"
- | "backdropClassName" | "style" | "onClick" | "elementRef" | "aria-labelledby"> {
+ | "backdropClassName" | "style" | "onClick" | "elementRef" | "aria-labelledby" | "placedByStylesheet"> {
id?: string;
className?: string;
/** The side of its anchor it prefers: beside a point towards the right, or below an element. */
@@ -388,7 +388,7 @@ export default function Menu<T>({ id, className, anchor, placement, bottomSheet,
{...popupProps}
anchor={anchor}
placement={placement ?? (anchor instanceof HTMLElement ? "bottom-start" : "right-start")}
- placedByStylesheet={bottomSheet || inline}
+ placedByStylesheet={popupProps.placedByStylesheet || bottomSheet || inline}
onPlaced={() => {
const menu = menuRef.current;
if (menu) {