feat(focus): recover focus when the active element is removed#41
Open
chiefcll wants to merge 1 commit into
Open
feat(focus): recover focus when the active element is removed#41chiefcll wants to merge 1 commit into
chiefcll wants to merge 1 commit into
Conversation
When the focused element unmounted (Show toggle, list item removed, route swap), nothing noticed: activeElement kept pointing at a destroyed node and key events walked a stale focus path, dead-ending the remote until the app manually refocused. The post-mutation scheduler now runs a recovery phase after any pass that destroyed nodes: if the active element is no longer attached, focus is restored via the nearest still-attached ancestor on the old focus path, letting that ancestor's forwardFocus/selected logic pick a sensible child. Opt out with Config.focusLossRecovery = false. Dev-mode diagnostics: a warning names the lost and recovering elements when recovery fires, the keyDebug "no handler" log prints the focus path leaf-to-root, and useFocusManager warns when nothing is focused 1s after startup (missing autofocus). Co-Authored-By: Claude Fable 5 <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.
What
Adds automatic focus-loss recovery plus dev-mode diagnostics for the most common focus failure mode in TV apps: the focused element unmounts and the remote goes dead.
Why
Nothing in the framework noticed when the focused element left the tree.
removeChildand the delete-flush phase never checked whether the removed subtree containedactiveElement(), so when a<Show>toggled, a list item was deleted, or a route swapped while its content was focused, theactiveElementsignal kept pointing at a destroyed node and key events walked a stale focus path. Every app had to defend against this manually.How
elementNode.ts): the unified post-mutation scheduler gets a phase 4 — after any pass that actually destroyed nodes (the only way the active element can leave the tree, so ordinary layout/focus passes pay nothing), if no focus change is pending,recoverActiveElement()runs.recoverActiveElement()(focusManager.ts): ifactiveElement()is detached, walk the old focus path to the nearest still-attached, rendered ancestor and call itssetFocus()— that ancestor'sforwardFocus/selectedlogic then picks a sensible child. Detachment is verified by checking each node is present in its parent'schildrenarray, sinceremoveChilddoesn't clearnode.parent.config.ts):Config.focusLossRecovery, defaulttrue.isDev-gated):console.warnwhen recovery fires, naming the lost element and the recovery ancestor, with element refs attached and a pointer toprintFocusHistory().Config.keyDebug"no event handler" log now prints the focus path leaf → root like a stack trace.useFocusManagerwarns once if nothing is focused 1s after startup (missingautofocus).Notes for reviewers
onBlur/onFocusChanged(false)callbacks viaupdateFocusPath. Blur handlers that touch the node should tolerate it being destroyed — same exposure as any late callback, but worth knowing.selectedreconciliation on child removal, focus traps, and a WebGL-drawnfocusDebugoverlay (the current overlay is DOM-renderer-only).Testing
New
tests/focusRecovery.test.tsxcovers: recovery to parent (and that the dev warning fires), deep-subtree removal skipping detached ancestors, recovery re-runningforwardFocusso a sibling gains focus, removal of a non-focused element not moving focus, and thefocusLossRecovery = falseopt-out. Full suite passes (139 tests),tscclean, no new lint warnings.🤖 Generated with Claude Code