# FileInput

Drop zone for files: the user drops files on it or clicks it (or focuses it and presses Enter or Space) to open the file picker. It keeps the chosen files in `files` and adds new ones to them (`replace` swaps them instead). `accept`, `maxSize` and `maxFiles` refuse the files that do not fit: they are not added, a message under the zone says why and `onreject` receives them. A real `<input type="file">` holds the same files, so `name`, `required` and form reset work in a native form. To show the chosen files use FileInputList; FileInput alone suits pages that move the files elsewhere as soon as they arrive (read `added` in `onchange`). Its state is exposed as `data-dragging`, `data-full`, `data-disabled` and `data-state` for app CSS.

```ts
import { FileInput } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/forms/FileInput.svelte`.

## Examples

### Basic

Drop files on the zone or click it (or Tab to it and press Enter) to pick them. `bind:files` holds every chosen file and `onchange` also says which ones were just `added`. `description` tells the user what is accepted.

```svelte
<script lang="ts">
	import { FileInput } from '@likable-hair/svelte';

	let files: File[] = $state([]);
	let last = $state('');
</script>

<div class="demo">
	<FileInput
		label="Attachments"
		description="PDF, JPG up to 10 MB"
		bind:files
		onchange={({ added }) => (last = added.map((file) => file.name).join(', '))}
	/>
	<p>{files.length} files{last ? ` · last added: ${last}` : ''}</p>
</div>

<style>
	.demo {
		display: grid;
		gap: 12px;
		width: 100%;
		max-width: 420px;
	}

	p {
		margin: 0;
		font-size: 13px;
	}
</style>
```

### Accepted files

`accept` limits the picker and is checked on dropped files too; `maxSize` (in bytes, 1 MB = 1,000,000) and `maxFiles` refuse what does not fit. Refused files are not added: a message under the zone says why and `onreject` receives them with the reason. With `maxFiles={1}` the picker chooses a single file.

```svelte
<script lang="ts">
	import { FileInput, type FileRejection } from '@likable-hair/svelte';

	let photos: File[] = $state([]);
	let refused: FileRejection[] = $state([]);
</script>

<div class="demo">
	<FileInput
		label="Salon photos"
		accept="image/*"
		maxSize={5_000_000}
		maxFiles={3}
		description="Images up to 5 MB, at most 3"
		bind:files={photos}
		onreject={({ rejected }) => (refused = rejected)}
	/>
	<FileInput label="Price list" accept=".xlsx,.csv" maxFiles={1} description="Excel or CSV" />
	<p>{photos.length} / 3 photos · last refused: {refused.map((item) => item.reason).join(', ') || 'none'}</p>
</div>

<style>
	.demo {
		display: grid;
		gap: 16px;
		width: 100%;
		max-width: 420px;
	}

	p {
		margin: 0;
		font-size: 13px;
	}
</style>
```

### Compact

`compact` puts the icon beside the texts on a single line, for toolbars and dense forms. `title`, `browseLabel`, `description` and `icon` replace the texts and the icon.

```svelte
<script lang="ts">
	import { FileInput } from '@likable-hair/svelte';
	import { mdiTrayArrowUp } from '@mdi/js';
</script>

<div class="demo">
	<FileInput compact description="CSV, at most 2 MB" accept=".csv" maxSize={2_000_000} />
	<FileInput
		compact
		icon={mdiTrayArrowUp}
		title="Import the customers"
		browseLabel="choose a file"
		maxFiles={1}
	/>
</div>

<style>
	.demo {
		display: grid;
		gap: 16px;
		width: 100%;
		max-width: 420px;
	}
</style>
```

### States

`state` with `hint` shows a message from the app, like the other fields. `disabled` turns off the zone and the native input.

```svelte
<script lang="ts">
	import { FileInput } from '@likable-hair/svelte';
</script>

<div class="demo">
	<FileInput label="Identity document" state="error" hint="The document is required" compact />
	<FileInput label="Signed contract" disabled description="Available after the approval" compact />
</div>

<style>
	.demo {
		display: grid;
		gap: 16px;
		width: 100%;
		max-width: 420px;
	}
</style>
```

### Custom content

`bodySnippet` replaces the content of the zone and receives `dragging` (files are over it) and `disabled`. The whole zone stays clickable and keeps the keyboard.

```svelte
<script lang="ts">
	import { FileInput, Icon } from '@likable-hair/svelte';
	import { mdiFileWordOutline } from '@mdi/js';
</script>

<div class="demo">
	<FileInput accept=".docx" maxFiles={1} replace --file-input-height="160px">
		{#snippet bodySnippet({ dragging })}
			<Icon path={mdiFileWordOutline} --icon-size="40px" />
			<strong>{dragging ? 'Release to replace the letter' : 'Welcome letter template'}</strong>
			<small>Word file only (.docx)</small>
		{/snippet}
	</FileInput>
</div>

<style>
	.demo {
		width: 100%;
		max-width: 420px;
	}

	small {
		color: var(--global-color-text-3);
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `files` (bindable) | `File[]` |  | The chosen files. Bind it to read them and to remove or replace them from the app. |
| `accept` | `string` |  | Native `accept`: extensions (`.pdf`) and MIME types (`image/*`, `application/pdf`), comma separated. Dropped files are checked too. |
| `maxSize` | `number` |  | Largest accepted file, in bytes (decimal units: 10 MB = `10_000_000`). |
| `maxFiles` | `number` |  | Most files the list can hold. When it is full the zone is disabled; with `1` the picker chooses a single file. |
| `replace` | `boolean` | `false` | New files replace the ones in `files` instead of being added to them. The zone never counts as full. |
| `disabled` | `boolean` | `false` | Disables the zone and the native input. |
| `readonly` | `boolean` | `false` | Hides the zone and the input: only the label, the hint and the content below stay. |
| `compact` | `boolean` | `false` | One-line zone, with a smaller icon and the texts beside it. |
| `label` | `string` |  | Visible label above the zone, linked to the input. |
| `hint` | `string` |  | Text below the zone. With `state` it becomes the error or success message. |
| `state` | `'error' \| 'success'` |  | Validation state: colors the zone border and the hint. `error` also sets `aria-invalid`. Refused files show an error on their own. |
| `icon` | `string` | `UPLOAD_ICON` | SVG path of the icon in the zone. Defaults to a cloud with an arrow. |
| `title` | `string` |  | Main text of the zone. Defaults to "Drop files here" ("Drop a file here" with `maxFiles={1}`), in the language of `locale`. |
| `description` | `string` |  | Text after the browse link, such as the accepted formats and size ("PDF, JPG up to 10 MB"). |
| `browseLabel` | `string` |  | The highlighted words after "or". Defaults to "browse your files", in the language of `locale`. |
| `locale` | `string` |  | BCP 47 locale of the texts and of the sizes in the messages (`it` and `en` are built in, other languages fall back to English). Defaults to the one set with `setDefaultLocale`, else `en`. |
| `id` | `string` |  | `id` of the input. Generated when missing, so the label always points to the input. |
| `name` | `string` |  | Native `name`: the files are sent with the form under this name. |
| `required` | `boolean` |  | Native `required`: a form with no files does not submit. |
| `input` (bindable) | `HTMLInputElement` |  | The native `<input type="file">`. |
| `class` | `{ container?: string; label?: string; zone?: string; hint?: string; }` | `{}` | Extra classes for each part. |

It also accepts the native attributes and events of `<input>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `labelSnippet` | `{ label: string \| undefined }` | Replaces the label content. It stays linked to the input. |
| `bodySnippet` | `{ dragging: boolean; disabled: boolean }` | Replaces the content of the zone (icon and texts). `disabled` is also true when the zone is full. |
| `hintSnippet` | `{ hint: string \| undefined }` | Replaces the hint below the zone. |
| `rejectedSnippet` | `{ rejected: FileRejection[] }` | Replaces the messages about refused files. |
| `children` |  | Content below the zone and the hint, inside the field (FileInputList puts its list here). |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onchange` | `(event: { files: File[]; added: File[]; removed: File[] }) => void` | Called whenever the files change: dropped or picked files (`added`), files replaced with `replace` (`removed`), form reset. |
| `onreject` | `(event: { rejected: FileRejection[] }) => void` | Called with the files that were refused and the reason of each. |

## Types

Exported by the package: `import type { FileRejection } from '@likable-hair/svelte';`

### FileRejection

A file that FileInput or FileInputList did not add, and why.

`interface FileRejection`

| Name | Type | Description |
| --- | --- | --- |
| `file` (required) | `File` | The refused file. |
| `reason` (required) | `'type' \| 'size' \| 'count' \| 'duplicate'` | `type`: not matched by `accept`; `size`: larger than `maxSize`; `count`: over `maxFiles`; `duplicate`: the same file (name, size and date) is already in the list. |

## CSS variables

For a single instance, pass them as style props: `<FileInput --file-input-background="tomato">`. The `--file-input-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--file-input-border-color` | `--file-input-default-border-color`: `var(--global-color-border-strong)` |
| `--file-input-hover-border-color` | `--file-input-default-hover-border-color`: `var(--global-color-primary)` |
| `--file-input-hint-color` | `--file-input-default-hint-color`: `var(--global-input-hint-color)` |
| `--file-input-gap` | `--file-input-default-gap`: `6px`<br>`--file-input-default-inner-gap`: `4px`<br>`--file-input-default-compact-inner-gap`: `12px` |
| `--file-input-width` | `--file-input-default-width`: `100%` |
| `--file-input-height` | `--file-input-default-height`: `auto` |
| `--file-input-error-color` | `--file-input-default-error-color`: `var(--global-input-error-color)` |
| `--file-input-success-color` | `--file-input-default-success-color`: `var(--global-input-success-color)` |
| `--file-input-label-color` | `--file-input-default-label-color`: `var(--global-input-label-color)` |
| `--file-input-label-font-size` | `--file-input-default-label-font-size`: `var(--global-input-label-font-size)` |
| `--file-input-label-font-weight` | `--file-input-default-label-font-weight`: `var(--global-input-label-font-weight)` |
| `--file-input-padding` | `--file-input-default-padding`: `24px`<br>`--file-input-default-compact-padding`: `10px 14px` |
| `--file-input-border-width` | `--file-input-default-border-width`: `1.5px` |
| `--file-input-border-style` | `--file-input-default-border-style`: `dashed` |
| `--file-input-border-radius` | `--file-input-default-border-radius`: `var(--global-radius-lg)` |
| `--file-input-background` | `--file-input-default-background`: `var(--global-color-surface)` |
| `--file-input-color` | `--file-input-default-color`: `var(--global-color-text)` |
| `--file-input-hover-background` | `--file-input-default-hover-background`: `var(--global-color-primary-soft)` |
| `--file-input-icon-hover-lift` | `--file-input-default-icon-hover-lift`: `-3px` |
| `--file-input-dragging-border-color` | `--file-input-default-dragging-border-color`: `var(--global-color-primary)` |
| `--file-input-dragging-background` | `--file-input-default-dragging-background`: `var(--global-color-primary-soft)` |
| `--file-input-focus-ring-width` | `--file-input-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--file-input-focus-ring-color` | `--file-input-default-focus-ring-color`: `var(--global-focus-ring-color)` |
| `--file-input-focus-ring-offset` | `--file-input-default-focus-ring-offset`: `var(--global-focus-ring-offset)` |
| `--file-input-disabled-opacity` | `--file-input-default-disabled-opacity`: `var(--global-input-disabled-opacity)` |
| `--file-input-icon-border-radius` | `--file-input-default-icon-border-radius`: `var(--global-radius-md)` |
| `--file-input-icon-background` | `--file-input-default-icon-background`: `var(--global-gradient)` |
| `--file-input-icon-color` | `--file-input-default-icon-color`: `var(--global-color-on-primary)` |
| `--file-input-icon-box-shadow` | `--file-input-default-icon-box-shadow`: `0 12px 28px -8px var(--global-color-primary-glow)` |
| `--file-input-title-color` | `--file-input-default-title-color`: `var(--global-color-text)` |
| `--file-input-title-font-size` | `--file-input-default-title-font-size`: `14px` |
| `--file-input-title-font-weight` | `--file-input-default-title-font-weight`: `600` |
| `--file-input-text-color` | `--file-input-default-text-color`: `var(--global-color-text-3)` |
| `--file-input-text-font-size` | `--file-input-default-text-font-size`: `12.5px` |
| `--file-input-browse-color` | `--file-input-default-browse-color`: `var(--global-color-primary)` |
| `--file-input-browse-font-weight` | `--file-input-default-browse-font-weight`: `500` |
| `--file-input-hint-font-size` | `--file-input-default-hint-font-size`: `var(--global-input-hint-font-size)` |

### Other defaults

| Variable | Value |
| --- | --- |
| `--file-input-default-icon-box-size` | `48px` |
| `--file-input-default-compact-icon-box-size` | `36px` |
| `--file-input-default-icon-size` | `26px` |
| `--file-input-default-compact-icon-size` | `20px` |
