Skip to content

Commit da94a13

Browse files
only21milcodexmaria-rcks
authored
fix(web): tab to equations only when they overflow (#23954)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 714f849 commit da94a13

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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";
24

35
import { RenderErrorBoundary } from "../RenderErrorBoundary";
46

@@ -14,6 +16,20 @@ export const MarkdownMath = memo(function MarkdownMath(props: {
1416
display: boolean;
1517
focusable?: boolean;
1618
}) {
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;
1733
const tex = props.tex.trim();
1834
const source = props.display ? `\\[\n${tex}\n\\]` : `\\(${tex}\\)`;
1935
const fallback = <span className="whitespace-pre-wrap">{source}</span>;
@@ -36,17 +52,21 @@ export const MarkdownMath = memo(function MarkdownMath(props: {
3652
}
3753
>
3854
<span
55+
ref={viewportRef}
3956
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}
4158
aria-label={
42-
props.focusable === false
59+
!scrollable
4360
? undefined
4461
: props.display
4562
? "Scroll display equation"
4663
: "Scroll inline equation"
4764
}
4865
>
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+
>
5070
<RenderErrorBoundary resetKeys={[tex]} fallback={fallback}>
5171
<Suspense fallback={fallback}>
5272
<MathTypeset tex={tex} display={props.display} fallback={fallback} />

0 commit comments

Comments
 (0)