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: implement an overlay mode
Adorian Doran5e4a5be8dbb3
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex fdc12282bb..c87f47f9f6 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2709,6 +2709,8 @@"empty_message": "Nothing to show for this note","empty_button": "Hide the panel","toggle": "Toggle right panel",+ "dock": "Dock panel",+ "close": "Close panel","custom_widget_go_to_source": "Go to source code"},"print_preview": {diff --git a/apps/client/src/widgets/sidebar/RightPaneToggleHandle.tsx b/apps/client/src/widgets/sidebar/RightPaneToggleHandle.tsxindex 427d9dcc22..22db778654 100644--- a/apps/client/src/widgets/sidebar/RightPaneToggleHandle.tsx+++ b/apps/client/src/widgets/sidebar/RightPaneToggleHandle.tsx@@ -4,7 +4,6 @@ import { Tooltip } from "bootstrap";import clsx from "clsx";import { useCallback, useMemo, useRef } from "preact/hooks";-import appContext from "../../components/app_context";import { t } from "../../services/i18n";import { useStaticTooltip } from "../react/hooks";@@ -14,16 +13,18 @@ const TOOLTIP_MARGIN_PX = 8;interface RightPaneToggleHandleProps {/** Owned and persisted by RightPanelContainer; this component only reflects it. */rightPaneVisible: boolean;+ /** Toggles the pane (opens it as an overlay, or closes it); owned by RightPanelContainer. */+ onToggle: () => void;}-export default function RightPaneToggleHandle({ rightPaneVisible }: RightPaneToggleHandleProps) {+export default function RightPaneToggleHandle({ rightPaneVisible, onToggle }: RightPaneToggleHandleProps) {const buttonRef = useRef<HTMLButtonElement>(null);// Pointer's vertical position captured once on hover; null until the mouse enters// (e.g. on keyboard focus) so the tooltip falls back to the handle's centre.const pointerY = useRef<number | null>(null);const handleClick = useCallback(() => {- appContext.triggerCommand("toggleRightPane");+ onToggle();// Reset Bootstrap's hover/focus trigger state. A click otherwise leaves the button// focused, which keeps the tooltip "stuck" and prevents it re-triggering on next hover.const button = buttonRef.current;@@ -31,7 +32,7 @@ export default function RightPaneToggleHandle({ rightPaneVisible }: RightPaneTogTooltip.getInstance(button)?.hide();button.blur();}- }, []);+ }, [ onToggle ]);// Anchor the tooltip to the pointer's entry height rather than the centre of the// full-height handle, by shifting it along the cross axis. Not tracked: `pointerY`diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.css b/apps/client/src/widgets/sidebar/RightPanelContainer.cssindex 8c7ad008fc..5fc3be3bc7 100644--- a/apps/client/src/widgets/sidebar/RightPanelContainer.css+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.css@@ -4,9 +4,39 @@regardless of whether the panel is open. */body.experimental-feature-new-layout #vertical-main-container {--right-pane-handle-width: 12px;- position: relative;/* +4px leaves a small gap between the panel (and #center-pane) and the handle. */- padding-inline-end: calc(var(--right-pane-handle-width) + 4px);+ --right-pane-handle-gutter: calc(var(--right-pane-handle-width) + 4px);+ position: relative;+ padding-inline-end: var(--right-pane-handle-gutter);+}++/* Overlay mode: float the pane above the content (no Split, so the content never reflows).+ Width is applied inline from the saved rightPaneWidth (see RightPanelContainer.tsx). */+body.experimental-feature-new-layout #right-pane.overlay {+ position: absolute;+ inset-block: 0;+ inset-inline-end: var(--right-pane-handle-gutter);+ /* Floor matching the docked Split minSize, so a small saved width can't make the overlay too thin. */+ min-width: 180px;+ z-index: 15;+ border-inline-start: 1px solid var(--main-border-color);+ box-shadow: -4px 0 12px rgba(0, 0, 0, 0.18);+ animation: right-pane-overlay-in 150ms ease;+}++@keyframes right-pane-overlay-in {+ from { transform: translateX(8px); opacity: 0; }+ to { transform: translateX(0); opacity: 1; }+}++/* Pin/close controls, shown only in overlay mode. */+body.experimental-feature-new-layout #right-pane .right-pane-overlay-actions {+ position: absolute;+ top: 4px;+ inset-inline-end: 4px;+ z-index: 1;+ display: flex;+ gap: 2px;}body.experimental-feature-new-layout #right-pane {diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.spec.ts b/apps/client/src/widgets/sidebar/RightPanelContainer.spec.tsnew file mode 100644index 0000000000..59c0995db3--- /dev/null+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.spec.ts@@ -0,0 +1,48 @@+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("treats the pane, the handle, and allowlisted popups as inside", () => {+ 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>+ <div id="center-pane"><p class="editor">z</p></div>+ `);++ const inside = [".toc-item", ".right-pane-toggle-handle", ".dropdown-item", ".tooltip"];+ for (const selector of inside) {+ expect(isWithinOverlay(document.querySelector(selector)), selector).toBe(true);+ }+ });++ it("treats content outside the pane/popups as outside, and ignores non-elements", () => {+ mount(`<div id="center-pane"><p class="editor">z</p></div>`);++ expect(isWithinOverlay(document.querySelector(".editor"))).toBe(false);+ expect(isWithinOverlay(document.body)).toBe(false);+ expect(isWithinOverlay(null)).toBe(false);+ expect(isWithinOverlay(document)).toBe(false);+ });++ it("classifies a focused iframe by its location (the PDF-viewer dismiss path)", () => {+ mount(`+ <div id="center-pane"><iframe class="pdf-viewer"></iframe></div>+ <div id="right-pane"><iframe class="sidebar-frame"></iframe></div>+ `);++ // Clicking the PDF viewer (center-pane iframe) should dismiss; a sidebar iframe should not.+ expect(isWithinOverlay(document.querySelector(".pdf-viewer"))).toBe(false);+ expect(isWithinOverlay(document.querySelector(".sidebar-frame"))).toBe(true);+ });+});diff --git a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx b/apps/client/src/widgets/sidebar/RightPanelContainer.tsxindex 84d10c2470..d16f76281d 100644--- a/apps/client/src/widgets/sidebar/RightPanelContainer.tsx+++ b/apps/client/src/widgets/sidebar/RightPanelContainer.tsx@@ -11,6 +11,7 @@ import { t } from "../../services/i18n";import options from "../../services/options";import { DEFAULT_GUTTER_SIZE } from "../../services/resizer";import { isStandalone } from "../../services/utils";+import ActionButton from "../react/ActionButton";import Button from "../react/Button";import { useActiveNoteContext, useLegacyWidget, useNoteProperty, useTriliumEvent, useTriliumOptionBool, useTriliumOptionJson } from "../react/hooks";import LazyComponent from "../react/LazyComponent";@@ -27,6 +28,13 @@ import TableOfContents from "./TableOfContents";const MIN_WIDTH_PERCENT = 5;+/**+ * - `closed`: hidden. - `docked`: a flex pane that reflows the content (resizable via Split).+ * - `overlay`: floats above the content without reflowing it; transient (closed on focus loss).+ * Only the docked/closed distinction is persisted (`rightPaneVisible`); overlay is runtime-only.+ */+type RightPaneMode = "closed" | "overlay" | "docked";+interface RightPanelWidgetDefinition {el: VNode;enabled: boolean;@@ -34,24 +42,52 @@ interface RightPanelWidgetDefinition {}export default function RightPanelContainer({ widgetsByParent }: { widgetsByParent: WidgetsByParent }) {- const [ rightPaneVisible, setRightPaneVisible ] = useState(options.is("rightPaneVisible"));- const items = useItems(rightPaneVisible, widgetsByParent);- useSplit(rightPaneVisible);- useTriliumEvent("toggleRightPane", useCallback(() => {- setRightPaneVisible(current => {- const newValue = !current;- options.save("rightPaneVisible", newValue.toString());- return newValue;+ const [ mode, setMode ] = useState<RightPaneMode>(() => options.is("rightPaneVisible") ? "docked" : "closed");+ const visible = mode !== "closed";+ const 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) => {+ setMode(prev => {+ const next = compute(prev);+ if ((prev === "docked") !== (next === "docked")) {+ options.save("rightPaneVisible", (next === "docked").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 ]);++ // Legacy entry points (tab-row toggle, empty-state button) open/close the *docked* pane.+ useTriliumEvent("toggleRightPane", useCallback(() => {+ transition(prev => prev === "closed" ? "docked" : "closed");+ }, [ transition ]));++ useOverlayDismiss(mode === "overlay", close);++ // The overlay reuses the docked width but can't be sized by Split (the content pane isn't flexed),+ // so it's applied as an inline width here — a genuinely computed value that can't live in CSS.+ const overlayStyle = mode === "overlay"+ ? { width: `${Math.max(MIN_WIDTH_PERCENT, options.getInt("rightPaneWidth") ?? MIN_WIDTH_PERCENT)}%` }+ : undefined;return (<>{/* Absolutely positioned in a reserved gutter on the viewport's right edge, so itstays put regardless of the panel's open/collapsed state (see RightPanelContainer.css). */}- <RightPaneToggleHandle rightPaneVisible={rightPaneVisible} />- <div id="right-pane">- {rightPaneVisible && (+ <RightPaneToggleHandle rightPaneVisible={visible} onToggle={toggleOverlay} />+ <div id="right-pane" class={mode === "overlay" ? "overlay" : undefined} style={overlayStyle}>+ {mode === "overlay" && (+ <div class="right-pane-overlay-actions">+ <ActionButton icon="bx bx-pin" text={t("right_pane.dock")} onClick={dock} />+ <ActionButton icon="bx bx-x" text={t("right_pane.close")} onClick={close} />+ </div>+ )}+ {visible && (items.length > 0 ? (items) : (@@ -161,6 +197,61 @@ function useSplit(visible: boolean) {}, [ visible ]);}+// Elements that should NOT dismiss the overlay when clicked/focused: the pane itself, the toggle+// handle, and popups that sidebar content renders into portals outside #right-pane (dropdowns,+// context menus, tooltips, modals). Tune this list as sidebar widgets add new popup roots.+const OVERLAY_KEEP_OPEN_SELECTOR = "#right-pane, .right-pane-toggle-handle, .dropdown-menu, .tooltip, .modal, .popover, .context-menu";++/** Whether an event target lies within the overlay or an allowlisted popup (i.e. should keep it open). */+export function isWithinOverlay(target: EventTarget | null): boolean {+ return target instanceof Element && target.closest(OVERLAY_KEEP_OPEN_SELECTOR) !== null;+}++/** While `active`, closes the overlay on an outside pointer press, focus move, or Escape. */+function useOverlayDismiss(active: boolean, onDismiss: () => void) {+ useEffect(() => {+ if (!active) return;++ let blurTimer: ReturnType<typeof setTimeout> | undefined;++ const onPointerDown = (e: PointerEvent) => {+ if (!isWithinOverlay(e.target)) onDismiss();+ };+ const onFocusIn = (e: FocusEvent) => {+ // Focus parked on <body> (e.g. after the handle blurs itself on open) isn't a real move-away.+ if (e.target === document.body || isWithinOverlay(e.target)) return;+ onDismiss();+ };+ const onWindowBlur = () => {+ // Clicks inside an iframe (e.g. the PDF viewer) don't reach the parent's pointerdown,+ // but they blur the window and move focus onto the <iframe> element. Read activeElement+ // on the next tick (some browsers update it after the blur fires).+ blurTimer = setTimeout(() => {+ const active = document.activeElement;+ if (active instanceof HTMLIFrameElement && !isWithinOverlay(active)) onDismiss();+ });+ };+ const onKeyDown = (e: KeyboardEvent) => {+ if (e.key === "Escape") {+ onDismiss();+ document.querySelector<HTMLElement>(".right-pane-toggle-handle")?.focus();+ }+ };++ document.addEventListener("pointerdown", onPointerDown, true);+ document.addEventListener("focusin", onFocusIn);+ document.addEventListener("keydown", onKeyDown);+ window.addEventListener("blur", onWindowBlur);+ return () => {+ document.removeEventListener("pointerdown", onPointerDown, true);+ document.removeEventListener("focusin", onFocusIn);+ document.removeEventListener("keydown", onKeyDown);+ window.removeEventListener("blur", onWindowBlur);+ if (blurTimer) clearTimeout(blurTimer);+ };+ }, [ active, onDismiss ]);+}+function CustomLegacyWidget({ originalWidget }: { originalWidget: LegacyRightPanelWidget }) {const containerRef = useRef<HTMLDivElement>(null);