Sonner 7

PreviousNext

A promise-based toast notification with loading, success, and error states

Docs
shadcn-studiocomponent

Preview

Loading preview…
registry/new-york/components/sonner/sonner-07.tsx
'use client'

import { toast } from 'sonner'

import { Button } from '@/registry/new-york/ui/button'

const SonnerWithPromiseDemo = () => {
  const promise = () =>
    new Promise((resolve, reject) =>
      setTimeout(() => {
        if (Math.random() < 0.5) {
          resolve('foo')
        } else {
          reject('fox')
        }
      }, 2000)
    )

  return (
    <Button
      variant='outline'
      onClick={() =>
        toast.promise(promise, {
          loading: 'Loading...',
          success: 'Toast has been added successfully!',
          error: 'Oops, there was an error adding the toast.'
        })
      }
    >
      Toast with promise
    </Button>
  )
}

export default SonnerWithPromiseDemo

Installation

npx shadcn@latest add @shadcn-studio/sonner-07

Usage

import { Sonner07 } from "@/components/sonner-07"
<Sonner07 />