Skip to content

Commit 5cec88b

Browse files
authored
[docs] Update form docs for a11y (#8064)
- add labels to inputs in examples - note that focus management applies to enhanced forms
1 parent 1264ae5 commit 5cec88b

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

documentation/docs/20-core-concepts/30-form-actions.md

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ To invoke this action from the `/login` page, just add a `<form>` — no JavaScr
2525
```svelte
2626
/// file: src/routes/login/+page.svelte
2727
<form method="POST">
28-
<input name="email" type="email">
29-
<input name="password" type="password">
28+
<label>
29+
Email
30+
<input name="email" type="email">
31+
</label>
32+
<label>
33+
Password
34+
<input name="password" type="password">
35+
</label>
3036
<button>Log in</button>
3137
</form>
3238
```
@@ -81,8 +87,14 @@ As well as the `action` attribute, we can use the `formaction` attribute on a bu
8187
/// file: src/routes/login/+page.svelte
8288
-<form method="POST">
8389
+<form method="POST" action="?/login">
84-
<input name="email" type="email">
85-
<input name="password" type="password">
90+
<label>
91+
Email
92+
<input name="email" type="email">
93+
</label>
94+
<label>
95+
Password
96+
<input name="password" type="password">
97+
</label>
8698
<button>Log in</button>
8799
+ <button formaction="?/register">Register</button>
88100
</form>
@@ -179,12 +191,17 @@ export const actions = {
179191
```diff
180192
/// file: src/routes/login/+page.svelte
181193
<form method="POST" action="?/login">
182-
- <input name="email" type="email">
183194
+ {#if form?.missing}<p class="error">The email field is required</p>{/if}
184195
+ {#if form?.incorrect}<p class="error">Invalid credentials!</p>{/if}
185-
+ <input name="email" type="email" value={form?.email ?? ''}>
186-
187-
<input name="password" type="password">
196+
<label>
197+
Email
198+
- <input name="email" type="email">
199+
+ <input name="email" type="email" value={form?.email ?? ''}>
200+
</label>
201+
<label>
202+
Password
203+
<input name="password" type="password">
204+
</label>
188205
<button>Log in</button>
189206
<button formaction="?/register">Register</button>
190207
</form>
@@ -323,6 +340,7 @@ Without an argument, `use:enhance` will emulate the browser-native behaviour, ju
323340
- reset the `<form>` element and invalidate all data using `invalidateAll` on a successful response
324341
- call `goto` on a redirect response
325342
- render the nearest `+error` boundary if an error occurs
343+
- [reset focus](/docs/accessibility#focus-management) to the appropriate element
326344

327345
To customise the behaviour, you can provide a `SubmitFunction` that runs immediately before the form is submitted, and (optionally) returns a callback that runs with the `ActionResult`. Note that if you return a callback, the default behavior mentioned above is not triggered. To get it back, call `update`.
328346

@@ -381,6 +399,8 @@ The behaviour of `applyAction(result)` depends on `result.type`:
381399
- `redirect` — calls `goto(result.location)`
382400
- `error` — renders the nearest `+error` boundary with `result.error`
383401

402+
In all cases, [focus will be reset](/docs/accessibility#focus-management).
403+
384404
### Custom event listener
385405

386406
We can also implement progressive enhancement ourselves, without `use:enhance`, with a normal event listener on the `<form>`:
@@ -448,7 +468,10 @@ Some forms don't need to `POST` data to the server — search inputs, for exampl
448468

449469
```html
450470
<form action="/search">
451-
<input name="q">
471+
<label>
472+
Search
473+
<input name="q">
474+
</label>
452475
</form>
453476
```
454477

documentation/docs/40-best-practices/10-accessibility.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ This will allow screen readers and other assistive technology to identify the ne
2929

3030
In traditional server-rendered applications, every navigation will reset focus to the top of the page. This ensures that people browsing the web with a keyboard or screen reader will start interacting with the page from the beginning.
3131

32-
To simulate this behavior during client-side routing, SvelteKit focuses the `<body>` element after each navigation. If you want to customize this behavior, you can implement custom focus management logic using the `afterNavigate` hook:
32+
To simulate this behavior during client-side routing, SvelteKit focuses the `<body>` element after each navigation and [enhanced form submission](https://kit.svelte.dev/docs/form-actions#progressive-enhancement). There is one exception - if an element with the [`autofocus`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autofocus) attribute is present, SvelteKit will focus that element instead. Make sure to [consider the implications for assistive technology](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/autofocus#accessibility_considerations) when using that attribute.
33+
34+
If you want to customize SvelteKit's focus management, you can use the `afterNavigate` hook:
3335

3436
```js
3537
/// <reference types="@sveltejs/kit" />

0 commit comments

Comments
 (0)