fix(site/src/pages/AgentsPage/components): keep hidden attachment controls tabbable and click-through (#31366)

On `main`, the attachment controls that show on hover or focus mishandle
keyboard and pointer input:

* The transcript's Download link hides with `visibility`, so Shift+Tab
skips it. Tab from View reaches it only because focus on View shows it
first.
* The composer's hidden Remove button overhangs the top of its chip.
Pointing at the overhang lands on the invisible button, which shows the
buttons, and a click there hits Remove.
* WebKit gives each hidden Download link a compositing layer for its
backdrop blur.

The composer's attachment card and the transcript's attachment frame now
set custom properties from their own hover and `focus-within`, and their
controls read them for opacity, pointer events and, on the Download
link, the blur. Hidden controls stay in the tab order in both directions
and take no pointer input. The blur applies only while the link shows
and fades with it, so hidden links add no compositing layers: with four
hidden links in view, WebKit composites 5 layers on `main` and 1 here.
Devices without hover now show the composer's buttons at rest, as the
transcript already did for the Download link.

The new stories capture each focus state, including Shift+Tab onto the
hidden Download link, Remove and Paste inline.

> 🤖 This PR was created with the help of Coder Agents.
This commit is contained in:
Mathias Fredriksson authored and GitHub committed 2026-11-26 13:20:37 +02:00
1 parent c69a58e596
commit 13e1f466f7
4 files changed
+61 -18

No files matched your search

@@ -41,6 +41,41 @@ export const SingleImage: Story = {
},
};
// Focus on View, Paste inline or Remove shows Paste inline and Remove. The
// hidden buttons stay in the tab order, so Shift+Tab from the end of the page
// reaches Remove and then Paste inline.
export const ViewFocused: Story = {
args: (() => {
const file = createMockFile("clipboard.txt", "text/plain", 2048);
return {
attachments: [file],
uploadStates: new Map<File, UploadState>([
[file, { status: "uploaded", fileId: "file-1" }],
]),
previewUrls: new Map<File, string>(),
textContents: new Map<File, string>([[file, "Pasted text."]]),
};
})(),
play: async () => {
await userEvent.tab();
},
};
export const PasteFocused: Story = {
args: ViewFocused.args,
play: async () => {
await userEvent.tab({ shift: true });
await userEvent.tab({ shift: true });
},
};
export const RemoveFocused: Story = {
args: ViewFocused.args,
play: async () => {
await userEvent.tab({ shift: true });
},
};
export const MultipleImages: Story = {
args: (() => {
const files = [
@@ -139,7 +139,8 @@ export const AttachmentPreview: React.FC<{
// Key combines file metadata with index as a fallback for
// duplicate names. Acceptable for a small, append-only list.
key={`${file.name}-${file.size}-${file.lastModified}-${index}`}
className="group relative"
// Not `group-focus-within`, which slows WebKit focus changes.
className="relative [--actions-opacity:0] [--actions-pointer-events:none] hover:[--actions-opacity:1] hover:[--actions-pointer-events:auto] focus-within:[--actions-opacity:1] focus-within:[--actions-pointer-events:auto] [@media(hover:none)]:[--actions-opacity:1] [@media(hover:none)]:[--actions-pointer-events:auto]"
>
{isRasterImageMediaType(file.type) && previewUrl ? (
<button
@@ -183,7 +184,7 @@ export const AttachmentPreview: React.FC<{
);
onInlineText(file, nextContent);
}}
className="absolute -bottom-2 -right-2 flex size-6 cursor-pointer items-center justify-center rounded-full bg-surface-primary text-content-secondary shadow-xs opacity-0 transition-opacity hover:bg-surface-secondary hover:text-content-primary group-hover:opacity-100 group-focus-within:opacity-100 focus:opacity-100"
className="absolute -bottom-2 -right-2 flex size-6 cursor-pointer items-center justify-center rounded-full bg-surface-primary text-content-secondary opacity-(--actions-opacity) shadow-xs transition-opacity [pointer-events:var(--actions-pointer-events)] hover:bg-surface-secondary hover:text-content-primary"
aria-label="Paste inline"
>
<ClipboardPasteIcon aria-hidden="true" className="size-3.5" />
@@ -217,7 +218,7 @@ export const AttachmentPreview: React.FC<{
<button
type="button"
onClick={() => onRemove(file)}
className="absolute -right-2 -top-2 flex size-6 cursor-pointer items-center justify-center rounded-full bg-surface-primary text-content-secondary shadow-xs opacity-0 transition-opacity hover:bg-surface-secondary hover:text-content-primary group-hover:opacity-100 group-focus-within:opacity-100 focus:opacity-100"
className="absolute -right-2 -top-2 flex size-6 cursor-pointer items-center justify-center rounded-full bg-surface-primary text-content-secondary opacity-(--actions-opacity) shadow-xs transition-opacity [pointer-events:var(--actions-pointer-events)] hover:bg-surface-secondary hover:text-content-primary"
aria-label={`Remove ${file.name}`}
>
<XIcon aria-hidden="true" className="size-3.5" />
@@ -170,7 +170,8 @@ export const getAttachmentDownload = (
const DownloadOverlay: React.FC<AttachmentDownload> = (download) => (
<AttachmentDownloadLink
{...download}
className="invisible absolute right-1 top-1 flex size-6 items-center justify-center rounded bg-surface-primary/80 text-content-secondary opacity-0 shadow-xs backdrop-blur-xs transition-opacity hover:text-content-primary group-hover/attachment:visible group-hover/attachment:opacity-100 group-focus-within/attachment:visible group-focus-within/attachment:opacity-100 [@media(hover:none)]:visible [@media(hover:none)]:opacity-100 focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-content-link"
// Blur only while shown: a hidden link with a backdrop filter still gets a compositing layer.
className="absolute right-1 top-1 flex size-6 items-center justify-center rounded bg-surface-primary/80 text-content-secondary opacity-(--download-opacity) shadow-xs [backdrop-filter:var(--download-backdrop-filter)] transition-[opacity,backdrop-filter] [pointer-events:var(--download-pointer-events)] hover:text-content-primary focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-content-link"
>
<DownloadIcon aria-hidden="true" className="size-3.5" />
</AttachmentDownloadLink>
@@ -184,7 +185,8 @@ const AttachmentPreviewFrame: React.FC<{
children: React.ReactNode;
}> = ({ href, displayName, downloadName, mediaType, children }) => {
return (
<div className="group/attachment relative inline-flex flex-col items-start">
// Not `group-focus-within`, which slows WebKit focus changes, or `visibility`, which makes Shift+Tab skip the link.
<div className="relative inline-flex flex-col items-start [--download-opacity:0] [--download-pointer-events:none] [--download-backdrop-filter:none] hover:[--download-opacity:1] hover:[--download-pointer-events:auto] hover:[--download-backdrop-filter:blur(var(--blur-xs))] focus-within:[--download-opacity:1] focus-within:[--download-pointer-events:auto] focus-within:[--download-backdrop-filter:blur(var(--blur-xs))] [@media(hover:none)]:[--download-opacity:1] [@media(hover:none)]:[--download-pointer-events:auto] [@media(hover:none)]:[--download-backdrop-filter:blur(var(--blur-xs))]">
{children}
<DownloadOverlay
href={href}
@@ -1386,22 +1386,27 @@ export const AssistantMessageWithImage: Story = {
"src",
`data:image/png;base64,${MockPngBase64}`,
);
expect(
canvas.queryByRole("link", { name: "Download generated-image.png" }),
).not.toBeInTheDocument();
expect(
canvas.queryByRole("button", { name: "Copy message" }),
).not.toBeInTheDocument();
const viewButton = canvas.getByRole("button", {
name: "View generated-image.png",
});
viewButton.focus();
expect(viewButton).toHaveFocus();
await waitFor(() => {
expect(
canvas.getByRole("link", { name: "Download generated-image.png" }),
).toBeVisible();
});
},
};
// Focus on View shows the Download link.
export const AssistantMessageWithImageViewFocused: Story = {
args: AssistantMessageWithImage.args,
play: ({ canvasElement }) => {
const canvas = within(canvasElement);
canvas.getByRole("button", { name: "View generated-image.png" }).focus();
},
};
// The hidden link stays in the tab order, so Shift+Tab from the end of the
// page reaches it.
export const AssistantMessageWithImageDownloadFocused: Story = {
args: AssistantMessageWithImage.args,
play: async () => {
await userEvent.tab({ shift: true });
},
};