用法
使用 useToast 可组合函数在你的应用中显示一个 Toast。
标题
将 title
字段传递给 toast.add
方法以显示标题。
<script setup lang="ts">
const props = defineProps<{
title: string
}>()
const toast = useToast()
function showToast() {
toast.add(props)
}
</script>
<template>
<UButton label="Show toast" color="neutral" variant="outline" @click="showToast" />
</template>
描述
将 description
字段传递给 toast.add
方法以显示描述。
<script setup lang="ts">
const props = defineProps<{
title: string
description: string
}>()
const toast = useToast()
function showToast() {
toast.add(props)
}
</script>
<template>
<UButton label="Show toast" color="neutral" variant="outline" @click="showToast" />
</template>
Icon
将 icon
字段传递给 toast.add
方法以显示 Icon。
<script setup lang="ts">
const props = defineProps<{
icon: string
}>()
const toast = useToast()
function showToast() {
toast.add({
title: 'Uh oh! Something went wrong.',
description: 'There was a problem with your request.',
icon: props.icon
})
}
</script>
<template>
<UButton label="Show toast" color="neutral" variant="outline" @click="showToast" />
</template>
Avatar
将 avatar
字段传递给 toast.add
方法以显示 Avatar。
<script setup lang="ts">
import type { AvatarProps } from '@nuxt/ui'
const props = defineProps<{
avatar: AvatarProps
}>()
const toast = useToast()
function showToast() {
toast.add({
title: 'User invited',
description: 'benjamincanac was invited to the team.',
avatar: props.avatar
})
}
</script>
<template>
<UButton label="Invite user" color="neutral" variant="outline" @click="showToast" />
</template>
颜色
将 color
字段传递给 toast.add
方法以更改 Toast 的颜色。
<script setup lang="ts">
import type { ToastProps } from '@nuxt/ui'
const props = defineProps<{
color: ToastProps['color']
}>()
const toast = useToast()
function showToast() {
toast.add({
title: 'Uh oh! Something went wrong.',
description: 'There was a problem with your request.',
icon: 'i-lucide-wifi',
color: props.color
})
}
</script>
<template>
<UButton label="Show toast" color="neutral" variant="outline" @click="showToast" />
</template>
关闭
传递 close
字段来自定义或隐藏关闭按钮(使用 false
值)。
<script setup lang="ts">
const toast = useToast()
function showToast() {
toast.add({
title: 'Uh oh! Something went wrong.',
description: 'There was a problem with your request.',
icon: 'i-lucide-wifi',
close: {
color: 'primary',
variant: 'outline',
class: 'rounded-full'
}
})
}
</script>
<template>
<UButton label="Show toast" color="neutral" variant="outline" @click="showToast" />
</template>
关闭图标
传递 closeIcon
字段来自定义关闭按钮的 Icon。默认为 i-lucide-x
。
<script setup lang="ts">
const props = defineProps<{
closeIcon: string
}>()
const toast = useToast()
function showToast() {
toast.add({
title: 'Uh oh! Something went wrong.',
description: 'There was a problem with your request.',
closeIcon: props.closeIcon
})
}
</script>
<template>
<UButton label="Show toast" color="neutral" variant="outline" @click="showToast" />
</template>
操作
传递 actions
字段来为 Alert 添加一些 Button 操作。
<script setup lang="ts">
const toast = useToast()
const props = defineProps<{
description: string
}>()
function showToast() {
toast.add({
title: 'Uh oh! Something went wrong.',
description: props.description,
actions: [{
icon: 'i-lucide-refresh-cw',
label: 'Retry',
color: 'neutral',
variant: 'outline',
onClick: (e) => {
e?.stopPropagation()
}
}]
})
}
</script>
<template>
<UButton label="Show toast" color="neutral" variant="outline" @click="showToast" />
</template>
方向
使用 orientation
属性更改 Toast 的方向。
<script setup lang="ts">
const toast = useToast()
const props = defineProps<{
orientation: 'horizontal' | 'vertical'
}>()
function showToast() {
toast.add({
title: 'Uh oh! Something went wrong.',
orientation: props.orientation,
actions: [{
icon: 'i-lucide-refresh-cw',
label: 'Retry',
color: 'neutral',
variant: 'outline',
onClick: (e) => {
e?.stopPropagation()
}
}]
})
}
</script>
<template>
<UButton label="Show toast" color="neutral" variant="outline" @click="showToast" />
</template>
示例
更改全局位置
更改 App 组件上的 toaster.position
属性以更改 Toast 的位置。
<script setup lang="ts">
const toast = useToast()
function addToCalendar() {
const eventDate = new Date(Date.now() + Math.random() * 31536000000)
const formattedDate = eventDate.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric'
})
toast.add({
title: 'Event added to calendar',
description: `This event is scheduled for ${formattedDate}.`,
icon: 'i-lucide-calendar-days'
})
}
</script>
<template>
<UButton
label="Add to calendar"
color="neutral"
variant="outline"
icon="i-lucide-plus"
@click="addToCalendar"
/>
</template>
更改全局持续时间
更改 App 组件上的 toaster.duration
属性以更改 Toast 的持续时间。
<script setup lang="ts">
const toast = useToast()
function addToCalendar() {
const eventDate = new Date(Date.now() + Math.random() * 31536000000)
const formattedDate = eventDate.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric'
})
toast.add({
title: 'Event added to calendar',
description: `This event is scheduled for ${formattedDate}.`,
icon: 'i-lucide-calendar-days'
})
}
</script>
<template>
<UButton
label="Add to calendar"
color="neutral"
variant="outline"
icon="i-lucide-plus"
@click="addToCalendar"
/>
</template>
堆叠 Toast
将 App 组件上的 toaster.expand
属性设置为 false
以显示堆叠 Toast。
<script setup lang="ts">
const toast = useToast()
function addToCalendar() {
const eventDate = new Date(Date.now() + Math.random() * 31536000000)
const formattedDate = eventDate.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric'
})
toast.add({
title: 'Event added to calendar',
description: `This event is scheduled for ${formattedDate}.`,
icon: 'i-lucide-calendar-days'
})
}
</script>
<template>
<UButton
label="Add to calendar"
color="neutral"
variant="outline"
icon="i-lucide-plus"
@click="addToCalendar"
/>
</template>
API
属性
属性 | 默认值 | 类型 |
---|---|---|
as |
|
此组件应渲染为的元素或组件。 |
title |
| |
description |
| |
icon |
| |
avatar |
| |
color |
|
|
orientation |
|
内容与操作之间的方向。 |
actions |
显示操作列表
| |
关闭 |
|
显示一个关闭按钮以关闭吐司通知。 |
closeIcon |
|
关闭按钮中显示的图标。 |
类型 |
控制吐司通知的可访问性敏感度。 对于用户操作产生的吐司通知,请选择 | |
默认打开 |
对话框初次渲染时的打开状态。当你不需要控制其打开状态时使用。 | |
打开 |
对话框的受控打开状态。可以绑定为 | |
持续时间 |
吐司通知应保持可见的毫秒数。覆盖提供给 | |
UI 配置 |
|
插槽
插槽 | 类型 |
---|---|
leading |
|
title |
|
description |
|
actions |
|
关闭 |
|
事件
事件 | 类型 |
---|---|
pause |
|
escapeKeyDown |
|
resume |
|
swipeStart |
|
swipeMove |
|
swipeCancel |
|
swipeEnd |
|
update:open |
|
主题
export default defineAppConfig({
ui: {
toast: {
slots: {
root: 'relative group overflow-hidden bg-default shadow-lg rounded-lg ring ring-default p-4 flex gap-2.5 focus:outline-none',
wrapper: 'w-0 flex-1 flex flex-col',
title: 'text-sm font-medium text-highlighted',
description: 'text-sm text-muted',
icon: 'shrink-0 size-5',
avatar: 'shrink-0',
avatarSize: '2xl',
actions: 'flex gap-1.5 shrink-0',
progress: 'absolute inset-x-0 bottom-0 h-1 z-[-1]',
close: 'p-0'
},
variants: {
color: {
primary: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-primary',
icon: 'text-primary',
progress: 'bg-primary'
},
secondary: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-secondary',
icon: 'text-secondary',
progress: 'bg-secondary'
},
success: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-success',
icon: 'text-success',
progress: 'bg-success'
},
info: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-info',
icon: 'text-info',
progress: 'bg-info'
},
warning: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-warning',
icon: 'text-warning',
progress: 'bg-warning'
},
error: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-error',
icon: 'text-error',
progress: 'bg-error'
},
neutral: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-inverted',
icon: 'text-highlighted',
progress: 'bg-inverted'
}
},
orientation: {
horizontal: {
root: 'items-center',
actions: 'items-center'
},
vertical: {
root: 'items-start',
actions: 'items-start mt-2.5'
}
},
title: {
true: {
description: 'mt-1'
}
}
},
defaultVariants: {
color: 'primary'
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
toast: {
slots: {
root: 'relative group overflow-hidden bg-default shadow-lg rounded-lg ring ring-default p-4 flex gap-2.5 focus:outline-none',
wrapper: 'w-0 flex-1 flex flex-col',
title: 'text-sm font-medium text-highlighted',
description: 'text-sm text-muted',
icon: 'shrink-0 size-5',
avatar: 'shrink-0',
avatarSize: '2xl',
actions: 'flex gap-1.5 shrink-0',
progress: 'absolute inset-x-0 bottom-0 h-1 z-[-1]',
close: 'p-0'
},
variants: {
color: {
primary: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-primary',
icon: 'text-primary',
progress: 'bg-primary'
},
secondary: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-secondary',
icon: 'text-secondary',
progress: 'bg-secondary'
},
success: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-success',
icon: 'text-success',
progress: 'bg-success'
},
info: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-info',
icon: 'text-info',
progress: 'bg-info'
},
warning: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-warning',
icon: 'text-warning',
progress: 'bg-warning'
},
error: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-error',
icon: 'text-error',
progress: 'bg-error'
},
neutral: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-inverted',
icon: 'text-highlighted',
progress: 'bg-inverted'
}
},
orientation: {
horizontal: {
root: 'items-center',
actions: 'items-center'
},
vertical: {
root: 'items-start',
actions: 'items-start mt-2.5'
}
},
title: {
true: {
description: 'mt-1'
}
}
},
defaultVariants: {
color: 'primary'
}
}
}
})
]
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import uiPro from '@nuxt/ui-pro/vite'
export default defineConfig({
plugins: [
vue(),
uiPro({
ui: {
toast: {
slots: {
root: 'relative group overflow-hidden bg-default shadow-lg rounded-lg ring ring-default p-4 flex gap-2.5 focus:outline-none',
wrapper: 'w-0 flex-1 flex flex-col',
title: 'text-sm font-medium text-highlighted',
description: 'text-sm text-muted',
icon: 'shrink-0 size-5',
avatar: 'shrink-0',
avatarSize: '2xl',
actions: 'flex gap-1.5 shrink-0',
progress: 'absolute inset-x-0 bottom-0 h-1 z-[-1]',
close: 'p-0'
},
variants: {
color: {
primary: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-primary',
icon: 'text-primary',
progress: 'bg-primary'
},
secondary: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-secondary',
icon: 'text-secondary',
progress: 'bg-secondary'
},
success: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-success',
icon: 'text-success',
progress: 'bg-success'
},
info: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-info',
icon: 'text-info',
progress: 'bg-info'
},
warning: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-warning',
icon: 'text-warning',
progress: 'bg-warning'
},
error: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-error',
icon: 'text-error',
progress: 'bg-error'
},
neutral: {
root: 'focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-inverted',
icon: 'text-highlighted',
progress: 'bg-inverted'
}
},
orientation: {
horizontal: {
root: 'items-center',
actions: 'items-center'
},
vertical: {
root: 'items-start',
actions: 'items-start mt-2.5'
}
},
title: {
true: {
description: 'mt-1'
}
}
},
defaultVariants: {
color: 'primary'
}
}
}
})
]
})