feat(web): expose each chat message as a heading for screen readers (#11199)

This commit is contained in:
Khai Shern, Toh authored and GitHub committed 2026-09-12 17:56:20 -07:00
1 parent 68c2277f50
commit 6cdbf76fa4
4 files changed
+78 -2

No files matched your search

@@ -694,6 +694,28 @@ describe("ChatMarkdown artifact-template cards", () => {
});
});
describe("ChatMarkdown heading levels", () => {
it("exposes headings below the host heading without changing their tags", () => {
const html = renderToStaticMarkup(
<ChatMarkdown
cwd="/tmp/project"
text={"# Top\n\n## Section\n\n###### Fine print"}
headingLevelOffset={3}
/>,
);
expect(html).toContain('<h1 aria-level="4">Top</h1>');
expect(html).toContain('<h2 aria-level="5">Section</h2>');
expect(html).toContain('<h6 aria-level="6">Fine print</h6>');
});
it("leaves heading levels alone when the markdown is not nested", () => {
const html = renderToStaticMarkup(<ChatMarkdown cwd="/tmp/project" text="# Top" />);
expect(html).toContain("<h1>Top</h1>");
});
});
describe("shouldUseMarkdownFileBrowserPrimaryAction", () => {
it("uses the browser when it is the only available primary action", () => {
expect(
+37 -1
View File
@@ -71,7 +71,11 @@ import React, {
useState,
type ReactNode,
} from "react";
import type { Components, Options as ReactMarkdownOptions } from "react-markdown";
import type {
Components,
ExtraProps as ReactMarkdownExtraProps,
Options as ReactMarkdownOptions,
} from "react-markdown";
import ReactMarkdown from "react-markdown";
import { toHtml } from "hast-util-to-html";
import { createIncrementalMarkdownPlugin } from "../markdown-incremental";
@@ -216,6 +220,10 @@ interface ChatMarkdownProps {
extraRemarkPlugins?: NonNullable<ReactMarkdownOptions["remarkPlugins"]>;
/** Renders a `t3-context://` link as a chip; without it the link shows its label as text. */
renderContextReference?: ((reference: ChatMarkdownContextReference) => ReactNode) | undefined;
/** Levels added to each markdown heading in the accessibility tree so the
text nests under the heading that introduces it, such as a chat message's
author. Rendered tags and their styling are unchanged. */
headingLevelOffset?: number | undefined;
}
export interface ChatMarkdownContextReference {
@@ -2217,6 +2225,7 @@ function useChatMarkdownState({
imageBaseDir,
onImageExpand,
renderContextReference,
headingLevelOffset = 0,
}: ChatMarkdownProps) {
const { resolvedTheme } = useTheme();
const [localMediaPreview, setLocalMediaPreview] = useState<ExpandedImagePreview | null>(null);
@@ -2615,6 +2624,7 @@ function useChatMarkdownState({
expandMedia,
fileLinkChip,
renderContextReference,
headingLevelOffset,
imageBaseDir,
inlineCodeFileLinkMetaByText,
isStreaming,
@@ -2643,6 +2653,7 @@ function useChatMarkdownState({
expandMedia,
fileLinkChip,
renderContextReference,
headingLevelOffset,
imageBaseDir,
inlineCodeFileLinkMetaByText,
isStreaming,
@@ -2679,8 +2690,33 @@ const ChatMarkdownRendererContext = React.createContext<
ReturnType<typeof useChatMarkdownState>["componentState"]
>(null!);
// Screen readers take a heading's level from its tag, which would let a `#` in a
// message outrank the heading placed above it. Override only the exposed level:
// the tag keeps driving the stylesheet and copy-as-markdown.
function markdownHeadingRenderer(level: 1 | 2 | 3 | 4 | 5 | 6) {
const Tag = `h${level}` as const;
return function MarkdownHeading({
node: _node,
...props
}: ComponentProps<typeof Tag> & ReactMarkdownExtraProps) {
const { headingLevelOffset } = use(ChatMarkdownRendererContext);
return (
<Tag
{...props}
aria-level={headingLevelOffset > 0 ? Math.min(level + headingLevelOffset, 6) : undefined}
/>
);
};
}
// Keep component types stable when streaming changes the message state.
const CHAT_MARKDOWN_COMPONENTS = {
h1: markdownHeadingRenderer(1),
h2: markdownHeadingRenderer(2),
h3: markdownHeadingRenderer(3),
h4: markdownHeadingRenderer(4),
h5: markdownHeadingRenderer(5),
h6: markdownHeadingRenderer(6),
div: function MarkdownDiv({ node, children, ...props }) {
const { onUseArtifactTemplate } = use(ChatMarkdownRendererContext);
const artifactTemplate = artifactTemplateFromHastProperties(node?.properties);
@@ -1455,6 +1455,16 @@ function UserVideoAttachment({ file }: { readonly file: ChatFileAttachment }) {
);
}
// Screen readers skim a transcript by heading, so every message announces its
// author as one. The thread title in ChatHeader is an <h2>; headings written
// inside a message are exposed below this level. Visually hidden and excluded
// from selection so sighted users and copied text are unaffected.
const MESSAGE_HEADING_LEVEL = 3;
function MessageAuthorHeading({ children }: { children: string }) {
return <h3 className="sr-only select-none">{children}</h3>;
}
function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
const ctx = use(TimelineRowCtx);
const { onImageExpand, onFileOpen } = ctx;
@@ -1614,6 +1624,7 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
return (
<div className="group flex flex-col items-end gap-1">
<div className="relative max-w-[80%] rounded-2xl bg-message p-3 text-message-foreground">
<MessageAuthorHeading>You</MessageAuthorHeading>
{(regularImages.length > 0 || userVideos.length > 0) && (
<div className="mb-2 grid max-w-[210px] grid-cols-2 gap-2">
{regularImages.map((image) => (
@@ -1845,6 +1856,7 @@ function AssistantTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "mess
return (
<>
<div className="relative min-w-0 px-1 py-0.5">
<MessageAuthorHeading>T3 Code</MessageAuthorHeading>
<AssistantCitationSource
messageId={row.message.id}
{...(ctx.threadRef ? { threadRef: ctx.threadRef } : {})}
@@ -1859,6 +1871,7 @@ function AssistantTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "mess
isStreaming={Boolean(row.message.streaming)}
lineBreaks={shouldPreserveAssistantLineBreaks(messageText)}
skills={ctx.skills}
headingLevelOffset={MESSAGE_HEADING_LEVEL}
onUseArtifactTemplate={ctx.onUseArtifactTemplate}
onImageExpand={ctx.onImageExpand}
/>
@@ -3214,6 +3227,7 @@ const UserMessageBody = memo(function UserMessageBody(props: {
lineBreaks
parseRawHtml={false}
renderContextReference={props.renderContextReference}
headingLevelOffset={MESSAGE_HEADING_LEVEL}
/>
);
});
@@ -150,7 +150,9 @@ export const ProposedPlanCard = memo(function ProposedPlanCard({
<div className="flex flex-wrap items-center justify-between gap-3">
<div className="flex min-w-0 items-center gap-2">
<Badge variant="secondary">Plan</Badge>
<p className="truncate text-sm font-medium text-foreground">{title}</p>
{/* Same heading level as the message author headings in the timeline,
so a plan's own headings nest beneath it in the outline. */}
<h3 className="truncate text-sm font-medium text-foreground">{title}</h3>
</div>
<Menu>
<MenuTrigger
@@ -177,6 +179,7 @@ export const ProposedPlanCard = memo(function ProposedPlanCard({
cwd={cwd}
threadRef={threadRef}
isStreaming={false}
headingLevelOffset={3}
/>
) : (
<ChatMarkdown
@@ -184,6 +187,7 @@ export const ProposedPlanCard = memo(function ProposedPlanCard({
cwd={cwd}
threadRef={threadRef}
isStreaming={false}
headingLevelOffset={3}
/>
)}
{canCollapse && !expanded ? (