File
nordaun-ui/fileFreeBlock
A context-based file uploader with customizable titles, descriptions and icons and a built-in workflow.
Live preview
live · rendered by the registry
Rendered by @nordaun/ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://ui.nordaun.com/r/file.jsonSource
1"use client";23import * as React from "react";45import { Alert, AlertDescription } from "@/components/ui/alert";6import { Button } from "@/components/ui/button";7import { Progress } from "@/components/ui/progress";8import { useFiles as useFileContext } from "@/hooks/use-file";9import { messages, Mime, workflow } from "@/lib/file";10import { cn } from "@/lib/utils";11import { X } from "lucide-react";1213const contextMap = new Map<string, React.Context<FileContext>>();1415export function bindContext(providerId: string): React.Context<FileContext> {16 if (!contextMap.has(providerId))17 contextMap.set(18 providerId,19 React.createContext<FileContext>(DefaultFileContext),20 );21 return contextMap.get(providerId)!;22}2324type Metadata = Pick<File, "name" | "size" | "type">;2526type FileContext = {27 providerId: string;28 maxFiles: number;29 maxSize: number;30 accept: Mime[];31 files: File[];32 metadatas: Metadata[];33 error: string | null;34 progress: number | null;35 addFiles: (files: FileList | null) => void;36 removeFile: (index: number) => void;37 clearFiles: () => void;38};3940type FileProviderProps = {41 providerId: string;42 maxFiles: number;43 maxSize: number;44 accept: Mime[];45 children: React.ReactNode;46};4748const DefaultFileContext: FileContext = {49 providerId: "",50 maxFiles: 1, // 1 File51 maxSize: 1024 * 1024, // 1 MB52 accept: [],53 files: [],54 metadatas: [],55 error: null,56 progress: null,57 addFiles: () => {},58 removeFile: () => {},59 clearFiles: () => {},60};6162function getFileSize(bytes: number) {63 if (!bytes || bytes === 0) return "0 B";64 const units = ["B", "KB", "MB", "GB", "TB", "PB"];65 const i = Math.floor(Math.log(bytes) / Math.log(1024));66 const size = bytes / Math.pow(1024, i);67 return `${size.toFixed(size % 1 === 0 ? 0 : 2)} ${units[i]}`;68}6970function FileProvider({71 providerId,72 maxFiles,73 maxSize,74 accept,75 children,76}: FileProviderProps) {77 const [files, setFiles] = React.useState<File[]>([]);78 const [metadatas, setMetadatas] = React.useState<Metadata[]>([]);79 const [error, setError] = React.useState<string | null>(null);80 const [progress, setProgress] = React.useState<number | null>(null);8182 const addFiles = (fileList: FileList | null) => {83 setError(null);8485 if (!fileList || fileList.length === 0)86 return setError(messages["noFiles"]);87 const newFiles: File[] = Array.from(fileList);8889// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from @nordaun/ui
All 8 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Audioaudio | @nordaun/ui | Blocks | Free | 1 dep | |
| Flagflag | @nordaun/ui | Blocks | Free | no deps · 2 files | |
| Loadingloading | @nordaun/ui | Blocks | Free | no deps | |
| Mapmap | @nordaun/ui | Blocks | Free | 2 deps · 2 files | |
| Passwordpassword | @nordaun/ui | Blocks | Free | 1 dep | |
| Phonephone | @nordaun/ui | Blocks | Free | 2 deps · 3 files | |
| Videovideo | @nordaun/ui | Blocks | Free | 1 dep · 2 files |
