Skip to main content

Svelte

Svelte components for the Uppy UI plugins.

Install​

npm install @uppy/svelte
note

You also need to install the UI plugin you want to use. For instance, @uppy/dashboard.

Use​

Uppy offers three ways to build user interfaces:

  1. Pre-composed, plug-and-play components. Mainly <Dashboard />. The downside is that you can’t customize the UI.
  2. Headless components (Svelte 5 only). Smaller components, easier to override the styles or compose them together with your own components.
  3. Hooks (Svelte 5 only). Attach our logic to your own components, no restrictions, create a tailor-made UI.

Components​

Pre-composed, plug-and-play components:

note

<DragDrop /> and <ProgressBar /> were removed in Uppy 5.0. Use the headless <Dropzone /> component or the useDropzone hook instead (Svelte 5 only). The @uppy/drag-drop plugin itself is still supported for setups without a framework.

Headless Components​

tip

Play around with the Svelte headless components in StackBlitz

note

All components must be placed within a UppyContextProvider. Headless components only work with Svelte 5

Uppy’s headless components are smaller and more flexible. All components must be placed within a UppyContextProvider. They come styled as well but you can override the styles with data attributes, for example with UploadButton:

button[data-uppy-element='upload-button'][data-state='uploading'] {
background-color: lightblue;
}

UppyContextProvider​

Provides Uppy context to child components and manages global upload status/progress.

<script>
import { UppyContextProvider } from '@uppy/svelte';
import Uppy from '@uppy/core';

const uppy = new Uppy();
</script>

<UppyContextProvider {uppy}>
<!-- Your components and/or Uppy components -->
</UppyContextProvider>

Context Value:

{
uppy: Uppy | undefined
status: 'init' | 'ready' | 'uploading' | 'paused' | 'error' | 'complete'
progress: number
}

<UploadButton />​

Attributes

  • data-uppy-element="upload-button"
  • data-state="init" | "uploading" | "paused" | "error" | "complete"

<Thumbnail />​

Component for displaying image thumbnails and/or SVG icons.

Props:

  • file: UppyFile<Meta, Body>
  • width?: string
  • height?: string
  • images?: boolean

<ProviderIcon />​

Common icons, such as a webcam, device, Dropbox, Google Drive, OneDrive, etc.

Props:

  • provider: 'device' | 'camera' | 'screen-capture' | 'audio' | 'dropbox' | 'facebook' | 'onedrive' | 'googlephotos' | 'googledrive'
  • fill?: string

Device

Camera

Screen Capture

Audio

Dropbox

Facebook

OneDrive

Google Photos

Google Drive

<FilesList />​

Component for displaying a list view of selected files.

Props:

  • editFile?: (file: UppyFile<Meta, Body>) => void

<FilesGrid />​

Grid layout component for displaying files in a grid format.

Props:

  • editFile?: (file: UppyFile<Meta, Body>) => void
  • columns?: number

<Dropzone />​

Drag-and-drop zone component for file uploads.

Props:

  • width?: string
  • height?: string
  • note?: string
  • noClick?: boolean

Hooks​

tip

Play around with the Svelte headless hooks in StackBlitz

note

All hooks must be used within a UppyContextProvider. Hooks only work in Svelte 5.

useDropzone(options?)​

Creates a dropzone for drag-and-drop file uploads using Uppy.

Arguments:

  • noClick?: boolean
  • onDragOver?: (event: Event) => void
  • onDragEnter?: (event: Event) => void
  • onDragLeave?: (event: Event) => void
  • onDrop?: (files: File[]) => void
  • onFileInputChange?: (files: File[]) => void

Returns:

  • getRootProps: Function
  • getInputProps: Function

useFileInput(props?)​

Creates a file input for programmatic file selection using Uppy.

Arguments:

  • multiple?: boolean
  • accept?: string

Returns:

  • getInputProps: Function
  • getButtonProps: Function

useRemoteSource(sourceId, options?)​

Manages remote source connections (cloud storage providers).

Arguments:

  • sourceId: 'Box' | 'Dropbox' | 'Facebook' | 'GoogleDrive' | 'OneDrive' | 'Unsplash' | 'Url' | 'Zoom'
  • options.onClose?: () => void: called by cancel after it clears the selection, and by done after it adds the selected files. When restrictions or onBeforeFileAdded reject every selected file, done keeps the selection and does not call it.

Returns:

{
state: {
authenticated: boolean | undefined
didFirstRender: boolean
searchString: string
loading: boolean | string
partialTree: PartialTree
currentFolderId: PartialTreeId
username: string | null
breadcrumbs: PartialTreeFolder[]
selectedAmount: number
error: string | null
}
login: Function
logout: Function
open: Function
checkbox: Function
done: () => Promise<void>
cancel: () => void
}

useWebcam({ onSubmit })​

Manages webcam functionality for capturing photos/videos.

Arguments:

  • onSubmit: () => void

Returns:

{
state: {
hasCamera: boolean
cameraReady: boolean
cameraError: null | Error
recordingLengthSeconds: number
videoSources: MediaDeviceInfo[]
currentDeviceId: string | MediaStreamTrack | null | undefined
recordedVideo: null | string
isRecording: boolean
status: 'init' | 'ready' | 'recording' | 'captured' | 'error'
}
stop: Function
start: Function
getVideoProps: Function
getSnapshotButtonProps: Function
getRecordButtonProps: Function
getStopRecordingButtonProps: Function
getSubmitButtonProps: Function
getDiscardButtonProps: Function
}

useScreenCapture({ onSubmit })​

Manages screen capture functionality for capturing screen image / video.

Arguments:

  • onSubmit?: () => void

Returns:

{
state: {
streamActive: boolean
audioStreamActive: boolean
recording: boolean
recordedVideo: string | null
screenRecError: Error | null
capturedScreenshotUrl: string | null
status: 'init' | 'ready' | 'recording' | 'captured' | 'error'
}
stop: Function
start: Function
getVideoProps: Function
getScreenshotButtonProps: Function
getRecordButtonProps: Function
getStopRecordingButtonProps: Function
getSubmitButtonProps: Function
getDiscardButtonProps: Function
}

useImageEditor({ file })​

Builds a custom image editor (crop, rotate, flip, zoom) for an image file. It uses the @uppy/image-editor plugin, which you have to add to Uppy with uppy.use(ImageEditor) and its default id, otherwise the hook throws. The plugin does not need a target or the Dashboard.

Spread getImageProps() on an <img> element. The editing buttons are disabled until that image has loaded. Saving replaces the file’s data in Uppy and emits file-editor:complete; cancelling emits file-editor:cancel. The hook doesn’t close your editor, so pass an onClick to the save and cancel button props to do that.

The cropping area needs the editor stylesheet: import @uppy/react/css/image-editor.css, @uppy/vue/css/image-editor.css or @uppy/svelte/css/image-editor.css.

Arguments:

  • file: UppyFile<Meta, Body>: the file to edit. Its data must be a Blob, so this works for local files, not for files from remote sources. The editor supports JPEG, GIF, PNG, BMP and WebP; check a file with the plugin’s canEditFile(file) before you open it.

Returns:

{
state: {
angle: number
isFlippedHorizontally: boolean
aspectRatio: 'free' | '1:1' | '16:9' | '9:16'
}
getImageProps: Function
getSaveButtonProps: Function
getCancelButtonProps: Function
getRotateButtonProps: Function
getFlipHorizontalButtonProps: Function
getZoomButtonProps: Function
getCropSquareButtonProps: Function
getCropLandscapeButtonProps: Function
getCropPortraitButtonProps: Function
getResetButtonProps: Function
getRotationSliderProps: Function
}
  • getSaveButtonProps({ onClick }) and getCancelButtonProps({ onClick }) take an optional onClick, which runs on click after saving or cancelling has started.
  • getRotateButtonProps(degrees) rotates by a step, for example 90 or -90.
  • getZoomButtonProps(ratio) zooms in for a positive ratio (0.1) and out for a negative one (-0.1).
  • getCropSquareButtonProps(), getCropLandscapeButtonProps() and getCropPortraitButtonProps() set the crop aspect ratio to 1:1, 16:9 and 9:16.
  • getRotationSliderProps() is for an <input>: a range from -45 to 45 degrees for fine rotation. In React the image turns while the slider moves. In Vue and Svelte the props use the native change event, so it turns when the slider is released.

See the image editor example.

useGooglePicker({ pickerType, storage })​

Opens Google’s own picker for Google Drive or Google Photos from your own button. It reads its settings from the Google Drive Picker or Google Photos Picker plugin, so add that plugin to Uppy first, with its default id, otherwise the hook throws. Those plugins need a Google OAuth client ID (Google Drive also needs an API key and an app ID) and a Companion instance; their docs explain the setup. Picked files are added to Uppy as remote files, which Companion downloads.

Arguments:

  • pickerType: 'drive' | 'photos': 'drive' uses the GoogleDrivePicker plugin, 'photos' the GooglePhotosPicker plugin.
  • storage?: AsyncStore: where the Google access token is kept between page loads. Defaults to localStorage. The hook doesn’t use the plugin’s storage option, so pass the same store here if you set one there.

Returns:

{
loading: boolean
accessToken: string | null | undefined
show: () => Promise<void>
logout: () => Promise<void>
}
  • show() asks the user to sign in to Google if needed, then opens the picker. Call it from a click handler, otherwise the browser may block the sign-in popup.
  • loading is true while the picker opens and, for Google Drive, while picked folders are listed.
  • accessToken is set while the user is signed in to Google.
  • logout() revokes the access token.

See the Google Drive Picker example.

Examples​

The prop getters of useImageEditor return Svelte event attributes (onclick, onchange, onload), so you can spread them on elements. Don’t destructure what useImageEditor and useGooglePicker return: their values are reactive through getters, which destructuring reads only once.

Example: image editor with useImageEditor​

A minimal editor component. See useImageEditor for all the controls. The hook reads file once, so wrap the component in a {#key} block when the file can change.

<!-- ImageEditor.svelte -->
<script lang="ts">
import type { UppyFile } from '@uppy/core'
import { useImageEditor } from '@uppy/svelte'

const { file, close }: { file: UppyFile<any, any>; close: () => void } =
$props()

const editor = useImageEditor({ file })
</script>

<img {...editor.getImageProps()} style="max-width: 100%" />
<input {...editor.getRotationSliderProps()} />
<button {...editor.getRotateButtonProps(90)}>Rotate</button>
<button {...editor.getFlipHorizontalButtonProps()}>Flip</button>
<button {...editor.getCropSquareButtonProps()}>Square</button>
<button {...editor.getResetButtonProps()}>Reset</button>
<button {...editor.getCancelButtonProps({ onClick: close })}>Cancel</button>
<button {...editor.getSaveButtonProps({ onClick: close })}>Save</button>

Render it inside an UppyContextProvider whose Uppy instance has the ImageEditor plugin, for example from <FilesList />:

<script lang="ts">
import '@uppy/svelte/css/style.css'
import '@uppy/svelte/css/image-editor.css'

import Uppy, { type UppyFile } from '@uppy/core'
import ImageEditor from '@uppy/image-editor'
import { Dropzone, FilesList, UppyContextProvider } from '@uppy/svelte'
import Editor from './ImageEditor.svelte'

const uppy = new Uppy().use(ImageEditor)
let editing = $state<UppyFile<any, any> | null>(null)

function editFile(file: UppyFile<any, any>) {
if (uppy.getPlugin('ImageEditor')?.canEditFile(file)) editing = file
}
</script>

<UppyContextProvider {uppy}>
{#if editing}
{#key editing.id}
<Editor file={editing} close={() => (editing = null)} />
{/key}
{/if}
<Dropzone />
<FilesList {editFile} />
</UppyContextProvider>

Example: Google Drive Picker with useGooglePicker​

A button that opens Google Drive’s picker. Render it inside an UppyContextProvider whose Uppy instance has the GoogleDrivePicker plugin, set up as explained in the Google Drive Picker docs:

new Uppy().use(GoogleDrivePicker, {
companionUrl: 'https://your-companion.com',
clientId: 'From Google Developer Console',
apiKey: 'From Google Developer Console',
appId: 'From Google Developer Console',
})

For Google Photos, use @uppy/google-photos-picker and pickerType: 'photos'. Add an uploader plugin, such as @uppy/tus, to upload the picked files.

<script lang="ts">
import { useGooglePicker } from '@uppy/svelte'

const drive = useGooglePicker({ pickerType: 'drive' })
</script>

<button type="button" disabled={drive.loading} onclick={() => drive.show()}>
Import from Google Drive
</button>
{#if drive.accessToken}
<button type="button" onclick={() => drive.logout()}>Sign out of Google</button>
{/if}