Skip to content

Commit 595288c

Browse files
committed
feat(web): view visual replies full screen with zoom
1 parent 33806e7 commit 595288c

6 files changed

Lines changed: 254 additions & 6 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// @vitest-environment jsdom
2+
3+
import { act } from "react";
4+
import { createRoot, type Root } from "react-dom/client";
5+
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6+
import { htmlRenderTheme, HTML_RENDER_DEFAULT_FONTS } from "@t3tools/shared/htmlRender";
7+
import { T3_CODE_DARK_THEME_COLORS } from "@t3tools/shared/themePalettes";
8+
import { EnvironmentId } from "@t3tools/contracts";
9+
10+
import { HtmlRenderDialog } from "./HtmlRenderDialog";
11+
import { HtmlRenderFrame } from "./HtmlRenderFrame";
12+
13+
const { refresh, showError } = vi.hoisted(() => ({ refresh: vi.fn(), showError: vi.fn() }));
14+
vi.mock("~/assets/assetUrls", () => ({
15+
useAssetUrlState: () => ({
16+
_tag: "Success",
17+
url: "https://environment.test/original.html",
18+
expiresAt: Date.now() + 60 * 60_000,
19+
}),
20+
useAssetUrlRefresh: () => refresh,
21+
}));
22+
vi.mock("../ui/toast", () => ({ toastManager: { add: showError } }));
23+
24+
vi.mock("~/hooks/useHtmlRenderTheme", () => ({
25+
useHtmlRenderTheme: () =>
26+
htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark", HTML_RENDER_DEFAULT_FONTS),
27+
}));
28+
29+
let root: Root;
30+
let container: HTMLDivElement;
31+
const onClose = vi.fn();
32+
33+
beforeEach(async () => {
34+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
35+
vi.stubGlobal(
36+
"ResizeObserver",
37+
class {
38+
observe() {}
39+
disconnect() {}
40+
unobserve() {}
41+
},
42+
);
43+
refresh.mockReset();
44+
showError.mockClear();
45+
onClose.mockClear();
46+
container = document.createElement("div");
47+
document.body.append(container);
48+
root = createRoot(container);
49+
await act(() =>
50+
root.render(
51+
<HtmlRenderDialog
52+
src="https://environment.test/page.html"
53+
title="Diagram"
54+
onClose={onClose}
55+
/>,
56+
),
57+
);
58+
});
59+
60+
afterEach(async () => {
61+
await act(() => root.unmount());
62+
container.remove();
63+
vi.unstubAllGlobals();
64+
});
65+
66+
async function click(label: string) {
67+
const button = document.querySelector<HTMLButtonElement>(`button[aria-label="${label}"]`);
68+
expect(button).not.toBeNull();
69+
await act(() => button!.click());
70+
}
71+
72+
it("zooms the loaded page within bounds and resets without reloading it", async () => {
73+
const frame = document.querySelector("iframe")!;
74+
const src = frame.src;
75+
for (let step = 0; step < 6; step += 1) await click("Zoom in");
76+
expect(document.querySelector<HTMLButtonElement>('[aria-label="Zoom in"]')!.disabled).toBe(true);
77+
expect(frame.parentElement!.style.getPropertyValue("zoom")).toBe("2");
78+
for (let step = 0; step < 8; step += 1) await click("Zoom out");
79+
expect(document.querySelector<HTMLButtonElement>('[aria-label="Zoom out"]')!.disabled).toBe(true);
80+
expect(frame.parentElement!.style.getPropertyValue("zoom")).toBe("0.5");
81+
await click("Reset zoom");
82+
expect(frame.parentElement!.style.getPropertyValue("zoom")).toBe("1");
83+
expect(document.querySelector("iframe")).toBe(frame);
84+
expect(frame.src).toBe(src);
85+
expect(frame.getAttribute("sandbox")).toBe("allow-scripts allow-forms");
86+
});
87+
88+
it("closes with the close button or Escape", async () => {
89+
await click("Close");
90+
expect(onClose).toHaveBeenCalledOnce();
91+
onClose.mockClear();
92+
await act(() => {
93+
document.activeElement?.dispatchEvent(
94+
new KeyboardEvent("keydown", { key: "Escape", bubbles: true }),
95+
);
96+
});
97+
expect(onClose).toHaveBeenCalledOnce();
98+
});
99+
100+
async function renderInline() {
101+
await act(() =>
102+
root.render(
103+
<HtmlRenderFrame
104+
environmentId={EnvironmentId.make("environment")}
105+
htmlRender={{ attachmentId: "html", title: "Diagram", height: 400 }}
106+
onOpen={() => undefined}
107+
/>,
108+
),
109+
);
110+
}
111+
112+
it("opens with a fresh URL while keeping the inline page loaded", async () => {
113+
await renderInline();
114+
const inlineFrame = document.querySelector("iframe")!;
115+
const originalSrc = inlineFrame.src;
116+
refresh.mockResolvedValue("https://environment.test/renewed.html");
117+
await click("Open full screen");
118+
expect(refresh).toHaveBeenCalledOnce();
119+
expect(document.querySelector('[role="dialog"] iframe')?.getAttribute("src")).toContain(
120+
"https://environment.test/renewed.html#",
121+
);
122+
expect(inlineFrame.src).toBe(originalSrc);
123+
await click("Close");
124+
expect(document.querySelector('[role="dialog"]')).toBeNull();
125+
expect(document.querySelector("iframe")).toBe(inlineFrame);
126+
});
127+
128+
it.each([null, new Error("Disconnected")])(
129+
"reports an unavailable URL instead of opening a blank page: %s",
130+
async (failure) => {
131+
await renderInline();
132+
if (failure === null) refresh.mockResolvedValue(null);
133+
else refresh.mockRejectedValue(failure);
134+
await click("Open full screen");
135+
expect(document.querySelector('[role="dialog"]')).toBeNull();
136+
expect(showError).toHaveBeenCalledWith(
137+
expect.objectContaining({ type: "error", title: "Page unavailable" }),
138+
);
139+
},
140+
);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { MinusIcon, PlusIcon } from "lucide-react";
2+
import { useState } from "react";
3+
4+
import { HtmlRenderDocument } from "../files/BrowserDocumentFrame";
5+
import { Button } from "../ui/button";
6+
import { Dialog, DialogHeader, DialogPopup, DialogTitle } from "../ui/dialog";
7+
8+
export function HtmlRenderDialog(props: {
9+
readonly src: string;
10+
readonly title: string;
11+
readonly onClose: () => void;
12+
}) {
13+
const [zoom, setZoom] = useState(1);
14+
return (
15+
<Dialog open onOpenChange={(open) => !open && props.onClose()}>
16+
<DialogPopup variant="fullscreen" bottomStickOnMobile={false}>
17+
<DialogHeader>
18+
<div className="flex min-w-0 items-center gap-3 pe-8">
19+
<div className="min-w-0 flex-1">
20+
<DialogTitle>{props.title}</DialogTitle>
21+
</div>
22+
<div className="flex items-center gap-1">
23+
<Button
24+
aria-label="Zoom out"
25+
size="icon-sm"
26+
variant="ghost"
27+
disabled={zoom === 0.5}
28+
onClick={() => setZoom((value) => value - 0.25)}
29+
>
30+
<MinusIcon />
31+
</Button>
32+
<Button aria-label="Reset zoom" size="sm" variant="ghost" onClick={() => setZoom(1)}>
33+
{Math.round(zoom * 100)}%
34+
</Button>
35+
<Button
36+
aria-label="Zoom in"
37+
size="icon-sm"
38+
variant="ghost"
39+
disabled={zoom === 2}
40+
onClick={() => setZoom((value) => value + 0.25)}
41+
>
42+
<PlusIcon />
43+
</Button>
44+
</div>
45+
</div>
46+
</DialogHeader>
47+
<div className="min-h-0 flex-1 overflow-auto [container-type:size]">
48+
<div className="h-[100cqh] w-[100cqw]" style={{ zoom }}>
49+
<HtmlRenderDocument src={props.src} title={props.title} className="block size-full" />
50+
</div>
51+
</div>
52+
</DialogPopup>
53+
</Dialog>
54+
);
55+
}

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import {
55
htmlRenderFrameHeight,
66
type HtmlRenderReference,
77
} from "@t3tools/shared/htmlRender";
8-
import { Maximize2Icon } from "lucide-react";
8+
import { Maximize2Icon, PanelRightIcon } from "lucide-react";
99
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
1010

1111
import { useAssetUrlRefresh, useAssetUrlState } from "~/assets/assetUrls";
1212
import type { ChatFileAttachment } from "~/types";
1313

1414
import { HtmlRenderDocument } from "../files/BrowserDocumentFrame";
1515
import { Button } from "../ui/button";
16+
import { toastManager } from "../ui/toast";
1617
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
18+
import { HtmlRenderDialog } from "./HtmlRenderDialog";
1719

1820
// A frame may load its URL a little after mounting.
1921
const MIN_URL_LIFE_MS = 5 * 60_000;
@@ -28,6 +30,7 @@ export function HtmlRenderFrame(props: {
2830
readonly environmentId: EnvironmentId;
2931
readonly htmlRender: HtmlRenderReference;
3032
readonly onOpen: (attachment: ChatFileAttachment) => void;
33+
readonly onFullscreenChange?: (fullscreen: boolean) => void;
3134
}) {
3235
const { attachmentId, title } = props.htmlRender;
3336
// The frame takes the page's measured height at its own width, read before
@@ -63,6 +66,13 @@ export function HtmlRenderFrame(props: {
6366
// page again; one near expiry is minted afresh, since a frame cannot report a
6467
// failed load. The page keeps its first URL for its lifetime.
6568
const [src, setSrc] = useState<string | null>(null);
69+
const [expandedSrc, setExpandedSrc] = useState<string | null>(null);
70+
const { onFullscreenChange } = props;
71+
useEffect(() => {
72+
if (expandedSrc === null) return;
73+
onFullscreenChange?.(true);
74+
return () => onFullscreenChange?.(false);
75+
}, [expandedSrc, onFullscreenChange]);
6676
const [failed, setFailed] = useState(false);
6777
const assetUrl = useAssetUrlState(src === null ? props.environmentId : null, resource);
6878
const cachedUrl = assetUrl._tag === "Success" ? assetUrl.url : null;
@@ -101,7 +111,31 @@ export function HtmlRenderFrame(props: {
101111
className="block size-full"
102112
onContentHeight={setContentHeight}
103113
/>
104-
<div className="absolute end-2 top-2 opacity-0 transition-opacity duration-150 focus-within:opacity-100 group-hover/html-render:opacity-100 pointer-coarse:opacity-100">
114+
<div className="absolute end-2 top-2 flex gap-1 opacity-0 transition-opacity duration-150 focus-within:opacity-100 group-hover/html-render:opacity-100 pointer-coarse:opacity-100">
115+
<Tooltip>
116+
<TooltipTrigger
117+
render={
118+
<Button
119+
aria-label="Open full screen"
120+
size="icon-xs"
121+
variant="glass"
122+
onClick={async () => {
123+
const url = await refresh().catch(() => null);
124+
if (url !== null) setExpandedSrc(url);
125+
else
126+
toastManager.add({
127+
type: "error",
128+
title: "Page unavailable",
129+
description: "Reconnect to the environment and try again.",
130+
});
131+
}}
132+
/>
133+
}
134+
>
135+
<Maximize2Icon className="size-3.5" />
136+
</TooltipTrigger>
137+
<TooltipPopup side="left">Open full screen</TooltipPopup>
138+
</Tooltip>
105139
<Tooltip>
106140
<TooltipTrigger
107141
render={
@@ -123,11 +157,18 @@ export function HtmlRenderFrame(props: {
123157
/>
124158
}
125159
>
126-
<Maximize2Icon className="size-3.5" />
160+
<PanelRightIcon className="size-3.5" />
127161
</TooltipTrigger>
128162
<TooltipPopup side="left">Open in panel</TooltipPopup>
129163
</Tooltip>
130164
</div>
165+
{expandedSrc !== null ? (
166+
<HtmlRenderDialog
167+
src={expandedSrc}
168+
title={title}
169+
onClose={() => setExpandedSrc(null)}
170+
/>
171+
) : null}
131172
</>
132173
) : failed ? (
133174
<p className="flex size-full items-center justify-center text-muted-foreground text-xs">

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2956,6 +2956,11 @@ function ProposedPlanTimelineRow({
29562956

29572957
function HtmlRenderTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "html-render" }> }) {
29582958
const ctx = use(TimelineRowCtx);
2959+
const { onAppFullscreenChange } = ctx;
2960+
const onFullscreenChange = useCallback(
2961+
(fullscreen: boolean) => onAppFullscreenChange(row.id, fullscreen),
2962+
[onAppFullscreenChange, row.id],
2963+
);
29592964

29602965
return (
29612966
<div className="min-w-0 px-1">
@@ -2965,6 +2970,7 @@ function HtmlRenderTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "htm
29652970
environmentId={ctx.activeThreadEnvironmentId}
29662971
htmlRender={row.htmlRender}
29672972
onOpen={ctx.onFileOpen}
2973+
onFullscreenChange={onFullscreenChange}
29682974
/>
29692975
</div>
29702976
);

‎apps/web/src/components/ui/dialog.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,30 @@ function DialogPopup({
6666
}: DialogPrimitive.Popup.Props & {
6767
showCloseButton?: boolean;
6868
bottomStickOnMobile?: boolean;
69-
variant?: "default" | "media";
69+
variant?: "default" | "media" | "fullscreen";
7070
}) {
7171
return (
7272
<DialogPortal>
7373
{/* Media opens from inside other overlays (a composer chip, a popover), so it sits above them. */}
74-
<DialogBackdrop className={variant === "media" ? "z-[60]" : undefined} variant={variant} />
74+
<DialogBackdrop
75+
className={variant === "media" ? "z-[60]" : undefined}
76+
variant={variant === "media" ? "media" : "default"}
77+
/>
7578
<DialogViewport
7679
className={cn(
7780
bottomStickOnMobile && "max-sm:grid-rows-[1fr_auto] max-sm:p-0 max-sm:pt-12",
7881
variant === "media" &&
7982
"z-[60] grid-rows-1 place-items-center px-4 py-6 [-webkit-app-region:no-drag]",
83+
variant === "fullscreen" && "grid-rows-1 p-0",
8084
)}
8185
>
8286
<DialogPrimitive.Popup
8387
className={cn(
8488
variant === "media" ? DIALOG_MEDIA_POPUP_CLASS : DIALOG_POPUP_CLASS,
8589
"row-start-2 text-popover-foreground",
8690
variant === "default" && "max-h-full max-w-lg",
91+
variant === "fullscreen" &&
92+
"row-start-1 size-full max-w-none rounded-none bg-background",
8793
bottomStickOnMobile && DIALOG_MOBILE_SHEET_CLASS,
8894
className,
8995
)}

‎docs/user/html-renders.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Agents can answer with a page instead of only text: a chart, table, diagram, image collage, or mockup. Ask for one ("show this as a chart", "make a collage of these screenshots") and the agent builds a self-contained HTML page, which appears in the thread above its written reply. It works with every provider, on web, desktop, and mobile.
44

5-
Pages use your current theme, including custom themes, and follow light and dark mode as you switch. Scripts run inside the page, but it is sandboxed away from T3 Code and your session. Links you click in a page open in your browser. Use the expand button to open a page full size; from there you can view its source or save it.
5+
Pages use your current theme, including custom themes, and follow light and dark mode as you switch. Scripts run inside the page, but it is sandboxed away from T3 Code and your session. Links you click in a page open in your browser. Use the expand button to open a page full screen. On web and desktop, use the zoom controls to see more detail, or open the page in the side panel to view its source or save it.
66

77
Agents can place local images in a page by file path. T3 Code embeds them when the page is published, so the page keeps working after the original files move or are deleted. Deleting the thread deletes its pages.
88

0 commit comments

Comments
 (0)