overlays
Notification
Display a toast notification in your app.
Usage
First of all, add the Notifications
component to your app, preferably inside app.vue
.
<template>
<div>
<UContainer>
<NuxtPage />
</UContainer>
<UNotifications />
</div>
</template>
This component will render the notifications at the bottom right of the screen by default. You can configure its behavior in the app.config.ts
through ui.notifications
:
export default defineAppConfig({
ui: {
notifications: {
// Show toasts at the top right of the screen
position: 'top-0 right-0'
}
}
})
Then, you can use the useToast
composable to add notifications to your app:
<script setup>
const toast = useToast()
</script>
<template>
<UButton label="Show toast" @click="toast.add({ title: 'Hello world!' })" />
</template>
When using toast.add
, this will push a new notification to the stack displayed in <UNotifications />
. All the props of the Notification
component can be passed to toast.add
.
<script setup>
const toast = useToast()
onMounted(() => {
toast.add({
id: 'update_downloaded',
title: 'Update downloaded.',
description: 'It will be installed on restart. Restart now?',
icon: 'i-octicon-desktop-download-24',
timeout: 0,
actions: [{
label: 'Restart',
click: () => {
}
}]
})
})
</script>
You can also use the Notification
component directly in your app as an alert for example.
Title
Pass a title
to your Notification.
Notification
<UNotification title="Notification" />
Description
You can add a description
in addition of the title
.
Notification
This is a notification.
<UNotification description="This is a notification." />
Icon
Use any icon from Iconify by setting the icon
prop by using this pattern: i-{collection_name}-{icon_name}
or change it globally in ui.notification.default.icon
.
Notification
This is a notification.
<UNotification icon="i-heroicons-check-circle" description="This is a notification." />
Avatar
Use the avatar prop as an object
and configure it with any of its props.
Notification
This is a notification.
<UNotification description="This is a notification." :avatar="{ src: 'https://avatars.githubusercontent.com/u/739984?v=4' }" />
Timeout
Use the timeout
prop to configure how long the Notification will remain. Set it to 0
to disable the timeout.
You will see a progress bar at the bottom of the Notification which will indicate the remaining time. When hovering the Notification, the progress bar will be paused.
Notification
This is a notification.
<UNotification :timeout="60000" />
Style
Use the color
prop to change the progress and icon color of the Notification.
Notification
This is a notification.
<UNotification icon="i-heroicons-check-badge" color="primary" />
Click
Use the click
prop to execute a function when the Notification is clicked.
<script setup>
const toast = useToast()
function onClick () {
alert('Clicked!')
}
</script>
<template>
<UButton label="Show toast" @click="toast.add({ title: 'Click me', click: onClick })" />
</template>
Callback
Use the callback
prop to execute a function when the Notification expires.
<script setup>
const toast = useToast()
function onCallback () {
alert('Notification expired!')
}
</script>
<template>
<UButton label="Show toast" @click="toast.add({ title: 'Expires soon...', timeout: 1000, callback: onCallback })" />
</template>
Close
Use the close-button
prop to hide or customize the close button on the Notification.
You can pass all the props of the Button component to customize it through the close-button
prop or globally through ui.notification.default.closeButton
.
Notification
<UNotification :close-button="{ icon: 'i-heroicons-archive-box-x-mark', color: 'primary', variant: 'outline', padded: true, size: '2xs', ui: { rounded: 'rounded-full' } }" />
Actions
Use the actions
prop to add actions to the Notification.
<script setup>
const toast = useToast()
</script>
<template>
<UButton label="Show toast" @click="toast.add({ title: 'Click me', click: () => alert('Clicked!') })" />
</template>
Like for closeButton
, you can pass all the props of the Button component inside the action or globally through ui.notification.default.actionButton
.
Notification
<UNotification :actions="[{ label: 'Action 1' }, { variant: 'solid', color: 'gray', label: 'Action 2' }]" />
Actions will render differently whether you have a description
set.
Notification
This is a notification.
<UNotification :actions="[{ variant: 'solid', color: 'primary', label: 'Action 1' }, { variant: 'outline', color: 'primary', label: 'Action 2' }]" />
Slots
title
/ description
Use the #title
and #description
slots to customize the Notification.
This can be handy when you want to display HTML content. To achieve this, you can define those slots in the top-level <UNotifications />
component in your app.vue
and use the v-html
directive.
<template>
<UNotifications>
<template #title="{ title }">
<span v-html="title" />
</template>
<template #description="{ description }">
<span v-html="description" />
</template>
</UNotifications>
</template>
This way, you can use HTML tags in the title
and description
props when using useToast
.
<script setup>
const toast = useToast()
</script>
<template>
<UButton label="Show toast" @click="toast.add({ title: 'Notification <i>italic</i>', description: 'This is an <u>underlined</u> and <b>bold</b> notification.' })" />
</template>
<UNotifications />
component are automatically passed down to the Notification
children.Props
undefined
null
config.default.color
null
config.default.icon
config.default.closeButton as Button
5000
[]
null
Preset
undefined