The autofocus issue
#18080
Replies: 2 comments 1 reply
-
Not really. You only need it if you want to change the element that should receive focus, dialogs already automatically focus the first interactive element. The piece of JS is required for pages that are created on the client since That certain warnings are prone to false positives with component nesting is an old issue. If I remember correctly it used to happen a lot with label/input relationships until it was disabled for labels that contain components (i.e. assuming that the component probably contains the relevant input). But since this is the other way around, with the warning being on the inside, the warning cannot be suppressed conditionally while keeping component analysis isolated. Some of these warning just need to be suppressed manually. |
Beta Was this translation helpful? Give feedback.
-
This is not true for popovers, though. And changing the autofocus element could be quite common in dialogs, for instance where you have an X button to close in the dialog header but you want to focus the first form element instead.
But we're telling devs that using
I just think that forcing developers to use comment directives to mark legitimite code has a pretty bad smell to it. I would do it in TypeScript or ESLint only as an absolute last resort. Would you at least agree that the warning message is incorrect? Currently, it doesn't give the developer the correct advice about usage. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I know a lot of appeals for this problem have gone unheeded in #6629. I'm hoping to start a reasoned discussion about it, as I believe the current warning is actually harmful for a11y in 2026.
Background
The Svelte compiler will warn you if you include the
autofocusattribute on any element.One exception is when the element is a descendant of a
<dialog>element, thanks to #16341:Unfortunately, this doesn't work for component composition:
Descendents of elements with the
popoverattribute will always display the warning.Svelte's weird relationship with
autofocusSvelte actually includes some logic to call
element.focus()on mount:So, Svelte's position on
autofocusis flawed: on the one hand, it tells you not to use it and, on the other hand, it helps you if use it. My first suggestion would be to deprecate this logic and remove it in Svelte 6. Here's why:autofocususe cases: top-layer components (currently popovers and dialogs) will handleautofocuscorrectly$effect()runeautofocuson non-top-layer elementsThe warning
My personal preference is that the warning would go away. Dialogs and popovers with interactive content absolutely should have an element with
autofocus. It would actually be better if Svelte could warn when this isn't the case (but I know that would be quite complicated to implement). I also think that, as a developer, I should not receive—and have to go out of my way to disable—warnings for what is essentially a good practice.If it helps,
<dialog>attained universal browser support in 2022,popoverin 2024. There are options for progressive enhancement implementations of these. It no longer makes sense to flatly discourage all uses ofautofocus.If the warning is here to stay, then I think the documentation should be updated to tell the developer to disable the warning and use
autofocuson descendants of<dialog>or<xyz popover>. It should not tell them they are doing something wrong for their legitimate use cases.Beta Was this translation helpful? Give feedback.
All reactions