<!-- generated by scripts/gen-docs.mjs from docs/system/file-type-icon.md. Edit the source and rerun. -->
# FileTypeIcon

## Description
Tells a person what kind of file they are looking at, such as a PDF or a spreadsheet, and shows in the same place how far the file's upload has gone or that it failed.

`import { FileTypeIcon } from "@/components/cobalt/ui/file-type-icon"`, source `registry/cobalt/ui/file-type-icon.tsx`, status `ready`.
Foundation: `hand-rolled`. Traps checked: `trap/illustration-announced`, `trap/color-only-status`, `trap/loading-layout-shift`, `trap/reduced-motion-ignored`, `trap/motion-layout-property`, `trap/loop-offscreen`, `trap/demo-on-timers`, `trap/numbers-shift` (n/a: the glyph draws no figures; the share is only in `aria-valuenow`), `trap/loader-small-dots` (n/a: there are no dots; an unknown share breathes the whole sheet).

`trap/illustration-announced` is answered by `aria-hidden` on the glyph when it is done or failed, and by one `progressbar` named by `label` while it uploads (ARIA). `trap/color-only-status` by a mark for every kind but the plain file, by the retry arrow for a failed upload, and by the file's name beside the glyph in every use (States). `trap/loading-layout-shift` by one box in every state, 40 by 40px at `size-10` whether done, uploading or failed, measured on the stress page (States). `trap/reduced-motion-ignored` by `motion-reduce:animate-none` on the breathing grey and by the system's reduced-motion rule, which leaves the rising colour no transform transition (Motion). `trap/motion-layout-property` by a `translateY` on the clip that shows the colour, never a height (Motion). `trap/loop-offscreen` is not answered: the breathing is a CSS animation that runs for as long as the share is unknown, on screen or off; only its opacity changes (Motion). `trap/demo-on-timers` by the uploading example, whose 300ms timer stands in for the transport's progress reports, which are the component's input, and never for its animation (Example files).

The three gaps this page once named are closed. A specific MIME type now decides the kind before the extension, so `clip.webm` sent as `audio/webm` is audio; a generic one lets the extension decide (Props). The breathing grey now runs from 45 to 22.5 percent opacity, measured in both themes, where it ran from 45 to 50 (Motion). And the share is rounded down, so 99.6 draws and reads 99 until the host passes `done` (State precedence).

### Foundation
Not applicable: hand-rolled.

| Given by the foundation | Added by the team |
|---|---|
| Not applicable: hand-rolled, an inline `svg` ported from Tag's `FileIcon.svelte`: the sheet with its folded corner on a 32-unit grid, the white marks, the word PDF at 7.2 units, `aria-hidden`; and from Tag's `file-kinds.ts` the ten kinds, their order and their colours | The colours read from the `--file-*` tokens and the marks from `--file-ink`, so a derived system changes them in its token file (D41); the size from `className`, 32px (`size-8`) by default; `fileKind` in `@/lib/cobalt/messages`, with Tag's patterns less `.svelte` and `.vue`; the upload drawn on the sheet from an `Upload`: grey at 45 percent with the kind's colour rising from the bottom to the share, rounded down, the grey breathing while the share is not known, through a pulse on a group around the grey sheet, so it reaches half the sheet's 45 percent, and the retry arrow in place of the mark when it failed; `role="progressbar"` named by `label`, else by the file's name, while it uploads |

## Examples
Ten files by name, one for each kind, each in its kind's colour with its mark, and a file with no extension in grey.

Real uses, 0 call sites (`rg -n "<FileTypeIcon\b" app registry --glob '!registry/cobalt/ui/*'`): every use sits in `registry/cobalt/ui`, the component's own folder, which the count leaves out.

Uses inside the registry:

- A file as a tile: FileTile's glyph for a file with no picture, `size-8` when compact and `size-10` otherwise, at half opacity for a refused file (`registry/cobalt/ui/file-tile.tsx:174-192`). Every file tile in the system is a FileTile: a file in a message (MessageFile, the `bubble` variant), an attachment in the composer (compact) and a row of FileDrop.
- The lightbox's bar: the glyph at `size-6` beside the file's name, inside an `aria-hidden` span (`registry/cobalt/ui/lightbox.tsx:211-212`).

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/file-type-icon/default.tsx` | default | Ten files by name, one for each kind, and a file with no extension in grey |
| `docs/system/examples/file-type-icon/default.tsx` | state:done | Each glyph at rest, in its kind's colour with its white mark |
| `docs/system/examples/file-type-icon/uploading.tsx` | state:uploading | A spreadsheet whose colour rises in steps of 10 percent every 300ms and then shows done; Upload again starts it over |
| `docs/system/examples/file-type-icon/uploading.tsx` | state:share-unknown | A PDF uploading with no share yet, its grey sheet breathing |
| `docs/system/examples/file-type-icon/failed.tsx` | state:failed | The same PDF done and failed, the failed one grey with the retry arrow |
| `docs/system/examples/file-type-icon/in-item.tsx` | composition:Item | Three files as list rows, the glyph at 36px as each row's media |

Example: Ten files by name, one for each kind, and a file with no extension in grey (`docs/system/examples/file-type-icon/default.tsx`)

```tsx
import { FileTypeIcon } from "@/components/cobalt/ui/file-type-icon";

const files = [
  "Brief.pdf",
  "Memo.docx",
  "Fund.xlsx",
  "Deck.pptx",
  "Photo.jpg",
  "Call.m4a",
  "Tour.mp4",
  "Export.zip",
  "Loader.ts",
  "Readme",
];

export default function Example() {
  return (
    <ul aria-label="File kinds" className="grid w-full max-w-lg grid-cols-3 sm:grid-cols-5 gap-x-6 gap-y-3 text-sm">
      {files.map((name) => (
        <li key={name} className="flex min-w-0 flex-col items-center gap-1.5 text-center">
          <FileTypeIcon name={name} className="size-10" />
          <span className="max-w-full truncate text-xs text-muted-foreground">{name}</span>
        </li>
      ))}
    </ul>
  );
}
```

Example: Three files as list rows, the glyph at 36px as each row's media (`docs/system/examples/file-type-icon/in-item.tsx`)

```tsx
import { FileTypeIcon } from "@/components/cobalt/ui/file-type-icon";
import { Item, ItemActions, ItemContent, ItemDescription, ItemGroup, ItemMedia, ItemTitle } from "@/components/cobalt/ui/item";

const files = [
  { name: "Form ADV Part 2A.pdf", about: "Filed by Harbour Capital", when: "Mar 3" },
  { name: "Q3 holdings.xlsx", about: "Shared by Ada Park", when: "Oct 2" },
  { name: "Due diligence notes.docx", about: "Edited by Tomás Reyes", when: "Sep 28" },
];

export default function Example() {
  return (
    <ItemGroup variant="card" render={<ul aria-label="Recent files" />} className="w-full max-w-md">
      {files.map((file) => (
        <Item key={file.name} render={<li />}>
          <ItemMedia>
            <FileTypeIcon name={file.name} className="size-9" />
          </ItemMedia>
          <ItemContent>
            <ItemTitle>{file.name}</ItemTitle>
            <ItemDescription>{file.about}</ItemDescription>
          </ItemContent>
          <ItemActions>{file.when}</ItemActions>
        </Item>
      ))}
    </ItemGroup>
  );
}
```

## Variants
Not applicable: FileTypeIcon has no variant prop. The kind comes from `name` and `mime` through `fileKind`, and the size from `className`.

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| done | no `upload`, or `upload={{ status: "done" }}` | read the kind beside the file's name | the kind's white mark: three lines for a document, a grid for a spreadsheet, rising bars for slides, a hill and a sun for an image, angle brackets for code, a zip line for an archive, a waveform for audio, a play triangle for video and the word PDF for a PDF; a file of no known kind has the fold alone | test |
| uploading | `upload={{ status: "uploading", progress }}` | wait | the sheet grey at 45 percent with the kind's colour rising from the bottom to the share, rounded down to a whole number; `role="progressbar"` with `aria-valuenow` | test |
| share-unknown | `upload={{ status: "uploading" }}` with no `progress` | wait | the grey sheet breathing between 45 and 22.5 percent opacity on a 2 second loop (measured every frame in light and dark), with the kind's mark; `role="progressbar"` with no `aria-valuenow` | test |
| failed | `upload={{ status: "failed" }}` | retry through the part that holds the glyph, such as FileTile's or the composer's Retry button | the sheet grey at 45 percent and the retry arrow in place of the kind's mark | test |

Example: Each glyph at rest, in its kind's colour with its white mark (`docs/system/examples/file-type-icon/default.tsx`)

```tsx
import { FileTypeIcon } from "@/components/cobalt/ui/file-type-icon";

const files = [
  "Brief.pdf",
  "Memo.docx",
  "Fund.xlsx",
  "Deck.pptx",
  "Photo.jpg",
  "Call.m4a",
  "Tour.mp4",
  "Export.zip",
  "Loader.ts",
  "Readme",
];

export default function Example() {
  return (
    <ul aria-label="File kinds" className="grid w-full max-w-lg grid-cols-3 sm:grid-cols-5 gap-x-6 gap-y-3 text-sm">
      {files.map((name) => (
        <li key={name} className="flex min-w-0 flex-col items-center gap-1.5 text-center">
          <FileTypeIcon name={name} className="size-10" />
          <span className="max-w-full truncate text-xs text-muted-foreground">{name}</span>
        </li>
      ))}
    </ul>
  );
}
```

Example: A spreadsheet whose colour rises in steps of 10 percent every 300ms and then shows done; Upload again starts it over (`docs/system/examples/file-type-icon/uploading.tsx`)

```tsx
import { useEffect, useState } from "react";

import { Button } from "@/components/cobalt/ui/button";
import { FileTypeIcon } from "@/components/cobalt/ui/file-type-icon";

export default function Example() {
  const [run, setRun] = useState(0);
  const [share, setShare] = useState(0);
  useEffect(() => {
    setShare(0);
    const timer = setInterval(() => setShare((now) => Math.min(100, now + 10)), 300);
    return () => clearInterval(timer);
  }, [run]);
  return (
    <div className="flex w-full flex-col items-start">
      <div className="flex items-end gap-6">
        <FileTypeIcon
          name="Holdings.xlsx"
          upload={share >= 100 ? { status: "done" } : { status: "uploading", progress: share }}
          label="Uploading Holdings.xlsx"
          className="size-12"
        />
        <FileTypeIcon name="Brochure.pdf" upload={{ status: "uploading" }} label="Uploading Brochure.pdf" className="size-12" />
      </div>
      {/* Docs control, apart from the product: plays the upload again. */}
      <div className="mt-4 flex flex-wrap items-center gap-2 self-stretch border-t border-dashed pt-3">
        <Button variant="secondary" size="sm" onClick={() => setRun((n) => n + 1)}>
          Upload again
        </Button>
      </div>
    </div>
  );
}
```

Example: A PDF uploading with no share yet, its grey sheet breathing (`docs/system/examples/file-type-icon/uploading.tsx`)

```tsx
import { useEffect, useState } from "react";

import { Button } from "@/components/cobalt/ui/button";
import { FileTypeIcon } from "@/components/cobalt/ui/file-type-icon";

export default function Example() {
  const [run, setRun] = useState(0);
  const [share, setShare] = useState(0);
  useEffect(() => {
    setShare(0);
    const timer = setInterval(() => setShare((now) => Math.min(100, now + 10)), 300);
    return () => clearInterval(timer);
  }, [run]);
  return (
    <div className="flex w-full flex-col items-start">
      <div className="flex items-end gap-6">
        <FileTypeIcon
          name="Holdings.xlsx"
          upload={share >= 100 ? { status: "done" } : { status: "uploading", progress: share }}
          label="Uploading Holdings.xlsx"
          className="size-12"
        />
        <FileTypeIcon name="Brochure.pdf" upload={{ status: "uploading" }} label="Uploading Brochure.pdf" className="size-12" />
      </div>
      {/* Docs control, apart from the product: plays the upload again. */}
      <div className="mt-4 flex flex-wrap items-center gap-2 self-stretch border-t border-dashed pt-3">
        <Button variant="secondary" size="sm" onClick={() => setRun((n) => n + 1)}>
          Upload again
        </Button>
      </div>
    </div>
  );
}
```

Example: The same PDF done and failed, the failed one grey with the retry arrow (`docs/system/examples/file-type-icon/failed.tsx`)

```tsx
import { FileTypeIcon } from "@/components/cobalt/ui/file-type-icon";

export default function Example() {
  return (
    <div className="flex items-end gap-6">
      <FileTypeIcon name="Side letter.pdf" className="size-12" />
      <FileTypeIcon name="Side letter.pdf" upload={{ status: "failed" }} className="size-12" />
    </div>
  );
}
```

### State precedence
- Uploading and failed: Not applicable: `upload` holds one status at a time, and `nextUpload` ignores a progress report that comes after a failure.
- Uploading at a share of 99.5 or more, and done: uploading wins. The share is rounded down, so 99.6 draws a sheet 99 percent coloured with its top still grey and reads `aria-valuenow="99"`; only `done` draws it whole (test).
- Failed and the kind's mark: failed wins. The mark goes, with the word PDF, the image's sun and the video's triangle, and the retry arrow takes its place.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| the share grows while uploading | announce | `duration-settle ease-out`, `--t-settle` 300ms on `--curve` | `transform`, a `translateY` of the clip that shows the kind's colour | the system's reduced-motion rule in `registry/cobalt/base.css` keeps transitions to colour and opacity, so the colour steps to each new share at once |
| the share is not known | announce | Tailwind's `animate-pulse`, 2 seconds on `cubic-bezier(0.4, 0, 0.6, 1)`, looping | `opacity` of a group around the grey sheet, from 1 to 0.5, so the sheet runs from 0.45 to 0.225, measured | `motion-reduce:animate-none`; the grey holds at 45 percent |

## Props

<!-- Props generated from registry/cobalt/ui/file-type-icon.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `name` | `string` |  | The file's name, whose extension says its kind when there is no MIME type. |
| `className?` | `string` |  |  |
| `label?` | `string` |  | The progress bar's name while it uploads: "Uploading Q3 holdings.xlsx". |
| `mime?` | `string` |  |  |
| `upload?` | `Upload` |  | Its upload, from `nextUpload`, drawn on the sheet. |

`name` is the file's name; its extension names the kind when `mime` is generic or names no kind. `mime` is the file's MIME type, such as `File.type`. `upload` is an `Upload` from `@/lib/cobalt/messages`, as `nextUpload` returns it: `{ status: "uploading", progress }` with `progress` from 0 to 100, or left out while it is unknown; `{ status: "failed" }`; or `{ status: "done" }`. Leaving `upload` out is the same as done, and a share outside 0 to 100 is clamped. `label` names the progress bar while it uploads and is unused otherwise. `className` sets the size, such as `size-10`, and placement; nothing else passes through to the `svg`.

`fileKind` knows ten kinds: image (`image/*`, and png, jpg, jpeg, gif, webp, heic, svg, avif), video (`video/*`, and mp4, mov, webm, m4v), audio (`audio/*`, and mp3, wav, m4a, aac, ogg, flac), pdf, spreadsheet (xls, xlsx, csv, tsv, numbers), presentation (ppt, pptx, key), archive (zip, tar, gz, tgz, 7z, rar), code (JavaScript, TypeScript, Python, Ruby, Go, Rust, Java, Kotlin, Swift, C, C++, C#, JSON, YAML, TOML, shell, HTML, CSS and SQL), document (doc, docx, pages, rtf, txt, md, markdown, and `text/*`), and file for anything else. A specific MIME type decides first: `photo.png` sent as `application/pdf` is a PDF and `clip.webm` sent as `audio/webm` is audio. A generic type, `application/octet-stream`, `text/plain` or none, lets the extension decide, so `Q3.xlsx` as `application/octet-stream` is a spreadsheet and `deploy.py` as `text/plain` is code, and is read only when the name has no extension it knows, so `notes` as `text/plain` is a document. A specific type it does not know also leaves the extension to decide. One misreading is undone by name: `app.ts` typed `video/mp2t` by its operating system is code (tests in `registry/cobalt/lib/messages.test.ts` and `file-type-icon.test.tsx`).

## Usage

### When to use
- A file sits beside its name in a list, a message, an attachment or a bar, and the reader should see its kind at a glance.
- A file is uploading or failed to upload, and the glyph beside its name should show it.

### When not to use
- The whole file row, with its name, its size or upload line and its actions. Use FileTile instead, which draws this glyph with them.
- The file is a picture the reader should see. Use FileTile instead, with `src`, which draws the picture as its own thumbnail.
- The place where files are chosen or dropped. Use FileDrop instead.

### Rules
- `rule/file-type-icon-pass-the-mime`: When the file's MIME type is known, such as `File.type` from a chooser or a drop, pass it as `mime` beside `name`, because a name with no extension, such as "Readme" or ".env", is drawn as a grey file of no known kind, and the MIME type can name its kind. Evidence: measured 2/2 names with no extension, "noextension" and ".env", drawn with `data-kind="file"` on docs/system/stress/file-type-icon.tsx; app 4/4 uses inside the registry that hold a MIME type pass it as `mime` (MessageFile, FileTile and both composer uses), since the lightbox's items carry none. Check: review of every `FileTypeIcon` that draws a `File` without `mime`.
  - Don't: `<FileTypeIcon name={file.name} />` for a `File` from a drop
  - Do: `<FileTypeIcon name={file.name} mime={file.type} />`
- `rule/file-type-icon-retry-is-a-button`: When an upload fails, wrap the glyph in a `button` with its own `aria-label`, such as "Retry Q3.xlsx", instead of a click handler on an element around it, because the glyph is `aria-hidden` and takes no focus, so a keyboard or a screen reader reaches the retry only through a named button. Evidence: single use registry/cobalt/ui/file-tile.tsx:227-230, FileTile's named Retry button, the one failed glyph in the registry since MessageFile, the composer and FileDrop draw FileTile; principle wcag: 2.1.1 Keyboard and 4.1.2 Name, role, value. Check: review of every `FileTypeIcon` with `upload={{ status: "failed" }}`.
  - Don't: `<span onClick={retry}><FileTypeIcon name={name} upload={{ status: "failed" }} /></span>`
  - Do: `<button type="button" aria-label={"Retry " + name} onClick={retry}><FileTypeIcon name={name} upload={{ status: "failed" }} /></button>`

### Content
- `rule/file-type-icon-upload-label`: When passing `upload` with the status "uploading", set `label` to "Uploading {name}", the verb and the whole file name in sentence case, because while it uploads the glyph is a progress bar and `label` is its name, read with the share. Evidence: app 3/3 uploading uses inside the registry name it so (MessageFile's and FileTile's `labels.uploading`, the composer's `uploadingLabel` and the name); principle wcag: 4.1.2 Name, role, value. Check: review of every `label` passed with an uploading `upload`; test `file-type-icon.test.tsx` expects `label` as the progress bar's `aria-label`.
  - Don't: `<FileTypeIcon name="Q3.xlsx" upload={{ status: "uploading", progress: 25 }} label="Loading" />`
  - Do: `<FileTypeIcon name="Q3.xlsx" upload={{ status: "uploading", progress: 25 }} label="Uploading Q3.xlsx" />`

### Anti-slop
- Follows `rule/message-attachment-file-glyph`.
- `rule/file-type-icon-no-name-on-glyph`: When the glyph sits beside the file's name, leave it hidden from a screen reader as it comes, instead of wrapping it in `role="img"` with an `aria-label` such as "PDF file", because the name beside it is read already, and a kind word before every file doubles what a screen reader hears in a list. Evidence: principle wcag: 1.1.1 Non-text content, a picture beside the words it shows is decoration; app 5/5 uses inside the registry leave it unnamed beside the name. Check: review of every `FileTypeIcon` inside an element with `role="img"`.
  - Don't: `<span role="img" aria-label="PDF file"><FileTypeIcon name="Guide.pdf" /></span>`
  - Do: `<FileTypeIcon name="Guide.pdf" />` beside `<span>Guide.pdf</span>`

### Limits
Not applicable: the glyph is one square box whose size the host sets with `className`. The system draws it from 24px, in the lightbox's bar, to 48px, in the examples, and docs/system/stress/file-type-icon.tsx renders every kind at 24, 32, 40 and 48px with the box unchanged in every state. The word PDF is set at 7.2 units of the 32-unit grid, 5.4px at 24px, where its box measured 8px tall.

## Accessibility
Rests on an inline `svg`, hidden from the accessibility tree when done or failed, and a `progressbar` while it uploads. The file's name beside it carries the kind in words, and in FileTile and MessageFile the line under the name says how far the upload is or that it failed, so the colours are never the only cue. The white marks are decoration. Their contrast on each sheet, measured from the tokens on the showcase, is the same in both themes: 2.66:1 on an archive, 2.9:1 on slides, 3.3:1 on a spreadsheet, 3.86:1 on a plain file, 3.97:1 on a document, 4.0:1 on audio, 4.13:1 on a PDF and on an image, 4.89:1 on video and 5.91:1 on code. Screen reader pass with VoiceOver: NEEDS REVIEW.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the glyph | passes over it; it takes no focus in any state | the next focusable element | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| glyph, done | none, `aria-hidden="true"` | none | `focusable="false"`, `data-kind` | nothing; the name beside it is read | test |
| glyph, failed | none, `aria-hidden="true"` | none | `focusable="false"` | nothing; the part that holds it says it failed | by hand |
| glyph, uploading | progressbar | `label`, else `name` | `aria-valuemin="0"`, `aria-valuemax="100"`, and `aria-valuenow` with the share rounded down, left out while the share is unknown | its name and its share when a screen reader reaches it | test |

## Tokens
| Part | State | Token |
|---|---|---|
| sheet, no known kind | done | `--file-other` |
| sheet, document | done | `--file-document` |
| sheet, spreadsheet | done | `--file-spreadsheet` |
| sheet, presentation | done | `--file-presentation` |
| sheet, PDF | done | `--file-pdf` |
| sheet, image | done | `--file-image` |
| sheet, code | done | `--file-code` |
| sheet, archive | done | `--file-archive` |
| sheet, audio | done | `--file-audio` |
| sheet, video | done | `--file-video` |
| sheet | uploading, failed | `--file-other` at 45 percent opacity |
| rising colour | uploading | the kind's `--file-*` token, clipped to the share |
| mark, fold, retry arrow, word PDF | all | `--file-ink`, which is `--on-photo`, white in both themes; the fold at 45 percent stroke opacity |
| rising colour | the share changes | `--t-settle`, `--curve` |

## Related
- FileTile: a file as a tile, with this glyph, its name, its size or upload line and its actions.
- MessageFile, on the Message page: a file sent in a message, drawn with this glyph.
- Lightbox: shows this glyph beside the name of the picture open in it.
- FileDrop: where files are chosen or dropped before they upload.
- Progress: an upload's share as a bar, where there is no file glyph to draw it on.
