Skip to content

Commit 3784fb9

Browse files
committed
feat: Plugins
1 parent 87f1dc6 commit 3784fb9

File tree

6 files changed

+79
-7
lines changed

6 files changed

+79
-7
lines changed

docs/.vuepress/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports = {
3838
'/guide/',
3939
'/guide/announcer.md',
4040
'/guide/announcer-router.md',
41+
'/guide/plugins.md',
4142
'/guide/accessibility.md',
4243
'/guide/support.md',
4344
]

docs/guide/plugins.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
3+
announcer: Announcer plugins page has loaded
4+
5+
---
6+
7+
# Plugins
8+
9+
Plugin is an interesting resource for you to create different ways to use the announcer and adapt to a specific problem in your app.
10+
11+
12+
```javascript
13+
// e.g. plugins/announcer/myPlugin.js
14+
export default {
15+
name: 'myPlugin',
16+
handler () {
17+
console.log('myPlugin')
18+
}
19+
}
20+
```
21+
22+
::: warning
23+
The handler function takes `$announcer` as a context (this), so you can use `this.assertive('my text')`
24+
:::
25+
26+
```javascript
27+
// src/main.js
28+
import Vue from 'vue'
29+
import VueAnnouncer from '@vue-a11y/announcer'
30+
31+
import myPlugin from '@plugins/announcer/myPlugin'
32+
33+
Vue.use(VueAnnouncer, {
34+
plugins: [myPlugin]
35+
})
36+
```
37+
38+
```javascript
39+
// e.g. component.vue
40+
export default {
41+
name: 'myComponent'
42+
43+
methods: {
44+
test () {
45+
this.$announcer.plugins.myPlugin()
46+
}
47+
}
48+
}
49+
```

index.d.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { PluginFunction } from 'vue';
22

3-
export interface Announcer
4-
{
3+
export interface AnnouncerPlugins {
4+
name: string,
5+
handler: any
6+
}
7+
export interface Announcer {
58
data: Record<string, any>;
69

710
options: Record<string, object>;
811

12+
plugins?: AnnouncerPlugins[];
13+
914
set(message: string, politeness: string): void;
1015

1116
polite(message: string): void;
@@ -15,18 +20,17 @@ export interface Announcer
1520
reset(): void;
1621

1722
setComplementRoute(complementRoute: string): void;
23+
1824
}
1925

20-
declare module 'vue/types/vue'
21-
{
26+
declare module 'vue/types/vue' {
2227
interface Vue
2328
{
2429
$announcer: Announcer;
2530
}
2631
}
2732

28-
declare class VueAnnouncer
29-
{
33+
declare class VueAnnouncer {
3034
static install: PluginFunction<never>;
3135
}
3236

src/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import VueAnnouncer from './vue-announcer.vue'
22
import { draf, defaultOptions } from './utils'
33

4+
const announcerPlugins = {}
5+
46
export default function install (Vue, options = {}, router = null) {
57
if (install.installed) return
68
install.installed = true
@@ -38,12 +40,21 @@ export default function install (Vue, options = {}, router = null) {
3840
this.data.politeness = this.options.politeness
3941
},
4042

43+
plugins: announcerPlugins,
44+
4145
setComplementRoute (complementRoute) {
4246
if (typeof complementRoute !== 'string') return
4347
options.complementRoute = complementRoute
4448
}
4549
}
4650

51+
// Register plugins
52+
if (options.plugins.length) {
53+
options.plugins.forEach(({ name, handler }) => {
54+
announcerPlugins[name] = handler.bind(Vue.prototype.$announcer)
55+
})
56+
}
57+
4758
// If set the router, will be announced the change of route
4859
if (router) {
4960
router.afterEach(to => {

src/plugins/spell.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
name: 'spell',
3+
handler (pass) {
4+
if (pass) pass.split('').forEach((char, index) => setTimeout(() => this.polite(char), index * 100))
5+
}
6+
}

src/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export const draf = (cb) => requestAnimationFrame(() => requestAnimationFrame(cb
22

33
export const defaultOptions = {
44
politeness: 'polite',
5-
complementRoute: 'has loaded'
5+
complementRoute: 'has loaded',
6+
plugins: []
67
}

0 commit comments

Comments
 (0)