Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/router/src/directives/router_link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export class ReactiveRouterState {
}
}

// Whether `RouterLink` adds a `tabindex="0"` to non-anchor host elements. This was removed for the
// public API (see #28345) because the element is put in the tab order without being made
// keyboard-operable. It is retained inside Google via the marker below while callers migrate.
// g3-only const ADD_TABINDEX_TO_NON_ANCHOR_ELEMENTS: boolean = true;
const ADD_TABINDEX_TO_NON_ANCHOR_ELEMENTS: boolean = false; // 3p-only

/**
* @description
*
Expand Down Expand Up @@ -434,7 +440,11 @@ export class RouterLink implements OnChanges, OnDestroy {
* during instantiation.
*/
private setTabIndexIfNotOnNativeEl(newTabIndex: string | null) {
if (this.tabIndexAttribute != null /* both `null` and `undefined` */ || this.isAnchorElement) {
if (
!ADD_TABINDEX_TO_NON_ANCHOR_ELEMENTS ||
this.tabIndexAttribute != null /* both `null` and `undefined` */ ||
this.isAnchorElement
) {
return;
}
this.applyAttributeValue('tabindex', newTabIndex);
Expand Down
2 changes: 1 addition & 1 deletion packages/router/test/integration/router_links.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export function routerLinkIntegrationSpec() {
expect(fixture.nativeElement).toHaveText('team 22 [ link, right: ]');

const button = fixture.nativeElement.querySelector('button');
expect(button.getAttribute('tabindex')).toEqual('0');
expect(button.hasAttribute('tabindex')).toBeFalse();
button.click();
await advance(fixture);

Expand Down
12 changes: 7 additions & 5 deletions packages/router/test/router_link_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {Router, RouterLink, RouterModule, provideRouter} from '../index';
import {RouterTestingHarness} from '../testing';

describe('RouterLink', () => {
it('does not modify tabindex if already set on non-anchor element', async () => {
it('does not touch a tabindex set in the template on a non-anchor element', async () => {
@Component({
template: `<div [routerLink]="link" tabindex="1"></div>`,
standalone: false,
Expand Down Expand Up @@ -73,19 +73,21 @@ describe('RouterLink', () => {
(router.navigateByUrl as jasmine.Spy).calls.reset();
});

it('null, removes tabIndex and does not navigate', async () => {
it('does not add a tabindex attribute', () => {
expect(link.hasAttribute('tabindex')).toBeFalse();
});

it('null, does not navigate', async () => {
fixture.componentInstance.link.set(null);
await fixture.whenStable();
expect(link.tabIndex).toEqual(-1);

link.click();
expect(router.navigateByUrl).not.toHaveBeenCalled();
});

it('undefined, removes tabIndex and does not navigate', async () => {
it('undefined, does not navigate', async () => {
fixture.componentInstance.link.set(undefined);
await fixture.whenStable();
expect(link.tabIndex).toEqual(-1);

link.click();
expect(router.navigateByUrl).not.toHaveBeenCalled();
Expand Down
Loading