Notification

在页面放置一次,任意 Alpine 组件可 $dispatch("notify", { type, content }) 发出 Toast。position / appearance / size / duration / event-name 通过 prop 控制。

bordered-left / md / bottom-right (event: notify)

<x-button color="info" appearance="outline"
    x-on:click="$dispatch('notify', { type: 'info', content: 'Checking for updates…' })">info</x-button>
<x-button color="success" appearance="outline"
    x-on:click="$dispatch('notify', { type: 'success', content: 'Saved' })">success</x-button>
<x-button color="warning" appearance="outline"
    x-on:click="$dispatch('notify', { type: 'warning', content: 'Your plan expires soon' })">warning</x-button>
<x-button color="error" appearance="outline"
    x-on:click="$dispatch('notify', { type: 'error', content: 'Connection failed' })">error</x-button>
<x-button color="neutral" appearance="base-100"
    x-on:click="
        $dispatch('notify', { type: 'success', content: 'First of three' });
        setTimeout(() => $dispatch('notify', { type: 'info', content: 'Second of three' }), 200);
        setTimeout(() => $dispatch('notify', { type: 'warning', content: 'Third of three' }), 400);
    ">stack 3</x-button>
加载中…
无法加载 HTML

solid / top-right / sm (event: notify-solid)

<x-button color="info" appearance="solid" size="sm"
    x-on:click="$dispatch('notify-solid', { type: 'info', content: 'Synced' })">info</x-button>
<x-button color="success" appearance="solid" size="sm"
    x-on:click="$dispatch('notify-solid', { type: 'success', content: 'Deploy succeeded' })">success</x-button>
<x-button color="warning" appearance="solid" size="sm"
    x-on:click="$dispatch('notify-solid', { type: 'warning', content: 'CPU usage is high' })">warning</x-button>
<x-button color="error" appearance="solid" size="sm"
    x-on:click="$dispatch('notify-solid', { type: 'error', content: 'Request timed out' })">error</x-button>
加载中…
无法加载 HTML

soft / bottom-left / lg (event: notify-left)

<x-button color="info" appearance="soft" size="sm"
    x-on:click="$dispatch('notify-left', { type: 'info', content: 'Backup started' })">info</x-button>
<x-button color="success" appearance="soft" size="sm"
    x-on:click="$dispatch('notify-left', { type: 'success', content: 'Archive complete' })">success</x-button>
<x-button color="warning" appearance="soft" size="sm"
    x-on:click="$dispatch('notify-left', { type: 'warning', content: 'Storage is running low' })">warning</x-button>
<x-button color="error" appearance="soft" size="sm"
    x-on:click="$dispatch('notify-left', { type: 'error', content: 'Backup failed' })">error</x-button>
加载中…
无法加载 HTML

outline / top-center / sm (event: notify-top)

<x-button color="info" appearance="outline" size="sm"
    x-on:click="$dispatch('notify-top', { type: 'info', content: 'Shown at the top center' })">info</x-button>
<x-button color="success" appearance="outline" size="sm"
    x-on:click="$dispatch('notify-top', { type: 'success', content: 'Copied' })">success</x-button>
<x-button color="warning" appearance="outline" size="sm"
    x-on:click="$dispatch('notify-top', { type: 'warning', content: 'Your session is expiring' })">warning</x-button>
<x-button color="error" appearance="outline" size="sm"
    x-on:click="$dispatch('notify-top', { type: 'error', content: 'You do not have permission' })">error</x-button>
加载中…
无法加载 HTML

用法

1. 只需在 layout 中放置一次

<x-notification-system />
加载中…
无法加载 HTML

2. 从任意组件中 dispatch

<button x-on:click="$dispatch('notify', { type: 'success', content: 'Saved' })">
    Save
</button>
加载中…
无法加载 HTML

3. 也可通过 Laravel 的 session flash 触发

return redirect()->back()->with('notify', ['type' => 'success', 'content' => 'Saved']);
加载中…
无法加载 HTML

4. 想更改位置和外观时

<x-notification-system position="top-right" appearance="solid" size="sm" :duration="5000" />
加载中…
无法加载 HTML

5. 如需在同一页面共存多个,请用 event-name 区分

<x-notification-system position="top-center" event-name="notify-toast" />
<button x-on:click="$dispatch('notify-toast', { type: 'info', content: '…' })">…</button>
加载中…
无法加载 HTML