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
fix(client): keep a popover open for the layers it raises over itself
Elian Doranc2e4cd091e9e
diff --git a/apps/client/src/widgets/collections/calendar/EventPopover.tsx b/apps/client/src/widgets/collections/calendar/EventPopover.tsxindex 5d8ce58ce8..63580205a4 100644--- a/apps/client/src/widgets/collections/calendar/EventPopover.tsx+++ b/apps/client/src/widgets/collections/calendar/EventPopover.tsx@@ -82,6 +82,10 @@ export default function EventPopover({ noteId, anchor, container, parentNote, isplacement={glob.isRtl ? "left-start" : "right-start"}getAnchorRect={() => eventAnchorRect(container, noteId, anchor)}updateKey={noteId}+ // A press on another chip is not a dismissal but a switch: the click behind it re-points+ // this popover at that event (see onEventClick), which would otherwise have to tear the+ // popover down on the press and build it again on the click.+ keepOpenSelector=".fc-event"onDismiss={close}><EmbeddedNoteScope component={component} noteContext={noteContext}>diff --git a/apps/client/src/widgets/react/Popover.tsx b/apps/client/src/widgets/react/Popover.tsxindex 1dc9ab3893..101ede5c0e 100644--- a/apps/client/src/widgets/react/Popover.tsx+++ b/apps/client/src/widgets/react/Popover.tsx@@ -6,6 +6,8 @@ import { ComponentChildren } from "preact";import { createPortal } from "preact/compat";import { useEffect, useRef } from "preact/hooks";+import { FLOATING_LAYER_SELECTOR, isWithinFloatingLayer } from "./floating_layers";+export interface PopoverProps {/*** Where the popover points: a rectangle in viewport coordinates, asked for afresh on every@@ -23,6 +25,13 @@ export interface PopoverProps {*/updateKey?: unknown;className?: string;+ /**+ * Presses matching this keep the popover open, over and above the app's own floating layers+ * (see {@link FLOATING_LAYER_SELECTOR}) — for what the popover stands beside and answers to+ * rather than closes for: the calendar's chips, each of which re-points the standing popover+ * at its own event.+ */+ keepOpenSelector?: string;/** Called on a press outside the popover; the owner decides what being dismissed means. The* press itself still lands where it fell — dismissal must not swallow it, or pressing the* thing the popover stands beside would take two tries. */@@ -36,7 +45,7 @@ export interface PopoverProps {* container clips it and no containment root flattens its frosting (see the Dropdown notes in* CLAUDE.md); positioned by Popper, which also keeps it in place while ancestors scroll.*/-export default function Popover({ getAnchorRect, placement, updateKey, className, onDismiss, children }: PopoverProps) {+export default function Popover({ getAnchorRect, placement, updateKey, className, keepOpenSelector, onDismiss, children }: PopoverProps) {const elRef = useRef<HTMLDivElement>(null);const popperRef = useRef<Instance>();@@ -76,15 +85,22 @@ export default function Popover({ getAnchorRect, placement, updateKey, classNameconst onPointerDown = (e: PointerEvent) => {const el = elRef.current;- if (el && e.target instanceof Node && !el.contains(e.target)) {- onDismiss();- }+ if (!el || !(e.target instanceof Node) || el.contains(e.target)) return;++ // A layer standing over the popover is not a place away from it: a dropdown the+ // popover opened lives in the body rather than within it, and dismissing on the press+ // would take the menu down before the click that chose anything could arrive (see+ // floating_layers.ts). Nor is what the popover answers to (see keepOpenSelector).+ if (isWithinFloatingLayer(e.target)) return;+ if (keepOpenSelector && e.target instanceof Element && e.target.closest(keepOpenSelector)) return;++ onDismiss();};// Captured, so the popover hears of the press wherever it lands — including places that// stop propagation for reasons of their own.document.addEventListener("pointerdown", onPointerDown, true);return () => document.removeEventListener("pointerdown", onPointerDown, true);- }, [ onDismiss ]);+ }, [ onDismiss, keepOpenSelector ]);return createPortal(<div ref={elRef} className={clsx("tn-popover", className)}>diff --git a/apps/client/src/widgets/react/floating_layers.ts b/apps/client/src/widgets/react/floating_layers.tsnew file mode 100644index 0000000000..ae64a44cd1--- /dev/null+++ b/apps/client/src/widgets/react/floating_layers.ts@@ -0,0 +1,24 @@+/**+ * The app's floating layers: popup roots that render into `document.body` rather than inside+ * whatever raised them — resize gutters, Bootstrap dropdowns, tooltips, modals and their backdrop,+ * popovers, CKEditor balloons, Flatpickr calendars, the attribute detail popup, the context menu,+ * and both autocomplete dropdowns (Preact `FormAutocomplete` and the jQuery note autocomplete).+ *+ * A surface that closes on an outside press — the peeked right pane, an anchored popover — cannot+ * tell "outside" by DOM containment alone. A dropdown opened from within it lives in the body, so+ * the press that works its controls reads as a press somewhere else entirely. The cost is worse+ * than a stray dismissal: the press is heard on `pointerdown`, which tears the surface down — and+ * with it the portaled menu — before the `click` that would have chosen anything ever arrives, so+ * the choice is silently lost rather than merely interrupted.+ *+ * Matching this means "a layer standing over the surface", which is never a reason to dismiss it:+ * such a layer sees to its own dismissal, and what raised it stays behind it.+ */+export const FLOATING_LAYER_SELECTOR = ".gutter, .dropdown-menu, .tooltip, .modal, .modal-backdrop, .popover, "+ + ".ck-balloon-panel, .ck-body, .flatpickr-calendar, .attr-detail, #context-menu-container, "+ + ".form-autocomplete-dropdown, .aa-dropdown-menu";++/** Whether a press landed in one of the app's floating layers. See {@link FLOATING_LAYER_SELECTOR}. */+export function isWithinFloatingLayer(target: EventTarget | null): boolean {+ return target instanceof Element && target.closest(FLOATING_LAYER_SELECTOR) !== null;+}diff --git a/apps/client/src/widgets/react/peek_pane.ts b/apps/client/src/widgets/react/peek_pane.tsindex 7efdadd6c3..6fa9f62f9a 100644--- a/apps/client/src/widgets/react/peek_pane.ts+++ b/apps/client/src/widgets/react/peek_pane.ts@@ -2,6 +2,7 @@ import { OptionNames } from "@triliumnext/commons";import { useCallback, useEffect, useRef, useState } from "preact/hooks";import options from "../../services/options";+import { FLOATING_LAYER_SELECTOR } from "./floating_layers";/*** A reusable side pane that can be in one of three modes:@@ -105,12 +106,9 @@ export function usePaneMode(visibleOption: OptionNames): PaneModeController {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:-// resize gutters, Bootstrap dropdowns/tooltips/modals (and their backdrop)/popovers, CKEditor balloons,-// Flatpickr calendars, the attribute detail popup, the context menu, and both autocomplete dropdowns-// (Preact `FormAutocomplete` and the jQuery note autocomplete).-const DEFAULT_KEEP_OPEN_SELECTOR = ".gutter, .dropdown-menu, .tooltip, .modal, .modal-backdrop, .popover, .ck-balloon-panel, .ck-body, .flatpickr-calendar, "- + ".attr-detail, #context-menu-container, .form-autocomplete-dropdown, .aa-dropdown-menu";+// The app-wide popup roots that render outside the pane (on document.body) but must NOT dismiss a+// peek — shared with every other surface that closes on an outside press (see floating_layers.ts).+const DEFAULT_KEEP_OPEN_SELECTOR = FLOATING_LAYER_SELECTOR;/** Whether an event target lies within the peek pane or an allowlisted popup (i.e. should keep it open). */export function isWithinPeek(target: EventTarget | null, keepOpenSelector: string): boolean {