Created
April 3, 2023 22:11
-
-
Save onlime/485b3a9de40649493fa6e218d6d448fd to your computer and use it in GitHub Desktop.
Vue3-Toastify in a Laravel/Inertia/Vue project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ref, watch } from 'vue' | |
import { usePage } from '@inertiajs/vue3' | |
import { toast } from 'vue3-toastify' | |
const toasts = ref() | |
const reToasted = ref(false) | |
function fireToast(notification, sticky = false) { | |
toast(notification.message, { | |
toastId: notification.id, | |
type: notification.type, | |
newestOnTop: true, | |
theme: toast.THEME.COLORED, | |
autoClose: !sticky && (notification.type == 'success' ? 5000 : false), | |
closeOnClick: !sticky, | |
onClose: () => (reToasted.value = false), | |
}) | |
} | |
function fireToasts(sticky = false) { | |
toasts.value?.forEach((notification) => fireToast(notification, sticky)) | |
} | |
function toastAgain() { | |
reToasted.value ? toast.remove() : fireToasts(true) | |
reToasted.value = !reToasted.value | |
} | |
watch( | |
// NOTE: Since Inertia.js 1.0.1, usePage() may return null initially. | |
() => usePage()?.props?.flash.toasts, | |
(newToasts) => { | |
reToasted.value = false | |
toasts.value = newToasts | |
fireToasts() | |
} | |
) | |
export function useToasts() { | |
return { | |
toasts, | |
toastAgain, | |
reToasted, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script setup> | |
import { BellAlertIcon, BellSlashIcon } from '@heroicons/vue/24/outline' | |
import { useToasts } from '@/Composables/ToastNotifications' | |
const { toasts, toastAgain, reToasted } = useToasts() | |
</script> | |
<template> | |
<div> | |
<button | |
v-if="toasts" | |
v-tooltip="$t('Notifications')" | |
class="flex h-10 w-10 items-center justify-center rounded-full bg-gray-100 hover:bg-gray-800 hover:text-white dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600 dark:hover:text-white" | |
:class="{ 'animate-pulse': !reToasted }" | |
@click="toastAgain()" | |
> | |
<component :is="reToasted ? 'BellSlashIcon' : 'BellAlertIcon'" class="h-5 w-5" /> | |
</button> | |
</div> | |
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import './bootstrap' | |
import 'vue3-toastify/dist/index.css' | |
import '../css/app.css' | |
import { createApp, h } from 'vue' | |
import { createInertiaApp } from '@inertiajs/vue3' | |
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers' | |
import Vue3Toastify from 'vue3-toastify' | |
createInertiaApp({ | |
resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')), | |
setup({ el, App, props, plugin }) { | |
return createApp({ render: () => h(App, props) }) | |
.use(plugin) | |
.use(Vue3Toastify) | |
.mount(el) | |
}, | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Inertia\Middleware; | |
class HandleInertiaRequests extends Middleware | |
{ | |
public function share(Request $request) | |
{ | |
return array_merge(parent::share($request), [ | |
'flash' => fn () => [ | |
'toasts'=> $request->session()->get('toasts'), | |
], | |
// ... | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (! function_exists('toast')) { | |
function toast(ToastType $type, string $message, ?RedirectResponse $response = null) | |
{ | |
$toasts = session()->get('toasts', []); | |
$toasts[] = [ | |
'id' => Str::uuid(), | |
'type' => $type->value, | |
'message' => $message, | |
]; | |
if ($response) { | |
return $response->with('toasts', $toasts); | |
} else { | |
session()->flash('toasts', $toasts); | |
} | |
} | |
} | |
if (! function_exists('toast_success')) { | |
function toast_success(string $message) | |
{ | |
return toast(ToastType::SUCCESS, $message); | |
} | |
} | |
if (! function_exists('toast_warning')) { | |
function toast_warning(string $message) | |
{ | |
return toast(ToastType::WARNING, $message); | |
} | |
} | |
if (! function_exists('toast_error')) { | |
function toast_error(string $message) | |
{ | |
return toast(ToastType::ERROR, $message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Enums; | |
enum ToastType: string | |
{ | |
case SUCCESS = 'success'; | |
case WARNING = 'warning'; | |
case ERROR = 'error'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, how to clear notifications in the session?