# FileInput — SvelteKit UI

Package: sveltekit-ui 1.1.94

Page: https://www.sveltekit-ui.com/components/file_input

Category: Input controls

allows users to select or drop files; optionally multiple, with drag-and-drop area and custom validation

Public component import: `FileInput`

Public manager import: `create_file_input_manager`

## Usage

```svelte
<script>
  // Import the shared stylesheet once in your application's root layout.
  import "sveltekit-ui/style.css"
  import { FileInput, create_file_input_manager } from "sveltekit-ui"

  let file_name = $state("")

  const manager = create_file_input_manager({
    label: "Choose a document",
    accept: ["application/pdf", "text/plain"],
    is_multiselect: false,
    on_get_file: (file) => { file_name = file.name },
    on_clear_files: () => { file_name = "" },
  })
</script>

<FileInput {manager} />
{#if file_name}<p role="status">Selected: {file_name}</p>{/if}
```

## Configuration

### `val`

- type: any
- default: null
- description: the current file or array of files selected

### `label`

- type: string
- default: null
- description: label for the file input

### `name`

- type: string
- default: file
- description: the name attribute for the underlying file input element

### `is_required`

- type: boolean
- default: false
- description: marks the input as required

### `is_read_only`

- type: boolean
- default: false
- description: makes the file input read-only if true

### `accept`

- type: array
- default:
  - image/png
  - image/jpeg
  - image/webp
  - image/gif
  - image/avif
  - image/svg+xml
  - application/pdf
  - video/mp4
  - audio/mpeg
  - audio/aac
  - application/json
- description: the accepted file types

### `is_multiselect`

- type: boolean
- default: false
- description: if true, the file input accepts multiple files

### `is_disabled`

- type: boolean
- default: false
- description: disables file input if true

### `border_radius`

- type: number
- default: 2
- description: border radius in rem for the drop zone

### `height`

- type: string
- default: null
- description: height (e.g., '200px') of the drop zone; if null, auto-sizes

### `icon_color`

- type: string
- default: var(--primary-t)
- description: color for the drop zone icon

### `border_color`

- type: string
- default: aabbccff
- description: hex color used for the dashed border

### `is_drop_zone`

- type: boolean
- default: true
- description: if true, enables drag-and-drop area

### `is_hide_drop_zone_if_file`

- type: boolean
- default: true
- description: if true and a file is already selected, hides the drop zone

### `ml`

- type: number
- default: 0
- description: margin-left in rem

### `mr`

- type: number
- default: 0
- description: margin-right in rem

### `mt`

- type: number
- default: 0
- description: margin-top in rem

### `mb`

- type: number
- default: 0
- description: margin-bottom in rem

### `select_file_button_text`

- type: string
- default: null
- description: custom text for the 'select file' button

### `on_get_file`

- type: function
- default: null
- description: callback when a file is selected or dropped; receives val

### `on_clear_files`

- type: function
- default: null
- description: callback when files are cleared by the user

## Methods

### `init()`

- description: initializes the file input manager and supporting managers (icon, buttons, etc.)
- parameters:
  - config: object
- returns: void

### `get_file()`

- description: handles the <input type='file'> onchange event, sets val, and calls on_get_file if provided
- parameters:
  - e: event
- returns: void

### `drop_handler()`

- description: handles drag-and-drop events, sets val for accepted file types, populates error_message otherwise
- parameters:
  - ev: event
- returns: void

### `clear_files()`

- description: clears the currently selected file(s) and calls on_clear_files if provided
- parameters:
- returns: void

## Returned state

### `id`

- type: string
- description: unique id for this file input instance

### `val`

- type: any
- description: the currently selected file or an array of files

### `name`

- type: string
- description: the name attribute of the underlying file input

### `label`

- type: string
- description: label text displayed above the file input

### `is_required`

- type: boolean
- description: true if the field is required

### `is_read_only`

- type: boolean
- description: true if file input is read-only

### `accept`

- type: array
- description: file type strings that are acceptable

### `is_multiselect`

- type: boolean
- description: true if multiple files can be selected

### `is_disabled`

- type: boolean
- description: if true, the file input is disabled

### `border_radius`

- type: number
- description: border radius (rem) for the drop zone

### `height`

- type: string
- description: custom height (e.g. '200px') for the drop zone

### `icon_color`

- type: string
- description: the color used for the file drop icon

### `border_color`

- type: string
- description: the color used for the dashed border

### `is_drop_zone`

- type: boolean
- description: enables drag-and-drop if true

### `is_hide_drop_zone_if_file`

- type: boolean
- description: hides drop zone if a file is already selected

### `ml`

- type: number
- description: margin-left in rem

### `mr`

- type: number
- description: margin-right in rem

### `mt`

- type: number
- description: margin-top in rem

### `mb`

- type: number
- description: margin-bottom in rem

### `select_file_button_text`

- type: string
- description: custom text for the 'select file' button

### `error_message`

- type: string
- description: shows errors if a dropped file does not match accepted types, etc.

### `svg_bg`

- type: string
- description: background-image dataurl for the dashed border overlay

### `error_button_manager`

- type: object
- description: button manager to dismiss the displayed error message

### `clear_files_button_manager`

- type: object
- description: button manager to clear the currently selected file(s)

### `file_drop_icon_manager`

- type: object
- description: icon manager for the displayed file drop icon
