-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tarek Touati
authored
May 8, 2020
1 parent
4276046
commit 1c3be8b
Showing
9 changed files
with
136 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Web Notification | ||
|
||
This functions provides simple web notifications that are displayed outside the page at the system level. | ||
|
||
## Methods | ||
|
||
`useNotification` exposes the following methods: | ||
|
||
```js | ||
import { useNotification } from 'vue-use-web'; | ||
|
||
const { showNotifcation } = useNotification('notification title', { | ||
icon: 'url of icon', | ||
body: 'body of the notification' | ||
}); | ||
``` | ||
|
||
| Method | Signature | Description | | ||
| --------------- | ------------ | ------------------------ | | ||
| showNotifcation | `() => void` | toggle the notification. | | ||
|
||
## Config | ||
|
||
`useNotification` function takes a required parameter that is the notification tilte and [optional config](https://developer.mozilla.org/en-US/docs/Web/API/Notification) | ||
|
||
```js | ||
import { useNotification } from 'vue-use-web'; | ||
|
||
const { showNotifcation } = useNotification( | ||
'notification title', | ||
{ | ||
icon: 'url of icon', | ||
body: 'body of the notification' | ||
}, | ||
{ | ||
onClick: () => alert('Notification clicked'), | ||
onClose: () => alert('Notification closed') | ||
} | ||
); | ||
``` | ||
|
||
## Example | ||
|
||
```vue | ||
<template> | ||
<div> | ||
<button @click="showNotifcation">Toggle notification</button> | ||
</div> | ||
</template> | ||
<script> | ||
import { useNotification } from 'vue-use-web'; | ||
import vueLogo from './logo.png' | ||
export default { | ||
setup() { | ||
const { showNotifcation } = useNotification( | ||
'notification title', | ||
{ | ||
icon: vueLogo, | ||
body: 'body of the notification', | ||
}, | ||
{ | ||
onClick: () => alert('Notification clicked'), | ||
onClose: () => alert('Notification closed'), | ||
} | ||
); | ||
return { showNotifcation }; | ||
}, | ||
}; | ||
</script> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,4 @@ export default { | |
</script> | ||
``` | ||
|
||
## Demo | ||
|
||
TODO: Cool Chat app maybe | ||
<!-- TODO: Demo Cool Chat app maybe --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { onMounted, Ref, ref, onUnmounted } from '@vue/composition-api'; | ||
|
||
type NotificationOptions = { | ||
dir?: 'auto' | 'ltr' | 'rtl'; | ||
lang?: string; | ||
body?: string; | ||
tag?: string; | ||
icon?: string; | ||
}; | ||
|
||
type NotificationMethods = { | ||
onClick: ((e: Event) => void) | null; | ||
onShow: ((e: Event) => void) | null; | ||
onError: ((e: Event) => void) | null; | ||
onClose: ((e: Event) => void) | null; | ||
}; | ||
|
||
const defaultNotificationOptions = { | ||
onClick: null, | ||
onShow: null, | ||
onError: null, | ||
onClose: null | ||
}; | ||
|
||
export function useNotification( | ||
title: string, | ||
options: NotificationOptions = {}, | ||
methods: NotificationMethods = defaultNotificationOptions | ||
) { | ||
const notification: Ref<Notification | null> = ref(null); | ||
const requestPermission = async () => { | ||
if ('permission' in Notification && Notification.permission !== 'denied') { | ||
await Notification.requestPermission(); | ||
} | ||
}; | ||
|
||
onMounted(requestPermission); | ||
|
||
onUnmounted(() => { | ||
notification.value = null; | ||
}); | ||
|
||
const showNotifcation = (): void => { | ||
notification.value = new Notification(title, options); | ||
notification.value.onclick = methods.onClick; | ||
notification.value.onshow = methods.onShow; | ||
notification.value.onerror = methods.onError; | ||
notification.value.onclose = methods.onClose; | ||
}; | ||
|
||
return { showNotifcation }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters