Skip to content

Commit 8956125

Browse files
authored
fix: improve a11y-click-events-have-key-events message (#9358)
1 parent 3d20b39 commit 8956125

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

.changeset/light-hounds-complain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: improve a11y-click-events-have-key-events message

documentation/docs/05-misc/02-accessibility-warnings.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,18 @@ Enforce that `autofocus` is not used on elements. Autofocusing elements can caus
5252

5353
## `a11y-click-events-have-key-events`
5454

55-
Enforce `on:click` is accompanied by at least one of the following: `on:keyup`, `on:keydown`, `on:keypress`. Coding for the keyboard is important for users with physical disabilities who cannot use a mouse, AT compatibility, and screenreader users.
55+
Enforce that visible, non-interactive elements with an `on:click` event are accompanied by a keyboard event handler.
5656

57-
This does not apply for interactive or hidden elements.
57+
Users should first consider whether an interactive element might be more appropriate such as a `<button type="button">` element for actions or `<a>` element for navigations. These elements are more semantically meaningful and will have built-in key handling. E.g. `Space` and `Enter` will trigger a `<button>` and `Enter` will trigger an `<a>` element.
58+
59+
If a non-interactive element is required then `on:click` should be accompanied by an `on:keyup` or `on:keydown` handler that enables the user to perform equivalent actions via the keyboard. In order for the user to be able to trigger a key press, the element will also need to be focusable by adding a [`tabindex`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex). While an `on:keypress` handler will also silence this warning, it should be noted that the `keypress` event is deprecated.
5860

5961
```svelte
60-
<!-- A11y: visible, non-interactive elements with an on:click event must be accompanied by an on:keydown, on:keyup, or on:keypress event. -->
62+
<!-- A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. -->
6163
<div on:click={() => {}} />
6264
```
6365

64-
Note that the `keypress` event is now deprecated, so it is officially recommended to use either the `keyup` or `keydown` event instead, accordingly.
66+
Coding for the keyboard is important for users with physical disabilities who cannot use a mouse, AT compatibility, and screenreader users.
6567

6668
## `a11y-distracting-elements`
6769

packages/svelte/src/compiler/compile/compiler_warnings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export default {
271271
a11y_click_events_have_key_events: {
272272
code: 'a11y-click-events-have-key-events',
273273
message:
274-
'A11y: visible, non-interactive elements with an on:click event must be accompanied by an on:keydown, on:keyup, or on:keypress event.'
274+
'A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type="button"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.'
275275
},
276276
a11y_missing_content: /** @param {string} name */ (name) => ({
277277
code: 'a11y-missing-content',

packages/svelte/test/validator/samples/a11y-click-events-have-key-events/warnings.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "a11y-click-events-have-key-events",
4-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by an on:keydown, on:keyup, or on:keypress event.",
4+
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
55
"start": {
66
"line": 13,
77
"column": 0
@@ -13,7 +13,7 @@
1313
},
1414
{
1515
"code": "a11y-click-events-have-key-events",
16-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by an on:keydown, on:keyup, or on:keypress event.",
16+
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
1717
"start": {
1818
"line": 15,
1919
"column": 0
@@ -25,7 +25,7 @@
2525
},
2626
{
2727
"code": "a11y-click-events-have-key-events",
28-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by an on:keydown, on:keyup, or on:keypress event.",
28+
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
2929
"start": {
3030
"line": 18,
3131
"column": 0
@@ -37,7 +37,7 @@
3737
},
3838
{
3939
"code": "a11y-click-events-have-key-events",
40-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by an on:keydown, on:keyup, or on:keypress event.",
40+
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
4141
"start": {
4242
"line": 20,
4343
"column": 0
@@ -49,7 +49,7 @@
4949
},
5050
{
5151
"code": "a11y-click-events-have-key-events",
52-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by an on:keydown, on:keyup, or on:keypress event.",
52+
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
5353
"start": {
5454
"line": 22,
5555
"column": 0
@@ -61,7 +61,7 @@
6161
},
6262
{
6363
"code": "a11y-click-events-have-key-events",
64-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by an on:keydown, on:keyup, or on:keypress event.",
64+
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
6565
"start": {
6666
"line": 24,
6767
"column": 0
@@ -73,7 +73,7 @@
7373
},
7474
{
7575
"code": "a11y-click-events-have-key-events",
76-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by an on:keydown, on:keyup, or on:keypress event.",
76+
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
7777
"start": {
7878
"line": 26,
7979
"column": 0

packages/svelte/test/validator/samples/slot-warning/warnings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "a11y-click-events-have-key-events",
4-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by an on:keydown, on:keyup, or on:keypress event.",
4+
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
55
"start": {
66
"column": 1,
77
"line": 7

packages/svelte/test/validator/samples/slot-warning2/warnings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "a11y-click-events-have-key-events",
4-
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by an on:keydown, on:keyup, or on:keypress event.",
4+
"message": "A11y: visible, non-interactive elements with an on:click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as <button type=\"button\"> or <a> might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details.",
55
"start": {
66
"column": 1,
77
"line": 8

0 commit comments

Comments
 (0)