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(popup): keep popups clear of the screen's safe areas
Elian Dorand5a2d58aefbd
diff --git a/apps/client/src/widgets/react/Popup.css b/apps/client/src/widgets/react/Popup.cssindex 40772d6743..523b0b55ff 100644--- a/apps/client/src/widgets/react/Popup.css+++ b/apps/client/src/widgets/react/Popup.css@@ -39,3 +39,15 @@ body .tn-popup {border-radius: inherit;overflow-y: auto;}++/* Read by `viewportPadding()` in Popup.tsx, as script cannot read `env()`. */+.tn-safe-area-probe {+ position: fixed;+ visibility: hidden;+ pointer-events: none;+ padding:+ var(--safe-area-inset-top, env(safe-area-inset-top))+ var(--safe-area-inset-right, env(safe-area-inset-right))+ var(--safe-area-inset-bottom, env(safe-area-inset-bottom))+ var(--safe-area-inset-left, env(safe-area-inset-left));+}diff --git a/apps/client/src/widgets/react/Popup.spec.tsx b/apps/client/src/widgets/react/Popup.spec.tsxindex b3b6beceae..091e485742 100644--- a/apps/client/src/widgets/react/Popup.spec.tsx+++ b/apps/client/src/widgets/react/Popup.spec.tsx@@ -97,6 +97,28 @@ describe("Popup", () => {expect(tall.style.maxHeight).toBe(`${800 - 330 - 5}px`);});+ it("keeps clear of the screen's safe areas, such as a notch or a gesture bar", async () => {+ // The insets a device reports, as the probe reads them from the stylesheet.+ const measure = window.getComputedStyle.bind(window);+ vi.spyOn(window, "getComputedStyle").mockImplementation((element, pseudo) => {+ if (!element.classList.contains("tn-safe-area-probe")) return measure(element, pseudo);+ const insets = { paddingTop: "20px", paddingRight: "0px", paddingBottom: "30px", paddingLeft: "0px" };+ return insets as CSSStyleDeclaration;+ });+ vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(1000);++ // Below the anchor, short of the bottom inset as well as the gap from the edge.+ anchorAt(100, 300);+ const below = await open();+ expect(below.style.maxHeight).toBe(`${800 - 330 - 5 - 30}px`);+ render(null, host);++ // Above it, short of the top inset.+ anchorAt(100, 740);+ const above = await open();+ expect(above.style.maxHeight).toBe(`${740 - 5 - 20}px`);+ });+it("grows past the viewport without its height cap, stands in a wrapper it is given, and says when it is placed", async () => {vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(1000);anchorAt(100, 300);diff --git a/apps/client/src/widgets/react/Popup.tsx b/apps/client/src/widgets/react/Popup.tsxindex e874f4e4f0..b4ee09a1ee 100644--- a/apps/client/src/widgets/react/Popup.tsx+++ b/apps/client/src/widgets/react/Popup.tsx@@ -237,16 +237,17 @@ export interface FloatingPlacement {* Resolves to the placement Floating UI chose.*/export async function placeFloating(element: HTMLElement, anchor: ReferenceElement, { placement, offset: gap, shiftAcross, capHeight, hideWithAnchor, arrow: pointer }: FloatingPlacement) {+ const padding = viewportPadding();const { x, y, placement: placed, middlewareData } = await computePosition(anchor, element, {strategy: "fixed",placement,middleware: [gap !== undefined && offset(gap),- flip({ fallbackPlacements: mirroredPlacements(placement), padding: VIEWPORT_PADDING }),- shift({ crossAxis: !!shiftAcross, padding: VIEWPORT_PADDING }),+ flip({ fallbackPlacements: mirroredPlacements(placement), padding }),+ shift({ crossAxis: !!shiftAcross, padding }),pointer && arrow({ element: pointer.element, padding: pointer.padding }),capHeight && size({- padding: VIEWPORT_PADDING,+ padding,apply({ availableHeight }) {element.style.maxHeight = `${availableHeight}px`;}@@ -267,6 +268,28 @@ export async function placeFloating(element: HTMLElement, anchor: ReferenceElemereturn placed;}+/**+ * How far a popup keeps from each edge of the viewport: `VIEWPORT_PADDING` past the safe-area+ * inset there, such as a notch or a gesture bar.+ */+function viewportPadding() {+ // An element whose padding is the insets (see Popup.css), as script cannot read `env()`. It+ // is in the page only while it is measured.+ const probe = document.createElement("div");+ probe.className = "tn-safe-area-probe";+ document.body.append(probe);+ const style = getComputedStyle(probe);+ const past = (inset: string) => VIEWPORT_PADDING + (parseFloat(inset) || 0);+ const padding = {+ top: past(style.paddingTop),+ right: past(style.paddingRight),+ bottom: past(style.paddingBottom),+ left: past(style.paddingLeft)+ };+ probe.remove();+ return padding;+}+const OPPOSITE_SIDES = { top: "bottom", bottom: "top", left: "right", right: "left" } as const;/** The placements to fall back on from `placement`: the opposite side, then the other alignment on each. */