Files
supabase/apps/design-system/registry/default/example/sonner-upload.tsx
Gildas Garcia 54a271b0f8 Upgrade sonner to 2x and update toast design (#52003)
## Problem

Two issues:
- There's an accessibility issue with sonner before v2 as it applies
`role="status"` to an `li` element
- The custom Tailwind classes we pass are not applied and once applied
correctly, the design is not what we want

## Solution

- Upgrade sonner to v2 (the aria attribute are now applied to the toasts
container)
- Fix the design, especially the close icon and the upload usages (with
progression)

In all screenshots, the close button is only displayed when hovering the
toast.

<img width="384" height="73" alt="image"
src="https://github.com/user-attachments/assets/c34038f1-30ce-43d7-a031-81727d61592c"
/>

<img width="776" height="156" alt="image"
src="https://github.com/user-attachments/assets/ec27ff42-6887-4b12-a39b-262a3adacf6b"
/>

<img width="385" height="78" alt="image"
src="https://github.com/user-attachments/assets/c6b36ec9-881c-4dbe-a0c0-81b2b2b5053f"
/>

## Review instructions

On [Design
System](https://design-system-hgbzzl10s-supabase.vercel.app/design-system/docs/components/sonner):
- Check each example with extra attention to the loading/promise and
upload ones

On
[Studio](https://studio-staging-1nx2cwequ-supabase.vercel.app/dashboard/),
in the Table Editor:
- Create a new table by importing a CSV file and check there's only one
loading icon while the progress is shown (requires a quite big CSV as
it's fast)
- Then import more data in the new table
2026-10-26 09:45:23 +01:00

85 lines
2.9 KiB
TypeScript

import { Check, Loader2 } from 'lucide-react'
import { toast } from 'sonner'
import { Button, Progress } from 'ui'
export default function SonnerUpload() {
const uploadPromise = (files: number, toastId: number) => {
toast.loading(<UploadContent progress={0} files={files} />, {
// Important, set icon to null to avoid having the default loading icon as we display it ourselves in UploadContent
icon: null,
id: toastId,
})
return new Promise((resolve) => {
let currentProgress = 0
toast.loading(<UploadContent progress={currentProgress} files={files} />, {
id: toastId,
})
const intervalId = setInterval(() => {
currentProgress += Math.floor(Math.random() * 10) + 1 // Increment progress by a random value
if (currentProgress > 100) currentProgress = 100 // Ensure progress does not exceed 100%
if (currentProgress >= 100) {
clearInterval(intervalId) // Clear the interval once upload is complete
resolve({ progress: currentProgress, message: 'Upload complete', files: 3, toastId }) // Resolve the promise when progress reaches 100
}
toast.loading(<UploadContent progress={currentProgress} files={files} />, {
id: toastId,
})
}, 200) // Adjust the interval time as needed
})
}
const UploadContent = ({ progress, files }: { progress: number; files: number }) => (
<div className="flex gap-3 w-full">
{progress >= 100 ? (
<Check className="text-foreground mt-0.5" size={16} strokeWidth={4} />
) : (
<Loader2 className="motion-safe:animate-spin text-foreground-muted mt-0.5" size={16} />
)}
<div className="flex flex-col gap-2 w-full">
<div className="flex w-full justify-between">
<p className="text-foreground text-sm">
{progress >= 100
? `Upload complete`
: files
? `Uploading ${files} files...`
: `Uploading...`}
</p>
<p className="text-foreground-light text-sm font-mono">{`${progress}%`}</p>
</div>
<Progress value={progress} className="w-full" />
<small className="text-foreground-muted text-xs">
Please do not close the browser until completed
</small>
</div>
</div>
)
return (
<div className="flex flex-col gap-3">
<Button
onClick={async () => {
// random id
const toastId = Math.random()
const max = 20 // specify the maximum value
const files = Math.floor(Math.random() * max)
try {
const promise = await uploadPromise(files, toastId)
// @ts-expect-error
toast.success(<UploadContent progress={promise.progress} files={promise.files} />, {
duration: 2000,
id: toastId,
})
} catch (error) {}
}}
>
Start upload
</Button>
</div>
)
}