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(mobile): keep the title row empty while an iPhone note slides in
Elian Doran771912335b6c
diff --git a/apps/client/src/widgets/layout/InlineTitle.spec.ts b/apps/client/src/widgets/layout/InlineTitle.spec.tsindex 44caf7007c..78ad7207e2 100644--- a/apps/client/src/widgets/layout/InlineTitle.spec.ts+++ b/apps/client/src/widgets/layout/InlineTitle.spec.ts@@ -1,7 +1,7 @@-import { describe, expect, it } from "vitest";+import { afterEach, describe, expect, it, vi } from "vitest";import { buildNote } from "../../test/easy-froca";-import { shouldShowInlineTitle } from "./InlineTitle";+import { observeTitleInView, shouldShowInlineTitle } from "./InlineTitle";describe("shouldShowInlineTitle", () => {const viewScope = { viewMode: "default" } as const;@@ -14,3 +14,35 @@ describe("shouldShowInlineTitle", () => {expect(shouldShowInlineTitle(codeNote, "code", viewScope)).toBe(false);});});++describe("observeTitleInView", () => {+ afterEach(() => {+ vi.unstubAllGlobals();+ document.body.innerHTML = "";+ });++ it("measures the title against its scrolling container, which a sliding note carries along", () => {+ const observers: { options?: IntersectionObserverInit; callback: IntersectionObserverCallback; disconnect: ReturnType<typeof vi.fn> }[] = [];+ vi.stubGlobal("IntersectionObserver", class {+ disconnect = vi.fn();+ observe = vi.fn();+ constructor(callback: IntersectionObserverCallback, options?: IntersectionObserverInit) {+ observers.push({ callback, options, disconnect: this.disconnect });+ }+ });+ const container = document.body.appendChild(document.createElement("div"));+ container.className = "scrolling-container";+ const title = container.appendChild(document.createElement("div"));+ const onChange = vi.fn();++ const stop = observeTitleInView(title, onChange);+ const [ observer ] = observers;+ expect(observer.options?.root).toBe(container);++ observer.callback([ { isIntersecting: false } as IntersectionObserverEntry ], {} as IntersectionObserver);+ expect(onChange).toHaveBeenLastCalledWith(false);++ stop();+ expect(observer.disconnect).toHaveBeenCalled();+ });+});diff --git a/apps/client/src/widgets/layout/InlineTitle.tsx b/apps/client/src/widgets/layout/InlineTitle.tsxindex b8324aa0f5..5c74a202c2 100644--- a/apps/client/src/widgets/layout/InlineTitle.tsx+++ b/apps/client/src/widgets/layout/InlineTitle.tsx@@ -49,19 +49,14 @@ export default function InlineTitle() {if (!titleRow) return;titleRow.classList.toggle("hide-title", true);- const observer = new IntersectionObserver((entries) => {- titleRow.classList.toggle("hide-title", entries[0].isIntersecting);- setTitleHidden(!entries[0].isIntersecting);- }, {- threshold: 0.85+ const stopObserving = containerRef.current && observeTitleInView(containerRef.current, (isInView) => {+ titleRow.classList.toggle("hide-title", isInView);+ setTitleHidden(!isInView);});- if (containerRef.current) {- observer.observe(containerRef.current);- }return () => {titleRow.classList.remove("hide-title");- observer.disconnect();+ stopObserving?.();};}, [ shown, parentComponent ]);@@ -81,6 +76,20 @@ export default function InlineTitle() {);}+/**+ * Calls `onChange` as `title` comes into view in its scrolling container or leaves it. Measured+ * against the container rather than the viewport, so a note sliding in from off-screen (the+ * navigation stack on iPhones) keeps its title in view while it moves.+ */+export function observeTitleInView(title: HTMLElement, onChange: (isInView: boolean) => void) {+ const observer = new IntersectionObserver((entries) => onChange(entries[0].isIntersecting), {+ root: title.closest(".scrolling-container"),+ threshold: 0.85+ });+ observer.observe(title);+ return () => observer.disconnect();+}+export function shouldShowInlineTitle(note: FNote | null | undefined, type: NoteType | undefined, viewScope: ViewScope | undefined) {if (viewScope?.viewMode !== "default") return false;// Options pages provide their own title via OptionsPageHeader, so this inline title stays hidden