Spaces:
Runtime error
Runtime error
Download Frontend/src/components/ui/Toast.jsx from jinksQspider/ExamCraftAI: direct link, hf CLI and curl.
- Browser
- Download file 4.56 kB
-
https://huggingface.co/spaces/jinksQspider/ExamCraftAI/resolve/main/Frontend/src/components/ui/Toast.jsx
- Command line
-
hf download hf://spaces/jinksQspider/ExamCraftAI/Frontend/src/components/ui/Toast.jsx
-
curl -L -o Toast.jsx https://huggingface.co/spaces/jinksQspider/ExamCraftAI/resolve/main/Frontend/src/components/ui/Toast.jsx
4.56 kB
| /* eslint-disable react-refresh/only-export-components */ | |
| import React, { createContext, useContext, useState, useCallback } from "react"; | |
| import { motion, AnimatePresence } from "framer-motion"; | |
| import { CheckCircle2, XCircle, AlertCircle, Info, X } from "lucide-react"; | |
| import { cn } from "../../utils/cn"; | |
| const ToastContext = createContext(); | |
| export function ToastProvider({ children }) { | |
| const [toasts, setToasts] = useState([]); | |
| const removeToast = useCallback((id) => { | |
| setToasts((prev) => prev.filter((toast) => toast.id !== id)); | |
| }, []); | |
| const addToast = useCallback(({ title, description, variant = "default", duration = 3000 }) => { | |
| const id = Math.random().toString(36).substr(2, 9); | |
| setToasts((prev) => [...prev, { id, title, description, variant, duration }]); | |
| if (duration !== Infinity) { | |
| setTimeout(() => { | |
| removeToast(id); | |
| }, duration); | |
| } | |
| }, [removeToast]); | |
| return ( | |
| <ToastContext.Provider value={{ addToast, toasts, removeToast }}> | |
| {children} | |
| <ToastContainer toasts={toasts} removeToast={removeToast} /> | |
| </ToastContext.Provider> | |
| ); | |
| } | |
| const icons = { | |
| default: <Info className="h-5 w-5 text-primary" />, | |
| success: <CheckCircle2 className="h-5 w-5 text-emerald-500" />, | |
| error: <XCircle className="h-5 w-5 text-destructive" />, | |
| warning: <AlertCircle className="h-5 w-5 text-accent" />, | |
| }; | |
| function ToastContainer({ toasts, removeToast }) { | |
| return ( | |
| <div className="fixed bottom-0 right-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 sm:bottom-0 sm:right-0 sm:top-auto sm:flex-col md:max-w-[420px]"> | |
| <AnimatePresence> | |
| {toasts.map((toast) => ( | |
| <motion.div | |
| key={toast.id} | |
| initial={{ opacity: 0, x: 50, scale: 0.95 }} | |
| animate={{ opacity: 1, x: 0, scale: 1 }} | |
| exit={{ opacity: 0, x: 100, scale: 0.9 }} | |
| className={cn( | |
| "group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-4 shadow-lg transition-all mt-4", | |
| "bg-background/80 backdrop-blur-md border border-border" | |
| )} | |
| style={{ borderRadius: "var(--radius)" }} | |
| > | |
| <div className="flex gap-3 items-start flex-1"> | |
| <div className="shrink-0 pt-0.5">{icons[toast.variant || "default"]}</div> | |
| <div className="grid gap-1"> | |
| {toast.title && <div className="text-sm font-semibold">{toast.title}</div>} | |
| {toast.description && ( | |
| <div className="text-sm opacity-90">{toast.description}</div> | |
| )} | |
| </div> | |
| </div> | |
| <button | |
| onClick={() => removeToast(toast.id)} | |
| className="absolute right-2 top-2 rounded-md p-1 text-foreground/50 opacity-0 transition-opacity hover:text-foreground focus:opacity-100 focus:outline-none group-hover:opacity-100" | |
| > | |
| <X className="h-4 w-4" /> | |
| </button> | |
| <div | |
| className={cn("absolute bottom-0 left-0 h-1", | |
| toast.variant === "success" ? "bg-emerald-500" : | |
| toast.variant === "error" ? "bg-destructive" : | |
| toast.variant === "warning" ? "bg-accent" : "bg-primary" | |
| )} | |
| style={{ | |
| animation: `toast-progress ${toast.duration}ms linear forwards`, | |
| }} | |
| /> | |
| </motion.div> | |
| ))} | |
| </AnimatePresence> | |
| <style dangerouslySetInnerHTML={{ | |
| __html: ` | |
| @keyframes toast-progress { | |
| from { width: 100%; } | |
| to { width: 0%; } | |
| } | |
| `}} /> | |
| </div> | |
| ); | |
| } | |
| export const useToast = () => { | |
| const context = useContext(ToastContext); | |
| if (!context) throw new Error("useToast must be used within ToastProvider"); | |
| return context; | |
| }; | |