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

Fix: Retain focus for persisted input elements during view transitions #8813

Merged
merged 3 commits into from
Oct 11, 2023

Conversation

martrapp
Copy link
Member

Changes

An element with transition:persist survives view transitions and continues its life on the next page. Its state is preserved. For example, a HTMLTextAreaElement retains the text entered up to that point. Unfortunately, persisted elements lose focus during the transition when they are removed from the DOM and reinserted.

This fix saves the current focus and cursor position and restores them on the new page.

Testing

Added e2e test

Docs

n/a bug fix

@changeset-bot
Copy link

changeset-bot bot commented Oct 11, 2023

🦋 Changeset detected

Latest commit: 2f8965e

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Oct 11, 2023
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really related to focus handling, but as I was going through the tests I noticed that we are running with output = server, when in fact almost all tests are static. So I marked the test that needs the server with prerender = false and changed to hybrid.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file was always called styles.css, but I noticed it only now

@martrapp martrapp changed the title Mt/focus Save and restore focus for persisted input elements during view transitions Oct 11, 2023
@martrapp martrapp changed the title Save and restore focus for persisted input elements during view transitions Fix: Retain focus for persisted input elements during view transitions Oct 11, 2023
Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems great, thanks for the fix and detailed test!

@martrapp martrapp merged commit 3bef32f into main Oct 11, 2023
@martrapp martrapp deleted the mt/focus branch October 11, 2023 22:08
@astrobot-houston astrobot-houston mentioned this pull request Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants