Skip to content

Commit

Permalink
fix(material/chips): unable to shift + tab out of a chip set (#26727)
Browse files Browse the repository at this point in the history
Fixes that it wasn't possible for the user to use shift + tab to move out of a chip set. We had some logic to handle it already, but it was resetting the tabindex before focus has a chance to escape.

Fixes #26698.

(cherry picked from commit 168619b)
  • Loading branch information
crisbeto committed Mar 6, 2023
1 parent 8c940be commit e7612c7
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/material/chips/chip-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,18 @@ export class MatChipSet
}

/**
* Removes the `tabindex` from the chip grid and resets it back afterwards, allowing the
* user to tab out of it. This prevents the grid from capturing focus and redirecting
* Removes the `tabindex` from the chip set and resets it back afterwards, allowing the
* user to tab out of it. This prevents the set from capturing focus and redirecting
* it back to the first chip, creating a focus trap, if it user tries to tab away.
*/
protected _allowFocusEscape() {
const previousTabIndex = this.tabIndex;

if (this.tabIndex !== -1) {
const previousTabIndex = this.tabIndex;
this.tabIndex = -1;

Promise.resolve().then(() => {
this.tabIndex = previousTabIndex;
this._changeDetectorRef.markForCheck();
});
// Note that this needs to be a `setTimeout`, because a `Promise.resolve`
// doesn't allow enough time for the focus to escape.
setTimeout(() => (this.tabIndex = previousTabIndex));
}
}

Expand Down

0 comments on commit e7612c7

Please sign in to comment.