Skip to content

Commit 1c3be8b

Browse files
author
Tarek Touati
authored
feat: add useNotification (#177)
1 parent 4276046 commit 1c3be8b

File tree

9 files changed

+136
-17
lines changed

9 files changed

+136
-17
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ Each composition function is designed to degrade gracefully so you can safely us
6363
- [Window Scroll Position](https://Tarektouati.github.io/vue-use-web/functions/scroll-position.html).
6464
- [Window Size](https://Tarektouati.github.io/vue-use-web/functions/window-size.html).
6565
- [Worker](https://Tarektouati.github.io/vue-use-web/functions/worker.html).
66-
- Bluetooth (WIP).
67-
- Notification (WIP).
68-
- Share (WIP).
66+
- [Notification](https://Tarektouati.github.io/vue-use-web/functions/worker.html).
67+
- Bluetooth (TODO).
68+
- Share (TODO).
69+
- MediaDevices (WIP)
70+
- ResizeObserver (WIP)
6971

7072
## Inspiration
7173

docs/.vuepress/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ module.exports = {
7878
'/functions/script',
7979
'/functions/websocket',
8080
'/functions/scroll-position',
81-
'/functions/window-size'
81+
'/functions/window-size',
82+
'/functions/notification'
8283
]
8384
},
8485
{

docs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ npm install @vue/composition-api vue-use-web
6161
- [Window Scroll Position](./functions/scroll-position.md).
6262
- [Window Size](./functions/window-size.md).
6363
- [Worker](./functions/worker.md).
64+
- [Notification](./functions/notification.md).
6465
- Bluetooth <Badge text="WIP" type="warn" />.
65-
- Notification <Badge text="WIP" type="warn" />.
6666
- Share <Badge text="WIP" type="warn" />.
6767

6868
## Usage

docs/functions/notification.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Web Notification
2+
3+
This functions provides simple web notifications that are displayed outside the page at the system level.
4+
5+
## Methods
6+
7+
`useNotification` exposes the following methods:
8+
9+
```js
10+
import { useNotification } from 'vue-use-web';
11+
12+
const { showNotifcation } = useNotification('notification title', {
13+
icon: 'url of icon',
14+
body: 'body of the notification'
15+
});
16+
```
17+
18+
| Method | Signature | Description |
19+
| --------------- | ------------ | ------------------------ |
20+
| showNotifcation | `() => void` | toggle the notification. |
21+
22+
## Config
23+
24+
`useNotification` function takes a required parameter that is the notification tilte and [optional config](https://developer.mozilla.org/en-US/docs/Web/API/Notification)
25+
26+
```js
27+
import { useNotification } from 'vue-use-web';
28+
29+
const { showNotifcation } = useNotification(
30+
'notification title',
31+
{
32+
icon: 'url of icon',
33+
body: 'body of the notification'
34+
},
35+
{
36+
onClick: () => alert('Notification clicked'),
37+
onClose: () => alert('Notification closed')
38+
}
39+
);
40+
```
41+
42+
## Example
43+
44+
```vue
45+
<template>
46+
<div>
47+
<button @click="showNotifcation">Toggle notification</button>
48+
</div>
49+
</template>
50+
<script>
51+
import { useNotification } from 'vue-use-web';
52+
import vueLogo from './logo.png'
53+
export default {
54+
setup() {
55+
const { showNotifcation } = useNotification(
56+
'notification title',
57+
{
58+
icon: vueLogo,
59+
body: 'body of the notification',
60+
},
61+
{
62+
onClick: () => alert('Notification clicked'),
63+
onClose: () => alert('Notification closed'),
64+
}
65+
);
66+
return { showNotifcation };
67+
},
68+
};
69+
</script>
70+
```

docs/functions/worker.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,4 @@ export default {
6868
</script>
6969
```
7070

71-
## Demo
72-
73-
TODO: Cool Chat app maybe
71+
<!-- TODO: Demo Cool Chat app maybe -->

scripts/build.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const mkdirpNode = require('mkdirp');
21
const chalk = require('chalk');
32
const { promisify } = require('util');
3+
const fs = require('fs');
44
const { configs, utils, paths } = require('./config');
55

6-
const mkdirp = promisify(mkdirpNode);
6+
const mkdir = promisify(fs.mkdir);
77

88
async function build () {
9-
await mkdirp(paths.dist);
9+
await mkdir(paths.dist, { recursive: true })
1010
// eslint-disable-next-line
1111
console.log(chalk.cyan('Generating ESM build...'));
1212
await utils.writeBundle(configs.esm, 'vue-use-web.esm.js');

scripts/defs.sh

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,5 @@ mkdir -p ./dist/types
66
echo "\e[92mDone"
77

88
echo "\e[34mGenerating main declarations..."
9-
tsc --emitDeclarationOnly
10-
echo "\e[92mDone"
11-
12-
echo "\e[34mCleaning up declaration folder..."
13-
mv ./dist/types/src/* ./dist/types
14-
rm -rf ./dist/types/src
9+
tsc ./src/*.ts --emitDeclarationOnly --skipLibCheck --declaration --outDir ./dist/types/
1510
echo "\e[92mDone"

src/Notification.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { onMounted, Ref, ref, onUnmounted } from '@vue/composition-api';
2+
3+
type NotificationOptions = {
4+
dir?: 'auto' | 'ltr' | 'rtl';
5+
lang?: string;
6+
body?: string;
7+
tag?: string;
8+
icon?: string;
9+
};
10+
11+
type NotificationMethods = {
12+
onClick: ((e: Event) => void) | null;
13+
onShow: ((e: Event) => void) | null;
14+
onError: ((e: Event) => void) | null;
15+
onClose: ((e: Event) => void) | null;
16+
};
17+
18+
const defaultNotificationOptions = {
19+
onClick: null,
20+
onShow: null,
21+
onError: null,
22+
onClose: null
23+
};
24+
25+
export function useNotification(
26+
title: string,
27+
options: NotificationOptions = {},
28+
methods: NotificationMethods = defaultNotificationOptions
29+
) {
30+
const notification: Ref<Notification | null> = ref(null);
31+
const requestPermission = async () => {
32+
if ('permission' in Notification && Notification.permission !== 'denied') {
33+
await Notification.requestPermission();
34+
}
35+
};
36+
37+
onMounted(requestPermission);
38+
39+
onUnmounted(() => {
40+
notification.value = null;
41+
});
42+
43+
const showNotifcation = (): void => {
44+
notification.value = new Notification(title, options);
45+
notification.value.onclick = methods.onClick;
46+
notification.value.onshow = methods.onShow;
47+
notification.value.onerror = methods.onError;
48+
notification.value.onclose = methods.onClose;
49+
};
50+
51+
return { showNotifcation };
52+
}

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ export * from './WebSocket';
2222
export * from './WindowScrollPosition';
2323
export * from './WindowSize';
2424
export * from './Worker';
25+
export * from './Notification';

0 commit comments

Comments
 (0)