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
feat(client): point a popover at what it was opened from
Elian Doran5a561b5880cd
diff --git a/apps/client/src/widgets/react/Popover.css b/apps/client/src/widgets/react/Popover.cssindex 5a4586c574..342998a90e 100644--- a/apps/client/src/widgets/react/Popover.css+++ b/apps/client/src/widgets/react/Popover.css@@ -4,6 +4,12 @@* it is are the caller's.*/.tn-popover {+ /* How far the arrow reaches out of the panel, and how wide it is where it meets it. The square+ is what makes it: stood on its corner, its diagonal is the base — 8.5 ≈ 12 / √2. */+ --tn-popover-arrow-depth: 6px;+ --tn-popover-arrow-base: 12px;+ --tn-popover-arrow-square: 8.5px;+/* Above the note split's own layers; menus and dialogs stand higher still. */z-index: 998;display: flex;@@ -16,3 +22,103 @@box-shadow: 0 2px 8px #0003;backdrop-filter: var(--dropdown-backdrop-filter, blur(10px));}++/*+ * The arrow: which side the popover is on says where it stands, and Popper slides it along that+ * side to sit over the anchor (see the arrow modifier in Popover.tsx).+ *+ * A window onto the square rather than the square itself. The panel's own background is+ * translucent (see `--menu-background-color`), so the half of the diamond that would lie inside the+ * panel would lay that translucency over itself and read as a lozenge floating within the panel.+ * Only what stands outside is drawn — plus a single pixel, which is what covers the panel's border+ * where the arrow meets it, so the arrow reads as an opening rather than a shape stuck on.+ */+.tn-popover-arrow {+ position: absolute;+ overflow: hidden;+}++.tn-popover-arrow::before {+ content: "";+ position: absolute;+ /* Said rather than inherited: the square's size is what the depth and the base are reckoned+ from, and borders counted on top of it would push the tip past the window that clips it. */+ box-sizing: border-box;+ width: var(--tn-popover-arrow-square);+ height: var(--tn-popover-arrow-square);+ background: var(--menu-background-color);+ /* On all four sides so the square keeps its size and stands square; only the two facing out of+ the panel are given a colour, the other two being the mouth. */+ border: 1px solid transparent;+ transform: rotate(45deg);+}++/*+ * Each side in turn. The window is the arrow's depth plus that one pixel, and the square is centred+ * on the panel's edge — which is the window's inner edge, hence the same arithmetic from whichever+ * outward side it is measured.+ */++/* stylelint-disable property-layout-mappings -- The sides here are physical because Popper's+ placements are: `data-popper-placement="right"` says the panel stands to the right of its anchor+ whatever the text direction, so its arrow belongs on the panel's physical left edge. A+ right-to-left app asks Popper for the other placement (see the callers) rather than having these+ flipped underneath it, and `inset-inline-start` would do exactly that — putting the arrow on the+ side the panel is anchored against. */+.tn-popover[data-popper-placement^="right"] > .tn-popover-arrow {+ top: 0;+ left: calc(-1 * var(--tn-popover-arrow-depth));+ width: calc(var(--tn-popover-arrow-depth) + 1px);+ height: var(--tn-popover-arrow-base);+}++.tn-popover[data-popper-placement^="right"] > .tn-popover-arrow::before {+ left: calc(var(--tn-popover-arrow-depth) - var(--tn-popover-arrow-square) / 2);+ top: calc((var(--tn-popover-arrow-base) - var(--tn-popover-arrow-square)) / 2);+ border-left-color: var(--dropdown-border-color);+ border-bottom-color: var(--dropdown-border-color);+}++.tn-popover[data-popper-placement^="left"] > .tn-popover-arrow {+ top: 0;+ right: calc(-1 * var(--tn-popover-arrow-depth));+ width: calc(var(--tn-popover-arrow-depth) + 1px);+ height: var(--tn-popover-arrow-base);+}++.tn-popover[data-popper-placement^="left"] > .tn-popover-arrow::before {+ right: calc(var(--tn-popover-arrow-depth) - var(--tn-popover-arrow-square) / 2);+ top: calc((var(--tn-popover-arrow-base) - var(--tn-popover-arrow-square)) / 2);+ border-top-color: var(--dropdown-border-color);+ border-right-color: var(--dropdown-border-color);+}++.tn-popover[data-popper-placement^="bottom"] > .tn-popover-arrow {+ left: 0;+ top: calc(-1 * var(--tn-popover-arrow-depth));+ height: calc(var(--tn-popover-arrow-depth) + 1px);+ width: var(--tn-popover-arrow-base);+}++.tn-popover[data-popper-placement^="bottom"] > .tn-popover-arrow::before {+ top: calc(var(--tn-popover-arrow-depth) - var(--tn-popover-arrow-square) / 2);+ left: calc((var(--tn-popover-arrow-base) - var(--tn-popover-arrow-square)) / 2);+ border-top-color: var(--dropdown-border-color);+ border-left-color: var(--dropdown-border-color);+}++.tn-popover[data-popper-placement^="top"] > .tn-popover-arrow {+ left: 0;+ bottom: calc(-1 * var(--tn-popover-arrow-depth));+ height: calc(var(--tn-popover-arrow-depth) + 1px);+ width: var(--tn-popover-arrow-base);+}++.tn-popover[data-popper-placement^="top"] > .tn-popover-arrow::before {+ bottom: calc(var(--tn-popover-arrow-depth) - var(--tn-popover-arrow-square) / 2);+ left: calc((var(--tn-popover-arrow-base) - var(--tn-popover-arrow-square)) / 2);+ border-bottom-color: var(--dropdown-border-color);+ border-right-color: var(--dropdown-border-color);+}++/* stylelint-enable property-layout-mappings */diff --git a/apps/client/src/widgets/react/Popover.tsx b/apps/client/src/widgets/react/Popover.tsxindex 101ede5c0e..2d1eb030ac 100644--- a/apps/client/src/widgets/react/Popover.tsx+++ b/apps/client/src/widgets/react/Popover.tsx@@ -47,6 +47,7 @@ export interface PopoverProps {*/export default function Popover({ getAnchorRect, placement, updateKey, className, keepOpenSelector, onDismiss, children }: PopoverProps) {const elRef = useRef<HTMLDivElement>(null);+ const arrowRef = useRef<HTMLDivElement>(null);const popperRef = useRef<Instance>();// Held in a ref so the popper built once keeps asking the newest question — rebuilding it on@@ -64,8 +65,13 @@ export default function Popover({ getAnchorRect, placement, updateKey, className// Fixed, as the element stands in the body rather than beside its anchor.strategy: "fixed",modifiers: [- { name: "offset", options: { offset: [ 0, 12 ] } },- { name: "preventOverflow", options: { padding: 8 } }+ // Room for the arrow to stand in, and a little air past its tip.+ { name: "offset", options: { offset: [ 0, 10 ] } },+ { name: "preventOverflow", options: { padding: 8 } },+ // Kept clear of the panel's rounded corners, where an arrow would grow out of thin+ // air; an anchor so near a corner that it cannot be pointed at squarely gets the+ // arrow as close as the padding allows.+ { name: "arrow", options: { element: arrowRef.current, padding: 10 } }]});popperRef.current = popper;@@ -104,6 +110,10 @@ export default function Popover({ getAnchorRect, placement, updateKey, classNamereturn createPortal(<div ref={elRef} className={clsx("tn-popover", className)}>+ {/* What the popover is pointing at, drawn by Popper against whichever side it ended up+ on (see the placements in Popover.css). First in the panel so its static position is+ the panel's own corner, which is what Popper's offset is reckoned from. */}+ <div ref={arrowRef} className="tn-popover-arrow" />{children}</div>,document.body