<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: src/components/mds-file-preview/usage/ in the magma monorepo.
-->

# mds-file-preview - examples

> The API of this component: [`AGENTS.md`](AGENTS.md).

Correct and idiomatic ways to use the `<mds-file-preview>` component, ordered from most common to most specialized. The examples assume a working knowledge of the variant / tone ladders documented in [`agents/variants.md`](../../../../agents/variants.md) and the shared component rules in [`agents/conventions.md`](../../../../agents/conventions.md).

#### Minimal File Card

The required prop is `filename`. The component infers icon, badge colour, and type description automatically from the extension. Always pass a real filename with extension.

```html
<mds-file-preview filename="relazione-annuale.pdf"></mds-file-preview>
```

#### File Card with Size

Pass `filesize` as a numeric byte count and the component formats it automatically (JEDEC: KB, MB, GB). Pass a plain string to show a pre-formatted label verbatim.

```html
<!-- Numeric bytes - auto-formatted to "9.77 MB" -->
<mds-file-preview filename="contratto.docx" filesize="10248594"></mds-file-preview>

<!-- Pre-formatted string - shown verbatim -->
<mds-file-preview filename="archivio.zip" filesize="12 MB"></mds-file-preview>
```

#### Download Interaction

Add `downloadable` to make the whole card a button: the component emits `mdsFileDownload` with `{ target, filename, extension }` on a click, Enter or Space. Without `downloadable` the card emits nothing. Listen to the event; do not add a click handler on a wrapper element.

```html
<mds-file-preview
  filename="documento.pdf"
  filesize="2048000"
  downloadable
></mds-file-preview>
```

```javascript
document.querySelector('mds-file-preview').addEventListener('mdsFileDownload', (e) => {
  const { filename, extension } = e.detail;
  console.log('Download richiesto:', filename, extension);
});
```

#### Delete Affordance

Add `deletable` to show a dismiss button in the top-right corner. The component emits `mdsFileDelete` with `{ target, filename, extension }`. The consumer is responsible for removing the element from the DOM.

```html
<mds-file-preview
  filename="bozza.docx"
  filesize="512000"
  deletable
></mds-file-preview>
```

```javascript
document.querySelector('mds-file-preview').addEventListener('mdsFileDelete', (e) => {
  e.detail.target.remove();
});
```

#### Image Thumbnail

Set `src` to show a thumbnail instead of the generic icon. The thumbnail renders only when the format supports preview (images) and no `message` is set; other formats fall back to the icon.

```html
<mds-file-preview
  filename="foto-profilo.jpg"
  filesize="84791746"
  src="./thumbnails/foto-profilo-thumb.webp"
></mds-file-preview>
```

#### Status / Feedback State

Set `message` to switch the card to a feedback layout - the thumbnail is suppressed and the text is displayed alongside the icon. Pair with `variant` to colour the status region semantically. `variant` has no visual effect without `message`.

```html
<!-- Errore di formato -->
<mds-file-preview
  filename="documento.exe"
  message="Formato file non consentito"
  variant="error"
></mds-file-preview>

<!-- Caricamento in corso -->
<mds-file-preview
  filename="video.mp4"
  filesize="52428800"
  message="Caricamento in corso..."
  variant="info"
></mds-file-preview>
```

#### Overriding Format Detection

Use `suffix` to force a known extension category when the filename has no extension or the extension is ambiguous. Use `description` to override only the textual type caption while leaving icon and badge inference intact.

```html
<!-- Force suffix when filename has no extension -->
<mds-file-preview filename="report" suffix="pdf"></mds-file-preview>

<!-- Custom description label, icon unchanged -->
<mds-file-preview
  filename="modulo.pdf"
  description="Modulo di iscrizione"
></mds-file-preview>
```

#### Custom Icon

Set `icon` to override the auto-detected glyph with any icon slug from the Magma library, or a `data:image/svg+xml;base64,` URI for a dynamic icon from an API.

```html
<!-- Named icon slug -->
<mds-file-preview
  filename="report.csv"
  filesize="32768"
  icon="mi/baseline/table-chart"
></mds-file-preview>
```

#### Filename Truncation

`truncate` defaults to `word`, which keeps the filename on one line with an ellipsis. Use `all` to clamp it to two lines with an ellipsis. Use `none` to allow the filename to wrap freely.

```html
<mds-file-preview
  filename="Questo-e-uno-dei-nomi-file-piu-lunghi-che-lumanita-abbia-mai-visto.docx"
  filesize="84791746"
  truncate="all"
></mds-file-preview>
```

#### Inside `mds-input-upload`

`<mds-file-preview>` is the child card rendered by [`mds-input-upload`](../mds-input-upload/AGENTS.md). When integrating with the upload component, let `mds-input-upload` manage the cards rather than instantiating `mds-file-preview` directly.

```html
<mds-input-upload
  max-files="5"
  accept=".pdf,.docx"
></mds-input-upload>
```

#### Styling Customization

Style the card only through its public `--mds-file-preview-*` CSS custom properties. Set them on the host or a parent selector; use the semantic color roles via `rgb(var(--magma-<role>))` and the `--magma-radius-*` scale so dark mode and the corner geometry keep working.

```css
.carta-allegato mds-file-preview {
  --mds-file-preview-background-color: rgb(var(--magma-accent-surface-subtle));
  --mds-file-preview-border-radius: var(--magma-radius-md);
  --mds-file-preview-color: rgb(var(--magma-accent-fg));
}
```
