Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(button): open a split button's actions in a `Dropdown` menu
Elian Dorana38984e16ef8
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex dd41f51d6f..d35578c385 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4493,5 +4493,8 @@"label_marker_negated": "Excludes a label. Press Enter to see the list of labels","relation_marker": "Refers to a relation. Press Enter to see the list of relations","relation_marker_negated": "Excludes a relation. Press Enter to see the list of relations"+ },+ "split_button": {+ "more_actions": "More actions"}}diff --git a/apps/client/src/widgets/react/Button.spec.tsx b/apps/client/src/widgets/react/Button.spec.tsxnew file mode 100644index 0000000000..58400d717a--- /dev/null+++ b/apps/client/src/widgets/react/Button.spec.tsx@@ -0,0 +1,55 @@+import { render } from "preact";+import { afterEach, describe, expect, it, vi } from "vitest";++import { SplitButton } from "./Button";+import { FormListItem } from "./FormList";++vi.mock("../../services/i18n", () => ({ t: (key: string) => key }));+// A dialog's focus trap would pull focus out of a menu portaled over it.+vi.mock("./modal_focustrap", () => ({ suspendModalFocusTraps: () => () => {} }));++describe("SplitButton", () => {+ const host = document.createElement("div");+ document.body.append(host);+ afterEach(() => render(null, host));++ /** A press and its release, as a pointer makes them. */+ const press = (element: Element) => {+ element.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));+ element.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }));+ };++ it("runs its own action from the main half, and opens the others as a menu from the split", async () => {+ const calls: string[] = [];+ render((+ <SplitButton text="Search" icon="bx bx-search" onClick={() => calls.push("search")}>+ <FormListItem icon="bx bxs-zap" onClick={() => calls.push("execute")}>Search and execute</FormListItem>+ </SplitButton>+ ), host);+ const [ main, toggle ] = host.querySelectorAll<HTMLButtonElement>(".btn-group button");+ if (!main || !toggle) throw new Error("expected both halves to render");++ expect(main.querySelector(".tn-icon")?.className).toContain("bx bx-search");+ expect(main.querySelector(".tn-icon")?.className).not.toContain("bx bx bx-search");+ press(main);+ expect(calls).toEqual([ "search" ]);+ expect(document.querySelector("[role=menu]")).toBeNull();++ // A menu of the app's own, which Bootstrap's data API has no part in.+ expect(toggle.hasAttribute("data-bs-toggle")).toBe(false);+ expect(toggle.getAttribute("aria-label")).toBe("split_button.more_actions");+ expect(toggle.getAttribute("aria-haspopup")).toBe("menu");+ press(toggle);+ const menu = await vi.waitFor(() => {+ const found = document.querySelector<HTMLElement>(".tn-popup.tn-menu");+ if (!found) throw new Error("expected the menu to open");+ return found;+ });+ const row = menu.querySelector<HTMLElement>("li.dropdown-item");+ expect(row?.textContent).toBe("Search and execute");++ if (row) press(row);+ expect(calls).toEqual([ "search", "execute" ]);+ await vi.waitFor(() => expect(document.querySelector(".tn-popup.tn-menu")).toBeNull());+ });+});diff --git a/apps/client/src/widgets/react/Button.tsx b/apps/client/src/widgets/react/Button.tsxindex 2ca797347e..63cc1d4eb5 100644--- a/apps/client/src/widgets/react/Button.tsx+++ b/apps/client/src/widgets/react/Button.tsx@@ -5,8 +5,10 @@ import type { ComponentChildren, CSSProperties, JSX, RefObject } from "preact";import { useMemo, useRef } from "preact/hooks";import { CommandNames } from "../../components/app_context";+import { t } from "../../services/i18n";import { isDesktop, isMobile } from "../../services/utils";import ActionButton from "./ActionButton";+import Dropdown from "./Dropdown";import { useStaticTooltip } from "./hooks";import Icon from "./Icon";import { renderShortcutKbds } from "./shortcut_kbd";@@ -126,27 +128,32 @@ export function ButtonGroup({ size, className, children }: { size?: "sm" | "lg";);}+/** A button with the related actions in a menu beside it. */export function SplitButton({ text, icon, children, ...restProps }: {text: string;+ /** The icon's full class, as `"bx bx-search"`. */icon?: string;title?: string;/** Click handler for the main button component (not the split). */onClick?: () => void;- /** The children inside the dropdown of the split. */+ /** The menu's rows: `FormListItem`s and the other `FormList` rows. */children: ComponentChildren;}) {return (<ButtonGroup><button type="button" class="btn btn-secondary" {...restProps}>- {icon && <Icon icon={`bx ${icon}`} />}+ {icon && <Icon icon={icon} />}{text}</button>- <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">- <span class="visually-hidden">Toggle Dropdown</span>- </button>- <ul class="dropdown-menu">+ <Dropdown+ buttonClassName="btn-secondary dropdown-toggle-split"+ buttonProps={{ "aria-label": t("split_button.more_actions") }}+ noSelectButtonStyle+ placement="bottom-end"+ mobileBottomSheet+ >{children}- </ul>+ </Dropdown></ButtonGroup>);}