Skip to content

Commit cc031c5

Browse files
Zeus-Deusclaude
andcommitted
fix(web): dragging inside an inline HTML page no longer flickers and cancels
The inline frame fits its height to the page. A page that grows when a drag starts made the frame resize mid-gesture, which fires resize in the page, and pages commonly cancel a drag on resize. The bootstrap now holds height reports while a pointer is pressed and reports once on release, and tells the client about the press so the timeline stops following the thread's end until release. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 67c0fc0 commit cc031c5

4 files changed

Lines changed: 141 additions & 4 deletions

File tree

‎apps/web/src/components/chat/HtmlRenderFrame.browser.test.tsx‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { act } from "react";
22
import { createRoot, type Root } from "react-dom/client";
33
import { afterEach, expect, it, vi } from "vite-plus/test";
4+
import { cdp } from "vite-plus/test/browser";
45
import { EnvironmentId } from "@t3tools/contracts";
56
import {
67
htmlRenderTheme,
78
HTML_RENDER_DEFAULT_FONTS,
89
injectHtmlRenderBootstrap,
10+
readHtmlRenderContentHeight,
911
} from "@t3tools/shared/htmlRender";
1012
import { T3_CODE_DARK_THEME_COLORS } from "@t3tools/shared/themePalettes";
1113

@@ -61,3 +63,87 @@ it("grows past the agent's height to fit the page the client lays out", async ()
6163
await expect.poll(() => box.style.height).toBe("2345px");
6264
expect(box.querySelector("iframe")?.clientHeight).toBe(2345);
6365
});
66+
67+
// Real input, so the press reaches the sandboxed page as a trusted event.
68+
async function mouse(type: "mousePressed" | "mouseReleased", x: number, y: number) {
69+
const frame = window.frameElement?.getBoundingClientRect();
70+
await cdp().send("Input.dispatchMouseEvent", {
71+
type,
72+
x: (frame?.left ?? 0) + x,
73+
y: (frame?.top ?? 0) + y,
74+
button: "left",
75+
buttons: type === "mousePressed" ? 1 : 0,
76+
clickCount: 1,
77+
});
78+
}
79+
80+
it("keeps its height through a press, so a page's drag survives growing", async () => {
81+
// Like many drag scripts, the page grows when a drag starts and cancels the
82+
// drag when its window resizes.
83+
page.url = URL.createObjectURL(
84+
new Blob(
85+
[
86+
injectHtmlRenderBootstrap(`<div id="list" style="height:200px"></div><script>
87+
var list = document.getElementById("list"), dragging = false, cancelled = false;
88+
addEventListener("load", function () { parent.postMessage({ test: "ready" }, "*"); });
89+
list.addEventListener("pointerdown", function (e) {
90+
dragging = true;
91+
list.setPointerCapture(e.pointerId);
92+
list.style.height = "260px";
93+
// A size report would be posted in the next frame, so this arrives after it.
94+
requestAnimationFrame(function () {
95+
requestAnimationFrame(function () { parent.postMessage({ test: "settled" }, "*"); });
96+
});
97+
});
98+
addEventListener("resize", function () { if (dragging) cancelled = true; });
99+
addEventListener("pointerup", function () {
100+
parent.postMessage({ test: "dropped", dragging: dragging, cancelled: cancelled }, "*");
101+
dragging = false;
102+
});
103+
</script>`),
104+
],
105+
{ type: "text/html" },
106+
),
107+
);
108+
// The page's own reports and its size reports, in the order the frame posted them.
109+
const reports: unknown[] = [];
110+
const onReport = (event: MessageEvent) => {
111+
const height = readHtmlRenderContentHeight(event.data);
112+
if (height !== undefined) reports.push({ height });
113+
else if (typeof (event.data as { test?: unknown } | null)?.test === "string") {
114+
reports.push(event.data);
115+
}
116+
};
117+
window.addEventListener("message", onReport);
118+
const fullscreen: boolean[] = [];
119+
container = document.createElement("div");
120+
document.body.append(container);
121+
root = createRoot(container);
122+
await act(() =>
123+
root!.render(
124+
<HtmlRenderFrame
125+
environmentId={EnvironmentId.make("environment-1")}
126+
htmlRender={{ attachmentId: "html", title: "Drag page", height: 200 }}
127+
threadRef={undefined}
128+
onFullscreenChange={(value) => fullscreen.push(value)}
129+
/>,
130+
),
131+
);
132+
const box = container.firstElementChild as HTMLElement;
133+
await expect.poll(() => reports).toContainEqual({ test: "ready" });
134+
135+
const rect = box.getBoundingClientRect();
136+
await mouse("mousePressed", rect.left + 20, rect.top + 20);
137+
await expect.poll(() => reports).toContainEqual({ test: "settled" });
138+
expect(reports).not.toContainEqual({ height: 260 });
139+
// The hold reaches the timeline, which stops following the thread's end.
140+
await expect.poll(() => fullscreen.at(-1)).toBe(true);
141+
await mouse("mouseReleased", rect.left + 20, rect.top + 20);
142+
143+
await expect
144+
.poll(() => reports)
145+
.toContainEqual({ test: "dropped", dragging: true, cancelled: false });
146+
await expect.poll(() => box.style.height).toBe("260px");
147+
await expect.poll(() => fullscreen.at(-1)).toBe(false);
148+
window.removeEventListener("message", onReport);
149+
});

‎apps/web/src/components/files/BrowserDocumentFrame.tsx‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
readHtmlRenderImageRequest,
1717
readHtmlRenderKeyDown,
1818
readHtmlRenderLinkRequest,
19+
readHtmlRenderPointerHeld,
1920
} from "@t3tools/shared/htmlRender";
2021
import {
2122
isMacPlatform,
@@ -270,12 +271,28 @@ export function HtmlRenderDocument(props: {
270271
window.addEventListener("message", openImage);
271272
return () => window.removeEventListener("message", openImage);
272273
}, []);
274+
// A press inside the page holds the frame still as full screen does: a
275+
// thread following its streaming output would scroll the frame out from
276+
// under a drag. A touch press is not yet a user activation, so the hold is
277+
// not gated on one; a page can at most pause following and keep its row mounted.
278+
const [pointerHeld, setPointerHeld] = useState(false);
279+
useEffect(() => {
280+
const hold = (event: MessageEvent) => {
281+
const held = readHtmlRenderPointerHeld(event.data);
282+
if (held !== undefined && event.source === frameRef.current?.contentWindow) {
283+
setPointerHeld(held);
284+
}
285+
};
286+
window.addEventListener("message", hold);
287+
return () => window.removeEventListener("message", hold);
288+
}, []);
273289
const { onFullscreenChange } = props;
290+
const holding = expandedImage !== null || pointerHeld;
274291
useEffect(() => {
275-
if (expandedImage === null) return;
292+
if (!holding) return;
276293
onFullscreenChange?.(true);
277294
return () => onFullscreenChange?.(false);
278-
}, [expandedImage, onFullscreenChange]);
295+
}, [holding, onFullscreenChange]);
279296
const { onContentHeight } = props;
280297
// A page posts its height once per change, so listen from the commit that
281298
// inserts the frame; a passive effect could run after a fast page's first post.

‎packages/shared/src/htmlRender.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
readHtmlRenderImageRequest,
1313
readHtmlRenderKeyDown,
1414
readHtmlRenderLinkRequest,
15+
readHtmlRenderPointerHeld,
1516
readHtmlRenderReference,
1617
} from "./htmlRender.ts";
1718
import { T3_CODE_DARK_THEME_COLORS, T3_CODE_LIGHT_THEME_COLORS } from "./themePalettes.ts";
@@ -170,6 +171,23 @@ describe("readHtmlRenderContentHeight", () => {
170171
});
171172
});
172173

174+
describe("readHtmlRenderPointerHeld", () => {
175+
it("reads only a boolean from a pointer-held notification", () => {
176+
const notification = (params: unknown) => ({
177+
jsonrpc: "2.0",
178+
method: "t3/notifications/pointer-held",
179+
params,
180+
});
181+
expect(readHtmlRenderPointerHeld(notification({ held: true }))).toBe(true);
182+
expect(readHtmlRenderPointerHeld(notification({ held: false }))).toBe(false);
183+
expect(readHtmlRenderPointerHeld(notification({ held: 1 }))).toBe(undefined);
184+
expect(readHtmlRenderPointerHeld(notification(null))).toBe(undefined);
185+
expect(readHtmlRenderPointerHeld({ ...notification({ held: true }), method: "x" })).toBe(
186+
undefined,
187+
);
188+
});
189+
});
190+
173191
describe("htmlRenderThemeMessage", () => {
174192
it("is an MCP Apps host-context-changed notification carrying the theme variables", () => {
175193
const theme = htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark");

‎packages/shared/src/htmlRender.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export const HTML_RENDER_LAYOUT_GUIDE = [
246246
"If a box needs its own background (a mock of a specific screen, a panel that must stand apart), give it at least 16px of padding on every side and var(--radius) corners, so content never touches its edge.",
247247
"Give charts fixed pixel heights rather than heights that scale with width.",
248248
"Let content set the page's height. The frame grows to fit the page, so a height that follows the viewport makes it grow again and again: html_preview and html_render reject vh, svh, lvh, and dvh units, and height:100% on html or body has the same problem.",
249+
"The frame's height changes whenever the page's does, and each change fires resize in the page, so a drag or other gesture should cancel on resize only when the width changed.",
249250
].join(" ");
250251

251252
const HOST_CONTEXT_CHANGED_METHOD = "ui/notifications/host-context-changed";
@@ -258,6 +259,8 @@ const SHORTCUTS_METHOD = "t3/notifications/shortcuts";
258259
const KEY_DOWN_METHOD = "t3/notifications/keydown";
259260
// Not part of MCP Apps: a click on a page image opens it in the app's image viewer.
260261
const OPEN_IMAGE_METHOD = "t3/notifications/open-image";
262+
// Not part of MCP Apps: a press inside the page asks the client to hold the frame still.
263+
const POINTER_HELD_METHOD = "t3/notifications/pointer-held";
261264

262265
export function readHtmlRenderContentHeight(data: unknown): number | undefined {
263266
if (typeof data !== "object" || data === null) return undefined;
@@ -270,6 +273,15 @@ export function readHtmlRenderContentHeight(data: unknown): number | undefined {
270273
return typeof height === "number" && Number.isFinite(height) && height > 0 ? height : undefined;
271274
}
272275

276+
export function readHtmlRenderPointerHeld(data: unknown): boolean | undefined {
277+
if (typeof data !== "object" || data === null) return undefined;
278+
const { jsonrpc, method, params } = data as Record<string, unknown>;
279+
if (jsonrpc !== "2.0" || method !== POINTER_HELD_METHOD) return undefined;
280+
const held =
281+
typeof params === "object" && params !== null ? (params as { held?: unknown }).held : undefined;
282+
return typeof held === "boolean" ? held : undefined;
283+
}
284+
273285
export function readHtmlRenderLinkRequest(data: unknown):
274286
| {
275287
readonly id: string | number;
@@ -397,12 +409,16 @@ function rootRule(theme: HtmlRenderTheme): string {
397409
// left click on a framed page's image, outside a link or button and not
398410
// prevented by any of the page's listeners, asks the client to open the image
399411
// in its viewer. A framed page also reports its content height, measured as the server measures it,
400-
// so its client can fit the frame to the page. A chord the client named as an
412+
// so its client can fit the frame to the page. Reports wait while a pointer is
413+
// pressed: resizing the frame fires resize in the page, and pages commonly
414+
// cancel a drag on resize. The page also tells its client a press is held, so
415+
// the thread does not scroll the frame out from under the pointer. Blur ends a
416+
// hold whose release never arrived, so the frame cannot stop fitting. A chord the client named as an
401417
// app shortcut is passed on rather than left to the browser, unless a text
402418
// field has focus or the page handled it: the deciding listener is added while
403419
// the key dispatches, so it runs after every listener the page has. Keys
404420
// match as resolveEventKeys reads them, so the client accepts what it claims.
405-
const BOOTSTRAP_SCRIPT = `(function(){var s=document.getElementById("t3-theme"),n=0;if(!s)return;var b=${JSON.stringify(BASE_CSS)};function a(t){if(!t||typeof t!=="object"||!t.variables||typeof t.variables!=="object")return;var c=":root{color-scheme:"+(t.appearance==="light"?"light":"dark")+";";for(var k in t.variables){if(/^--[a-z0-9-]+$/.test(k))c+=k+":"+String(t.variables[k]).replace(/[;{}<>]/g,"")+";";}s.textContent=c+"}"+b;}try{var m=/[#&]${THEME_FRAGMENT_KEY}=([^&]*)/.exec(location.hash);if(m){a(JSON.parse(decodeURIComponent(m[1])));history.replaceState(history.state,"",location.pathname+location.search);}}catch(e){}window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(d&&d.jsonrpc==="2.0"&&d.method===${JSON.stringify(HOST_CONTEXT_CHANGED_METHOD)}&&p&&p.styles)a({appearance:p.theme,variables:p.styles.variables});});document.addEventListener("click",function(e){var l=e.isTrusted?e.composedPath().find(function(t){return t&&t.matches&&t.matches("a[href]");}):null,u;if(!l)return;try{u=new URL(l.getAttribute("href"),document.baseURI);}catch(x){return;}if(!/^https?:$/.test(u.protocol)||u.href.split("#")[0]===location.href.split("#")[0])return;if(window.parent!==window){e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",id:"t3-link-"+(++n),method:${JSON.stringify(OPEN_LINK_METHOD)},params:{url:u.href,metaKey:e.metaKey,ctrlKey:e.ctrlKey}},"*");}else{l.setAttribute("target","_blank");l.setAttribute("rel","noopener");}},true);if(window.parent!==window){var h,o,z=function(){var r=document.documentElement,v=Math.ceil(r.scrollHeight>r.clientHeight?r.scrollHeight:r.getBoundingClientRect().height);if(v===h)return;h=v;window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(SIZE_CHANGED_METHOD)},params:{height:v}},"*");};if(window.ResizeObserver){o=new ResizeObserver(z);o.observe(document.documentElement);}document.addEventListener("DOMContentLoaded",function(){if(o&&document.body)o.observe(document.body);z();});window.addEventListener("load",z);window.addEventListener("click",function(e){var t=e.composedPath()[0],u;if(!e.isTrusted||e.defaultPrevented||e.button!==0||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||!t||t.tagName!=="IMG"||t.closest("a[href],button"))return;u=t.currentSrc||t.src;if(u)setTimeout(function(){if(!e.defaultPrevented)window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(OPEN_IMAGE_METHOD)},params:{src:u,name:t.alt||t.title||""}},"*");});});var K=[],B=[],C=${JSON.stringify(EVENT_CODE_SHORTCUT_KEYS)};window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(e.source===window.parent&&d&&d.jsonrpc==="2.0"&&d.method===${JSON.stringify(SHORTCUTS_METHOD)}&&p&&Array.isArray(p.shortcuts)&&Array.isArray(p.blocked)){K=p.shortcuts;B=p.blocked;}});window.addEventListener("keydown",function(e){var t=e.composedPath()[0],c=e.code||"",l=e.key.toLowerCase(),x=/^[a-z]$/.test(l)?[l]:[l,/^Key[A-Z]$/.test(c)?c.slice(3).toLowerCase():"",C.hasOwnProperty(c)?C[c]:""],f,m=function(s){return s&&s.metaKey===e.metaKey&&s.ctrlKey===e.ctrlKey&&s.shiftKey===e.shiftKey&&s.altKey===e.altKey&&x.indexOf(s.key)>-1;};if(!e.isTrusted||e.isComposing||e.getModifierState("AltGraph")&&!/^[a-z0-9]$/i.test(e.key)||t&&(t.isContentEditable||/^(?:INPUT|TEXTAREA|SELECT)$/.test(t.tagName))||!K.some(m)||B.some(m))return;f=function(v){window.removeEventListener("keydown",f);if(v!==e||e.defaultPrevented)return;e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(KEY_DOWN_METHOD)},params:{key:e.key,code:e.code,metaKey:e.metaKey,ctrlKey:e.ctrlKey,shiftKey:e.shiftKey,altKey:e.altKey,repeat:e.repeat}},"*");};window.addEventListener("keydown",f);setTimeout(function(){window.removeEventListener("keydown",f);});},true);}})();`;
421+
const BOOTSTRAP_SCRIPT = `(function(){var s=document.getElementById("t3-theme"),n=0;if(!s)return;var b=${JSON.stringify(BASE_CSS)};function a(t){if(!t||typeof t!=="object"||!t.variables||typeof t.variables!=="object")return;var c=":root{color-scheme:"+(t.appearance==="light"?"light":"dark")+";";for(var k in t.variables){if(/^--[a-z0-9-]+$/.test(k))c+=k+":"+String(t.variables[k]).replace(/[;{}<>]/g,"")+";";}s.textContent=c+"}"+b;}try{var m=/[#&]${THEME_FRAGMENT_KEY}=([^&]*)/.exec(location.hash);if(m){a(JSON.parse(decodeURIComponent(m[1])));history.replaceState(history.state,"",location.pathname+location.search);}}catch(e){}window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(d&&d.jsonrpc==="2.0"&&d.method===${JSON.stringify(HOST_CONTEXT_CHANGED_METHOD)}&&p&&p.styles)a({appearance:p.theme,variables:p.styles.variables});});document.addEventListener("click",function(e){var l=e.isTrusted?e.composedPath().find(function(t){return t&&t.matches&&t.matches("a[href]");}):null,u;if(!l)return;try{u=new URL(l.getAttribute("href"),document.baseURI);}catch(x){return;}if(!/^https?:$/.test(u.protocol)||u.href.split("#")[0]===location.href.split("#")[0])return;if(window.parent!==window){e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",id:"t3-link-"+(++n),method:${JSON.stringify(OPEN_LINK_METHOD)},params:{url:u.href,metaKey:e.metaKey,ctrlKey:e.ctrlKey}},"*");}else{l.setAttribute("target","_blank");l.setAttribute("rel","noopener");}},true);if(window.parent!==window){var h,o,P={},D=0,Y=function(p){window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(POINTER_HELD_METHOD)},params:{held:p}},"*");},z=function(){if(D)return;var r=document.documentElement,v=Math.ceil(r.scrollHeight>r.clientHeight?r.scrollHeight:r.getBoundingClientRect().height);if(v===h)return;h=v;window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(SIZE_CHANGED_METHOD)},params:{height:v}},"*");};if(window.ResizeObserver){o=new ResizeObserver(z);o.observe(document.documentElement);}document.addEventListener("DOMContentLoaded",function(){if(o&&document.body)o.observe(document.body);z();});window.addEventListener("load",z);var U=function(e){if(!P[e.pointerId])return;delete P[e.pointerId];if(--D)return;Y(false);requestAnimationFrame(z);};window.addEventListener("pointerdown",function(e){if(!e.isTrusted||P[e.pointerId])return;P[e.pointerId]=1;if(!D++)Y(true);},true);window.addEventListener("pointerup",U,true);window.addEventListener("pointercancel",U,true);window.addEventListener("blur",function(){if(!D)return;P={};D=0;Y(false);requestAnimationFrame(z);});window.addEventListener("click",function(e){var t=e.composedPath()[0],u;if(!e.isTrusted||e.defaultPrevented||e.button!==0||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||!t||t.tagName!=="IMG"||t.closest("a[href],button"))return;u=t.currentSrc||t.src;if(u)setTimeout(function(){if(!e.defaultPrevented)window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(OPEN_IMAGE_METHOD)},params:{src:u,name:t.alt||t.title||""}},"*");});});var K=[],B=[],C=${JSON.stringify(EVENT_CODE_SHORTCUT_KEYS)};window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(e.source===window.parent&&d&&d.jsonrpc==="2.0"&&d.method===${JSON.stringify(SHORTCUTS_METHOD)}&&p&&Array.isArray(p.shortcuts)&&Array.isArray(p.blocked)){K=p.shortcuts;B=p.blocked;}});window.addEventListener("keydown",function(e){var t=e.composedPath()[0],c=e.code||"",l=e.key.toLowerCase(),x=/^[a-z]$/.test(l)?[l]:[l,/^Key[A-Z]$/.test(c)?c.slice(3).toLowerCase():"",C.hasOwnProperty(c)?C[c]:""],f,m=function(s){return s&&s.metaKey===e.metaKey&&s.ctrlKey===e.ctrlKey&&s.shiftKey===e.shiftKey&&s.altKey===e.altKey&&x.indexOf(s.key)>-1;};if(!e.isTrusted||e.isComposing||e.getModifierState("AltGraph")&&!/^[a-z0-9]$/i.test(e.key)||t&&(t.isContentEditable||/^(?:INPUT|TEXTAREA|SELECT)$/.test(t.tagName))||!K.some(m)||B.some(m))return;f=function(v){window.removeEventListener("keydown",f);if(v!==e||e.defaultPrevented)return;e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(KEY_DOWN_METHOD)},params:{key:e.key,code:e.code,metaKey:e.metaKey,ctrlKey:e.ctrlKey,shiftKey:e.shiftKey,altKey:e.altKey,repeat:e.repeat}},"*");};window.addEventListener("keydown",f);setTimeout(function(){window.removeEventListener("keydown",f);});},true);}})();`;
406422

407423
function bootstrapMarkup(markup: string): string {
408424
const dark = htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark");

0 commit comments

Comments
 (0)