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: keep the widgets mounted between peeking sessions
Adorian Doranecf507d9c497
diff --git a/apps/client/src/widgets/react/peek_pane.spec.tsx b/apps/client/src/widgets/react/peek_pane.spec.tsxindex 582c5d547d..8ae64d92c3 100644--- a/apps/client/src/widgets/react/peek_pane.spec.tsx+++ b/apps/client/src/widgets/react/peek_pane.spec.tsx@@ -2,7 +2,7 @@ import { render } from "preact";import { act } from "preact/test-utils";import { afterEach, describe, expect, it, vi } from "vitest";-import { isWithinPeek, persistedPaneVisible, reducePaneMode, usePeekDismiss } from "./peek_pane";+import { isWithinPeek, PaneModeController, persistedPaneVisible, reducePaneMode, usePaneMode, usePeekDismiss } from "./peek_pane";describe("reducePaneMode", () => {it("opens as peek from closed and closes otherwise", () => {@@ -17,14 +17,60 @@ describe("reducePaneMode", () => {expect(reducePaneMode("docked", "toggleDocked")).toBe("closed");});- it("docks and closes unconditionally", () => {+ it("docks, and both close and dismiss reach closed, unconditionally", () => {for (const prev of ["closed", "peek", "docked"] as const) {expect(reducePaneMode(prev, "dock")).toBe("docked");expect(reducePaneMode(prev, "close")).toBe("closed");+ expect(reducePaneMode(prev, "dismiss")).toBe("closed");}});});+describe("usePaneMode", () => {+ afterEach(() => {+ document.body.innerHTML = "";+ });++ it("keeps content mounted across a soft dismiss, and unmounts on a hard close", () => {+ let controller: PaneModeController | undefined;+ const host = document.createElement("div");+ document.body.appendChild(host);+ function Harness() {+ controller = usePaneMode("rightPaneVisible");+ return null;+ }+ act(() => render(<Harness />, host));++ // Starts closed (the mocked option is unset) and unmounted.+ expect(controller?.mode).toBe("closed");+ expect(controller?.mounted).toBe(false);++ act(() => controller?.togglePeek());+ expect(controller?.mode).toBe("peek");+ expect(controller?.visible).toBe(true);+ expect(controller?.mounted).toBe(true);++ // Peek button while open (togglePeek) soft-dismisses: hidden but still mounted.+ act(() => controller?.togglePeek());+ expect(controller?.mode).toBe("closed");+ expect(controller?.visible).toBe(false);+ expect(controller?.mounted).toBe(true);++ // Re-peek reuses it; outside-press/Esc (dismiss) is also soft.+ act(() => controller?.togglePeek());+ act(() => controller?.dismiss());+ expect(controller?.visible).toBe(false);+ expect(controller?.mounted).toBe(true);++ // The × button (hard close) unmounts.+ act(() => controller?.close());+ expect(controller?.visible).toBe(false);+ expect(controller?.mounted).toBe(false);++ act(() => render(null, host));+ });+});+describe("persistedPaneVisible", () => {it("persists only when the docked/closed distinction changes", () => {// No docked-ness change -> no write (peek is ephemeral).diff --git a/apps/client/src/widgets/react/peek_pane.ts b/apps/client/src/widgets/react/peek_pane.tsindex 1b0dfe7faf..52f99f845e 100644--- a/apps/client/src/widgets/react/peek_pane.ts+++ b/apps/client/src/widgets/react/peek_pane.ts@@ -15,7 +15,10 @@ import options from "../../services/options";* reference implementation). Only the docked/closed distinction is persisted — peek is runtime-only.*/export type PaneMode = "closed" | "peek" | "docked";-export type PaneAction = "togglePeek" | "toggleDocked" | "dock" | "close";+// `togglePeek` and `dismiss` are *soft* closes (content stays mounted, hidden, so re-peeking preserves+// widget state); `close` (the × button) and the docked toggle are *hard* closes that unmount. The+// mode result is the same; the mount distinction lives in usePaneMode.+export type PaneAction = "togglePeek" | "toggleDocked" | "dock" | "close" | "dismiss";/** The next mode for a given action (pure). `toggle*` open from closed and close otherwise. */export function reducePaneMode(prev: PaneMode, action: PaneAction): PaneMode {@@ -24,6 +27,7 @@ export function reducePaneMode(prev: PaneMode, action: PaneAction): PaneMode {case "toggleDocked": return prev === "closed" ? "docked" : "closed";case "dock": return "docked";case "close": return "closed";+ case "dismiss": return "closed";}}@@ -37,23 +41,47 @@ export function persistedPaneVisible(prev: PaneMode, next: PaneMode): boolean |export interface PaneModeController {mode: PaneMode;+ /** Whether the pane is shown (peek or docked) — drives CSS show/hide. */visible: boolean;+ /** Whether the content should be in the DOM. Stays true across a soft `dismiss` so the content+ persists between consecutive peeks; goes false on a hard `close`. */+ mounted: boolean;togglePeek: () => void;toggleDocked: () => void;dock: () => void;close: () => void;+ dismiss: () => void;+}++interface PaneState {+ mode: PaneMode;+ mounted: boolean;}/**- * Owns a pane's mode and its persistence. `visibleOption` stores only the docked/closed distinction.- * The state updater stays pure (it can run more than once, e.g. in Strict Mode); persistence runs in- * an effect so it isn't duplicated by re-invoked updaters.+ * Owns a pane's mode, mount lifecycle and persistence. `visibleOption` stores only the docked/closed+ * distinction. The state updater stays pure (it can run more than once, e.g. in Strict Mode);+ * persistence runs in an effect so it isn't duplicated by re-invoked updaters.*/export function usePaneMode(visibleOption: OptionNames): PaneModeController {- const [ mode, setMode ] = useState<PaneMode>(() => options.is(visibleOption) ? "docked" : "closed");+ const [ { mode, mounted }, setState ] = useState<PaneState>(() => {+ const docked = options.is(visibleOption);+ return { mode: docked ? "docked" : "closed", mounted: docked };+ });const apply = useCallback((action: PaneAction) => {- setMode(prev => reducePaneMode(prev, action));+ setState(prev => {+ const mode = reducePaneMode(prev.mode, action);+ let mounted: boolean;+ if (mode !== "closed") {+ mounted = true; // shown → mounted+ } else if (action === "togglePeek" || action === "dismiss") {+ mounted = prev.mounted; // soft close: peek button / outside-press / Esc keep the content mounted (hidden)+ } else {+ mounted = false; // hard close: the × button (`close`) / docked toggle unmount the content+ }+ return { mode, mounted };+ });}, []);const prevModeRef = useRef<PaneMode>(mode);@@ -72,8 +100,9 @@ export function usePaneMode(visibleOption: OptionNames): PaneModeController {const toggleDocked = useCallback(() => apply("toggleDocked"), [ apply ]);const dock = useCallback(() => apply("dock"), [ apply ]);const close = useCallback(() => apply("close"), [ apply ]);+ const dismiss = useCallback(() => apply("dismiss"), [ apply ]);- return { mode, visible: mode !== "closed", togglePeek, toggleDocked, dock, close };+ return { mode, visible: mode !== "closed", mounted, togglePeek, toggleDocked, dock, close, dismiss };}// App-wide popup roots that render outside the pane (on document.body) but must NOT dismiss a peek:diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.css b/apps/client/src/widgets/sidebar/RightPanelContainer.cssindex 474414eec1..41d870fb08 100644--- a/apps/client/src/widgets/sidebar/RightPanelContainer.css+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.css@@ -29,6 +29,12 @@ body.experimental-feature-new-layout #right-pane-host {flex-direction: row;}+/* A soft-dismissed peek keeps its content mounted (so re-peeking is instant and stateful); this+ takes it out of layout/paint without unmounting it. Applied whenever the pane isn't shown. */+body.experimental-feature-new-layout #right-pane-host.hidden {+ display: none;+}+/* Docked: #right-pane fills the host (which Split sizes against #center-pane). */body.experimental-feature-new-layout #right-pane-host:not(.peek) > #right-pane {flex: 1 1 0;diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.tsxindex 56e46c24ec..fe91a37cc1 100644--- a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx@@ -2,6 +2,7 @@import "./RightPanelContainer.css";import Split from "@triliumnext/split.js";+import clsx from "clsx";import { VNode } from "preact";import { useEffect, useRef } from "preact/hooks";@@ -37,8 +38,8 @@ interface RightPanelWidgetDefinition {}export default function RightPanelContainer({ widgetsByParent }: { widgetsByParent: WidgetsByParent }) {- const { mode, visible, togglePeek, toggleDocked, dock, close } = usePaneMode("rightPaneVisible");- const items = useItems(visible, widgetsByParent);+ const { mode, visible, mounted, togglePeek, toggleDocked, dock, close, dismiss } = usePaneMode("rightPaneVisible");+ const items = useItems(mounted, widgetsByParent);useSplit(mode);// Legacy entry points (tab-row toggle, empty-state button) open/close the *docked* pane;@@ -46,7 +47,9 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPareuseTriliumEvent("toggleRightPane", toggleDocked);useTriliumEvent("peekRightPane", togglePeek);- usePeekDismiss(mode === "peek", close, {+ // Outside-press / Esc *soft*-dismisses the peek: it hides but stays mounted, so re-peeking is+ // instant and preserves widget state. The × button and the peek button hard-close (unmount).+ usePeekDismiss(mode === "peek", dismiss, {keepOpenSelector: "#right-pane, .right-pane-peek-button",focusSelector: ".right-pane-peek-button"});@@ -58,8 +61,9 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPare<RightPanePeekButton rightPaneVisible={visible} onToggle={togglePeek} />{/* Persistent host so #right-pane never reparents between modes (which would remount theright pane widgets). Docked: an in-flow flex child Split resizes against #center-pane.- Peek: an absolute layer over the content where Split resizes the spacer vs the pane. */}- <div id="right-pane-host" class={mode === "peek" ? "peek" : undefined}>+ Peek: an absolute layer over the content where Split resizes the spacer vs the pane.+ `hidden` (display:none) keeps soft-dismissed peek content mounted but out of layout. */}+ <div id="right-pane-host" class={clsx(mode === "peek" && "peek", !visible && "hidden")}>{/* The spacer is both the left Split target and the dismiss backdrop in peek mode:it covers the content (a click dismisses) and shields the PDF iframe so Split'sdrag tracking isn't interrupted. Always rendered + CSS-toggled to keep the host's@@ -74,7 +78,7 @@ export default function RightPanelContainer({ widgetsByParent }: { widgetsByPare<ActionButton icon="bx bx-x" text={t("right_pane.close")} onClick={close} /></div>)}- {visible && (+ {mounted && (items.length > 0 ? (items) : (