Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not autofocus inert or hidden elements #654

Merged
merged 2 commits into from
Jul 29, 2022

Conversation

seanpdoyle
Copy link
Contributor

Closes hotwired/turbo#546

According to the specification, there is an algorithm for
determining the focusable area, that is, the elements with the
[autofocus] attribute that can receive focus:

Elements that meet all the following criteria:

  • the element's tabindex value is non-null, or the element is determined by the user agent to be focusable;
  • the element is either not a shadow host, or has a shadow root whose delegates focus is false;
  • the element is not actually disabled;
  • the element is not inert;
  • the element is either being rendered or being used as relevant canvas fallback content.

This commit extends the Snapshot.firstAutofocusableElement to
programmatically determine the focusable area based on the available
attributes, including checks for <details> and <dialog> ancestors
that do not declare [open].

src/core/snapshot.ts Outdated Show resolved Hide resolved
@seanpdoyle seanpdoyle force-pushed the autofocus-avoids-inert-elements branch 2 times, most recently from 8d36ac5 to 24a416e Compare July 29, 2022 18:07
Closes [hotwired#546][]

According to the specification, there is an [algorithm][] for
determining the [focusable area][], that is, the elements with the
`[autofocus]` attribute that can receive focus:

> Elements that meet all the following criteria:
> * the element's [tabindex][] value is non-null, or the element is determined by the user agent to be focusable;
> * the element is either not a shadow host, or has a shadow root whose delegates focus is false;
> * the element is not actually [disabled][];
> * the element is not [inert][];
> * the element is either being rendered or being used as relevant canvas fallback content.

This commit extends the `Snapshot.firstAutofocusableElement` to
programmatically determine the focusable area based on the available
attributes, including checks for `<details>` and `<dialog>` ancestors
that do not declare `[open]`.

[hotwired#546]: hotwired#546
[tabindex]: https://html.spec.whatwg.org/multipage/interaction.html#tabindex-value
[algorithm]: https://html.spec.whatwg.org/multipage/interaction.html#get-the-focusable-area
[disabled]: https://html.spec.whatwg.org/multipage/semantics-other.html#concept-element-disabled
[inert]: https://html.spec.whatwg.org/multipage/interaction.html#inert
[focusable area]: https://html.spec.whatwg.org/multipage/interaction.html#focusable-area
[hidden]: https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden
@seanpdoyle seanpdoyle force-pushed the autofocus-avoids-inert-elements branch from 24a416e to 0c6bc0e Compare July 29, 2022 19:49
Two tests covering the `[recursive]` attribute were depending on the
`nextBeat()` helper, which caused inconsistent failures due to timing
issues.

This commit replaces the time-based `nextBeat()` calls with promises to
wait for `turbo:frame-load` events to fire on the `<turbo-frame
id="recursive">` and the nested `<turbo-frame id="composer">` elements.
@dhh dhh merged commit 26a4f8b into hotwired:main Jul 29, 2022
@seanpdoyle seanpdoyle deleted the autofocus-avoids-inert-elements branch July 29, 2022 21:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants