supabase
componentdropzone-nextjssource: ok · hit
Dropzone (File Upload)
Displays a control for easier uploading of files directly to Supabase Storage.
npx shadcn add "https://supabase.com/ui/r/dropzone-nextjs.json"free
No selection tags yet — empty means no evidence, not lack of support.
Dependencies
Bundle weight · lighticonslucide-react
Registry dependencies
button
Files5 · source cached 10/14/2026, 12:45:55 PM
registry/default/blocks/dropzone/components/dropzone.tsx
'use client'
import { CheckCircle, File, Loader2, Upload, X } from 'lucide-react'
import { createContext, useCallback, useContext, type PropsWithChildren } from 'react'
import { cn } from '@/lib/utils'
import { type UseSupabaseUploadReturn } from '@/registry/default/blocks/dropzone/hooks/use-supabase-upload'
import { Button } from '@/registry/default/components/ui/button'
export const formatBytes = (
bytes: number,
decimals = 2,
size?: 'bytes' | 'KB' | 'MB' | 'GB' | 'TB' | 'PB' | 'EB' | 'ZB' | 'YB'
) => {
const k = 1000
const dm = decimals < 0 ? 0 : decimals
const sizes = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
if (bytes === 0 || bytes === undefined) return size !== undefined ? `0 ${size}` : '0 bytes'
const i = size !== undefined ? sizes.indexOf(size) : Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
}
type DropzoneContextType = Omit<UseSupabaseUploadReturn, 'getRootProps' | 'getInputProps'>
const DropzoneContext = createContext<DropzoneContextType | undefined>(undefined)
type DropzoneProps = UseSupabaseUploadReturn & {
className?: string
}
const Dropzone = ({
className,
children,
getRootProps,
getInputProps,
...restProps
}: PropsWithChildren<DropzoneProps>) => {
const isSuccess = restProps.isSuccess
const isActive = restProps.isDragActive
const isInvalid =
(restProps.isDragActive && restProps.isDragReject) ||
(restProps.errors.length > 0 && !restProps.isSuccess) ||
restProps.files.some((file) => file.errors.length !== 0)
return (
<DropzoneContext.Provider value={{ ...restProps }}>
<div
{...getRootProps({
className: cn(
'border-2 border-gray-300 rounded-lg p-6 text-center bg-card transition-colors duration-300 text-foreground',
className,
isSuccess ? 'border-solid' : 'border-dashed',
isActive && 'border-primary bg-primary/10',
isInvalid && 'border-destructive bg-destructive/10'
),
})}
>
<input {...getInputProps()} />
{children}
</div>
</DropzoneContext.Provider>
)
}
const DropzoneContent = ({ className }: { className?: string }) => {
const {
files,
setFiles,
onUpload,
loading,
successes,
errors,
maxFileSize,
maxFiles,
isSuccess,
} = useDropzoneContext()
const exceedMaxFiles = files.length > maxFiles
const handleRemoveFile = useCallback(
(fileName: string) => {
setFiles(files.filter((file) => file.name !== fileName))
},
[files, setFiles]
)
if (isSuccess) {
return (
<div className={cn('flex flex-row items-center gap-x-2 justify-center', className)}>
<CheckCircle size={16} className="text-primary" />
<p className="text-primary text-sm">
Successfully uploaded {files.length} file{files.length > 1 ? 's' : ''}
</p>
</div>
)
}
return (
<div className={cn('flex flex-col', className)}>
{files.map((file, idx) => {
const fileError = errors.find((e) => e.name === file.name)
const isSuccessfullyUploaded = !!successes.find((e) => e === file.name)
return (
<div
key={`${file.name}-${idx}`}
className="flex items-center gap-x-4 border-b py-2 first:mt-4 last:mb-4 "
>
{file.type.startsWith('image/') ? (
<div className="h-10 w-10 rounded-sm border overflow-hidden shrink-0 bg-muted flex items-center justify-center">
<img src={file.preview} alt={file.name} className="object-cover" />
</div>
) : (
<div className="h-10 w-10 rounded-sm border bg-muted flex items-center justify-center">
<File size={18} />
</div>
)}
<div className="shrink grow flex flex-col items-start truncate">
<p title={file.name} className="text-sm truncate max-w-full">
{file.name}
</p>
{file.errors.length > 0 ? (
<p className="text-xs text-destructive">
{file.errors
.map((e) =>
e.message.startsWith('File is larger than')
? `File is larger than ${formatBytes(maxFileSize, 2)} (Size: ${formatBytes(file.size, 2)})`
: e.message
)
.join(', ')}
</p>
) : loading && !isSuccessfullyUploaded ? (
<p className="text-xs text-muted-foreground">Uploading file...</p>
) : !!fileError ? (
<p className="text-xs text-destructive">Failed to upload: {fileError.message}</p>
) : isSuccessfullyUploaded ? (
<p className="text-xs text-primary">Successfully uploaded file</p>
) : (
<p className="text-xs text-muted-foreground">{formatBytes(file.size, 2)}</p>
)}
</div>
{!loading && !isSuccessfullyUploaded && (
<Button
size="icon"
variant="link"
className="shrink-0 justify-self-end text-muted-foreground hover:text-foreground"
onClick={() => handleRemoveFile(file.name)}
>
<X />
</Button>
)}
</div>
)
})}
{exceedMaxFiles && (
<p className="text-sm text-left mt-2 text-destructive">
You may upload only up to {maxFiles} files, please remove {files.length - maxFiles} file
{files.length - maxFiles > 1 ? 's' : ''}.
</p>
)}
{files.length > 0 && !exceedMaxFiles && (
<div className="mt-2">
<Button
variant="outline"
onClick={onUpload}
disabled={files.some((file) => file.errors.length !== 0) || loading}
>
{loading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Uploading...
</>
) : (
<>Upload files</>
)}
</Button>
</div>
)}
</div>
)
}
const DropzoneEmptyState = ({ className }: { className?: string }) => {
const { maxFiles, maxFileSize, inputRef, isSuccess } = useDropzoneContext()
if (isSuccess) {
return null
}
return (
<div className={cn('flex flex-col items-center gap-y-2', className)}>
<Upload size={20} className="text-muted-foreground" />
<p className="text-sm">
Upload{!!maxFiles && maxFiles > 1 ? ` ${maxFiles}` : ''} file
{!maxFiles || maxFiles > 1 ? 's' : ''}
</p>
<div className="flex flex-col items-center gap-y-1">
<p className="text-xs text-muted-foreground">
Drag and drop or{' '}
<a
onClick={() => inputRef.current?.click()}
className="underline cursor-pointer transition hover:text-foreground"
>
select {maxFiles === 1 ? `file` : 'files'}
</a>{' '}
to upload
</p>
{maxFileSize !== Number.POSITIVE_INFINITY && (
<p className="text-xs text-muted-foreground">
Maximum file size: {formatBytes(maxFileSize, 2)}
</p>
)}
</div>
</div>
)
}
const useDropzoneContext = () => {
const context = useContext(DropzoneContext)
if (!context) {
throw new Error('useDropzoneContext must be used within a Dropzone')
}
return context
}
export { Dropzone, DropzoneContent, DropzoneEmptyState, useDropzoneContext }
registry/default/blocks/dropzone/hooks/use-supabase-upload.ts
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useDropzone, type FileError, type FileRejection } from 'react-dropzone'
import { createClient } from '@/registry/default/clients/nextjs/lib/supabase/client'
const supabase = createClient()
interface FileWithPreview extends File {
preview?: string
errors: readonly FileError[]
}
type UseSupabaseUploadOptions = {
/**
* Name of bucket to upload files to in your Supabase project
*/
bucketName: string
/**
* Folder to upload files to in the specified bucket within your Supabase project.
*
* Defaults to uploading files to the root of the bucket
*
* e.g If specified path is `test`, your file will be uploaded as `test/file_name`
*/
path?: string
/**
* Allowed MIME types for each file upload (e.g `image/png`, `text/html`, etc). Wildcards are also supported (e.g `image/*`).
*
* Defaults to allowing uploading of all MIME types.
*/
allowedMimeTypes?: string[]
/**
* Maximum upload size of each file allowed in bytes. (e.g 1000 bytes = 1 KB)
*/
maxFileSize?: number
/**
* Maximum number of files allowed per upload.
*/
maxFiles?: number
/**
* The number of seconds the asset is cached in the browser and in the Supabase CDN.
*
* This is set in the Cache-Control: max-age=<seconds> header. Defaults to 3600 seconds.
*/
cacheControl?: number
/**
* When set to true, the file is overwritten if it exists.
*
* When set to false, an error is thrown if the object already exists. Defaults to `false`
*/
upsert?: boolean
}
type UseSupabaseUploadReturn = ReturnType<typeof useSupabaseUpload>
const useSupabaseUpload = (options: UseSupabaseUploadOptions) => {
const {
bucketName,
path,
allowedMimeTypes = [],
maxFileSize = Number.POSITIVE_INFINITY,
maxFiles = 1,
cacheControl = 3600,
upsert = false,
} = options
const [files, setFiles] = useState<FileWithPreview[]>([])
const [loading, setLoading] = useState<boolean>(false)
const [errors, setErrors] = useState<{ name: string; message: string }[]>([])
const [successes, setSuccesses] = useState<string[]>([])
const isSuccess = useMemo(() => {
if (errors.length === 0 && successes.length === 0) {
return false
}
if (errors.length === 0 && successes.length === files.length) {
return true
}
return false
}, [errors.length, successes.length, files.length])
const onDrop = useCallback(
(acceptedFiles: File[], fileRejections: FileRejection[]) => {
const validFiles = acceptedFiles
.filter((file) => !files.find((x) => x.name === file.name))
.map((file) => {
;(file as FileWithPreview).preview = URL.createObjectURL(file)
;(file as FileWithPreview).errors = []
return file as FileWithPreview
})
const invalidFiles = fileRejections.map(({ file, errors }) => {
;(file as FileWithPreview).preview = URL.createObjectURL(file)
;(file as FileWithPreview).errors = errors
return file as FileWithPreview
})
const newFiles = [...files, ...validFiles, ...invalidFiles]
setFiles(newFiles)
},
[files, setFiles]
)
const dropzoneProps = useDropzone({
onDrop,
noClick: true,
accept: allowedMimeTypes.reduce((acc, type) => ({ ...acc, [type]: [] }), {}),
maxSize: maxFileSize,
maxFiles: maxFiles,
multiple: maxFiles !== 1,
})
const onUpload = useCallback(async () => {
setLoading(true)
// [Joshen] This is to support handling partial successes
// If any files didn't upload for any reason, hitting "Upload" again will only upload the files that had errors
const filesWithErrors = errors.map((x) => x.name)
const filesToUpload =
filesWithErrors.length > 0
? [
...files.filter((f) => filesWithErrors.includes(f.name)),
...files.filter((f) => !successes.includes(f.name)),
]
: files
const responses = await Promise.all(
filesToUpload.map(async (file) => {
const { error } = await supabase.storage
.from(bucketName)
.upload(!!path ? `${path}/${file.name}` : file.name, file, {
cacheControl: cacheControl.toString(),
upsert,
})
if (error) {
return { name: file.name, message: error.message }
} else {
return { name: file.name, message: undefined }
}
})
)
const responseErrors = responses.filter((x) => x.message !== undefined)
// if there were errors previously, this function tried to upload the files again so we should clear/overwrite the existing errors.
setErrors(responseErrors)
const responseSuccesses = responses.filter((x) => x.message === undefined)
const newSuccesses = Array.from(
new Set([...successes, ...responseSuccesses.map((x) => x.name)])
)
setSuccesses(newSuccesses)
setLoading(false)
}, [files, path, bucketName, errors, successes])
useEffect(() => {
if (files.length === 0) {
setErrors([])
}
// If the number of files doesn't exceed the maxFiles parameter, remove the error 'Too many files' from each file
if (files.length <= maxFiles) {
let changed = false
const newFiles = files.map((file) => {
if (file.errors.some((e) => e.code === 'too-many-files')) {
file.errors = file.errors.filter((e) => e.code !== 'too-many-files')
changed = true
}
return file
})
if (changed) {
setFiles(newFiles)
}
}
}, [files.length, setFiles, maxFiles])
return {
files,
setFiles,
successes,
isSuccess,
loading,
errors,
setErrors,
onUpload,
maxFileSize: maxFileSize,
maxFiles: maxFiles,
allowedMimeTypes,
...dropzoneProps,
}
}
export { useSupabaseUpload, type UseSupabaseUploadOptions, type UseSupabaseUploadReturn }
registry/default/clients/nextjs/lib/supabase/client.ts
import { createBrowserClient } from '@supabase/ssr'
export function createClient() {
return createBrowserClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!
)
}
registry/default/clients/nextjs/lib/supabase/middleware.ts
import { createServerClient } from '@supabase/ssr'
import { NextResponse, type NextRequest } from 'next/server'
export async function updateSession(request: NextRequest) {
let supabaseResponse = NextResponse.next({
request,
})
// With Fluid compute, don't put this client in a global environment
// variable. Always create a new one on each request.
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
{
cookies: {
getAll() {
return request.cookies.getAll()
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value }) => request.cookies.set(name, value))
supabaseResponse = NextResponse.next({
request,
})
cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options)
)
},
},
}
)
// Do not run code between createServerClient and
// supabase.auth.getClaims(). A simple mistake could make it very hard to debug
// issues with users being randomly logged out.
// IMPORTANT: If you remove getClaims() and you use server-side rendering
// with the Supabase client, your users may be randomly logged out.
const { data } = await supabase.auth.getClaims()
const user = data?.claims
if (
!user &&
!request.nextUrl.pathname.startsWith('/login') &&
!request.nextUrl.pathname.startsWith('/auth') &&
// the OAuth consent route sends unauthenticated visitors to the login page
// itself, so that it can preserve the authorization in the `next` parameter
request.nextUrl.pathname !== '/oauth/consent'
) {
// no user, potentially respond by redirecting the user to the login page
const url = request.nextUrl.clone()
url.pathname = '/auth/login'
return NextResponse.redirect(url)
}
// IMPORTANT: You *must* return the supabaseResponse object as it is.
// If you're creating a new response object with NextResponse.next() make sure to:
// 1. Pass the request in it, like so:
// const myNewResponse = NextResponse.next({ request })
// 2. Copy over the cookies, like so:
// myNewResponse.cookies.setAll(supabaseResponse.cookies.getAll())
// 3. Change the myNewResponse object to fit your needs, but avoid changing
// the cookies!
// 4. Finally:
// return myNewResponse
// If this is not done, you may be causing the browser and server to go out
// of sync and terminate the user's session prematurely!
return supabaseResponse
}
registry/default/clients/nextjs/lib/supabase/server.ts
import { createServerClient } from '@supabase/ssr'
import { cookies } from 'next/headers'
/**
* If using Fluid compute: Don't put this client in a global variable. Always create a new client within each
* function when using it.
*/
export async function createClient() {
const cookieStore = await cookies()
return createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,
{
cookies: {
getAll() {
return cookieStore.getAll()
},
setAll(cookiesToSet) {
try {
cookiesToSet.forEach(({ name, value, options }) =>
cookieStore.set(name, value, options)
)
} catch {
// The `setAll` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
},
},
}
)
}