← All proposals

Chat Attachment

Net-new component: one attached item for chat.

Net-new componentOpen in Storybook

proposals/ChatAttachment/ChatAttachment.mdx45 lines · View on GitHub
import { Canvas, Controls, Meta, Title } from '@storybook/addon-docs/blocks';

import * as ChatAttachmentStories from './ChatAttachment.stories';

<Meta of={ChatAttachmentStories} name='Usage' />

<Title />

## Purpose

One attached file or image in a chat. It goes in the `fieldHeader` slot of `Composer`, or anywhere a message shows its attachments.

## Usage

- File chips are a fixed width. A long name is shortened in the middle so the extension stays visible; hover or focus shows the full name.
- Images are a square thumbnail. When there is no preview, an image icon is shown instead.
- `status='error'` marks a failed attachment (critical colors, exclamation icon); pass `errorMessage` for the reason, which the tooltip and screen readers get. The sentence telling the user what to do belongs in the screen, for example `Composer`'s helper message.
- The remove button shows on hover and keyboard focus, and always on touch screens. Leave out `onRemove` for attachments that cannot be removed, such as in a sent message. When you set `onRemove`, also set `removeLabel` (for example "Remove report.md"); without it the button has no accessible name.

Do not use it for:

- Choosing or dropping files. Use `FileUpload`, the form field with a drop zone.
- A short label or category on an item. Use `Tag`.

## Accessibility

- The full file name is always in the DOM as visually hidden text. The shortened text on screen is hidden from screen readers, so the name is read once, in full. An image thumbnail uses the file name as its alt text.
- With `status='error'`, `errorMessage` is read as plain text before the name when the user reaches the chip. The chip does not announce the error on its own (no live region). The screen owns the one live announcement, for example `Composer`'s helper message.
- The remove button is a real button named by `removeLabel`. It is hidden until hover, keyboard focus on the chip, or a touch device, and it stays in the tab order while hidden, so keyboard users reach it and see it appear.
- The tooltip with the full name opens on hover and on focus. A chip with no `onRemove` has nothing focusable, so the tooltip is hover-only for keyboard users; the full name is still in the text.
- Not checked: the a11y panel, and contrast in light and dark.

## Implementation

See the table below for the available props. The other stories show every variant and state.

- `fileName` — Name of the file. Shown in full in the tooltip and to screen readers; the visible text is shortened in the middle when it is long.
- `fileType` — What kind of file this is: `file` (default) shows an icon and the name; `image` shows a square thumbnail, or an image icon when there is no `previewUrl`.
- `previewUrl` — URL of the image to show as a thumbnail. Only used when `fileType` is `image`.

<hr />

<Canvas of={ChatAttachmentStories.Default} />
<Controls of={ChatAttachmentStories.Default} />