Skip to content
TypeScript

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

Victor Vogelda94a13e59fa
diff --git a/apps/web/src/components/chat/MarkdownMath.browser.test.tsx b/apps/web/src/components/chat/MarkdownMath.browser.test.tsx
new file mode 100644
index 00000000000..ec5b38541dd
--- /dev/null
+++ b/apps/web/src/components/chat/MarkdownMath.browser.test.tsx
@@ -0,0 +1,36 @@
+import { expect, it } from "vite-plus/test";
+import { userEvent } from "vite-plus/test/browser";
+
+import { render } from "../../../test/render";
+import "../../index.css";
+
+import { MarkdownMath } from "./MarkdownMath";
+
+it("keeps keyboard scrolling available as a streamed equation outgrows its viewport", async () => {
+ const equation = (tex: string, width = 180) => (
+ <div style={{ width }}>
+ <button>Before equation</button>
+ <MarkdownMath tex={tex} display />
+ <button>After equation</button>
+ </div>
+ );
+ await using screen = await render(equation("x"));
+ await screen.getByRole("button", { name: "Before equation" }).click();
+ await userEvent.keyboard("{Tab}");
+ await expect.element(screen.getByRole("button", { name: "After equation" })).toHaveFocus();
+
+ await screen.rerender(equation("x_1 + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 + x_8"));
+ const scroller = screen.getByLabelText("Scroll display equation");
+ await expect.element(scroller).toHaveAttribute("tabindex", "0");
+ await screen.getByRole("button", { name: "Before equation" }).click();
+ await userEvent.keyboard("{Tab}");
+ await expect.element(scroller).toHaveFocus();
+ await userEvent.keyboard("{ArrowRight}");
+ await expect.element(scroller).not.toHaveProperty("scrollLeft", 0);
+
+ await screen.rerender(equation("x_1 + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 + x_8", 1000));
+ await expect.element(screen.getByLabelText("Scroll display equation")).not.toBeInTheDocument();
+ await screen.getByRole("button", { name: "Before equation" }).click();
+ await userEvent.keyboard("{Tab}");
+ await expect.element(screen.getByRole("button", { name: "After equation" })).toHaveFocus();
+});
diff --git a/apps/web/src/components/chat/MarkdownMath.tsx b/apps/web/src/components/chat/MarkdownMath.tsx
index 7f6af570a32..5a90e81e9ce 100644
--- a/apps/web/src/components/chat/MarkdownMath.tsx
+++ b/apps/web/src/components/chat/MarkdownMath.tsx
@@ -1,4 +1,6 @@
-import { lazy, memo, Suspense } from "react";
+import { lazy, memo, Suspense, useLayoutEffect, useRef, useState } from "react";
+
+import { observeResize } from "../../lib/observeResize";
import { RenderErrorBoundary } from "../RenderErrorBoundary";
@@ -14,6 +16,20 @@ export const MarkdownMath = memo(function MarkdownMath(props: {
display: boolean;
focusable?: boolean;
}) {
+ const viewportRef = useRef<HTMLSpanElement>(null);
+ const contentRef = useRef<HTMLSpanElement>(null);
+ const [overflows, setOverflows] = useState(false);
+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- observes formula DOM width to make overflowing equations keyboard scrollable
+ useLayoutEffect(() => {
+ const viewport = viewportRef.current;
+ const content = contentRef.current;
+ if (!viewport || !content || props.focusable === false) return;
+ const measure = () => setOverflows(viewport.scrollWidth > viewport.clientWidth);
+ measure();
+ // Typesetting and streamed TeX can change content width without resizing the viewport.
+ return observeResize([viewport, content], measure);
+ }, [props.focusable]);
+ const scrollable = props.focusable !== false && overflows;
const tex = props.tex.trim();
const source = props.display ? `\\[\n${tex}\n\\]` : `\\(${tex}\\)`;
const fallback = <span className="whitespace-pre-wrap">{source}</span>;
@@ -36,17 +52,21 @@ export const MarkdownMath = memo(function MarkdownMath(props: {
}
>
<span
+ ref={viewportRef}
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"
- tabIndex={props.focusable === false ? -1 : 0}
+ tabIndex={scrollable ? 0 : -1}
aria-label={
- props.focusable === false
+ !scrollable
? undefined
: props.display
? "Scroll display equation"
: "Scroll inline equation"
}
>
- <span className={props.display ? "inline-block w-max min-w-full" : "inline-block w-max"}>
+ <span
+ ref={contentRef}
+ className={props.display ? "inline-block w-max min-w-full" : "inline-block w-max"}
+ >
<RenderErrorBoundary resetKeys={[tex]} fallback={fallback}>
<Suspense fallback={fallback}>
<MathTypeset tex={tex} display={props.display} fallback={fallback} />