Skip to content

Commit 3762d26

Browse files
committed
fix(tabs): disabled tab link not preventing router navigation
Fixes users being able to navigate to a new route by clicking on a disabled `mat-tab-link`. Fixes #10354.
1 parent e09ec34 commit 3762d26

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

src/lib/tabs/tab-nav-bar/tab-nav-bar.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
flex-basis: 0;
2525
flex-grow: 1;
2626
}
27+
28+
&.mat-tab-disabled {
29+
// We use `pointer-events` to make the element unclickable when it's disabled, rather than
30+
// preventing the default action through JS, because we can't prevent the action reliably
31+
// due to other directives potentially registering their events earlier. This shouldn't cause
32+
// the user to click through, because we always have a `.mat-tab-links` behind the link.
33+
pointer-events: none;
34+
}
2735
}
2836

2937

src/lib/tabs/tab-nav-bar/tab-nav-bar.spec.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {async, ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
22
import {Component, ViewChild, ViewChildren, QueryList} from '@angular/core';
33
import {By} from '@angular/platform-browser';
4-
import {dispatchFakeEvent, dispatchMouseEvent, createMouseEvent} from '@angular/cdk/testing';
4+
import {dispatchFakeEvent, dispatchMouseEvent} from '@angular/cdk/testing';
55
import {Direction, Directionality} from '@angular/cdk/bidi';
66
import {Subject} from 'rxjs';
77
import {MatTabLink, MatTabNav, MatTabsModule} from '../index';
@@ -150,21 +150,15 @@ describe('MatTabNavBar', () => {
150150
.toBe(true, 'Expected element to no longer be keyboard focusable if disabled.');
151151
});
152152

153-
it('should prevent default action for clicks if links are disabled', () => {
153+
it('should make disabled links unclickable', () => {
154154
const tabLinkElement = fixture.debugElement.query(By.css('a')).nativeElement;
155155

156-
const mouseEvent = createMouseEvent('click');
157-
spyOn(mouseEvent, 'preventDefault');
158-
tabLinkElement.dispatchEvent(mouseEvent);
159-
expect(mouseEvent.preventDefault).not.toHaveBeenCalled();
156+
expect(getComputedStyle(tabLinkElement).pointerEvents).not.toBe('none');
160157

161158
fixture.componentInstance.disabled = true;
162159
fixture.detectChanges();
163160

164-
const mouseEventWhileDisabled = createMouseEvent('click');
165-
spyOn(mouseEventWhileDisabled, 'preventDefault');
166-
tabLinkElement.dispatchEvent(mouseEventWhileDisabled);
167-
expect(mouseEventWhileDisabled.preventDefault).toHaveBeenCalled();
161+
expect(getComputedStyle(tabLinkElement).pointerEvents).toBe('none');
168162
});
169163

170164
it('should show ripples for tab links', () => {

src/lib/tabs/tab-nav-bar/tab-nav-bar.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ export const _MatTabLinkMixinBase =
178178
'[attr.tabIndex]': 'tabIndex',
179179
'[class.mat-tab-disabled]': 'disabled',
180180
'[class.mat-tab-label-active]': 'active',
181-
'(click)': '_handleClick($event)'
182181
}
183182
})
184183
export class MatTabLink extends _MatTabLinkMixinBase
@@ -258,13 +257,4 @@ export class MatTabLink extends _MatTabLinkMixinBase
258257
this._focusMonitor.stopMonitoring(this._elementRef.nativeElement);
259258
}
260259
}
261-
262-
/**
263-
* Handles the click event, preventing default navigation if the tab link is disabled.
264-
*/
265-
_handleClick(event: MouseEvent) {
266-
if (this.disabled) {
267-
event.preventDefault();
268-
}
269-
}
270260
}

0 commit comments

Comments
 (0)