Skip to content

Build your personal knowledge base with Trilium Notes

refactor(dropdown): take `placement` and `autoClose` as props of its own

Elian Doran600f4ea620e8
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
index 88f76517e1..9f1a9080a0 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
@@ -1016,7 +1016,7 @@ function RelatedNotesBadge({ attribute, currentNoteId }: { attribute: Attribute;
tooltip={t("attribute_detail.other_notes_with_name", { attributeType: type, attributeName: name })}
// The menu stays nested in the popup: the badge places it with `position: fixed`, and the popup
// sets `contain: none` and no transform, so it is not a containing block and does not clip it.
- dropdownOptions={{ dropdownContainerClassName: "related-notes-menu" }}
+ dropdownProps={{ dropdownContainerClassName: "related-notes-menu" }}
>
{/* The icon comes from the item rather than from the link, so that the note entries and the
search entry below them line up on the same slot. */}
diff --git a/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx b/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx
index e9709fe036..418f486309 100644
--- a/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx
+++ b/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx
@@ -62,7 +62,7 @@ export default function RecurrenceEditor({ note }: { note: FNote }) {
<DropdownPanel
className="calendar-recurrence-dropdown"
text={modelTitle(model)}
- dropdownOptions={{ autoClose: "outside" }}
+ autoClose="outside"
>
<EventFieldList>
<FrequencyField model={model} commit={commit} />
diff --git a/apps/client/src/widgets/launch_bar/CalendarWidget.tsx b/apps/client/src/widgets/launch_bar/CalendarWidget.tsx
index 1376c69bbf..d3f11d2523 100644
--- a/apps/client/src/widgets/launch_bar/CalendarWidget.tsx
+++ b/apps/client/src/widgets/launch_bar/CalendarWidget.tsx
@@ -77,9 +77,7 @@ export default function CalendarWidget({ launcherNote }: LauncherNoteProps) {
}
}}
dropdownRef={dropdownRef}
- dropdownOptions={{
- autoClose: "outside"
- }}
+ autoClose="outside"
>
{calendarArgs && date && <div className="calendar-dropdown-widget" style={{ width: 400 }}>
<CalendarHeader date={date} setDate={setDate} />
@@ -142,7 +140,6 @@ function CalendarMonthSelector({ date, setDate }: CalendarHeaderProps) {
keyProperty="index" titleProperty="text"
onChange={(index) => setDate(date.set("month", parseInt(index, 10)))}
buttonProps={{ "data-calendar-input": "month" }}
- dropdownOptions={{ display: "static" }}
/>
<AdjustDateButton date={date} setDate={setDate} direction="next" unit="month" />
</div>
diff --git a/apps/client/src/widgets/launch_bar/launch_bar_widgets.tsx b/apps/client/src/widgets/launch_bar/launch_bar_widgets.tsx
index b00ac9a766..5e1394636f 100644
--- a/apps/client/src/widgets/launch_bar/launch_bar_widgets.tsx
+++ b/apps/client/src/widgets/launch_bar/launch_bar_widgets.tsx
@@ -47,7 +47,7 @@ export function LaunchBarActionButton({ className, launcherNote, onContextMenu,
);
}
-export function LaunchBarDropdownButton({ children, icon, dropdownOptions, launcherNote, buttonProps, panel, ...props }: Pick<DropdownProps, "title" | "children" | "onShown" | "dropdownOptions" | "dropdownRef" | "buttonProps"> & {
+export function LaunchBarDropdownButton({ children, icon, launcherNote, buttonProps, panel, ...props }: Pick<DropdownProps, "title" | "children" | "onShown" | "autoClose" | "dropdownRef" | "buttonProps"> & {
icon: string,
launcherNote?: FNote,
/** Opens a {@link DropdownPanel} rather than a menu, for content other than `FormList` rows. */
@@ -69,12 +69,7 @@ export function LaunchBarDropdownButton({ children, icon, dropdownOptions, launc
text={<Icon icon={icon} />}
titlePosition={titlePosition}
titleOptions={DROPDOWN_TITLE_OPTIONS}
- dropdownOptions={{
- ...dropdownOptions,
- popperConfig: {
- placement: titlePosition
- }
- }}
+ placement={titlePosition}
mobileBackdrop
buttonProps={resolvedButtonProps}
{...props}
diff --git a/apps/client/src/widgets/layout/ActiveContentBadges.tsx b/apps/client/src/widgets/layout/ActiveContentBadges.tsx
index 358f95fc2e..81ca47aadb 100644
--- a/apps/client/src/widgets/layout/ActiveContentBadges.tsx
+++ b/apps/client/src/widgets/layout/ActiveContentBadges.tsx
@@ -175,7 +175,7 @@ function ActiveContentBadge({ info, note }: { note: FNote, info: ActiveContentIn
className={clsx("active-content-badge", info.canToggleEnabled && !info.isEnabled && "disabled")}
icon={icon}
text={title}
- dropdownOptions={{
+ dropdownProps={{
dropdownContainerClassName: "mobile-bottom-menu",
mobileBackdrop: true
}}
diff --git a/apps/client/src/widgets/layout/Breadcrumb.tsx b/apps/client/src/widgets/layout/Breadcrumb.tsx
index a288893c1e..b5f28e6143 100644
--- a/apps/client/src/widgets/layout/Breadcrumb.tsx
+++ b/apps/client/src/widgets/layout/Breadcrumb.tsx
@@ -203,7 +203,7 @@ function BreadcrumbSeparator(props: BreadcrumbSeparatorProps) {
buttonClassName="icon-action breadcrumb-separator"
hideToggleArrow
dropdownContainerClassName="breadcrumb-child-list"
- dropdownOptions={{ popperConfig: { placement: "top" } }}
+ placement="top"
>
<BreadcrumbSeparatorDropdownContent {...props} />
</Dropdown>
diff --git a/apps/client/src/widgets/layout/StatusBar.tsx b/apps/client/src/widgets/layout/StatusBar.tsx
index 9c2b11102b..c33873003d 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, dropdownContainerClassName, titleOptions, dropdownOptions, panel, ...dropdownProps }: Omit<DropdownPanelProps,"hideToggleArrow" | "title" | "titlePosition"> & {
+function StatusBarDropdown({ children, icon, text, buttonClassName, dropdownContainerClassName, titleOptions, 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. */
@@ -111,12 +111,7 @@ function StatusBarDropdown({ children, icon, text, buttonClassName, dropdownCont
animation: false,
...titleOptions
}}
- dropdownOptions={{
- popperConfig: {
- placement: "top"
- },
- ...dropdownOptions
- }}
+ placement="top"
text={<>
{icon && (<><Icon icon={icon} />&nbsp;</>)}
<span className="text">{text}</span>
@@ -272,7 +267,7 @@ export function NoteInfoBadge(context: NoteInfoContext) {
title={t("status_bar.note_info_title")}
dropdownRef={dropdownRef}
dropdownContainerClassName="dropdown-note-info"
- dropdownOptions={{autoClose: "outside" }}
+ autoClose="outside"
onShown={() => setDropdownShown(true)}
onHidden={() => setDropdownShown(false)}
>
diff --git a/apps/client/src/widgets/react/Badge.tsx b/apps/client/src/widgets/react/Badge.tsx
index b33b40b447..9b79ba06eb 100644
--- a/apps/client/src/widgets/react/Badge.tsx
+++ b/apps/client/src/widgets/react/Badge.tsx
@@ -54,9 +54,9 @@ export function Badge({ icon, className, text, tooltip, href, outline, ...contai
);
}
-export function BadgeWithDropdown({ text, children, tooltip, className, dropdownOptions, ...props }: BadgeProps & {
+export function BadgeWithDropdown({ text, children, tooltip, className, dropdownProps, ...props }: BadgeProps & {
children: ComponentChildren,
- dropdownOptions?: Partial<DropdownProps>
+ dropdownProps?: Partial<DropdownProps>
}) {
return (
<Dropdown
@@ -73,14 +73,8 @@ export function BadgeWithDropdown({ text, children, tooltip, className, dropdown
hideToggleArrow
title={tooltip}
titlePosition="bottom"
- {...dropdownOptions}
- dropdownOptions={{
- ...dropdownOptions?.dropdownOptions,
- popperConfig: {
- ...dropdownOptions?.dropdownOptions?.popperConfig,
- placement: "bottom"
- }
- }}
+ {...dropdownProps}
+ placement="bottom"
>{children}</Dropdown>
);
}
diff --git a/apps/client/src/widgets/react/Dropdown.spec.tsx b/apps/client/src/widgets/react/Dropdown.spec.tsx
index 9f660c1efc..8ba1402d7c 100644
--- a/apps/client/src/widgets/react/Dropdown.spec.tsx
+++ b/apps/client/src/widgets/react/Dropdown.spec.tsx
@@ -160,7 +160,7 @@ describe("Dropdown", () => {
});
it("stays up for a click inside when it closes only on one outside", async () => {
- const { toggle } = renderDropdown({ dropdownOptions: { autoClose: "outside" } }, items);
+ const { toggle } = renderDropdown({ autoClose: "outside" }, items);
click(toggle);
await vi.waitFor(() => expect(popup()).not.toBeNull());
@@ -171,7 +171,7 @@ describe("Dropdown", () => {
});
it("opens above its toggle when asked to", async () => {
- const { toggle } = renderDropdown({ dropdownOptions: { popperConfig: { placement: "top" } } });
+ const { toggle } = renderDropdown({ placement: "top" });
vi.spyOn(toggle, "getBoundingClientRect").mockReturnValue(DOMRect.fromRect({ x: 100, y: 500, width: 80, height: 30 }));
click(toggle);
// Centered over the toggle, and ending 2px above it: 500 - 100 - 2.
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsx
index 4835b822a4..806713ceb4 100644
--- a/apps/client/src/widgets/react/Dropdown.tsx
+++ b/apps/client/src/widgets/react/Dropdown.tsx
@@ -1,5 +1,5 @@
import type { Placement } from "@floating-ui/dom";
-import type { Dropdown as BootstrapDropdown, Tooltip } from "bootstrap";
+import type { Tooltip } from "bootstrap";
import clsx from "clsx";
import { ComponentChildren, HTMLAttributes } from "preact";
import { CSSProperties, HTMLProps } from "preact/compat";
@@ -37,7 +37,14 @@ export interface DropdownProps extends Pick<HTMLProps<HTMLDivElement>, "id" | "c
forceShown?: boolean;
onShown?: () => void;
onHidden?: () => void;
- dropdownOptions?: Partial<BootstrapDropdown.Options>;
+ /** The side of the toggle the popup prefers; it flips to the other near the viewport's edge. */
+ placement?: Placement;
+ /**
+ * What closes the popup besides Escape and the toggle: `true` a click inside and a press
+ * outside, `"inside"` or `"outside"` that one alone, `false` neither. A row that stops its click
+ * keeps a menu open whatever this says.
+ */
+ autoClose?: boolean | "inside" | "outside";
dropdownRef?: MutableRef<DropdownHandle | null>;
titlePosition?: "top" | "right" | "bottom" | "left";
titleOptions?: Partial<Tooltip.Options>;
@@ -149,7 +156,7 @@ interface PopupSlot {
* The toggle both kinds of dropdown share: the button, its tooltip, the open state and its
* callbacks, the handle, and the phone's cover. `popup` draws what opens under it.
*/
-function DropdownToggle({ id, className, buttonClassName, isStatic, title, text, dropdownContainerStyle, dropdownContainerClassName, dropdownContainerRef: externalContainerRef, hideToggleArrow, iconAction, disabled, noSelectButtonStyle, forceShown, onShown, onHidden, dropdownOptions, buttonProps, dropdownRef, titlePosition, titleOptions, mobileBackdrop: mobileBackdropProp, mobileBottomSheet, backdrop, hasPopup, onToggleArrow, popup }: Omit<DropdownProps, "children"> & {
+function DropdownToggle({ id, className, buttonClassName, isStatic, title, text, dropdownContainerStyle, dropdownContainerClassName, dropdownContainerRef: externalContainerRef, hideToggleArrow, iconAction, disabled, noSelectButtonStyle, forceShown, onShown, onHidden, placement, autoClose = true, buttonProps, dropdownRef, titlePosition, titleOptions, mobileBackdrop: mobileBackdropProp, mobileBottomSheet, backdrop, hasPopup, onToggleArrow, popup }: Omit<DropdownProps, "children"> & {
hasPopup: "menu" | "true";
/** Called for Up or Down on the toggle while the popup is up. */
onToggleArrow?(popup: HTMLElement, edge: "first" | "last"): void;
@@ -165,11 +172,6 @@ function DropdownToggle({ id, className, buttonClassName, isStatic, title, text,
const [ shown, setShown ] = useState(!!forceShown && !disabled);
/** The item to start at once the popup shows, for a popup a key on the toggle opened. */
const [ startAt, setStartAt ] = useState<"first" | "last">();
- // As Bootstrap's `autoClose`: `true` closes on a click inside and a press outside, "inside" and
- // "outside" on that one alone, `false` on neither. Escape closes it whatever this says.
- const autoClose = dropdownOptions?.autoClose ?? true;
- const popperConfig = dropdownOptions?.popperConfig;
- const placement = toFloatingPlacement(popperConfig && typeof popperConfig === "object" ? popperConfig.placement : undefined);
// Memoized so useTooltip's effect (keyed on config identity) doesn't dispose and recreate the
// Bootstrap tooltip on every re-render — only when the title (or positioning) actually changes.
@@ -380,8 +382,3 @@ function focusItem(popup: HTMLElement, where: "first" | "last" | "next" | "previ
else target = items[(index + (where === "next" ? 1 : -1) + items.length) % items.length];
target?.focus();
}
-
-/** A placement as Popper names it, as Floating UI does; Popper's `auto` ones fall back to the default. */
-function toFloatingPlacement(placement: string | undefined): Placement | undefined {
- return placement && !placement.startsWith("auto") ? placement as Placement : undefined;
-}
diff --git a/apps/client/src/widgets/react/IconPicker.tsx b/apps/client/src/widgets/react/IconPicker.tsx
index f0461f2019..dc08cf6db1 100644
--- a/apps/client/src/widgets/react/IconPicker.tsx
+++ b/apps/client/src/widgets/react/IconPicker.tsx
@@ -178,20 +178,7 @@ function IconPickerDropdownButton({
disabled={disabled}
dropdownRef={dropdownRef}
dropdownContainerStyle={{ width: "620px" }}
- dropdownOptions={{
- autoClose: "outside",
- // Popper guards only the main axis against overflow, so a menu this tall, hung off
- // a button with room for it neither above nor below, was left hanging off the
- // bottom of the screen. Guarding the other axis too slides it back into view; the
- // side it opens on is still `flip`'s to choose.
- popperConfig: (defaults) => ({
- ...defaults,
- modifiers: [
- ...(defaults.modifiers ?? []),
- { name: "preventOverflow", options: { altAxis: true, padding: 8 } }
- ]
- })
- }}
+ autoClose="outside"
backdrop={backdrop}
hideToggleArrow
onShown={() => {
diff --git a/packages/commons/src/lib/script_api_preact.ts b/packages/commons/src/lib/script_api_preact.ts
index eeb991202b..bc6a4a74d3 100644
--- a/packages/commons/src/lib/script_api_preact.ts
+++ b/packages/commons/src/lib/script_api_preact.ts
@@ -147,20 +147,34 @@ interface DropdownProps {
hideToggleArrow?: boolean;
iconAction?: boolean;
noSelectButtonStyle?: boolean;
- noDropdownListStyle?: boolean;
disabled?: boolean;
text?: ComponentChildren;
forceShown?: boolean;
onShown?: () => void;
onHidden?: () => void;
- dropdownOptions?: Record<string, unknown>;
+ /** The side of the toggle the popup prefers, as Floating UI names it: `"top"`, `"bottom-start"`, … */
+ placement?: "top" | "top-start" | "top-end" | "right" | "right-start" | "right-end"
+ | "bottom" | "bottom-start" | "bottom-end" | "left" | "left-start" | "left-end";
+ /**
+ * What closes the popup besides Escape and the toggle: `true` a click inside and a press
+ * outside, `"inside"` or `"outside"` that one alone, `false` neither.
+ */
+ autoClose?: boolean | "inside" | "outside";
dropdownRef?: RefObject<unknown>;
titlePosition?: "top" | "right" | "bottom" | "left";
titleOptions?: Record<string, unknown>;
mobileBackdrop?: boolean;
}
+/** A menu under a toggle, whose rows are `FormListItem`s and the other `FormList` rows. */
export declare const Dropdown: FunctionComponent<DropdownProps>;
+interface DropdownPanelProps extends DropdownProps {
+ /** Caps the panel to the room beside its toggle and scrolls its content inside it. */
+ scrollable?: boolean;
+}
+/** A popup under a toggle for content other than a menu's rows, such as a form or a picker. */
+export declare const DropdownPanel: FunctionComponent<DropdownPanelProps>;
+
interface FormCheckboxProps {
name?: string;
label: ComponentChildren;