| 1 |
# Swup A11y Plugin |
| 2 |
|
| 3 |
A [swup](https://swup.js.org) plugin for enhanced accessibility. |
| 4 |
|
| 5 |
Loading new content via AJAX is a great experience for most users, but comes with serious |
| 6 |
shortcomings for screen reader users. This plugin will improve that: |
| 7 |
|
| 8 |
- **Announce page visits** to screenreaders by reading the new page title |
| 9 |
- **Restore focus** after swapping out the content |
| 10 |
- **Skip animations** for users with a preference for reduced motion |
| 11 |
|
| 12 |
Accessibility can be hard to get right. That's why we're keen to hear your feedback. Share your |
| 13 |
experience and suggest improvements by [opening an issue](https://github.com/swup/a11y-plugin/issues/new/choose) |
| 14 |
on this repo. Let's make swup better together! |
| 15 |
|
| 16 |
## Installation |
| 17 |
|
| 18 |
Install the plugin from npm and import it into your bundle. |
| 19 |
|
| 20 |
```bash |
| 21 |
npm install @swup/a11y-plugin |
| 22 |
``` |
| 23 |
|
| 24 |
```js |
| 25 |
import SwupA11yPlugin from '@swup/a11y-plugin'; |
| 26 |
``` |
| 27 |
|
| 28 |
Or include the minified production file from a CDN: |
| 29 |
|
| 30 |
```html |
| 31 |
<script src="https://unpkg.com/@swup/a11y-plugin@5"></script> |
| 32 |
``` |
| 33 |
|
| 34 |
## Usage |
| 35 |
|
| 36 |
To run this plugin, include an instance in the swup options. |
| 37 |
|
| 38 |
```javascript |
| 39 |
const swup = new Swup({ |
| 40 |
plugins: [new SwupA11yPlugin()] |
| 41 |
}); |
| 42 |
``` |
| 43 |
|
| 44 |
## Markup |
| 45 |
|
| 46 |
The plugin should work out of the box if you use proper semantic markup for your content, i.e. a |
| 47 |
descriptive `h1` for each page heading. See the options below for customizing what tags to look for. |
| 48 |
|
| 49 |
<!-- prettier-ignore --> |
| 50 |
```html |
| 51 |
<body> <!-- will be focused --> |
| 52 |
<header> |
| 53 |
Logo |
| 54 |
</header> |
| 55 |
<main> |
| 56 |
<h1>Page Title</h1> <!-- will be announced --> |
| 57 |
<p>Lorem ipsum dolor sit amet</p> |
| 58 |
</main> |
| 59 |
</body> |
| 60 |
``` |
| 61 |
|
| 62 |
## Announcements |
| 63 |
|
| 64 |
The plugin will announce the new page to screen readers after navigating to it. It will look for the |
| 65 |
following and announce the first one found: |
| 66 |
|
| 67 |
- Heading label: `<h1 aria-label="About"></h1>` |
| 68 |
- Heading content: `<h1>About</h1>` |
| 69 |
- Document title: `<title>About</title>` |
| 70 |
- Page URL: `/about/` |
| 71 |
|
| 72 |
The easiest way to announce a page title differing from the main heading is using `aria-label`. The |
| 73 |
example below will be announced as `Homepage`. |
| 74 |
|
| 75 |
```html |
| 76 |
<h1 aria-label="Homepage">Project Title</h1> |
| 77 |
``` |
| 78 |
|
| 79 |
## Focus |
| 80 |
|
| 81 |
On page navigation, the plugin will reset focus to the body, imitating default browser behavior. |
| 82 |
When clicking anchor links on the same page, the plugin will focus the target of the link. |
| 83 |
|
| 84 |
> [!TIP] |
| 85 |
> When linking to other sections on the page, make sure to target a single item that is descriptive |
| 86 |
> of the section, e.g. a heading or a button. Linking to a large container has multiple issues: |
| 87 |
> Some browsers will scroll to the center of the targeted container, disorienting near-sighted users |
| 88 |
> with increased zoom. Screen readers will also start reading out the content of the targeted |
| 89 |
> element, which can be overwhelming when a whole content section is being read out. |
| 90 |
|
| 91 |
## Styling |
| 92 |
|
| 93 |
Browsers will display a visible outline around elements focused by this plugin. That's great for |
| 94 |
orientation of keyboard users, but tends to be annoying for mouse users. We can make both groups of |
| 95 |
users happy by using a combination of the `:focus` and `:focus-visible` pseudo-class to hide any |
| 96 |
focus outlines while the site is being used with a mouse: |
| 97 |
|
| 98 |
```css |
| 99 |
:focus:not(:focus-visible) { |
| 100 |
outline: none; |
| 101 |
} |
| 102 |
``` |
| 103 |
|
| 104 |
See these guides on [Controlling focus](https://web.dev/control-focus-with-tabindex/) |
| 105 |
and [Styling focus](https://web.dev/style-focus/) for details and more examples. |
| 106 |
|
| 107 |
## Options |
| 108 |
|
| 109 |
All options with their default values: |
| 110 |
|
| 111 |
```javascript |
| 112 |
{ |
| 113 |
headingSelector: 'h1', |
| 114 |
respectReducedMotion: true, |
| 115 |
autofocus: false, |
| 116 |
announcements: { |
| 117 |
visit: 'Navigated to: {title}', |
| 118 |
url: 'New page at {url}' |
| 119 |
} |
| 120 |
} |
| 121 |
``` |
| 122 |
|
| 123 |
### headingSelector |
| 124 |
|
| 125 |
The selector for finding page headings. The content of the first found heading will be read to |
| 126 |
screenreaders after a new page was loaded. |
| 127 |
|
| 128 |
### respectReducedMotion |
| 129 |
|
| 130 |
Whether to respects users' preference for reduced motion. |
| 131 |
|
| 132 |
Disable animated page transitions and animated scrolling if a user has enabled a |
| 133 |
setting on their device to minimize the amount of non-essential motion. Learn more about |
| 134 |
[prefers-reduced-motion](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion). |
| 135 |
|
| 136 |
### autofocus |
| 137 |
|
| 138 |
Whether to focus elements with an `autofocus` attribute after navigation. |
| 139 |
|
| 140 |
> [!CAUTION] |
| 141 |
> Make sure to use this wisely. Automatically focussing elements can be useful to draw attention to |
| 142 |
> inputs, but it comes with a list of drawbacks on its own, especially for screen-reading technology. |
| 143 |
> See [Autofocus accessibility considerations](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autofocus#accessibility_considerations) |
| 144 |
> for details. |
| 145 |
|
| 146 |
### announcements |
| 147 |
|
| 148 |
How the new page is announced. A visit is announced differently depending on whether the new page |
| 149 |
has a title or not. If found, the main heading or document title is announced. If neither is found, |
| 150 |
the new url will be announced instead: |
| 151 |
|
| 152 |
- **Title found?** Read `announcements.visit`, replacing `{title}` with the new title |
| 153 |
- **No title?** Read `announcements.visit` too, but replacing `{title}` with the content of `announcements.url` |
| 154 |
|
| 155 |
```js |
| 156 |
{ |
| 157 |
announcements: { |
| 158 |
visit: 'Navigated to: {title}', |
| 159 |
url: 'New page at {url}' |
| 160 |
} |
| 161 |
} |
| 162 |
``` |
| 163 |
|
| 164 |
#### Translations |
| 165 |
|
| 166 |
For multi-language sites, pass in a nested object keyed by locale. The locale must match the |
| 167 |
`html` element's [lang](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/lang) attribute |
| 168 |
exactly. Use an asterisk `*` to declare fallback translations. |
| 169 |
|
| 170 |
> [!NOTE] |
| 171 |
> Swup will not update the lang attribute on its own. For that, you can either install the |
| 172 |
> [Head Plugin](https://swup.js.org/plugins/head-plugin/) to do it automatically, or you can do update |
| 173 |
> it yourself in the `content:replace` hook. |
| 174 |
|
| 175 |
```js |
| 176 |
{ |
| 177 |
announcements: { |
| 178 |
'en-US': { |
| 179 |
visit: 'Navigated to: {title}', |
| 180 |
url: 'New page at {url}' |
| 181 |
}, |
| 182 |
'de-DE': { |
| 183 |
visit: 'Navigiert zu: {title}', |
| 184 |
url: 'Neue Seite unter {url}' |
| 185 |
}, |
| 186 |
'fr-FR': { |
| 187 |
visit: 'Navigué vers : {title}', |
| 188 |
url: 'Nouvelle page à {url}' |
| 189 |
}, |
| 190 |
'*': { |
| 191 |
visit: '{title}', |
| 192 |
url: '{url}' |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
``` |
| 197 |
|
| 198 |
## Visit object |
| 199 |
|
| 200 |
The plugin extends the visit object with a new `a11y` key that can be used to customize the |
| 201 |
behavior on the fly. |
| 202 |
|
| 203 |
```js |
| 204 |
{ |
| 205 |
from: { ... }, |
| 206 |
to: { ... }, |
| 207 |
a11y: { |
| 208 |
announce: 'Navigated to: About', |
| 209 |
focus: 'body' |
| 210 |
} |
| 211 |
} |
| 212 |
``` |
| 213 |
|
| 214 |
### visit.a11y.announce |
| 215 |
|
| 216 |
The text to announce after the new page was loaded. This is the final text after choosing the |
| 217 |
correct language from the [announcements](#announcements) option and filling in any placeholders. |
| 218 |
Modify it to read a custom announcement. |
| 219 |
|
| 220 |
Since the text can only be populated once the new page was fetched and its contents are available, |
| 221 |
the only place to inspect or modify this would be right before the `content:announce` hook. |
| 222 |
|
| 223 |
```js |
| 224 |
swup.hooks.before('content:announce', (visit) => { |
| 225 |
visit.a11y.announce = 'New page loaded'; |
| 226 |
}); |
| 227 |
``` |
| 228 |
|
| 229 |
### visit.a11y.focus |
| 230 |
|
| 231 |
The element to receive focus after the new page was loaded, by default the `body`. Can be customized |
| 232 |
per visit. Set it to a selector `string` to select an element, or set it to `false` to not move the |
| 233 |
focus on this visit. |
| 234 |
|
| 235 |
## Hooks |
| 236 |
|
| 237 |
The plugin adds two new hooks: `content:announce` and `content:focus`. Both run directly |
| 238 |
after the internal `content:replace` handler, when the new content is already in the DOM. |
| 239 |
|
| 240 |
### content:announce |
| 241 |
|
| 242 |
Executes the announcement of the new page title. |
| 243 |
|
| 244 |
```js |
| 245 |
swup.hooks.on('content:announce', () => console.log('New content was announced')); |
| 246 |
``` |
| 247 |
|
| 248 |
### content:focus |
| 249 |
|
| 250 |
Executes the focussing of the new page. |
| 251 |
|
| 252 |
```js |
| 253 |
swup.hooks.on('content:focus', () => console.log('New content received focus')); |
| 254 |
``` |
| 255 |
|
| 256 |
## Methods on the swup instance |
| 257 |
|
| 258 |
The plugin adds the following method to the swup instance: |
| 259 |
|
| 260 |
### announce |
| 261 |
|
| 262 |
Announce something programmatically. Use this if you are making use of |
| 263 |
[`options.resolveUrl`](https://swup.js.org/options/#resolve-url) and still want |
| 264 |
state changes to be announced. |
| 265 |
|
| 266 |
```js |
| 267 |
swup.announce?.(`Filtered by ${myFilterString}`); |
| 268 |
``` |