Skip to content

fix(material/chips): correct focus management on chip destruction#33329

Open
zsheikh-eng wants to merge 1 commit into
angular:mainfrom
zsheikh-eng:fix-chip-focus-bug
Open

fix(material/chips): correct focus management on chip destruction#33329
zsheikh-eng wants to merge 1 commit into
angular:mainfrom
zsheikh-eng:fix-chip-focus-bug

Conversation

@zsheikh-eng
Copy link
Copy Markdown

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.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CDK: ListKeyManager activeItem has incorrect reference after QueryList changes

1 participant