Skip to content

Commit dda2402

Browse files
committed
Added option to skip to non-announcer when changing specific route
1 parent 6199194 commit dda2402

File tree

4 files changed

+70
-36
lines changed

4 files changed

+70
-36
lines changed

example/src/pages/About.vue

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,51 @@
77
data-va="toasted"
88
@click="notify"
99
>
10-
trigger notification
10+
show error
1111
</button>
12+
<div class="msg-error">
13+
{{ errorMessage }}
14+
</div>
1215
</div>
1316
</template>
1417

1518
<script>
1619
export default {
1720
name: 'About',
18-
methods: {
19-
notify () {
20-
const message = 'Hi, it\'s a toasted notification'
21-
this.$toasted.success(message)
22-
this.$announcer.set(message)
21+
data () {
22+
return {
23+
title: '',
24+
errorMessage: null
2325
}
2426
},
25-
head: {
26-
title: {
27-
inner: 'About page'
27+
head () {
28+
return {
29+
title: {
30+
inner: this.title
31+
}
32+
}
33+
},
34+
created () {
35+
// get data post in server
36+
setTimeout(() => {
37+
this.title = 'Title of my awesome post'
38+
this.$emit('updateHead')
39+
this.$announcer.set(`${this.title} has loaded`, 'polite') // It's also possible to use the default complementRoute (this.$announcer.options.complementRoute)
40+
}, 2000)
41+
},
42+
methods: {
43+
notify () {
44+
this.errorMessage = 'It\'s error message'
45+
this.$announcer.set(this.errorMessage, 'assertive')
2846
}
2947
}
3048
}
3149
</script>
3250

33-
<style>
34-
51+
<style scoped>
52+
.msg-error {
53+
color: #d4351c;
54+
padding: 10px;
55+
font-weight: bold;
56+
}
3557
</style>

example/src/router.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,27 @@ const router = new VueRouter({
1616
component: Home,
1717
meta: {
1818
announcer: {
19-
message: 'Home page',
2019
complementRoute: 'has loaded'
2120
}
2221
}
2322
},
2423
{
2524
name: 'about',
2625
path: '/about',
27-
component: About
26+
component: About,
27+
meta: {
28+
announcer: {
29+
skip: true
30+
}
31+
}
2832
},
2933
{
3034
name: 'contact',
3135
path: '/contact',
3236
component: Contact,
3337
meta: {
3438
announcer: {
39+
message: 'contact page',
3540
politeness: 'assetive',
3641
complementRoute: 'has fully loaded'
3742
}

src/constants.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/index.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,58 @@
1-
import { OPTIONS } from './constants'
21
import VueAnnouncer from './vue-announcer.vue'
32

43
const draf = (cb) => requestAnimationFrame(() => requestAnimationFrame(cb))
54

65
export default function install (Vue, options = {}, router = null) {
7-
options = { ...OPTIONS, ...options }
6+
// merge options
7+
options = {
8+
politeness: 'polite',
9+
complementRoute: 'has loaded',
10+
...options
11+
}
812

13+
// Register vue-announcer component
914
Vue.component('VueAnnouncer', VueAnnouncer)
15+
1016
Vue.prototype.$announcer = {
17+
data: null,
1118
options,
1219

1320
set (message, politeness) {
14-
if (this.data) {
15-
this.reset()
16-
if (politeness) {
17-
this.data.politeness = politeness
18-
}
19-
draf(() => {
20-
this.data.content = message
21-
})
21+
if (!this.data) return
22+
this.reset()
23+
if (politeness) {
24+
this.data.politeness = politeness
2225
}
26+
draf(() => {
27+
this.data.content = message
28+
})
2329
},
2430

2531
reset () {
26-
this.data = Object.assign(this.data, {
27-
content: '',
28-
politeness: this.options.politeness
29-
})
32+
this.data.content = ''
33+
this.data.politeness = this.options.politeness
3034
},
3135

3236
setComplementRoute (complementRoute) {
3337
if (typeof complementRoute !== 'string') return
3438
options.complementRoute = complementRoute
35-
},
36-
37-
data: null
39+
}
3840
}
3941

4042
// If set the router, will be announced the change of route
4143
if (router) {
4244
router.afterEach(to => {
43-
draf(() => { // Resolves the problem of getting the correct title when the meta announcer is not passed
44-
const announcer = to.meta.announcer || {}
45+
const announcer = to.meta.announcer || {}
46+
47+
// Skip: Used, for example, when an async title exists, in which case the announcement is made manually by the set method.
48+
// It is also possible to achieve the same result, using politeness: 'off', but it will be necessary
49+
// to set the "assertive" or "polite" when using the set method.
50+
// for example: this.$announcer.set('my async title', 'polite')
51+
if (announcer.skip) return
52+
53+
// draf: Resolves the problem of getting the correct document.title when the meta announcer is not passed
54+
// Tested on Vuepress
55+
draf(() => {
4556
const msg = announcer.message || document.title.trim()
4657
const complement = announcer.complementRoute || options.complementRoute
4758
const politeness = announcer.politeness || null

0 commit comments

Comments
 (0)