Skip to content

Commit f1dc190

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 0f17f16 commit f1dc190

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
@@ -23,6 +23,14 @@
2323
flex-basis: 0;
2424
flex-grow: 1;
2525
}
26+
27+
&.mat-tab-disabled {
28+
// We use `pointer-events` to make the element unclickable when it's disabled, rather than
29+
// preventing the default action through JS, because we can't prevent the action reliably
30+
// due to other directives potentially registering their events earlier. This shouldn't cause
31+
// the user to click through, because we always have a `.mat-tab-links` behind the link.
32+
pointer-events: none;
33+
}
2634
}
2735

2836

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
@@ -177,7 +177,6 @@ export const _MatTabLinkMixinBase =
177177
'[attr.tabIndex]': 'tabIndex',
178178
'[class.mat-tab-disabled]': 'disabled',
179179
'[class.mat-tab-label-active]': 'active',
180-
'(click)': '_handleClick($event)'
181180
}
182181
})
183182
export class MatTabLink extends _MatTabLinkMixinBase
@@ -238,13 +237,4 @@ export class MatTabLink extends _MatTabLinkMixinBase
238237
ngOnDestroy() {
239238
this._tabLinkRipple._removeTriggerEvents();
240239
}
241-
242-
/**
243-
* Handles the click event, preventing default navigation if the tab link is disabled.
244-
*/
245-
_handleClick(event: MouseEvent) {
246-
if (this.disabled) {
247-
event.preventDefault();
248-
}
249-
}
250240
}

0 commit comments

Comments
 (0)