انتقل إلى المحتوى
الأدلة
التنقل
التنبيهات
الطبقات العائمة
التخطيط
عرض البيانات
النماذج
.md

التنبيهات

toast

قائمة إشعارات منبثقة: أطلق حدث toast على window أو استدعِ toast() لعرض إشعارات متراكمة بأنواع مختلفة وأزرار إجراءات وتحديث في مكانها.

التثبيت

php artisan ui:add toast

الاستخدام

{{-- One region per page, near </body>. position: bottom-right (default),
     bottom-left, bottom-center, top-right, top-left or top-center.
     max: how many show at once (default 3); the rest wait in a queue. --}}
<x-ui.toast position="top-center" :max="3" close-label="Close" />

{{-- Show one by dispatching the event. Every field but title is optional. --}}
<x-ui.button
    onclick="window.dispatchEvent(new CustomEvent('toast', { detail: {
        title: 'Saved',
        description: 'Your changes have been saved.',
        variant: 'success', // default | success | error | info | warning | loading
        duration: 4000,     // 0 or Infinity stays until dismissed
    } }))">
    Save
</x-ui.button>

<script>
    // Same id updates the toast in place and restarts its timer.
    window.dispatchEvent(new CustomEvent('toast', { detail: { id: 'export', title: 'Exporting…', variant: 'loading' } }));
    fetch('/export').then(() => window.dispatchEvent(new CustomEvent('toast', {
        detail: { id: 'export', title: 'Export ready', variant: 'success', action: { label: 'Download', href: '/exports/latest' } },
    })));

    // An action button fires a window event with the toast's detail, then closes it.
    window.dispatchEvent(new CustomEvent('toast', { detail: {
        title: 'Message archived', action: { label: 'Undo', event: 'undo-archive' },
    } }));
    window.addEventListener('undo-archive', (event) => restore(event.detail));

    // Optional helpers do the same and return the id.
    const id = toast.loading('Uploading…');
    toast.success('Uploaded', { id });
    toast.error('Upload failed', { description: 'Try again.' });
    toast.dismiss(id); // no id dismisses all
</script>

{{-- From a controller: flash it, then dispatch it from the layout.

    return back()->with('toast', ['title' => 'Saved', 'variant' => 'success']);
--}}
@if (session('toast'))
    <script>
        window.addEventListener('DOMContentLoaded', () => {
            window.dispatchEvent(new CustomEvent('toast', { detail: @json(session('toast')) }));
        });
    </script>
@endif

ضع منطقة واحدة قرب </body>، فهي تحمل سكربتها المضمّن. اعرض إشعارًا عبر window.dispatchEvent(new CustomEvent('toast', { detail: { title, description, variant, duration, id, action } })) أو الدوال المساعدة مثل toast.success(title, options) التي تُرجع المعرّف. إعادة استخدام id تحدّث الإشعار نفسه (من التحميل إلى النجاح)، وduration: 0 يبقيه ظاهرًا، وaction: { label, event } يطلق حدثًا على window بهذا الاسم. الخصائص: position وmax وlabel وclose-label.

الخصائص

كل خاصية مع قيمتها الافتراضية. مرّر أيًّا منها كسمة، واستخدم : لربط قيمة PHP.

@props([
    'position' => 'bottom-right',
    // How many toasts show at once; the rest wait their turn.
    'max' => 3,
    'label' => 'Notifications',
    'closeLabel' => 'Close',
])