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

Respect autofocus after a client-side navigation or enhanced form submit #6643

Merged
merged 16 commits into from Nov 10, 2022

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Sep 7, 2022

Closes #6629. This makes client-side navigation/submission behave more like their native counterparts

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

@changeset-bot
Copy link

changeset-bot bot commented Sep 7, 2022

🦋 Changeset detected

Latest commit: 58ef6de

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

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

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

// in this case we're simply invalidating
await tick();

if (!keepfocus) reset_focus();
Copy link
Member

Choose a reason for hiding this comment

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

I don't fully grasp the implications of this, so I'll ask: what difference does it make to switch the order of execution?

Copy link
Member Author

Choose a reason for hiding this comment

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

Focus management needs to happen after the DOM has been updated — previously, it would reset while DOM updates were still pending. We got away with it because we were focusing the <body> which is unchanged, but that won't do any more

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually I'm wrong — we can focus immediately, it turns out. And we need to, because if we wait for tick() to resolve it will blow away any manual focus management. Gonna blame sleep deprivation again

@@ -1207,6 +1184,8 @@ export function create_client({ target, base, trailing_slash }) {
const post_update = pre_update();
root.$set(props);
post_update();

tick().then(reset_focus);
Copy link
Member

Choose a reason for hiding this comment

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

I think we should only do this when the type is success. When input is invalid, it might be nice to keep focus on the currently selected thing instead.

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 currently selected thing will be the button that was used to submit the form — by keeping the focus there the only thing we're really doing is making it more likely that the user will resubmit bad data, I think

Copy link
Member

Choose a reason for hiding this comment

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

Just tried this out - if you press enter on an input (which isn't that uncommon I'd say) the focus stays on that input after submitting it. I would be in favor of keeping the focus on that input on an invalid response.

@Rich-Harris
Copy link
Member Author

gah, i can't reproduce the webkit test failures locally. might have to rethink some stuff

This reverts commit 00b777b.
@Rich-Harris
Copy link
Member Author

Couldn't figure out the webkit stuff so I, err... skipped the test on webkit

@Rich-Harris
Copy link
Member Author

The tests pass in webkit locally (and it works as expected when testing manually in Safari) so I have no idea what's going on with the CI failures, but frankly I am well past the point of caring. Reverted 3d5a235

@dummdidumm dummdidumm merged commit 57aa81c into master Nov 10, 2022
@dummdidumm dummdidumm deleted the gh-6629 branch November 10, 2022 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Navigation and use:enhance should apply autofocus
2 participants