Skip to content

Commit 403377d

Browse files
crisbetommalerba
authored andcommitted
fix(material-experimental/mdc-chips): removed chip still focus… (#18083)
When an MDC chip is removed it gets transitioned to `width: 0`, but it's up to the consumer to remove it from the DOM. This means that it's possible for the user to tab into it. These changes add `display: none` after the animation is done so the element becomes non-focusable.
1 parent be17c25 commit 403377d

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/material-experimental/mdc-chips/chip.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ describe('MDC-based MatChip', () => {
104104
expect(testComponent.chipRemove).toHaveBeenCalledWith({chip: chipInstance});
105105
});
106106

107+
it('should make the chip non-focusable when it is removed', () => {
108+
chipInstance.remove();
109+
fixture.detectChanges();
110+
111+
const fakeEvent = createFakeEvent('transitionend');
112+
(fakeEvent as any).propertyName = 'width';
113+
chipNativeElement.dispatchEvent(fakeEvent);
114+
115+
expect(chipNativeElement.style.display).toBe('none');
116+
});
117+
107118
it('should be able to disable ripples through ripple global options at runtime', () => {
108119
expect(chipInstance.rippleDisabled).toBe(false, 'Expected chip ripples to be enabled.');
109120

src/material-experimental/mdc-chips/chip.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,13 @@ export class MatChip extends _MatChipMixinBase implements AfterContentInit, Afte
266266
// future.
267267
},
268268
notifyTrailingIconInteraction: () => this.removeIconInteraction.emit(this.id),
269-
notifyRemoval: () => this.removed.emit({chip: this}),
269+
notifyRemoval: () => {
270+
this.removed.emit({ chip: this });
271+
272+
// When MDC removes a chip it just transitions it to `width: 0px` which means that it's still
273+
// in the DOM and it's still focusable. Make it `display: none` so users can't tab into it.
274+
this._elementRef.nativeElement.style.display = 'none';
275+
},
270276
getComputedStyleValue: propertyName => {
271277
// This function is run when a chip is removed so it might be
272278
// invoked during server-side rendering. Add some extra checks just in case.

0 commit comments

Comments
 (0)