-
Notifications
You must be signed in to change notification settings - Fork 436
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
Restore focus when transposing Permanent Elements #436
Conversation
0391fc7
to
582d8a8
Compare
ff9d8c1
to
35992ab
Compare
As an alternative to handling this internally, Turbo could dispatch a new That way, if they want to do any attribute merging, they have an opportunity to do so based off addEventListener("turbo:before-permanent-element-render", ({ target, detail: { newElement } }) => {
const validationMessageId = newElement.getAttribute("aria-describedby")
if (validationMessageId) target.setAttribute("aria-describedby", validationMessageId)
}) If they want to preserve focus or anything else, they can assign their own addEventListener("turbo:before-permanent-element-render", (event) => {
const { detail: { render } } = event
event.render = (currentElement, newElement) => {
const activeElement = currentElement.contains(document.activeElement) ?
document.activeElement :
null
render(currentElement, newElement)
activeElement?.focus()
}
}) |
10d7d67
to
1e52436
Compare
f394ab9
to
d159910
Compare
b00d01a
to
347a0b0
Compare
Follow-up to [hotwired#573][]. ESLint support was recently merged and integrated in to the Continuous Integration checks, but excluded several existing linting violations. This commit's diff was generated by executing `yarn lint --fix`. These violations were discovered when rebasing [hotwired#436][]. Once this is passing and merged, these changes will be rebased into that branch's changeset. [hotwired#573]: hotwired#573 [hotwired#436]: https://github.com/hotwired/turbo/pull/436/files
Follow-up to [hotwired#573][]. ESLint support was recently merged and integrated in to the Continuous Integration checks, but excluded several existing linting violations. This commit's diff was generated by executing `yarn lint --fix`. These violations were discovered when rebasing [hotwired#436][]. Once this is passing and merged, these changes will be rebased into that branch's changeset. [hotwired#573]: hotwired#573 [hotwired#436]: https://github.com/hotwired/turbo/pull/436/files
Follow-up to [hotwired#573][]. ESLint support was recently merged and integrated in to the Continuous Integration checks, but excluded several existing linting violations. This commit's diff was generated by executing `yarn lint --fix`. These violations were discovered when rebasing [hotwired#436][]. Once this is passing and merged, these changes will be rebased into that branch's changeset. [hotwired#573]: hotwired#573 [hotwired#436]: https://github.com/hotwired/turbo/pull/436/files
Follow-up to [hotwired#573][]. ESLint support was recently merged and integrated in to the Continuous Integration checks, but excluded several existing linting violations. This commit's diff was generated by executing `yarn lint --fix`. These violations were discovered when rebasing [hotwired#436][]. Once this is passing and merged, these changes will be rebased into that branch's changeset. [hotwired#573]: hotwired#573 [hotwired#436]: https://github.com/hotwired/turbo/pull/436/files
Follow-up to [hotwired#573][]. ESLint support was recently merged and integrated in to the Continuous Integration checks, but excluded several existing linting violations. This commit's diff was generated by executing `yarn lint --fix`. These violations were discovered when rebasing [hotwired#436][]. Once this is passing and merged, these changes will be rebased into that branch's changeset. [hotwired#573]: hotwired#573 [hotwired#436]: https://github.com/hotwired/turbo/pull/436/files
* Generated: execute `yarn lint --fix` Follow-up to [#573][]. ESLint support was recently merged and integrated in to the Continuous Integration checks, but excluded several existing linting violations. This commit's diff was generated by executing `yarn lint --fix`. These violations were discovered when rebasing [#436][]. Once this is passing and merged, these changes will be rebased into that branch's changeset. [#573]: #573 [#436]: https://github.com/hotwired/turbo/pull/436/files * Resolve failing `FormSubmissionTest` There is a race condition in the `FormSubmissionTest` file's coverage for confirmation. If evaluated quickly enough, the test's assertions might be checked before the subsequent Turbo Driver navigation occurs, which would fail the check for the Form Submission's change to the current path. This commit adds an additional `await` expression to wait until the next [turbo:load][] event to fire, so that the entire navigation can complete before the assertions are evaluated. [turbo:load]: https://turbo.hotwired.dev/reference/events
2890f27
to
78ea3a8
Compare
Problem --- When an interactive element like an `<input>`, `<select>`, `<textarea>`, or `<button>` has an `[id]` attribute and is marked as `[data-turbo-permanent]`, the element and its internal state is preserved _except_ for its focus state. Imagine a search form: ```html <form> <label for="search">Search</label> <input type="search" id="search" name="q" data-turbo-permanent> </form> ``` When typing a query into the box and submitting the `<form>` by <kbd>enter</kbd>, the request is submitted, the page transitions, the element retains its `[value]` attribute, but focus is lost. Solution --- When transposing a permanent element during a `Renderer` descendant's (e.g. [PageRenderer](./src/core/drive/page_renderer.ts), [ErrorRenderer](./src/core/drive/error_renderer.ts), or [FrameRenderer](./src/core/frames/frame_renderer.ts)) render cycle, capture the `document.activeElement`. Once the permanent element is inserted into the new page, use that retained reference to re-focus the element.
78ea3a8
to
7951681
Compare
@dhh this is ready for review. |
Really clever, @seanpdoyle. I like it. |
I've opened #622 as a follow-up to this change. It leverages the |
Problem
When an interactive element like an
<input>
,<select>
,<textarea>
,or
<button>
has an[id]
attribute and is marked as[data-turbo-permanent]
, the element and its internal state ispreserved except for its focus state.
Imagine a search form:
When typing a query into the box and submitting the
<form>
byenter, the request is submitted, the page transitions, the
element retains its
[value]
attribute, but focus is lost.Solution
When transposing a permanent element during a
Renderer
descendant's(e.g. PageRenderer, ErrorRenderer, or FrameRenderer) render cycle,
capture the
document.activeElement
.Once the permanent element is inserted into the new page, use that
retained reference to re-focus the element.