Skip to content

Commit 3500b73

Browse files
authored
Merge pull request #8 from ldawkes/dynamic-route-complement
Add $announcer.setComplementRoute
2 parents cef4df0 + e7356f0 commit 3500b73

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ Vue.use(VueAnnouncer, {
8383
}, router)
8484
```
8585

86+
### Methods
87+
**Note: These methods are registered on the `$announcer` property injected into the Vue instance**
88+
89+
#### `$announcer.setComplementRoute(complementRoute)`
90+
91+
If you need to set the `complementRoute` option dynamically without reloading the application, for example if you're
92+
dynamically loading translations, you can use this method to update it.
93+
94+
```javascript
95+
export default {
96+
onTranslationsUpdated (translations) {
97+
this.$announcer.setComplementRoute(translations.complementRouteKey /* = 'ha cargado' */)
98+
}
99+
}
100+
```
101+
86102
### Custom message to each page (optional)
87103
You can set a property on the meta object, which will serve as information to get the message that will be announced to the screen reader on each page. e.g.:
88104
```javascript

example/src/router.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ const router = new VueRouter({
1111
mode: 'history',
1212
routes: [
1313
{
14+
beforeEnter (to, from, next) {
15+
router.app.$announcer.setComplementRoute('has loaded')
16+
17+
next()
18+
},
1419
name: 'home',
1520
path: '/',
1621
component: Home,
@@ -19,6 +24,11 @@ const router = new VueRouter({
1924
}
2025
},
2126
{
27+
beforeEnter (to, from, next) {
28+
router.app.$announcer.setComplementRoute('has loaded')
29+
30+
next()
31+
},
2232
name: 'about',
2333
path: '/about',
2434
component: About,
@@ -27,6 +37,11 @@ const router = new VueRouter({
2737
}
2838
},
2939
{
40+
beforeEnter (to, from, next) {
41+
router.app.$announcer.setComplementRoute('has fully loaded')
42+
43+
next()
44+
},
3045
name: 'contact',
3146
path: '/contact',
3247
component: Contact,

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface Announcer
55
data: Record<string, any>;
66

77
set(message: string): void;
8+
9+
setComplementRoute(complementRoute: string): void;
810
}
911

1012
declare module 'vue/types/vue'

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ export default function install (Vue, options = {}, router = null) {
1515
})
1616
}
1717
},
18+
19+
setComplementRoute (complementRoute) {
20+
if (typeof (complementRoute) !== 'string') {
21+
return
22+
}
23+
24+
options.complementRoute = complementRoute
25+
},
1826
data: null
1927
}
2028

tests/e2e/integration/vue-announcer.test.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,31 @@ describe('Announcer test', () => {
1212
cy.get('[data-va="announcer"]').should('contain', 'has loaded')
1313
})
1414

15+
it('Should contain the custom complement when the route changes', () => {
16+
cy.get('a[href="/contact"]').click()
17+
cy.get('[data-va="announcer"]').should('contain', 'has fully loaded')
18+
})
19+
20+
it('Should handle setting the custom complement multiple times', () => {
21+
cy.get('a[href="/contact"]').click()
22+
cy.get('[data-va="announcer"]').should('contain', 'has fully loaded')
23+
24+
cy.get('a[href="/about"]').click()
25+
cy.get('[data-va="announcer"]').should('contain', 'has loaded')
26+
})
27+
1528
it('Should be equal toasted message with the announced', () => {
1629
cy.get('a[href="/about"]').click()
1730
cy.get('[data-va="toasted"]').click()
1831

1932
cy.get('.toasted-container')
20-
.find('.toasted')
21-
.invoke('text')
22-
.then(text1 => {
23-
cy.get('[data-va="announcer"]').invoke('text').should(text2 => {
24-
console.log(text1, text2)
25-
expect(text1).to.eq(text2)
33+
.find('.toasted')
34+
.invoke('text')
35+
.then(text1 => {
36+
cy.get('[data-va="announcer"]').invoke('text').should(text2 => {
37+
console.log(text1, text2)
38+
expect(text1).to.eq(text2)
39+
})
2640
})
27-
})
2841
})
2942
})

0 commit comments

Comments
 (0)