Skip to content

Commit de2b1d3

Browse files
fix(web): keep HTML prototype selection rings visible (pingdotgg#23745)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent f8646e0 commit de2b1d3

3 files changed

Lines changed: 110 additions & 2 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"claudeSystemAppendBytes": 5135,
33
"codexAdditionalContextBytes": 6865,
4-
"mcpToolListJsonBytes": 45907
4+
"mcpToolListJsonBytes": 46007
55
}

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { injectHtmlRenderBootstrap } from "@t3tools/shared/htmlRender";
12
import { describe, expect, it, vi } from "vite-plus/test";
23

34
import "~/index.css";
@@ -34,3 +35,109 @@ describe("HtmlRenderDocument", () => {
3435
expect(onContentHeight).toHaveBeenCalledTimes(1);
3536
});
3637
});
38+
39+
it("keeps the first and last selected card rings inside the fitted renderer frame", async () => {
40+
const html = injectHtmlRenderBootstrap(`<!doctype html><html><head><style>
41+
main { display: grid; gap: 12px; }
42+
button { box-sizing: border-box; width: 100%; height: 72px; border: 0; }
43+
button[aria-pressed="true"] { outline: 3px solid blue; outline-offset: 2px; }
44+
</style></head><body><main>
45+
<button id="first" aria-pressed="false">Meadow</button>
46+
<button id="middle" aria-pressed="false">Harbor</button>
47+
<button id="last" aria-pressed="false">Orchid</button>
48+
</main><script>
49+
document.querySelectorAll("button").forEach(function(button) {
50+
button.addEventListener("click", function() {
51+
document.querySelectorAll("button").forEach(function(card) {
52+
card.setAttribute("aria-pressed", String(card === button));
53+
});
54+
});
55+
});
56+
window.addEventListener("message", function(event) {
57+
if (event.source !== window.parent || event.data.type !== "select-card") return;
58+
var button = document.getElementById(event.data.id);
59+
button.click();
60+
var rect = button.getBoundingClientRect();
61+
var style = getComputedStyle(button);
62+
var ring = parseFloat(style.outlineWidth) + parseFloat(style.outlineOffset);
63+
window.parent.postMessage({
64+
type: "selected-card-bounds",
65+
id: button.id,
66+
selected: button.getAttribute("aria-pressed") === "true",
67+
ring: ring,
68+
left: rect.left - ring,
69+
right: rect.right + ring,
70+
top: rect.top - ring,
71+
bottom: rect.bottom + ring,
72+
pageHeight: document.documentElement.getBoundingClientRect().height,
73+
viewportHeight: window.innerHeight,
74+
viewportWidth: window.innerWidth,
75+
scrollWidth: document.documentElement.scrollWidth
76+
}, "*");
77+
});
78+
</script></body></html>`);
79+
const src = URL.createObjectURL(new Blob([html], { type: "text/html" }));
80+
using _url = { [Symbol.dispose]: () => URL.revokeObjectURL(src) };
81+
const container = document.createElement("div");
82+
container.style.width = "728px";
83+
const reports = new Map<
84+
string,
85+
{
86+
selected: boolean;
87+
ring: number;
88+
left: number;
89+
right: number;
90+
top: number;
91+
bottom: number;
92+
pageHeight: number;
93+
viewportHeight: number;
94+
viewportWidth: number;
95+
scrollWidth: number;
96+
}
97+
>();
98+
const messages = new AbortController();
99+
using _messages = { [Symbol.dispose]: () => messages.abort() };
100+
window.addEventListener(
101+
"message",
102+
(event) => {
103+
const frame = container.querySelector("iframe");
104+
if (event.source === frame?.contentWindow && event.data?.type === "selected-card-bounds") {
105+
reports.set(event.data.id, event.data);
106+
}
107+
},
108+
{ signal: messages.signal },
109+
);
110+
const contentHeight = vi.fn((height: number) => {
111+
container.style.setProperty("--content-height", `${height}px`);
112+
});
113+
await using screen = await render(
114+
<>
115+
<style>{`iframe[title="Edge cards"] {
116+
display: block; width: 100%; height: var(--content-height, 120px); border: 0;
117+
}`}</style>
118+
<HtmlRenderDocument src={src} title="Edge cards" onContentHeight={contentHeight} />
119+
</>,
120+
{ container },
121+
);
122+
123+
await vi.waitFor(() => expect(contentHeight).toHaveBeenCalled());
124+
const frame = screen.getByTitle("Edge cards").element() as HTMLIFrameElement;
125+
await vi.waitFor(() => {
126+
expect(frame.getBoundingClientRect().height).toBe(contentHeight.mock.lastCall?.[0]);
127+
});
128+
129+
for (const id of ["first", "last"]) {
130+
frame.contentWindow?.postMessage({ type: "select-card", id }, "*");
131+
await vi.waitFor(() => expect(reports.get(id)?.selected).toBe(true));
132+
const bounds = reports.get(id)!;
133+
expect(bounds.ring).toBe(5);
134+
expect(bounds.viewportWidth).toBe(728);
135+
expect(bounds.pageHeight).toBe(contentHeight.mock.lastCall?.[0]);
136+
expect(bounds.viewportHeight).toBe(bounds.pageHeight);
137+
expect(bounds.left).toBeGreaterThanOrEqual(0);
138+
expect(bounds.top).toBeGreaterThanOrEqual(0);
139+
expect(bounds.right).toBeLessThanOrEqual(bounds.viewportWidth);
140+
expect(bounds.bottom).toBeLessThanOrEqual(bounds.viewportHeight);
141+
expect(bounds.scrollWidth).toBeLessThanOrEqual(bounds.viewportWidth);
142+
}
143+
});

‎packages/shared/src/htmlRender.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export const HTML_RENDER_THEME_GUIDE = [
232232
export const HTML_RENDER_LAYOUT_GUIDE = [
233233
`Frame width is reply width (${HTML_RENDER_COLUMN_WIDTH}px desktop, about 360px phones), on the thread background.`,
234234
"Leave html, body, and the outer element with no background color, no outer card, border, banner title, or side padding. This wins over any rule that sets a page background.",
235+
"T3 adds an overridable 8px html gutter for selection rings and shadows. Use fluid widths within it.",
235236
"A box that needs its own background gets 16px padding and var(--radius) corners. Give charts fixed pixel heights.",
236237
"Let content set the height. Do not use vh units or height:100% on html or body. Both are rejected or loop.",
237238
].join(" ");
@@ -353,7 +354,7 @@ export function htmlRenderThemeMessage(theme: HtmlRenderTheme) {
353354
}
354355

355356
const BASE_CSS =
356-
"html{background:var(--background);color:var(--foreground);font-family:var(--font-sans);font-size:14px;line-height:1.5;-webkit-font-smoothing:antialiased;-webkit-text-size-adjust:100%;scrollbar-width:none}" +
357+
"html{box-sizing:border-box;padding:8px;background:var(--background);color:var(--foreground);font-family:var(--font-sans);font-size:14px;line-height:1.5;-webkit-font-smoothing:antialiased;-webkit-text-size-adjust:100%;scrollbar-width:none}" +
357358
"html::-webkit-scrollbar{display:none}body{margin:0}code,kbd,pre,samp{font-family:var(--font-mono)}" +
358359
// Focus outlines paint inside the element, so the frame edge never clips them.
359360
":where(:focus-visible){outline:2px solid var(--ring)}:focus-visible{outline-width:2px!important;outline-offset:-2px!important}" +

0 commit comments

Comments
 (0)