Skip to content

Commit 572474e

Browse files
committed
announce page changes (#307)
1 parent af7609b commit 572474e

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Announce page changes

packages/kit/src/core/create_app.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function generate_app(manifest_data) {
133133
return trim(`
134134
<!-- This file is generated by @sveltejs/kit — do not edit it! -->
135135
<script>
136-
import { setContext, afterUpdate } from 'svelte';
136+
import { setContext, afterUpdate, onMount } from 'svelte';
137137
import ErrorComponent from ${JSON.stringify(manifest_data.error.url)};
138138
139139
// error handling
@@ -153,6 +153,22 @@ function generate_app(manifest_data) {
153153
154154
$: stores.page.set(page);
155155
afterUpdate(stores.page.notify);
156+
157+
let mounted = false;
158+
let navigated = false;
159+
let title = null;
160+
161+
onMount(() => {
162+
const unsubscribe = stores.page.subscribe(() => {
163+
if (mounted) {
164+
navigated = true;
165+
title = document.title;
166+
}
167+
});
168+
169+
mounted = true;
170+
return unsubscribe;
171+
});
156172
</script>
157173
158174
<Layout {...(props_0 || {})}>
@@ -162,5 +178,25 @@ function generate_app(manifest_data) {
162178
${pyramid.replace(/\n/g, '\n\t\t\t\t')}
163179
{/if}
164180
</Layout>
181+
182+
{#if mounted}
183+
<div id="svelte-announcer" aria-live="assertive" aria-atomic="true">
184+
{#if navigated}
185+
Navigated to {title}
186+
{/if}
187+
</div>
188+
{/if}
189+
190+
<style>
191+
#svelte-announcer {
192+
clip: rect(0 0 0 0);
193+
clip-path: inset(50%);
194+
height: 1px;
195+
overflow: hidden;
196+
position: absolute;
197+
white-space: nowrap;
198+
width: 1px;
199+
}
200+
</style>
165201
`);
166202
}

test/apps/basics/src/routes/accessibility/__tests__.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,22 @@ export default function (test) {
1818
assert.equal(await evaluate(() => document.activeElement.nodeName), 'A');
1919
assert.equal(await evaluate(() => document.activeElement.textContent), 'a');
2020
});
21+
22+
test('announces client-side navigation', async ({ visit, click, contains, html, js }) => {
23+
await visit('/accessibility/a');
24+
25+
const has_live_region = await contains('aria-live');
26+
27+
if (js) {
28+
assert.ok(has_live_region);
29+
30+
// live region should exist, but be empty
31+
assert.equal(await html('[aria-live]'), '');
32+
33+
await click('[href="/accessibility/b"]');
34+
assert.equal(await html('[aria-live]'), 'Navigated to b'); // TODO i18n
35+
} else {
36+
assert.ok(!has_live_region);
37+
}
38+
});
2139
}

0 commit comments

Comments
 (0)