Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/attachments-remove-button-focus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ai-elements": patch
---

Show the AttachmentRemove button on keyboard focus and on touch screens
2 changes: 1 addition & 1 deletion apps/docs/content/components/(chatbot)/attachments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Displays the filename and optional media type.

### `<AttachmentRemove />`

Remove button that appears on hover.
Remove button that appears on hover or keyboard focus. It is always visible on touch screens and in the `list` variant.

<TypeTable
type={{
Expand Down
45 changes: 45 additions & 0 deletions packages/elements/__tests__/attachments.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { render, screen } from "@testing-library/react";

import type { AttachmentData } from "../src/attachments";

import { Attachment, AttachmentRemove, Attachments } from "../src/attachments";

const file: AttachmentData = {
filename: "notes.txt",
id: "file-1",
mediaType: "text/plain",
type: "file",
url: "data:text/plain;base64,aGVsbG8=",
};

describe("attachmentRemove", () => {
it("is visible on keyboard focus and on touch screens in the grid variant", () => {
render(
<Attachments variant="grid">
<Attachment data={file} onRemove={vi.fn()}>
<AttachmentRemove />
</Attachment>
</Attachments>
);

expect(screen.getByRole("button", { name: "Remove" })).toHaveClass(
"focus-visible:opacity-100",
"pointer-coarse:opacity-100"
);
});

it("is visible on keyboard focus and on touch screens in the inline variant", () => {
render(
<Attachments variant="inline">
<Attachment data={file} onRemove={vi.fn()}>
<AttachmentRemove />
</Attachment>
</Attachments>
);

expect(screen.getByRole("button", { name: "Remove" })).toHaveClass(
"focus-visible:opacity-100",
"pointer-coarse:opacity-100"
);
});
});
4 changes: 2 additions & 2 deletions packages/elements/src/attachments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,13 @@ export const AttachmentRemove = ({
variant === "grid" && [
"absolute top-2 right-2 size-6 rounded-full p-0",
"bg-background/80 backdrop-blur-sm",
"opacity-0 transition-opacity group-hover:opacity-100",
"opacity-0 transition-opacity group-hover:opacity-100 focus-visible:opacity-100 pointer-coarse:opacity-100",
"hover:bg-background",
"[&>svg]:size-3",
],
variant === "inline" && [
"size-5 rounded p-0",
"opacity-0 transition-opacity group-hover:opacity-100",
"opacity-0 transition-opacity group-hover:opacity-100 focus-visible:opacity-100 pointer-coarse:opacity-100",
"[&>svg]:size-2.5",
],
variant === "list" && ["size-8 shrink-0 rounded p-0", "[&>svg]:size-4"],
Expand Down