Skip to content

Commit

Permalink
announce page changes (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jan 7, 2021
1 parent af7609b commit 572474e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/selfish-avocados-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Announce page changes
38 changes: 37 additions & 1 deletion packages/kit/src/core/create_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function generate_app(manifest_data) {
return trim(`
<!-- This file is generated by @sveltejs/kit — do not edit it! -->
<script>
import { setContext, afterUpdate } from 'svelte';
import { setContext, afterUpdate, onMount } from 'svelte';
import ErrorComponent from ${JSON.stringify(manifest_data.error.url)};
// error handling
Expand All @@ -153,6 +153,22 @@ function generate_app(manifest_data) {
$: stores.page.set(page);
afterUpdate(stores.page.notify);
let mounted = false;
let navigated = false;
let title = null;
onMount(() => {
const unsubscribe = stores.page.subscribe(() => {
if (mounted) {
navigated = true;
title = document.title;
}
});
mounted = true;
return unsubscribe;
});
</script>
<Layout {...(props_0 || {})}>
Expand All @@ -162,5 +178,25 @@ function generate_app(manifest_data) {
${pyramid.replace(/\n/g, '\n\t\t\t\t')}
{/if}
</Layout>
{#if mounted}
<div id="svelte-announcer" aria-live="assertive" aria-atomic="true">
{#if navigated}
Navigated to {title}
{/if}
</div>
{/if}
<style>
#svelte-announcer {
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
</style>
`);
}
18 changes: 18 additions & 0 deletions test/apps/basics/src/routes/accessibility/__tests__.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@ export default function (test) {
assert.equal(await evaluate(() => document.activeElement.nodeName), 'A');
assert.equal(await evaluate(() => document.activeElement.textContent), 'a');
});

test('announces client-side navigation', async ({ visit, click, contains, html, js }) => {
await visit('/accessibility/a');

const has_live_region = await contains('aria-live');

if (js) {
assert.ok(has_live_region);

// live region should exist, but be empty
assert.equal(await html('[aria-live]'), '');

await click('[href="/accessibility/b"]');
assert.equal(await html('[aria-live]'), 'Navigated to b'); // TODO i18n
} else {
assert.ok(!has_live_region);
}
});
}

0 comments on commit 572474e

Please sign in to comment.