fix(router): stop adding tabindex to non-anchor elements with routerLink by arturovt · Pull Request #70573 · angular/angular · GitHub
Skip to content

fix(router): stop adding tabindex to non-anchor elements with routerLink - #70573

Open
arturovt wants to merge 1 commit into
angular:mainfrom
arturovt:fix/router_28345_no-auto-tabindex
Open

fix(router): stop adding tabindex to non-anchor elements with routerLink#70573
arturovt wants to merge 1 commit into
angular:mainfrom
arturovt:fix/router_28345_no-auto-tabindex

Conversation

@arturovt

@arturovt arturovt commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

RouterLink automatically set tabindex="0" on any non-<a>/<area>
element it was applied to, with no way to opt out short of writing a
literal tabindex attribute in the template. It also made the element
focusable without making it keyboard-operable, since RouterLink only
listens for click events and never handled Enter/Space.

The behavior is now gated behind ADD_TABINDEX_TO_NON_ANCHOR_ELEMENTS,
which is false in the published package and kept true inside Google
(via a sync marker) while internal callers migrate off it. Because it is a
module-level const, bundlers fold it to false and drop the guarded
setTabIndexIfNotOnNativeEl body, so the published package carries none of
the tabindex logic.

To let the rest tree-shake with it:

  • Renderer2 is dropped. It was used only to write the tabindex
    attribute; that write now goes straight to the host element.
  • @Attribute('tabindex') is replaced with a HostAttributeToken
    injected only inside the ADD_TABINDEX_TO_NON_ANCHOR_ELEMENTS branch of
    the constructor, so it is removed by the same fold.

The RouterLink constructor now takes (router, route, el, locationStrategy?).

BREAKING CHANGE: RouterLink no longer adds tabindex="0" to non-anchor
elements (e.g. <div routerLink>, <button routerLink>) by default. If
you need such an element to be part of the tab order, add tabindex="0"
to it directly in the template.

Fixes #28345

@angular-robot angular-robot Bot added detected: breaking change PR contains a commit with a breaking change area: router labels Sep 4, 2026
@ngbot ngbot Bot added this to the Backlog milestone Sep 4, 2026
@arturovt

arturovt commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

@arturovt
arturovt marked this pull request as ready for review September 4, 2026 20:00
@pullapprove
pullapprove Bot requested a review from crisbeto September 4, 2026 20:01
@arturovt
arturovt force-pushed the fix/router_28345_no-auto-tabindex branch from 8e90c73 to d5ff504 Compare September 5, 2026 20:36
RouterLink automatically set `tabindex="0"` on any non-`<a>`/`<area>`
element it was applied to, with no way to opt out short of writing a
literal tabindex attribute in the template. It also made the element
focusable without making it keyboard-operable, since RouterLink only
listens for click events and never handled Enter/Space.

The behavior is now gated behind `ADD_TABINDEX_TO_NON_ANCHOR_ELEMENTS`,
which is `false` in the published package and kept `true` inside Google
(via a sync marker) while internal callers migrate off it. Because it is a
module-level `const`, bundlers fold it to `false` and drop the guarded
`setTabIndexIfNotOnNativeEl` body, so the published package carries none of
the tabindex logic.

To let the rest tree-shake with it:
- `Renderer2` is dropped. It was used only to write the tabindex
  attribute; that write now goes straight to the host element.
- `@Attribute('tabindex')` is replaced with a `HostAttributeToken`
  injected only inside the `ADD_TABINDEX_TO_NON_ANCHOR_ELEMENTS` branch of
  the constructor, so it is removed by the same fold.

The `RouterLink` constructor now takes `(router, route, el,
locationStrategy?)`.

BREAKING CHANGE: RouterLink no longer adds `tabindex="0"` to non-anchor
elements (e.g. `<div routerLink>`, `<button routerLink>`) by default. If
you need such an element to be part of the tab order, add `tabindex="0"`
to it directly in the template.

Fixes angular#28345
@arturovt
arturovt force-pushed the fix/router_28345_no-auto-tabindex branch from d5ff504 to 858d62a Compare September 5, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: router detected: breaking change PR contains a commit with a breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

routerLink directive always makes element focusable

1 participant