Repository: sveltejs/svelte (2025-07-11T05:11:49Z)

Issue:
Wrong a11y wargning on <dialog> with autofocus attribute
### Describe the bug

Ally warning "Avoid using autofocus" appears on `<dialog autofocus>` element definition. 
But using of autofocus on dialog or its descendants is recommended by the standard: https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element

> As such, authors should use the [autofocus](https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus) attribute on the descendant element of the dialog that the user is expected to immediately interact with after the dialog opens. If there is no such element, then authors should use the [autofocus](https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus) attribute on the [dialog](https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element) element itself.

### Reproduction

```html
<dialog autofocus>
	I'm a dialog
</dialog>
```

https://svelte.dev/repl/483135a2842e4da6ba6f24ea63430cf2?version=4.2.0

### Logs

_No response_

### System Info

```shell
N/A
```


### Severity

annoyance


Discussion:
It may not be necessary in many cases; as the spec states:
> The [dialog focusing steps](https://html.spec.whatwg.org/multipage/interactive-elements.html#dialog-focusing-steps) attempt to pick a good candidate for initial focus when a dialog is shown

It looks like browsers will already focus the first interactive element and if there is none, the dialog itself.
So you should only need it...
- on an interactive element if it is not the first but you want to focus it instead
- on the dialog if there are interactive elements inside but you do not want to focus those

I guess a common case would be a close button you want to skip, though.
Agree that using this attribute could be not necessary, but still recommended and definitely not an error, so should not be treated as an a11y issue.
I'd like to bump this issue since I just encountered it. The MDN docs specifically say to use `autofocus` on the `<dialog>` when there isn't a specific element that should have first focus.



Patch:
diff --git a/.changeset/cuddly-humans-end.md b/.changeset/cuddly-humans-end.md
new file mode 100644
index 000000000000..d02e08de7aac
--- /dev/null
+++ b/.changeset/cuddly-humans-end.md
@@ -0,0 +1,5 @@
+---
+'svelte': patch
+---
+
+fix: silence autofocus a11y warning inside `<dialog>`
diff --git a/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/a11y.js b/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/a11y.js
index 1f58a28cad73..e47c8c0c9764 100644
--- a/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/a11y.js
+++ b/packages/svelte/src/compiler/phases/2-analyze/visitors/shared/a11y.js
@@ -875,7 +875,7 @@ export function check_element(node, context) {
 		}
 
 		// no-autofocus
-		if (name === 'autofocus') {
+		if (name === 'autofocus' && node.name !== 'dialog' && !is_parent(context.path, ['dialog'])) {
 			w.a11y_autofocus(attribute);
 		}
 


Test patch:
diff --git a/packages/svelte/tests/validator/samples/a11y-no-autofocus/input.svelte b/packages/svelte/tests/validator/samples/a11y-no-autofocus/input.svelte
index 769dbe8c5b69..7b1dccd1e835 100644
--- a/packages/svelte/tests/validator/samples/a11y-no-autofocus/input.svelte
+++ b/packages/svelte/tests/validator/samples/a11y-no-autofocus/input.svelte
@@ -1,1 +1,6 @@
-<div autofocus></div>
\ No newline at end of file
+<div autofocus></div>
+<dialog autofocus>
+</dialog>
+<dialog>
+	<input autofocus>
+</dialog>