web: keep mobile composition stable across keyboard and Back navigation
This commit is contained in:
+17
-6
@@ -67,6 +67,7 @@ import {
|
||||
readEphemeralChatIds,
|
||||
} from "./views/chat.js";
|
||||
import { createSearchView } from "./views/search.js";
|
||||
import { installMobileViewport } from "./mobileViewport.js";
|
||||
|
||||
function defaultServerUrl() {
|
||||
if (typeof location === "undefined" || !location.host) return "";
|
||||
@@ -76,6 +77,7 @@ function defaultServerUrl() {
|
||||
|
||||
const client = new PipelinesClient();
|
||||
const repo = new PipelinesRepository(client);
|
||||
installMobileViewport();
|
||||
|
||||
// Register the app's shell service worker where the browser allows it (a secure
|
||||
// context: https, localhost, or the desktop app). On a plain-LAN http origin the
|
||||
@@ -1035,7 +1037,7 @@ function restoreListFocus(restore) {
|
||||
// first focusable descendant rather than a non-focusable div.
|
||||
const target =
|
||||
row.matches("button, a, [tabindex]") ? row : row.querySelector("button, a, [tabindex]") ?? row;
|
||||
target.focus();
|
||||
target.focus({ preventScroll: true });
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1107,7 +1109,14 @@ function render() {
|
||||
renderAttentionSheet();
|
||||
}
|
||||
|
||||
const listScrollPositions = new Map();
|
||||
|
||||
function renderContent() {
|
||||
const previousList = content.querySelector(".list-pane");
|
||||
const previousScroll = previousList?.querySelector(".list-scroll");
|
||||
if (previousScroll && previousList.getClientRects().length > 0) {
|
||||
listScrollPositions.set(previousList.dataset.listView, previousScroll.scrollTop);
|
||||
}
|
||||
// The graph owns wheel/pointer/resize listeners; drop the previous mount
|
||||
// before rebuilding the DOM so a re-render, tab switch or view switch never
|
||||
// leaves an observer or listener behind.
|
||||
@@ -1159,7 +1168,7 @@ function renderContent() {
|
||||
layout.append(
|
||||
h(
|
||||
"section",
|
||||
{ class: "list-pane", "aria-label": "Pipelines list" },
|
||||
{ class: "list-pane", "data-list-view": view, "aria-label": "Pipelines list" },
|
||||
paneHeading("Pipelines", { detailOpen }),
|
||||
renderPipelineList(),
|
||||
),
|
||||
@@ -1169,7 +1178,7 @@ function renderContent() {
|
||||
layout.append(
|
||||
h(
|
||||
"section",
|
||||
{ class: "list-pane", "aria-label": "Runs list" },
|
||||
{ class: "list-pane", "data-list-view": view, "aria-label": "Runs list" },
|
||||
paneHeading("Runs", { detailOpen }),
|
||||
renderRunList(),
|
||||
),
|
||||
@@ -1179,7 +1188,7 @@ function renderContent() {
|
||||
layout.append(
|
||||
h(
|
||||
"section",
|
||||
{ class: "list-pane", "aria-label": "Templates list" },
|
||||
{ class: "list-pane", "data-list-view": view, "aria-label": "Templates list" },
|
||||
paneHeading("Templates", { detailOpen }),
|
||||
renderTemplateGallery(),
|
||||
),
|
||||
@@ -1189,7 +1198,7 @@ function renderContent() {
|
||||
layout.append(
|
||||
h(
|
||||
"section",
|
||||
{ class: "list-pane", "aria-label": "Chat list" },
|
||||
{ class: "list-pane", "data-list-view": view, "aria-label": "Chat list" },
|
||||
paneHeading("Chat", { detailOpen }),
|
||||
renderChatList(),
|
||||
),
|
||||
@@ -1222,6 +1231,8 @@ function renderContent() {
|
||||
}
|
||||
}
|
||||
content.replaceChildren(...nodes);
|
||||
const restoredList = content.querySelector(".list-pane .list-scroll");
|
||||
if (restoredList) restoredList.scrollTop = listScrollPositions.get(state.view) ?? 0;
|
||||
if (!reduceMotion) {
|
||||
for (const node of content.querySelectorAll("[data-animate-key]")) {
|
||||
const key = node.getAttribute("data-animate-key");
|
||||
@@ -2659,4 +2670,4 @@ if (bootServerUrl) {
|
||||
state.url = sameOrigin;
|
||||
openConfiguredEndpoint({ surfaceError: false });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
// CSS viewport units account for browser toolbars, not reliably for the soft
|
||||
// keyboard. Use the visual viewport only for a substantial keyboard-sized
|
||||
// reduction; do not fight pinch zoom, small toolbar changes or user scrolling.
|
||||
export function mobileViewportState({ width, height, layoutHeight, baselineHeight, scale = 1, offsetTop = 0, editing = false, wasOpen = false }) {
|
||||
const visible = Number(height);
|
||||
const baseline = Math.max(Number(layoutHeight) || 0, Number(baselineHeight) || 0);
|
||||
const keyboard = (editing || wasOpen) && width < 600 && Math.abs(scale - 1) < 0.01 &&
|
||||
visible > 0 && baseline - visible >= Math.max(100, baseline * 0.2);
|
||||
return { keyboard, height: keyboard ? Math.round(visible) : null, top: keyboard ? Math.max(0, Math.round(offsetTop)) : 0 };
|
||||
}
|
||||
|
||||
/** Update geometry only: never rebuild DOM, blur an input, or scroll the page. */
|
||||
export function installMobileViewport(win = window, doc = document) {
|
||||
const viewport = win.visualViewport;
|
||||
if (!viewport) return () => {};
|
||||
const root = doc.documentElement;
|
||||
let baseline = win.innerHeight;
|
||||
let width = win.innerWidth;
|
||||
let open = false;
|
||||
let frame = null;
|
||||
function update() {
|
||||
frame = null;
|
||||
const active = doc.activeElement;
|
||||
const editing = Boolean(active?.matches?.("textarea, input:not([type=checkbox]):not([type=radio]):not([type=button]):not([type=submit]), [contenteditable=true]"));
|
||||
if (width !== win.innerWidth) {
|
||||
width = win.innerWidth;
|
||||
baseline = win.innerHeight;
|
||||
} else if (!editing && !open) {
|
||||
baseline = Math.max(win.innerHeight, viewport.height);
|
||||
}
|
||||
const state = mobileViewportState({ width, height: viewport.height, layoutHeight: win.innerHeight, baselineHeight: baseline, scale: viewport.scale, offsetTop: viewport.offsetTop, editing, wasOpen: open });
|
||||
open = state.keyboard;
|
||||
root.classList.toggle("keyboard-open", open);
|
||||
if (open) {
|
||||
root.style.setProperty("--keyboard-visible-height", `${state.height}px`);
|
||||
root.style.setProperty("--keyboard-viewport-top", `${state.top}px`);
|
||||
} else {
|
||||
root.style.removeProperty("--keyboard-visible-height");
|
||||
root.style.removeProperty("--keyboard-viewport-top");
|
||||
}
|
||||
}
|
||||
function schedule() {
|
||||
if (frame == null) frame = win.requestAnimationFrame(update);
|
||||
}
|
||||
for (const event of ["resize", "scroll"]) viewport.addEventListener(event, schedule);
|
||||
win.addEventListener("resize", schedule);
|
||||
doc.addEventListener("focusin", schedule);
|
||||
doc.addEventListener("focusout", schedule);
|
||||
update();
|
||||
return () => {
|
||||
for (const event of ["resize", "scroll"]) viewport.removeEventListener(event, schedule);
|
||||
win.removeEventListener("resize", schedule);
|
||||
doc.removeEventListener("focusin", schedule);
|
||||
doc.removeEventListener("focusout", schedule);
|
||||
if (frame != null) win.cancelAnimationFrame(frame);
|
||||
root.classList.remove("keyboard-open");
|
||||
root.style.removeProperty("--keyboard-visible-height");
|
||||
root.style.removeProperty("--keyboard-viewport-top");
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user