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
client/right pane: fix some issues
Adorian Doran872a8320467e
diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.spec.ts b/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsdeleted file mode 100644index 8f59556e85..0000000000--- a/apps/client/src/widgets/sidebar/RightPanelContainer.spec.ts+++ /dev/null@@ -1,43 +0,0 @@-import { afterEach, describe, expect, it } from "vitest";--import { isWithinOverlay } from "./RightPanelContainer";--describe("isWithinOverlay", () => {- afterEach(() => {- document.body.innerHTML = "";- });-- function mount(html: string) {- document.body.innerHTML = html;- }-- it("keeps the overlay open for the pane, the handle, and allowlisted popups", () => {- mount(`- <div id="right-pane"><span class="toc-item">x</span></div>- <button class="right-pane-toggle-handle"></button>- <ul class="dropdown-menu"><li class="dropdown-item">y</li></ul>- <div class="tooltip">t</div>- `);-- for (const selector of [".toc-item", ".right-pane-toggle-handle", ".dropdown-item", ".tooltip"]) {- expect(isWithinOverlay(document.querySelector(selector)), selector).toBe(true);- }- });-- it("dismisses for the backdrop, surrounding chrome, and non-elements", () => {- mount(`- <div class="right-pane-overlay-backdrop active"></div>- <div id="center-pane"><p class="editor">z</p></div>- <div class="tab-row-container"><span class="tab">t</span></div>- <div id="left-pane"><span class="tree-item">n</span></div>- `);-- // The backdrop is deliberately outside the allowlist, so clicking the covered content dismisses.- for (const selector of [".right-pane-overlay-backdrop", ".editor", ".tab", ".tree-item"]) {- expect(isWithinOverlay(document.querySelector(selector)), selector).toBe(false);- }- expect(isWithinOverlay(document.body)).toBe(false);- expect(isWithinOverlay(null)).toBe(false);- expect(isWithinOverlay(document)).toBe(false);- });-});diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsxnew file mode 100644index 0000000000..0658142208--- /dev/null+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsx@@ -0,0 +1,120 @@+import { render } from "preact";+import { act } from "preact/test-utils";+import { afterEach, describe, expect, it, vi } from "vitest";++import { isWithinOverlay, persistedRightPaneVisible, reduceRightPaneMode, useOverlayDismiss } from "./RightPanelContainer";++describe("reduceRightPaneMode", () => {+ it("opens as overlay from closed and closes otherwise", () => {+ expect(reduceRightPaneMode("closed", "toggleOverlay")).toBe("overlay");+ expect(reduceRightPaneMode("overlay", "toggleOverlay")).toBe("closed");+ expect(reduceRightPaneMode("docked", "toggleOverlay")).toBe("closed");+ });++ it("opens as docked from closed and closes otherwise", () => {+ expect(reduceRightPaneMode("closed", "toggleDocked")).toBe("docked");+ expect(reduceRightPaneMode("overlay", "toggleDocked")).toBe("closed");+ expect(reduceRightPaneMode("docked", "toggleDocked")).toBe("closed");+ });++ it("docks and closes unconditionally", () => {+ for (const prev of ["closed", "overlay", "docked"] as const) {+ expect(reduceRightPaneMode(prev, "dock")).toBe("docked");+ expect(reduceRightPaneMode(prev, "close")).toBe("closed");+ }+ });+});++describe("persistedRightPaneVisible", () => {+ it("persists only when the docked/closed distinction changes", () => {+ // No docked-ness change -> no write (overlay is ephemeral).+ expect(persistedRightPaneVisible("closed", "overlay")).toBe(null);+ expect(persistedRightPaneVisible("overlay", "closed")).toBe(null);+ // Becoming docked persists true; leaving docked persists false.+ expect(persistedRightPaneVisible("closed", "docked")).toBe(true);+ expect(persistedRightPaneVisible("overlay", "docked")).toBe(true);+ expect(persistedRightPaneVisible("docked", "closed")).toBe(false);+ expect(persistedRightPaneVisible("docked", "overlay")).toBe(false);+ });+});++describe("isWithinOverlay", () => {+ afterEach(() => {+ document.body.innerHTML = "";+ });++ it("keeps the overlay open for the pane, the handle, and allowlisted popups", () => {+ document.body.innerHTML = `+ <div id="right-pane"><span class="toc-item">x</span></div>+ <button class="right-pane-toggle-handle"></button>+ <ul class="dropdown-menu"><li class="dropdown-item">y</li></ul>+ <div class="tooltip">t</div>+ `;+ for (const selector of [".toc-item", ".right-pane-toggle-handle", ".dropdown-item", ".tooltip"]) {+ expect(isWithinOverlay(document.querySelector(selector)), selector).toBe(true);+ }+ });++ it("dismisses for the backdrop, surrounding chrome, and non-elements", () => {+ document.body.innerHTML = `+ <div class="right-pane-overlay-backdrop active"></div>+ <div id="center-pane"><p class="editor">z</p></div>+ <div class="tab-row-container"><span class="tab">t</span></div>+ <div id="left-pane"><span class="tree-item">n</span></div>+ `;+ // The backdrop is deliberately outside the allowlist, so clicking the covered content dismisses.+ for (const selector of [".right-pane-overlay-backdrop", ".editor", ".tab", ".tree-item"]) {+ expect(isWithinOverlay(document.querySelector(selector)), selector).toBe(false);+ }+ expect(isWithinOverlay(document.body)).toBe(false);+ expect(isWithinOverlay(null)).toBe(false);+ expect(isWithinOverlay(document)).toBe(false);+ });+});++describe("useOverlayDismiss", () => {+ afterEach(() => {+ document.body.innerHTML = "";+ });++ function renderHook(active: boolean, onDismiss: () => void) {+ const host = document.createElement("div");+ document.body.appendChild(host);+ function Harness() {+ useOverlayDismiss(active, onDismiss);+ return null;+ }+ // act() flushes the effect so the document listeners are attached before we dispatch.+ act(() => render(<Harness />, host));+ return () => act(() => render(null, host)); // unmount+ }++ it("dismisses on an outside press but not on a press within the overlay", () => {+ document.body.innerHTML = `<div id="right-pane"></div><div id="center-pane"></div>`;+ const onDismiss = vi.fn();+ const unmount = renderHook(true, onDismiss);++ document.querySelector("#right-pane")?.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true }));+ expect(onDismiss).not.toHaveBeenCalled();++ document.querySelector("#center-pane")?.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true }));+ expect(onDismiss).toHaveBeenCalledTimes(1);++ unmount();+ });++ it("dismisses on Escape, and does nothing while inactive", () => {+ const onDismiss = vi.fn();++ const unmountInactive = renderHook(false, onDismiss);+ document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }));+ document.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true }));+ expect(onDismiss).not.toHaveBeenCalled();+ unmountInactive();++ const unmountActive = renderHook(true, onDismiss);+ document.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }));+ expect(onDismiss).toHaveBeenCalledTimes(1);+ unmountActive();+ });+});diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.tsxindex 5803d6863d..7450a843ec 100644--- a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx@@ -47,25 +47,25 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPareconst items = useItems(visible, widgetsByParent);useSplit(mode === "docked");- // Persist only the docked/closed distinction; overlay is ephemeral, so it never writes the option.- const transition = useCallback((compute: (prev: RightPaneMode) => RightPaneMode) => {+ // Apply a transition, persisting only when the docked/closed distinction changes+ // (overlay is ephemeral, so it never writes the option).+ const apply = useCallback((action: RightPaneAction) => {setMode(prev => {- const next = compute(prev);- if ((prev === "docked") !== (next === "docked")) {- options.save("rightPaneVisible", (next === "docked").toString());+ const next = reduceRightPaneMode(prev, action);+ const persist = persistedRightPaneVisible(prev, next);+ if (persist !== null) {+ options.save("rightPaneVisible", persist.toString());}return next;});}, []);- const toggleOverlay = useCallback(() => transition(prev => prev === "closed" ? "overlay" : "closed"), [ transition ]);- const dock = useCallback(() => transition(() => "docked"), [ transition ]);- const close = useCallback(() => transition(() => "closed"), [ transition ]);+ const toggleOverlay = useCallback(() => apply("toggleOverlay"), [ apply ]);+ const dock = useCallback(() => apply("dock"), [ apply ]);+ const close = useCallback(() => apply("close"), [ apply ]);// Legacy entry points (tab-row toggle, empty-state button) open/close the *docked* pane.- useTriliumEvent("toggleRightPane", useCallback(() => {- transition(prev => prev === "closed" ? "docked" : "closed");- }, [ transition ]));+ useTriliumEvent("toggleRightPane", useCallback(() => apply("toggleDocked"), [ apply ]));useOverlayDismiss(mode === "overlay", close);@@ -203,6 +203,26 @@ function useSplit(visible: boolean) {}, [ visible ]);}+type RightPaneAction = "toggleOverlay" | "toggleDocked" | "dock" | "close";++/** The next mode for a given action (pure). `toggle*` open from closed and close otherwise. */+export function reduceRightPaneMode(prev: RightPaneMode, action: RightPaneAction): RightPaneMode {+ switch (action) {+ case "toggleOverlay": return prev === "closed" ? "overlay" : "closed";+ case "toggleDocked": return prev === "closed" ? "docked" : "closed";+ case "dock": return "docked";+ case "close": return "closed";+ }+}++/**+ * The value to write to the persisted `rightPaneVisible` option for a transition, or null when it+ * shouldn't change — only the docked/closed distinction is persisted; overlay is runtime-only.+ */+export function persistedRightPaneVisible(prev: RightPaneMode, next: RightPaneMode): boolean | null {+ return (prev === "docked") !== (next === "docked") ? next === "docked" : null;+}+// Clicks within these keep the overlay open: the pane, the toggle handle, and popups that sidebar// content renders into portals outside #right-pane (dropdowns, tooltips, modals). The backdrop is// intentionally NOT listed, so clicking the (covered) content area dismisses like other outside clicks.@@ -219,7 +239,7 @@ export function isWithinOverlay(target: EventTarget | null): boolean {* chrome the backdrop doesn't cover (tree, toolbar, tabs) are caught directly. Capture phase so a* child's `stopPropagation` can't keep a stale overlay open.*/-function useOverlayDismiss(active: boolean, onDismiss: () => void) {+export function useOverlayDismiss(active: boolean, onDismiss: () => void) {useEffect(() => {if (!active) return;const onPointerDown = (e: PointerEvent) => {