The create_drag utility provides reusable drag-and-drop functionality for creating custom file upload interfaces. It handles all aspects of drag-and-drop interactions including visual feedback, file validation, and multiple upload modes.
Creates a complete drag-and-drop system with configurable options and programmatic file picker access.
/**
* Creates drag-and-drop functionality for file uploads
* @returns Object with drag action and file picker function
*/
function create_drag(): {
drag: (node: HTMLElement, options: DragActionOptions) => ActionReturn;
open_file_upload: () => void;
};The drag function is a Svelte action that adds comprehensive drag-and-drop functionality to any HTML element:
/**
* Svelte action for adding drag-and-drop functionality to DOM elements
* @param node - HTML element to attach drag-and-drop to
* @param options - Configuration options for drag behavior
* @returns Action return object with update and destroy methods
*/
function drag(node: HTMLElement, options: DragActionOptions): ActionReturn;
// Action return interface for Svelte actions
interface ActionReturn {
/** Update drag options dynamically */
update: (new_options: DragActionOptions) => void;
/** Remove all event listeners and cleanup */
destroy: () => void;
}Configuration interface for customizing drag-and-drop behavior:
interface DragActionOptions {
/** Disable click-to-upload functionality */
disable_click?: boolean;
/** Accepted file types (string, array, or null for any) */
accepted_types?: string | string[] | null;
/** Upload mode - single file, multiple files, or directory */
mode?: "single" | "multiple" | "directory";
/** Callback fired when drag state changes */
on_drag_change?: (dragging: boolean) => void;
/** Callback fired when files are selected or dropped */
on_files?: (files: File[]) => void;
}Programmatically trigger the file picker dialog:
/**
* Programmatically open the file picker dialog
* Uses the last configured options from the drag action
*/
function open_file_upload(): void;Usage Examples:
import { create_drag } from "@gradio/upload";
// Create drag system
const { drag, open_file_upload } = create_drag();
// Basic drag-and-drop setup
let dragElement;
let isDragging = false;
// In Svelte component
<div
bind:this={dragElement}
use:drag={{
accepted_types: ["image", "video"],
mode: "multiple",
on_drag_change: (dragging) => {
isDragging = dragging;
},
on_files: (files) => {
console.log("Files received:", files);
handleFileUpload(files);
}
}}
class:dragging={isDragging}
>
Drop files here or click to browse
</div>
// Programmatic file picker
<button on:click={open_file_upload}>
Choose Files
</button>The mode option controls file selection behavior:
// Single file selection
mode: "single"
// - File input allows single file selection
// - Returns array with one file maximum
// Multiple file selection
mode: "multiple"
// - File input allows multiple file selection
// - Returns array with multiple files
// Directory selection
mode: "directory"
// - File input allows directory selection
// - Returns array with all files in selected directory
// - Sets webkitdirectory, directory, and mozdirectory attributesThe accepted_types option supports various formats:
// Single type
accepted_types: "image" // Accepts image/*
accepted_types: ".pdf" // Accepts .pdf files
accepted_types: "image/jpeg" // Accepts only JPEG
// Multiple types
accepted_types: ["image", "video"] // image/* and video/*
accepted_types: [".pdf", ".doc", ".docx"] // Specific extensions
accepted_types: ["image/jpeg", "image/png"] // Specific MIME types
// Any files
accepted_types: null // No restrictions
accepted_types: "*" // No restrictions
accepted_types: "file/*" // No restrictionsThe drag action handles all standard drag-and-drop events:
on_files callbackdisable_click is falseOptions can be updated dynamically using the returned update function:
let dragAction;
// Initial setup
<div use:drag={dragAction}></div>
// Update options dynamically
function updateDragOptions() {
dragAction = {
accepted_types: ["image"],
mode: "single",
on_files: handleSingleImage
};
}The action automatically cleans up all event listeners when the component is destroyed or the action is removed. Manual cleanup can be triggered using the destroy method:
const actionReturn = drag(element, options);
// Later...
actionReturn.destroy();The drag action creates a hidden file input element with proper configuration:
accepted_typesmodeThe create_drag utility is used internally by the Upload component but can also be used independently for custom upload interfaces:
// Custom upload component using create_drag
import { create_drag } from "@gradio/upload";
const { drag, open_file_upload } = create_drag();
<div
use:drag={{
accepted_types: fileTypes,
mode: uploadMode,
disable_click: disableClick,
on_drag_change: (dragging) => setDragging(dragging),
on_files: processFiles
}}
class="custom-upload-area"
>
<slot />
</div>