Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useWebNotification.md
1---2category: Browser3---45# useWebNotification67Reactive [Notification](https://developer.mozilla.org/en-US/docs/Web/API/notification)89The Web Notification interface of the Notifications API is used to configure and display desktop notifications to the user.1011## Usage1213::: tip14Before an app can send a notification, the user must grant the application the right to do so. The user's OS settings may also prevent expected notification behaviour.15:::1617```ts18import { useWebNotification } from '@vueuse/core'1920const {21isSupported,22notification,23permissionGranted,24show,25close,26onClick,27onShow,28onError,29onClose,30} = useWebNotification({31title: 'Hello, VueUse world!',32dir: 'auto',33lang: 'en',34renotify: true,35tag: 'test',36})3738if (isSupported.value && permissionGranted.value)39show()40```4142This composable also utilizes the createEventHook utility from '@vueuse/shared`:4344```ts45import { useWebNotification } from '@vueuse/core'4647const { onClick, onShow, onError, onClose, } = useWebNotification()48// ---cut---49onClick((evt: Event) => {50// Do something with the notification on:click event...51})5253onShow((evt: Event) => {54// Do something with the notification on:show event...55})5657onError((evt: Event) => {58// Do something with the notification on:error event...59})6061onClose((evt: Event) => {62// Do something with the notification on:close event...63})64```6566## Type Declarations6768```ts69export interface WebNotificationOptions {70/**71* The title read-only property of the Notification interface indicates72* the title of the notification73*74* @default ''75*/76title?: string77/**78* The body string of the notification as specified in the constructor's79* options parameter.80*81* @default ''82*/83body?: string84/**85* The text direction of the notification as specified in the constructor's86* options parameter.87*88* @default ''89*/90dir?: "auto" | "ltr" | "rtl"91/**92* The language code of the notification as specified in the constructor's93* options parameter.94*95* @default DOMString96*/97lang?: string98/**99* The ID of the notification(if any) as specified in the constructor's options100* parameter.101*102* @default ''103*/104tag?: string105/**106* The URL of the image used as an icon of the notification as specified107* in the constructor's options parameter.108*109* @default ''110*/111icon?: string112/**113* Specifies whether the user should be notified after a new notification114* replaces an old one.115*116* @default false117*/118renotify?: boolean119/**120* A boolean value indicating that a notification should remain active until the121* user clicks or dismisses it, rather than closing automatically.122*123* @default false124*/125requireInteraction?: boolean126/**127* The silent read-only property of the Notification interface specifies128* whether the notification should be silent, i.e., no sounds or vibrations129* should be issued, regardless of the device settings.130*131* @default false132*/133silent?: boolean134/**135* Specifies a vibration pattern for devices with vibration hardware to emit.136* A vibration pattern, as specified in the Vibration API spec137*138* @see https://w3c.github.io/vibration/139*/140vibrate?: number[]141}142export interface UseWebNotificationOptions143extends ConfigurableWindow, WebNotificationOptions {144/**145* Request for permissions onMounted if it's not granted.146*147* Can be disabled and calling `ensurePermissions` to grant afterwords.148*149* @default true150*/151requestPermissions?: boolean152}153export interface UseWebNotificationReturn extends Supportable {154notification: Ref<Notification | null>155ensurePermissions: () => Promise<boolean | undefined>156permissionGranted: ShallowRef<boolean>157show: (158overrides?: WebNotificationOptions,159) => Promise<Notification | undefined>160close: () => void161onClick: EventHookOn<Event>162onShow: EventHookOn<Event>163onError: EventHookOn<Event>164onClose: EventHookOn<Event>165}166/**167* Reactive useWebNotification168*169* @see https://vueuse.org/useWebNotification170* @see https://developer.mozilla.org/en-US/docs/Web/API/notification171*/172export declare function useWebNotification(173options?: UseWebNotificationOptions,174): UseWebNotificationReturn175```176