Files
heroui-vue/apps/docs/demos/toast-callbacks.vue
T

113 lines
3.3 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { ref } from "vue";
import { IconCheck } from "@tabler/icons-vue";
import { onBeforeUnmount } from "vue";
import {
Button,
ToastProvider,
ToastQueue,
createToastFunction,
} from "@heroui-vue/components";
const queue = new ToastQueue();
const toast = createToastFunction(queue);
onBeforeUnmount(() => queue.destroy());
const closedHistory = ref<{ message: string; time: string }[]>([]);
function history(message: string) {
closedHistory.value = [
{ message, time: new Date().toLocaleTimeString() },
...closedHistory.value,
].slice(0, 5);
}
</script>
<template>
<div class="flex h-full max-w-2xl flex-col items-center justify-center gap-6">
<div class="flex w-full flex-wrap items-center justify-center gap-4">
<Button
size="sm"
variant="secondary"
@click="
toast('文件已保存', {
timeout: 3000,
onClose: () => history('文件已保存(3 秒后关闭)'),
})
"
>
自定义超时(3 秒)
</Button>
<Button
size="sm"
variant="secondary"
@click="
toast('更改已保存', {
timeout: 10000,
onClose: () => history('更改已保存(10 秒后关闭)'),
})
"
>
自定义超时(10 秒)
</Button>
<Button
size="sm"
variant="secondary"
@click="
toast.success('活动已创建', {
onClose: () => history('活动已创建(默认超时后关闭)'),
})
"
>
使用 onClose 回调
</Button>
<Button
size="sm"
variant="secondary"
@click="
toast('重要通知', {
description: '此 Toast 将保持显示直至关闭',
timeout: 0,
onClose: () => history('重要通知(手动关闭)'),
})
"
>
持久显示 Toast
</Button>
</div>
<div class="w-full space-y-2">
<div class="flex items-center justify-between">
<h3 class="text-sm font-medium">关闭历史</h3>
<Button
v-if="closedHistory.length"
size="sm"
variant="tertiary"
class="h-6 text-xs"
@click="closedHistory = []"
>
清空
</Button>
</div>
<div
class="min-h-[120px] space-y-2 rounded-lg border border-border bg-surface p-4"
>
<p v-if="!closedHistory.length" class="text-sm text-muted">
尚无已关闭的 Toast。请尝试关闭上方的 Toast!
</p>
<div
v-for="(item, index) in closedHistory"
:key="item.time + '-' + index"
class="flex items-start justify-between gap-3 rounded-md border border-border bg-default px-3 py-2 text-sm"
>
<div class="flex-1">
<span class="font-medium">{{ item.message }}</span>
<span class="ms-2 text-xs text-muted">({{ item.time }})</span>
</div>
<div
class="flex size-5 shrink-0 items-center justify-center rounded-full bg-success/10 text-success"
>
<IconCheck class="size-3" aria-hidden="true" />
</div>
</div>
</div>
</div>
<ToastProvider :queue="queue" to="body" />
</div>
</template>