# Toast Notification

Toast notification system with multiple toasts, stacking animations, and hover effects using Motion for React.

Live demo: https://bearerop.in/craft/toast-notification

## Install

### 1. Install dependencies

```bash
npm install motion lucide-react
```

### 2. Copy and paste the following code into your project.

#### Usage

```tsx
// Step 1: Add ToastProvider to your root layout (recommended for global usage)
// src/app/layout.tsx or src/components/toast-provider.tsx

'use client'

import { useToast } from '@/hooks/use-toast-notification'
import { ToastContainer } from '@/components/craft/toast-container'

export function ToastProvider() {
    const { toasts, removeToast } = useToast()
    return <ToastContainer toasts={toasts} onClose={removeToast} />
}

// Then in your layout.tsx:
import { ToastProvider } from '@/components/toast-provider'

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        {children}
        <ToastProvider />
      </body>
    </html>
  )
}

// Step 2: Use the toast hook in any component
'use client'

import { useToast } from '@/hooks/use-toast-notification'

export default function MyComponent() {
  const { success, warning, danger, info } = useToast()
  
  return (
    <div>
      <button onClick={() => success('Success!', 'Your action was completed')}>
        Show Success Toast
      </button>
      <button onClick={() => warning('Warning!', 'Please review your settings')}>
        Show Warning Toast
      </button>
      <button onClick={() => danger('Error!', 'Something went wrong')}>
        Show Error Toast
      </button>
      <button onClick={() => info('Info', 'Here is some information')}>
        Show Info Toast
      </button>
    </div>
  )
}
```

#### Source

```tsx
// toast-notification.tsx
'use client'

import { useEffect } from 'react'
import { motion, AnimatePresence } from 'motion/react'
import { CheckCircle2, AlertTriangle, XCircle, Info, X } from 'lucide-react'

export type ToastVariant = 'success' | 'warning' | 'danger' | 'info'
export type ToastWidth = 'sm' | 'md' | 'lg' | 'full'

export interface ToastProps {
  isOpen: boolean
  onClose: () => void
  variant?: ToastVariant
  title: string
  message: string
  duration?: number
  showCloseButton?: boolean
  width?: ToastWidth
  standalone?: boolean
}

const variantConfig = {
  success: { icon: CheckCircle2, iconBg: 'bg-green-500/15', iconColor: 'text-green-300' },
  warning: { icon: AlertTriangle, iconBg: 'bg-yellow-500/15', iconColor: 'text-yellow-300' },
  danger: { icon: XCircle, iconBg: 'bg-red-500/15', iconColor: 'text-red-300' },
  info: { icon: Info, iconBg: 'bg-blue-500/15', iconColor: 'text-blue-300' },
}

const widthConfig: Record<ToastWidth, string> = {
  sm: 'sm:max-w-sm', md: 'sm:max-w-md', lg: 'sm:max-w-lg', full: 'sm:max-w-2xl',
}

export function Toast({ isOpen, onClose, variant = 'info', title, message, duration = 5000, showCloseButton = true, width = 'md', standalone = true }: ToastProps) {
  const config = variantConfig[variant]
  const IconComponent = config.icon
  const widthClass = widthConfig[width]

  useEffect(() => {
    if (isOpen && duration > 0 && standalone) {
      const timer = setTimeout(() => onClose(), duration)
      return () => clearTimeout(timer)
    }
  }, [isOpen, duration, onClose, standalone])

  const toastContent = (
    <motion.div initial={{ opacity: 0, y: standalone ? 100 : 0 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: standalone ? 100 : 0 }} transition={{ duration: 0.4, ease: [0.32, 0.72, 0, 1] }} className="pointer-events-auto flex items-center gap-2 sm:gap-3 rounded-xl sm:rounded-2xl bg-[#141118] px-3 sm:px-4 py-2.5 sm:py-3 text-xs text-white shadow-[0_18px_60px_rgba(0,0,0,0.75)] border border-white/10 backdrop-blur-xl">
      <motion.div initial={{ scale: 0.9, opacity: 0 }} animate={{ scale: 1, opacity: 1 }} transition={{ type: 'spring', duration: 0.4, bounce: 0, delay: 0.15 }} className={`flex h-5 w-5 sm:h-6 sm:w-6 items-center justify-center rounded-full ${config.iconBg} ${config.iconColor} flex-shrink-0`}>
        <IconComponent className="h-3 w-3 sm:h-3.5 sm:w-3.5" />
      </motion.div>
      <div className="space-y-0.5 min-w-0 flex-1">
        <motion.p initial={{ opacity: 0, y: 4 }} animate={{ opacity: 1, y: 0 }} transition={{ duration: 0.3, delay: 0.1, ease: [0.32, 0.72, 0, 1] }} className="font-medium text-xs sm:text-sm">{title}</motion.p>
        <motion.p initial={{ opacity: 0, y: 4 }} animate={{ opacity: 1, y: 0 }} transition={{ duration: 0.3, delay: 0.15, ease: [0.32, 0.72, 0, 1] }} className="text-[10px] sm:text-[11px] text-white/70">{message}</motion.p>
      </div>
      {showCloseButton && <motion.button initial={{ opacity: 0, scale: 0.9 }} animate={{ opacity: 1, scale: 1, transition: { duration: 0.2, delay: 0.25, ease: [0.32, 0.72, 0, 1] } }} whileTap={{ scale: 0.97 }} transition={{ duration: 0.15, ease: 'easeOut' }} onClick={onClose} className="flex-shrink-0 p-1 hover:bg-white/10 rounded-lg transition-colors" aria-label="Close notification"><X className="h-3.5 w-3.5 sm:h-4 sm:w-4 text-white/50 hover:text-white/80" /></motion.button>}
    </motion.div>
  )

  if (standalone) {
    return <AnimatePresence mode="wait">{isOpen && <div className={`pointer-events-none fixed bottom-4 sm:bottom-6 left-1/2 z-[60] -translate-x-1/2 w-full max-w-[calc(100%-2rem)] ${widthClass} px-4 sm:px-0`}>{toastContent}</div>}</AnimatePresence>
  }

  return <div className={`w-full ${widthClass} mx-auto`}>{toastContent}</div>
}
```

## Features

- **Multiple toasts support**
- **Stacking animations**
- **Hover to expand**
- **Auto-dismiss (configurable)**
- **4 variants (success, warning, danger, info)**
- **4 width options (sm, md, lg, full)**
- **Mobile responsive**
- **TypeScript support**

## Props

| Component | Prop | Type | Default |
| --- | --- | --- | --- |
| Toast | variant | success \| warning \| danger \| info | info |
| Toast | width | sm \| md \| lg \| full | md |
| Toast | duration | number | 5000 |
| ToastContainer | maxVisible | number | 3 |
