fix(material/chips): correct focus management on chip destruction#33329
Open
zsheikh-eng wants to merge 1 commit into
Open
fix(material/chips): correct focus management on chip destruction#33329zsheikh-eng wants to merge 1 commit into
zsheikh-eng wants to merge 1 commit into
Conversation
Un-privatized _redirectDestroyedChipFocus to allow MatChipGrid to override the aggressive focus-stealing behavior with a silent updateActiveItem call, respecting downstream layout stability. Fixes angular#14345
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.
Description
Fixes #14345
The Bug:
When a chip is removed from MatChipSet or MatChipGrid, keyboard navigation (like using Shift+Tab or arrow keys) breaks. This happens because the internal FocusKeyManager retains a stale reference to the deleted chip. Because the layout changes instantaneously during removal, the browser focus fails to restore reliably, causing focus to drop entirely to the document body and failing standard focus order requirements (WCAG 2.1 Success Criterion 2.4.3).
The Fix:
Initial attempts to fix this by explicitly calling chipToFocus.focus() or setActiveItem() failed because they forcefully trigger the browser's event loop, stealing the user's cursor from complex downstream inputs.
The correct solution is a silent state update. This PR:
Un-privatizes the redirection logic (_lastDestroyedFocusedChipIndex and _redirectDestroyedChipFocus) in the base MatChipSet class.
Allows MatChipGrid to override this behavior and natively use updateActiveItem().
Why this path?
Silent State Update: It fixes the stale reference seamlessly without aggressively forcing a browser focus event, preventing focus-stealing from adjacent input fields.
Predictable Accessibility: It aligns with WCAG expectations. When a chip is deleted, the active state is managed cleanly, allowing a subsequent Tab press to move to the next logical DOM element.
Testing:
Added a unit test to chip-grid.spec.ts asserting that updateActiveItem properly clears the stale internal state without forcefully hijacking document.activeElement.