Skip to content

Commit 4f92976

Browse files
committed
feat(toasts): add flash message handling and integrate sonner for toast notifications
1 parent bc8b863 commit 4f92976

File tree

6 files changed

+87
-6
lines changed

6 files changed

+87
-6
lines changed

app/Http/Middleware/HandleInertiaRequests.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public function share(Request $request): array
5252
...(new Ziggy)->toArray(),
5353
'location' => $request->url(),
5454
],
55+
'flash' => [
56+
'success' => fn () => $request->session()->get('success'),
57+
'error' => fn () => $request->session()->get('error'),
58+
'warning' => fn () => $request->session()->get('warning'),
59+
'info' => fn () => $request->session()->get('info'),
60+
],
5561
'sidebarOpen' => ! $request->hasCookie('sidebar_state') || $request->cookie('sidebar_state') === 'true',
5662
];
5763
}

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@
4848
"globals": "^15.14.0",
4949
"laravel-vite-plugin": "^1.0",
5050
"lucide-react": "^0.475.0",
51+
"next-themes": "^0.4.6",
5152
"react": "^19.0.0",
5253
"react-dom": "^19.0.0",
54+
"sonner": "^2.0.3",
5355
"tailwind-merge": "^3.0.1",
5456
"tailwindcss": "^4.0.0",
5557
"tailwindcss-animate": "^1.0.7",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useTheme } from "next-themes"
2+
import { Toaster as Sonner, ToasterProps } from "sonner"
3+
4+
const Toaster = ({ ...props }: ToasterProps) => {
5+
const { theme = "system" } = useTheme()
6+
7+
return (
8+
<Sonner
9+
theme={theme as ToasterProps["theme"]}
10+
className="toaster group"
11+
style={
12+
{
13+
"--normal-bg": "var(--popover)",
14+
"--normal-text": "var(--popover-foreground)",
15+
"--normal-border": "var(--border)",
16+
} as React.CSSProperties
17+
}
18+
{...props}
19+
/>
20+
)
21+
}
22+
23+
export { Toaster }
Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1+
import { Toaster } from '@/components/ui/sonner';
12
import AppLayoutTemplate from '@/layouts/app/app-sidebar-layout';
2-
import { type BreadcrumbItem } from '@/types';
3+
import { type BreadcrumbItem, SharedData } from '@/types';
4+
import { usePage } from '@inertiajs/react';
35
import { type ReactNode } from 'react';
6+
import { toast } from 'sonner';
47

58
interface AppLayoutProps {
69
children: ReactNode;
710
breadcrumbs?: BreadcrumbItem[];
811
}
912

10-
export default ({ children, breadcrumbs, ...props }: AppLayoutProps) => (
11-
<AppLayoutTemplate breadcrumbs={breadcrumbs} {...props}>
12-
{children}
13-
</AppLayoutTemplate>
14-
);
13+
export default ({ children, breadcrumbs, ...props }: AppLayoutProps) => {
14+
const { flash } = usePage<SharedData>().props;
15+
16+
if (flash.success) {
17+
toast.success(flash.success);
18+
}
19+
if (flash.error) {
20+
toast.error(flash.error);
21+
}
22+
if (flash.info) {
23+
toast.info(flash.info);
24+
}
25+
if (flash.warning) {
26+
toast.warning(flash.warning);
27+
}
28+
29+
return (
30+
<AppLayoutTemplate breadcrumbs={breadcrumbs} {...props}>
31+
<Toaster richColors position="top-right" closeButton />
32+
{children}
33+
</AppLayoutTemplate>
34+
);
35+
};

resources/js/types/index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ export interface SharedData {
2727
quote: { message: string; author: string };
2828
auth: Auth;
2929
ziggy: Config & { location: string };
30+
flash: {
31+
success?: string;
32+
error?: string;
33+
info?: string;
34+
warning?: string;
35+
};
36+
3037
sidebarOpen: boolean;
38+
3139
[key: string]: unknown;
3240
}
3341

@@ -39,5 +47,6 @@ export interface User {
3947
email_verified_at: string | null;
4048
created_at: string;
4149
updated_at: string;
50+
4251
[key: string]: unknown; // This allows for additional properties...
4352
}

0 commit comments

Comments
 (0)