fix: blur active element before component update during navigation#15452
Open
MathiasWP wants to merge 2 commits intosveltejs:mainfrom
Open
fix: blur active element before component update during navigation#15452MathiasWP wants to merge 2 commits intosveltejs:mainfrom
MathiasWP wants to merge 2 commits intosveltejs:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 3894e26 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
f3bd85d to
a0aad54
Compare
…veltejs#14575) Blur the active element before `root.$set()` so that blur/focusout handlers fire while the old component's data is still valid. Previously, data was nulled out before blur fired, causing TypeErrors in blur handlers that accessed component data. Guarded by `!keepfocus` and only applies when the active element is not the body. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
a0aad54 to
2a3818c
Compare
The svelte-check type checker flags `window.__blur_test_result` as an error since the property doesn't exist on the Window type. Use the same `/** @type {any} */ (window)` cast pattern used in the test file. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #14575
Summary
root.$set()so that blur/focusout handlers fire while the old component's data is still validroot.$set()would null out component data before the browser firedbluron the old active element, causingTypeError: Cannot set properties of undefinedin blur handlers that accesseddatagoto()and popstate (back button) navigation pathsEdge case analysis
Safe — no breakage expected
<body>after navigation (reset_focus()). The blur just fires slightly earlier now.keepFocus: true: Guarded by!keepfocus— no change in behavior.activeElement !== document.body— no spurious blur fires.reset_focus()still runs after component update.Edge cases (low risk)
goto(): The existingnav_tokensystem will abort the original navigation. This is already possible today — the fix just means blur fires slightly earlier, so such navigations could cancel more often. Very uncommon pattern and already fragile.alert()/confirm(): These block the JS thread, delaying navigation. Same behavior as before, just fires at a slightly different time.root.$set(), mutations could theoretically conflict — but Svelte's component update replaces the DOM anyway, so this is safe in practice.Test plan
pnpm buildpassesblur handler can access data during navigation(goto path)blur handler can access data during popstate navigation(back button path)pnpm -F @sveltejs/kit test:unit— no regressions (447 passed)🤖 Generated with Claude Code