From ae523f78f5bf05dedbfd0df100f94992a805ee1f Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 12 Nov 2026 16:34:04 -0500 Subject: [PATCH] Show the server's Preview on a phone, and switch to it when a server starts On a phone the editor area shows one panel, and its Preview toggle only looked for file previews, so the default Preview, a browser panel waiting for a server, was unreachable. The toggle now picks the file preview for the file being edited, else a server's preview, else any file preview. A browser panel opening on or binding to a server shows in the editor area, as it appears beside the editor on a wide screen. --- packages/app/src/shell/dom/shell.ts | 39 ++++++++++++++++++++++++++--- packages/app/tests/compact.spec.ts | 24 +++++++++++++++++- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/packages/app/src/shell/dom/shell.ts b/packages/app/src/shell/dom/shell.ts index ffe7c92..f179920 100644 --- a/packages/app/src/shell/dom/shell.ts +++ b/packages/app/src/shell/dom/shell.ts @@ -199,6 +199,10 @@ export class ShellElement extends HTMLElement { #previewShown = false; #sheetOpen = false; #lastMain: PanelId | null = null; + /** The preview a server just opened or bound, which the toggle shows until another tab is picked. */ + #servedPreview: PanelId | null = null; + /** Each browser panel's address at the last render, to notice one opening on or binding to a server. */ + #browserUrls: Map | undefined; /** The panel whose body is in the drawer while it's open. */ #drawerPanel: PanelId | undefined; /** The editing view the key bar drives: the last one that had focus. */ @@ -303,6 +307,14 @@ export class ShellElement extends HTMLElement { #render(layout: Layout) { const workbench = this.#workbench!; const compact = this.#wantsCompact(); + // On a phone the editor area shows one panel, so a page a server just started shows there, as it + // would beside the editor on a wide screen. + const served = this.#newlyServed(layout); + if (compact && served) { + this.#servedPreview = served; + this.#previewShown = true; + this.#lastMain = mainStack(layout.root).active; + } if (compact !== this.#compact) { this.#compact = compact; setState(this.#internals, "compact", compact); @@ -489,7 +501,10 @@ export class ShellElement extends HTMLElement { const chrome = this.#chrome!; const main = mainStack(layout.root); // Picking another tab in the editor area means "show me that", so it leaves the preview view. - if (main.active !== this.#lastMain) this.#previewShown = false; + if (main.active !== this.#lastMain) { + this.#previewShown = false; + this.#servedPreview = null; + } this.#lastMain = main.active; const preview = this.#previewPanel(layout); if (!preview) this.#previewShown = false; @@ -511,13 +526,29 @@ export class ShellElement extends HTMLElement { return mainStack(layout.root).active; } - /** The preview the header toggles: the one for the active editor's file if there is one, else the first. */ + /** + * The preview the header toggles: the one a server just opened or bound, else the file preview for + * the active editor's file, else a server's preview (one bound to a server first), else the first + * file preview. + */ #previewPanel(layout: Layout): PanelId | undefined { - const previews = Object.values(layout.panels).filter((p) => p.kind === "preview"); + if (this.#servedPreview && layout.panels[this.#servedPreview]) return this.#servedPreview; + const panels = Object.values(layout.panels); + const files = panels.filter((p) => p.kind === "preview"); + const servers = panels.filter((p) => p.kind === "browser"); const active = mainStack(layout.root).active; const editing = active ? layout.panels[active] : undefined; const path = editing?.kind === "editor" ? editing.input.path : undefined; - return (previews.find((p) => p.input.path === path) ?? previews[0])?.id; + return (files.find((p) => p.input.path === path) ?? servers.find((p) => p.input.url !== null) ?? servers[0] ?? files[0])?.id; + } + + /** A browser panel that opened on, or was bound to, a server since the last render. */ + #newlyServed(layout: Layout): PanelId | undefined { + const urls = new Map(Object.values(layout.panels).flatMap((p) => (p.kind === "browser" ? [[p.id, p.input.url] as const] : []))); + const before = this.#browserUrls; + this.#browserUrls = urls; + if (!before) return undefined; + return [...urls].find(([id, url]) => url !== null && before.get(id) !== url)?.[0]; } #sheetStack(layout: Layout): Stack | undefined { diff --git a/packages/app/tests/compact.spec.ts b/packages/app/tests/compact.spec.ts index 687570d..c06f29a 100644 --- a/packages/app/tests/compact.spec.ts +++ b/packages/app/tests/compact.spec.ts @@ -18,7 +18,7 @@ test.beforeEach(async ({ page }) => { await page.evaluate(() => localStorage.clear()); }); -/** Writes an HTML page and opens its preview, since the Preview toggle hides itself without one. */ +/** Writes an HTML page and opens its file preview. */ async function openPreview(page: Page) { await page.waitForFunction(() => Boolean((globalThis as any).demo && (globalThis as any).workbench)); await page.evaluate(() => { @@ -103,6 +103,28 @@ test("Preview swaps the editor area to the preview without reloading its frame", await expect(page.getByRole("textbox", { name: "page.html" })).toBeVisible(); }); +test("Preview shows the server preview, waiting for a server, on a fresh project", async ({ page }) => { + await expect(button(page, "Preview")).toBeVisible(); + await expect(button(page, "Preview")).toHaveAttribute("aria-pressed", "false"); + await button(page, "Preview").tap(); + await expect(button(page, "Preview")).toHaveAttribute("aria-pressed", "true"); + await expect(page.getByRole("heading", { name: "No server is running" })).toBeVisible(); + await expect(page.locator("[part=title]")).toHaveText("Preview"); + await expect(page.getByRole("textbox", { name: "index.ts" })).toBeHidden(); + await button(page, "Preview").tap(); + await expect(page.getByRole("textbox", { name: "index.ts" })).toBeVisible(); +}); + +test("a server starting from the terminal switches the editor area to its preview", async ({ page }) => { + await button(page, "Terminal").tap(); + await expect(terminalLog(page)).toContainText("~ $", { timeout: 20_000 }); + await run(page, "npm run dev"); + await expect(page.frameLocator("nib-browser iframe").getByRole("heading", { level: 1 })).toHaveText("Hello from nibling", { timeout: 30_000 }); + await expect(button(page, "Preview")).toHaveAttribute("aria-pressed", "true"); + await expect(page.locator("[part=title]")).toHaveText("localhost:3000"); + await expect(page.getByRole("textbox", { name: "index.ts" })).toBeHidden(); +}); + test("the terminal is a bottom sheet that opens from its handle and closes with Escape", async ({ page }) => { const handle = button(page, "Terminal"); await expect(handle).toHaveAttribute("aria-expanded", "false"); -- knotmirror 0.0.1