Imperative Root Control
Turn any parent element into a dropzone using a ref to the UplofileRoot component.
Preview
Parent Container Dropzone
This entire area handles drag and drop imperatively via ref
Or drop specifically here
Code
1import { useRef, useState } from "react";2import type { DragEvent } from "react";3import type { UplofileRootRef } from "uplofile";4import {5 UplofileDropzone,6 UplofilePreview,7 UplofileRoot,8} from "@/components/ui/uplofile.ts";9import { mockUpload } from "@/lib/utils.ts";1011export default function RootImperativeDemo() {12 const uplofileRef = useRef<UplofileRootRef>(null);13 const [isDragging, setIsDragging] = useState(false);1415 // Handle drag events on the parent container16 const handleDragOver = (e: DragEvent) => {17 e.preventDefault();18 setIsDragging(true);19 uplofileRef.current?.onDragOver?.(e);20 };2122 const handleDragLeave = () => {23 setIsDragging(false);24 };2526 const handleDrop = (e: DragEvent) => {27 e.preventDefault();28 setIsDragging(false);29 uplofileRef.current?.onDrop?.(e);30 };3132 const handleManualUpload = () => {33 uplofileRef.current?.openFileDialog();34 };3536 return (37 <div38 className={`flex flex-col items-center justify-center min-h-[400px] p-10 border-4 border-dashed transition-colors ${39 isDragging ? "border-primary bg-primary/10" : "border-muted bg-muted/5"40 }`}41 onDragOver={handleDragOver}42 onDragLeave={handleDragLeave}43 onDrop={handleDrop}44 >45 <div className="text-center mb-6">46 <h3 className="text-lg font-medium mb-2">Parent Container Dropzone</h3>47 <p className="text-sm text-muted-foreground mb-4">48 This entire area handles drag and drop imperatively via ref49 </p>50 <button51 onClick={handleManualUpload}52 className="px-4 py-2 bg-primary text-primary-foreground rounded-md text-sm font-medium hover:bg-primary/90 transition-colors"53 >54 Open File Dialog via Ref55 </button>56 </div>5758 <UplofileRoot ref={uplofileRef} upload={mockUpload}>59 <UplofilePreview />60 {/* We can still use the internal dropzone if we want */}61 <UplofileDropzone className="mt-4 border border-dashed p-4 rounded text-sm text-muted-foreground">62 Or drop specifically here63 </UplofileDropzone>64 </UplofileRoot>65 </div>66 );67}
Key Points
- → Use
refto accessUplofileRootmethods - → Implement custom drag and drop logic on parent containers using
onDragOverandonDrop - → Trigger the file dialog programmatically with
openFileDialog() - → Perfect for cases where the drop target is larger than the uploader itself