Skip to content

Build your personal knowledge base with Trilium Notes

refactor(dropdown): frost panels on their frame, and scroll a panel's content inside it

Elian Doranf483af7ea1ea
diff --git a/.claude/skills/building-client-ui/SKILL.md b/.claude/skills/building-client-ui/SKILL.md
index ddbb451e93..869a6691b5 100644
--- a/.claude/skills/building-client-ui/SKILL.md
+++ b/.claude/skills/building-client-ui/SKILL.md
@@ -113,7 +113,7 @@ Two rules that apply to all of them:
Both kinds of dropdown share a toggle and open in a `Popup` portaled onto `<body>`, placed by Floating UI, so an ancestor's `overflow`, `transform` or `filter` never clips, displaces or flattens them. Which one to use depends on what opens:
- **`Dropdown`** opens a **menu** (`Menu`). Its rows are `FormListItem`, `FormDropdownSubmenu`, `FormListHeader`, `FormDropdownDivider` and `FormListCustomItem`; the menu takes focus and moves between them with the arrow keys, Home/End and typeahead, and tells assistive technology it is a menu. It caps its height to the viewport and scrolls in `.tn-menu-scroll`, while its backdrop blur stays on the frame's `::before`, so it is frosted everywhere.
-- **`DropdownPanel`** opens anything else: a picker, a form, a list of links, help text. Its fields keep their keys; Up and Down only move between `.dropdown-item`s. It carries `tn-dropdown-list`, which moves the blur onto the element itself — a filter that blurs nothing over note content — so pass **`noDropdownListStyle`** on a panel that does not scroll.
+- **`DropdownPanel`** opens anything else: a picker, a form, a list of links, help text. Its fields keep their keys; Up and Down only move between `.dropdown-item`s. Its blur, like a menu's, is on its `::before`, so its own frame must never scroll: for content that can outgrow the screen, pass **`scrollable`**, which caps the panel to the room it has and scrolls the content inside it.
A wrong pick fails quietly: a form in a `Dropdown` loses Up, Down, Tab and Escape to the menu, and a list of rows in a `DropdownPanel` gets none of the menu's keys. On a phone, pass `mobileBottomSheet` to open either as a sheet from the bottom of the screen over a dimmed page.
diff --git a/apps/client/src/widgets/EmbeddedNotePane.tsx b/apps/client/src/widgets/EmbeddedNotePane.tsx
index 86c17aaf93..d5c9ace59a 100644
--- a/apps/client/src/widgets/EmbeddedNotePane.tsx
+++ b/apps/client/src/widgets/EmbeddedNotePane.tsx
@@ -337,7 +337,6 @@ export function NoteColorAction({ note, title }: { note: FNote; title: string })
title={title}
iconAction
hideToggleArrow
- noDropdownListStyle
>
<NoteColorPicker note={note} />
</DropdownPanel>
diff --git a/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx b/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx
index ecade638c0..e9709fe036 100644
--- a/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx
+++ b/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx
@@ -62,7 +62,6 @@ export default function RecurrenceEditor({ note }: { note: FNote }) {
<DropdownPanel
className="calendar-recurrence-dropdown"
text={modelTitle(model)}
- noDropdownListStyle
dropdownOptions={{ autoClose: "outside" }}
>
<EventFieldList>
diff --git a/apps/client/src/widgets/layout/StatusBar.css b/apps/client/src/widgets/layout/StatusBar.css
index c88f9bbae8..9a4d6c9528 100644
--- a/apps/client/src/widgets/layout/StatusBar.css
+++ b/apps/client/src/widgets/layout/StatusBar.css
@@ -53,29 +53,6 @@
.dropdown-toggle {
padding: 0.1em 0.25em;
}
-
- .dropdown-menu {
- width: max-content;
- }
- }
-
- .dropdown-note-info {
- padding: 1em !important;
- }
-
- .dropdown-note-paths {
- .note-paths-widget {
- padding: 0.5em;
- }
- }
-
- .dropdown-backlinks {
- --menu-padding-size: .9em;
-
- min-width: 300px;
- max-width: 500px;
- max-height: 60vh;
- overflow-y: auto;
}
}
@@ -128,6 +105,30 @@
}
+/* The status bar's popups, which open on the page's body. */
+.dropdown-menu.status-bar-dropdown-menu {
+ width: max-content;
+
+ &.dropdown-note-info {
+ padding: 1em !important;
+ }
+
+ &.dropdown-note-paths .note-paths-widget {
+ padding: 0.5em;
+ }
+
+ &.dropdown-backlinks {
+ --menu-padding-size: .9em;
+
+ min-width: 300px;
+ max-width: 500px;
+
+ > .tn-panel-scroll {
+ max-height: 60vh;
+ }
+ }
+}
+
body.experimental-feature-new-layout .note-info-content {
ul {
--row-block-margin: .2em;
diff --git a/apps/client/src/widgets/layout/StatusBar.tsx b/apps/client/src/widgets/layout/StatusBar.tsx
index 1c9cf727c6..5e1e9f998a 100644
--- a/apps/client/src/widgets/layout/StatusBar.tsx
+++ b/apps/client/src/widgets/layout/StatusBar.tsx
@@ -91,7 +91,7 @@ export default function StatusBar() {
);
}
-function StatusBarDropdown({ children, icon, text, buttonClassName, titleOptions, dropdownOptions, panel, ...dropdownProps }: Omit<DropdownPanelProps, "hideToggleArrow" | "title" | "titlePosition"> & {
+function StatusBarDropdown({ children, icon, text, buttonClassName, dropdownContainerClassName, titleOptions, dropdownOptions, panel, ...dropdownProps }: Omit<DropdownPanelProps,"hideToggleArrow" | "title" | "titlePosition"> & {
title: string;
icon?: string;
/** Opens a {@link DropdownPanel} rather than a menu, for content other than `FormList` rows. */
@@ -101,6 +101,7 @@ function StatusBarDropdown({ children, icon, text, buttonClassName, titleOptions
return (
<Component
buttonClassName={clsx("status-bar-dropdown-button", buttonClassName)}
+ dropdownContainerClassName={clsx("status-bar-dropdown-menu", dropdownContainerClassName)}
titlePosition="top"
titleOptions={{
popperConfig: {
@@ -350,6 +351,7 @@ function BacklinksBadge({ note, viewScope }: StatusBarContext) {
text={t("status_bar.backlinks", { count })}
title={t("status_bar.backlinks_title", { count })}
dropdownContainerClassName="dropdown-backlinks"
+ scrollable
>
<BacklinksWidget note={note} />
</StatusBarDropdown>
@@ -494,7 +496,6 @@ function NotePaths({ note, hoistedNoteId, notePath }: StatusBarContext) {
text={t("status_bar.note_paths", { count })}
dropdownRef={dropdownRef}
dropdownContainerClassName="dropdown-note-paths"
- noDropdownListStyle
>
<NotePathsWidget sortedNotePaths={sortedNotePaths} currentNotePath={notePath} />
</StatusBarDropdown>
diff --git a/apps/client/src/widgets/react/Dropdown.css b/apps/client/src/widgets/react/Dropdown.css
new file mode 100644
index 0000000000..523427d5bf
--- /dev/null
+++ b/apps/client/src/widgets/react/Dropdown.css
@@ -0,0 +1,18 @@
+/* A scrollable panel's frame does not scroll; its content does, in the height it is capped to. */
+.dropdown-menu:has(> .tn-panel-scroll) {
+ display: flex;
+ flex-direction: column;
+}
+
+.tn-panel-scroll {
+ /* Matches the panel's own padding in the theme's `.dropdown-menu:not(.static)` rule. */
+ --tn-panel-padding: var(--padding, var(--menu-padding-size));
+ flex: 1 1 auto;
+ min-height: 0;
+ /* Spans the panel's padding, so the content scrolls up to its edge rather than being cut off
+ inside it. */
+ margin: calc(0px - var(--tn-panel-padding));
+ padding: var(--tn-panel-padding);
+ border-radius: inherit;
+ overflow-y: auto;
+}
diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsx
index 9ae974790e..9f660c1efc 100644
--- a/apps/client/src/widgets/react/Dropdown.spec.tsx
+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx
@@ -2,7 +2,7 @@ import { type ComponentChildren, render } from "preact";
import { useRef } from "preact/hooks";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import Dropdown, { type DropdownHandle, DropdownPanel, type DropdownProps } from "./Dropdown";
+import Dropdown, { type DropdownHandle, DropdownPanel, type DropdownPanelProps } from "./Dropdown";
import { FormDropdownSubmenu, FormListItem } from "./FormList";
// A dialog's focus trap would pull focus out of a menu portaled over it.
@@ -33,7 +33,7 @@ describe("Dropdown", () => {
});
/** Renders a `Dropdown`, or a `DropdownPanel` for `panel`. */
- function renderDropdown({ panel, ...props }: Partial<DropdownProps> & { panel?: boolean } = {}, content: ComponentChildren = <li className="dropdown-item">By title</li>) {
+ function renderDropdown({ panel, ...props }: Partial<DropdownPanelProps> & { panel?: boolean } = {}, content: ComponentChildren = <li className="dropdown-item">By title</li>) {
let handle: { current: DropdownHandle | null } = { current: null };
const Component = panel ? DropdownPanel : Dropdown;
function Harness() {
@@ -192,7 +192,8 @@ describe("Dropdown", () => {
// A plain popup, which assistive technology is not told is a menu.
expect(toggle.getAttribute("aria-haspopup")).toBe("true");
expect(popup()?.getAttribute("role")).toBeNull();
- expect(popup()?.classList.contains("tn-dropdown-list")).toBe(true);
+ // Its frame does not scroll, so the theme's blur stays on its `::before`.
+ expect(popup()?.classList.contains("tn-dropdown-list")).toBe(false);
// Bootstrap reacts on a toggle only with `data-bs-toggle`, which this one does not carry.
bootstrapHeard.mockClear();
@@ -215,6 +216,23 @@ describe("Dropdown", () => {
document.removeEventListener("keydown", bootstrapHeard, true);
});
+ it("scrolls a scrollable panel's content inside it, in the room the viewport leaves", async () => {
+ // Content taller than the room: 100px each, in a viewport of 800.
+ const { toggle } = renderDropdown({ panel: true, scrollable: true });
+ click(toggle);
+ await vi.waitFor(() => expect(popup()?.style.visibility).toBe("visible"));
+ expect(popup()?.querySelector(":scope > .tn-panel-scroll")?.textContent).toBe("By title");
+ expect(popup()?.style.maxHeight).not.toBe("");
+ render(null, host);
+
+ // Otherwise the content stands in the panel, which grows with it.
+ const { toggle: plain } = renderDropdown({ panel: true });
+ click(plain);
+ await vi.waitFor(() => expect(popup()?.style.visibility).toBe("visible"));
+ expect(popup()?.querySelector(".tn-panel-scroll")).toBeNull();
+ expect(popup()?.style.maxHeight).toBe("");
+ });
+
it("suspends the focus traps of the dialogs under it while it is up, and restores them after", async () => {
const restore = vi.fn();
focusTraps.suspend.mockReturnValue(restore);
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsx
index 3d8793945d..fac64dbded 100644
--- a/apps/client/src/widgets/react/Dropdown.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tsx
@@ -1,5 +1,7 @@
import type { Placement } from "@floating-ui/dom";
import type { Dropdown as BootstrapDropdown, Tooltip } from "bootstrap";
+import "./Dropdown.css";
+
import clsx from "clsx";
import { ComponentChildren, HTMLAttributes } from "preact";
import { CSSProperties, HTMLProps } from "preact/compat";
@@ -57,14 +59,11 @@ export interface DropdownProps extends Pick<HTMLProps<HTMLDivElement>, "id" | "c
export interface DropdownPanelProps extends DropdownProps {
/**
- * Drops the `tn-dropdown-list` class the panel otherwise carries.
- *
- * That class is for a panel that scrolls: it moves the theme's backdrop blur off the `::before`
- * layer, which would scroll away with the content, and onto the panel element itself. Over note
- * content the element's filter blurs nothing, leaving the panel merely translucent, so set this
- * on a panel that does not scroll. A `Dropdown` menu keeps its blur on a layer that stays put.
+ * Caps the panel to the room beside its toggle and scrolls its content inside it, for content
+ * that can outgrow the screen. The panel's frame stays put, so the theme's blur on its
+ * `::before` covers it whatever the content scrolls to.
*/
- noDropdownListStyle?: boolean;
+ scrollable?: boolean;
}
/** Opens and closes a dropdown from outside it, for a caller that decides when. */
@@ -107,7 +106,7 @@ export default function Dropdown(props: DropdownProps) {
* A popup under a toggle for content other than a menu's rows, such as a picker, a form or a
* list of links. Its fields keep their keys; Up and Down move between its `.dropdown-item`s.
*/
-export function DropdownPanel({ noDropdownListStyle, ...props }: DropdownPanelProps) {
+export function DropdownPanel({ scrollable, ...props }: DropdownPanelProps) {
return (
<DropdownToggle
{...props}
@@ -116,16 +115,17 @@ export function DropdownPanel({ noDropdownListStyle, ...props }: DropdownPanelPr
popup={({ popupProps, className, bottomSheet, startAt, dismiss }) => (
<PanelPopup
{...popupProps}
- // Bootstrap's dropdowns grew past the viewport, and a `FormDropdownSubmenu`
- // nested in the panel would be clipped by one that scrolls.
- capHeight={false}
+ // Otherwise it grows with its content, as Bootstrap's dropdowns did: a
+ // `FormDropdownSubmenu` nested in it would be clipped by one that scrolls.
+ capHeight={!!scrollable}
placedByStylesheet={bottomSheet}
- className={clsx("dropdown-menu show", className, bottomSheet && "mobile-bottom-menu",
- !noDropdownListStyle && "tn-dropdown-list")}
+ className={clsx("dropdown-menu show", className, bottomSheet && "mobile-bottom-menu")}
startAt={startAt}
onDismiss={dismiss}
>
- {props.children}
+ {scrollable
+ ? <div className="tn-panel-scroll">{props.children}</div>
+ : props.children}
</PanelPopup>
)}
/>
diff --git a/apps/client/src/widgets/react/HelpDropdown.tsx b/apps/client/src/widgets/react/HelpDropdown.tsx
index 6ae325bcea..1b1f3f06cf 100644
--- a/apps/client/src/widgets/react/HelpDropdown.tsx
+++ b/apps/client/src/widgets/react/HelpDropdown.tsx
@@ -41,7 +41,6 @@ export default function HelpDropdown({ children, helpPage, openInPopup, classNam
iconAction
hideToggleArrow
noSelectButtonStyle
- noDropdownListStyle
>
{children}