Connect, collaborate, and grow with a community of TypeScript developers
feat: toast notifications ui (#1288)
Mohamed Aziz Ouichni7f22570cf3c9
diff --git a/apps/web/src/app/[locale]/settings/_components/edit-profile-tab.tsx b/apps/web/src/app/[locale]/settings/_components/edit-profile-tab.tsxindex a1f7ef58..eff1636a 100644--- a/apps/web/src/app/[locale]/settings/_components/edit-profile-tab.tsx+++ b/apps/web/src/app/[locale]/settings/_components/edit-profile-tab.tsx@@ -66,7 +66,8 @@ function ProfileForm({ user }: Props) {try {await updateProfile(data);toast({- title: 'profile updated',+ title: 'Profile Updated',+ description: 'Your profile has been successfully updated.',variant: 'success',});} catch (error) {diff --git a/packages/ui/src/components/toast.tsx b/packages/ui/src/components/toast.tsxindex 8555b983..4cbfd506 100644--- a/packages/ui/src/components/toast.tsx+++ b/packages/ui/src/components/toast.tsx@@ -23,14 +23,13 @@ const ToastViewport = React.forwardRef<ToastViewport.displayName = ToastPrimitives.Viewport.displayName;const toastVariants = cva(- 'group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-6 pr-8 shadow-lg transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full data-[state=open]:sm:slide-in-from-bottom-full text-white',+ 'group pointer-events-auto bg-white dark:bg-black/80 to-difficulty-easy border-white border-2 relative flex w-full overflow-hidden rounded-xl border gap-x-3 p-6 pl-4 pr-8 shadow-lg transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full data-[state=open]:sm:slide-in-from-bottom-full',{variants: {variant: {- default: 'border bg-background',- destructive:- 'destructive group border-destructive bg-destructive text-destructive-foreground',- success: 'bg-green-700',+ default: '',+ destructive: '',+ success: '',},},defaultVariants: {@@ -39,6 +38,22 @@ const toastVariants = cva(},);+const bgToastVariants = cva('absolute -left-1 bottom-0 right-0 top-0', {+ variants: {+ variant: {+ default:+ 'bg-[linear-gradient(to_right,#C4B5FD_1px,transparent_1px),linear-gradient(to_bottom,#C4B5FD,transparent_1px)] bg-[size:18px_18px] [mask-image:radial-gradient(ellipse_50%_70%_at_1%_30%,#000_20%,transparent_60%)]',+ destructive:+ 'bg-[linear-gradient(to_right,#FCA5A5_1px,transparent_1px),linear-gradient(to_bottom,#FCA5A5_1px,transparent_1px)] bg-[size:18px_18px] [mask-image:radial-gradient(ellipse_50%_70%_at_1%_30%,#000_20%,transparent_60%)]',+ success:+ 'bg-[linear-gradient(to_right,#86EFAC_1px,transparent_1px),linear-gradient(to_bottom,#86EFAC_1px,transparent_1px)] bg-[size:18px_18px] [mask-image:radial-gradient(ellipse_50%_70%_at_1%_30%,#000_20%,transparent_60%)]',+ },+ defaultVariants: {+ variant: 'default',+ },+ },+});+const Toast = React.forwardRef<React.ElementRef<typeof ToastPrimitives.Root>,React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> & VariantProps<typeof toastVariants>@@ -48,7 +63,10 @@ const Toast = React.forwardRef<className={cn(toastVariants({ variant }), className)}ref={ref}{...props}- />+ >+ <div className={cn(bgToastVariants({ variant }))} />+ {props.children}+ </ToastPrimitives.Root>);});Toast.displayName = ToastPrimitives.Root.displayName;@@ -59,7 +77,7 @@ const ToastAction = React.forwardRef<>(({ className, ...props }, ref) => (<ToastPrimitives.ActionclassName={cn(- 'ring-offset-background focus:ring-ring group-[.destructive]:border-destructive-foreground group-[.destructive]:dark:hover:text-destructive-foreground group-[.destructive]:hover:bg-destructive-foreground group-[.destructive]:hover:text-destructive-foreground group-[.destructive]:focus:ring-destructive inline-flex h-8 shrink-0 items-center justify-center rounded-md border border-white bg-transparent px-3 text-sm font-medium transition-colors hover:bg-transparent/10 focus:outline-none focus:ring-2 focus:ring-offset-0 disabled:pointer-events-none disabled:opacity-50 group-[.destructive]:hover:bg-transparent/10',+ 'ring-offset-background focus:ring-ring group-[.destructive]:border-destructive-foreground group-[.destructive]:dark:hover:text-destructive-foreground group-[.destructive]:hover:bg-destructive-foreground group-[.destructive]:hover:text-destructive-foreground group-[.destructive]:focus:ring-destructive inline-flex h-8 shrink-0 items-center justify-center rounded-md border border-black bg-transparent px-3 text-sm font-medium transition-colors hover:bg-transparent/10 focus:outline-none focus:ring-2 focus:ring-offset-0 disabled:pointer-events-none disabled:opacity-50 group-[.destructive]:hover:bg-transparent/10 dark:border-white',className,)}ref={ref}@@ -90,7 +108,11 @@ const ToastTitle = React.forwardRef<React.ElementRef<typeof ToastPrimitives.Title>,React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>>(({ className, ...props }, ref) => (- <ToastPrimitives.Title className={cn('text-sm font-semibold', className)} ref={ref} {...props} />+ <ToastPrimitives.Title+ className={cn('z-10 text-sm font-semibold', className)}+ ref={ref}+ {...props}+ />));ToastTitle.displayName = ToastPrimitives.Title.displayName;@@ -99,7 +121,7 @@ const ToastDescription = React.forwardRef<React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>>(({ className, ...props }, ref) => (<ToastPrimitives.Description- className={cn('text-sm opacity-90', className)}+ className={cn('z-10 text-sm text-black/50 opacity-90 dark:text-white/50', className)}ref={ref}{...props}/>diff --git a/packages/ui/src/components/toaster.tsx b/packages/ui/src/components/toaster.tsxindex 3bd0de84..3fa885bd 100644--- a/packages/ui/src/components/toaster.tsx+++ b/packages/ui/src/components/toaster.tsx@@ -9,15 +9,31 @@ import {ToastViewport,} from './toast';import { useToast } from './use-toast';+import { CheckCircle2, AlertTriangle, InfoIcon } from 'lucide-react';++const ICONS = {+ default: <InfoIcon fill="#8B5CF6" color="#fff" size={32} />,+ success: <CheckCircle2 fill="#16A34A" color="#fff" size={32} />,+ destructive: <AlertTriangle fill="#DC1C1C" color="#fff" size={32} />,+};export function Toaster() {const { toasts } = useToast();return (<ToastProvider>- {toasts.map(function ({ id, title, description, action, ...props }) {+ {toasts.map(function ({ id, title, description, action, variant, ...props }) {+ const Icon = variant ? ICONS[variant] : null;+return (- <Toast key={id} {...props}>+ <Toast key={id} {...props} variant={variant}>+ <div className="z-10">+ {Icon && (+ <div className="flex h-[50px] w-[50px] items-center justify-center rounded-full border-2 border-white bg-white shadow-[inset_3px_2px_2px_rgba(0,0,0,0.06)]">+ {Icon}+ </div>+ )}+ </div><div className="grid gap-1">{title ? <ToastTitle>{title}</ToastTitle> : null}{description ? <ToastDescription>{description}</ToastDescription> : null}