@devries-cameron20 / t3code Mirror · read-only / commits / da94a13e59
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 10:45 UTC

fix(web): tab to equations only when they overflow (#23954)

Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>

Victor Vogel committed Oct 30, 2026 at 16:26 UTC da94a13e59faf41b049c7eb73f99e3663472901f
2 files changed +60 -4
apps/web/src/components/chat/MarkdownMath.browser.test.tsx new
+36
@@ -0,0 +1,36 @@
1 +import { expect, it } from "vite-plus/test";
2 +import { userEvent } from "vite-plus/test/browser";
3 +
4 +import { render } from "../../../test/render";
5 +import "../../index.css";
6 +
7 +import { MarkdownMath } from "./MarkdownMath";
8 +
9 +it("keeps keyboard scrolling available as a streamed equation outgrows its viewport", async () => {
10 + const equation = (tex: string, width = 180) => (
11 + <div style={{ width }}>
12 + <button>Before equation</button>
13 + <MarkdownMath tex={tex} display />
14 + <button>After equation</button>
15 + </div>
16 + );
17 + await using screen = await render(equation("x"));
18 + await screen.getByRole("button", { name: "Before equation" }).click();
19 + await userEvent.keyboard("{Tab}");
20 + await expect.element(screen.getByRole("button", { name: "After equation" })).toHaveFocus();
21 +
22 + await screen.rerender(equation("x_1 + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 + x_8"));
23 + const scroller = screen.getByLabelText("Scroll display equation");
24 + await expect.element(scroller).toHaveAttribute("tabindex", "0");
25 + await screen.getByRole("button", { name: "Before equation" }).click();
26 + await userEvent.keyboard("{Tab}");
27 + await expect.element(scroller).toHaveFocus();
28 + await userEvent.keyboard("{ArrowRight}");
29 + await expect.element(scroller).not.toHaveProperty("scrollLeft", 0);
30 +
31 + await screen.rerender(equation("x_1 + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 + x_8", 1000));
32 + await expect.element(screen.getByLabelText("Scroll display equation")).not.toBeInTheDocument();
33 + await screen.getByRole("button", { name: "Before equation" }).click();
34 + await userEvent.keyboard("{Tab}");
35 + await expect.element(screen.getByRole("button", { name: "After equation" })).toHaveFocus();
36 +});
apps/web/src/components/chat/MarkdownMath.tsx
+24 -4
@@ -1,4 +1,6 @@
1 -import { lazy, memo, Suspense } from "react";
1 +import { lazy, memo, Suspense, useLayoutEffect, useRef, useState } from "react";
2 +
3 +import { observeResize } from "../../lib/observeResize";
4
5 import { RenderErrorBoundary } from "../RenderErrorBoundary";
6
@@ -14,6 +16,20 @@ export const MarkdownMath = memo(function MarkdownMath(props: {
16 display: boolean;
17 focusable?: boolean;
18 }) {
19 + const viewportRef = useRef<HTMLSpanElement>(null);
20 + const contentRef = useRef<HTMLSpanElement>(null);
21 + const [overflows, setOverflows] = useState(false);
22 + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- observes formula DOM width to make overflowing equations keyboard scrollable
23 + useLayoutEffect(() => {
24 + const viewport = viewportRef.current;
25 + const content = contentRef.current;
26 + if (!viewport || !content || props.focusable === false) return;
27 + const measure = () => setOverflows(viewport.scrollWidth > viewport.clientWidth);
28 + measure();
29 + // Typesetting and streamed TeX can change content width without resizing the viewport.
30 + return observeResize([viewport, content], measure);
31 + }, [props.focusable]);
32 + const scrollable = props.focusable !== false && overflows;
33 const tex = props.tex.trim();
34 const source = props.display ? `\\[\n${tex}\n\\]` : `\\(${tex}\\)`;
35 const fallback = <span className="whitespace-pre-wrap">{source}</span>;
@@ -36,17 +52,21 @@ export const MarkdownMath = memo(function MarkdownMath(props: {
52 }
53 >
54 <span
55 + ref={viewportRef}
56 className="block max-w-full overflow-x-auto overflow-y-hidden in-[.chat-markdown-table-container]:max-w-none in-[.chat-markdown-table-container]:overflow-visible px-0.5 py-0.75 focus-visible:outline-1 focus-visible:outline-solid focus-visible:-outline-offset-1 focus-visible:outline-border [&::-webkit-scrollbar]:h-2 [&::-webkit-scrollbar-thumb]:rounded [&::-webkit-scrollbar-thumb]:border [&::-webkit-scrollbar-thumb]:border-transparent [&::-webkit-scrollbar-thumb]:bg-clip-content"
40 - tabIndex={props.focusable === false ? -1 : 0}
57 + tabIndex={scrollable ? 0 : -1}
58 aria-label={
42 - props.focusable === false
59 + !scrollable
60 ? undefined
61 : props.display
62 ? "Scroll display equation"
63 : "Scroll inline equation"
64 }
65 >
49 - <span className={props.display ? "inline-block w-max min-w-full" : "inline-block w-max"}>
66 + <span
67 + ref={contentRef}
68 + className={props.display ? "inline-block w-max min-w-full" : "inline-block w-max"}
69 + >
70 <RenderErrorBoundary resetKeys={[tex]} fallback={fallback}>
71 <Suspense fallback={fallback}>
72 <MathTypeset tex={tex} display={props.display} fallback={fallback} />