sveltejs / sveltejs/kit

Allow setting `keepFocus` when navigating using history api

Open
#10,872 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

router
Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the problem

It is impossible to go back using the goto method, so I have to use history.go(-1).
This forces sveltekit to provide a hardcoded keepFocus value (https://github.com/sveltejs/kit/blob/master/packages/kit/src/runtime/client/client.js#L1685).
This means that clicking a button that causes history navigation is forced to lose focus unnecessarily.

Describe the proposed solution

Ideally, the focus is never lost, I would like to be able to set a contextual value somewhere (global variable, localstate, cookie, shared global store, whatever is appropriate), and have the popstate handler use that to set the keepFocus flag.

Alternatives considered

I currently am forcing the button to refocus after being clicked, but this is not ideal as a screen reader will announce focus lost, and focus regained.

Importance

would make my life easier

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in packages/kit/src/runtime/client/client.js around the hardcoded keepFocus value at line 1685, then trace the popstate handling used by history navigation. Define how callers would provide contextual focus behavior and verify that history navigation preserves focus without the current forced value; no test file is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.