Skip to content

fix(material/paginator): prevent keyboard nav to disabled buttons #30627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
Merged
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
4 changes: 3 additions & 1 deletion src/material/button/button-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ export class MatButtonBase implements AfterViewInit, OnDestroy {
/**
* Natively disabled buttons prevent focus and any pointer events from reaching the button.
* In some scenarios this might not be desirable, because it can prevent users from finding out
* why the button is disabled (e.g. via tooltip).
* why the button is disabled (e.g. via tooltip). This is also useful for buttons that may
* become disabled when activated, which would cause focus to be transferred to the document
* body instead of remaining on the button.
*
* Enabling this input will change the button so that it is styled to be disabled and will be
* marked as `aria-disabled`, but it will allow the button to receive events and focus.
Expand Down
10 changes: 10 additions & 0 deletions src/material/paginator/paginator.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
{{_intl.getRangeLabel(pageIndex, pageSize, length)}}
</div>

<!--
The buttons use `disabledInteractive` so that they can retain focus if they become disabled,
otherwise focus is moved to the document body. However, users should not be able to navigate
into these buttons, so `tabindex` is set to -1 when disabled.
-->

@if (showFirstLastButtons) {
<button mat-icon-button type="button"
class="mat-mdc-paginator-navigation-first"
Expand All @@ -50,6 +56,7 @@
[matTooltipDisabled]="_previousButtonsDisabled()"
matTooltipPosition="above"
[disabled]="_previousButtonsDisabled()"
[tabindex]="_previousButtonsDisabled() ? -1 : null"
disabledInteractive>
<svg class="mat-mdc-paginator-icon"
viewBox="0 0 24 24"
Expand All @@ -67,6 +74,7 @@
[matTooltipDisabled]="_previousButtonsDisabled()"
matTooltipPosition="above"
[disabled]="_previousButtonsDisabled()"
[tabindex]="_previousButtonsDisabled() ? -1 : null"
disabledInteractive>
<svg class="mat-mdc-paginator-icon"
viewBox="0 0 24 24"
Expand All @@ -83,6 +91,7 @@
[matTooltipDisabled]="_nextButtonsDisabled()"
matTooltipPosition="above"
[disabled]="_nextButtonsDisabled()"
[tabindex]="_nextButtonsDisabled() ? -1 : null"
disabledInteractive>
<svg class="mat-mdc-paginator-icon"
viewBox="0 0 24 24"
Expand All @@ -100,6 +109,7 @@
[matTooltipDisabled]="_nextButtonsDisabled()"
matTooltipPosition="above"
[disabled]="_nextButtonsDisabled()"
[tabindex]="_nextButtonsDisabled() ? -1 : null"
disabledInteractive>
<svg class="mat-mdc-paginator-icon"
viewBox="0 0 24 24"
Expand Down
8 changes: 8 additions & 0 deletions src/material/paginator/paginator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,19 +568,27 @@ describe('MatPaginator', () => {

expect(select.disabled).toBe(false);
expect(getPreviousButton(fixture).hasAttribute('disabled')).toBe(false);
expect(getPreviousButton(fixture).hasAttribute('tabindex')).toBe(false);
expect(getNextButton(fixture).hasAttribute('disabled')).toBe(false);
expect(getNextButton(fixture).hasAttribute('tabindex')).toBe(false);
expect(getFirstButton(fixture).hasAttribute('disabled')).toBe(false);
expect(getFirstButton(fixture).hasAttribute('tabindex')).toBe(false);
expect(getLastButton(fixture).hasAttribute('disabled')).toBe(false);
expect(getLastButton(fixture).hasAttribute('tabindex')).toBe(false);

fixture.componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();

expect(select.disabled).toBe(true);
expect(getPreviousButton(fixture).hasAttribute('aria-disabled')).toBe(true);
expect(getPreviousButton(fixture).getAttribute('tabindex')).toBe('-1');
expect(getNextButton(fixture).hasAttribute('aria-disabled')).toBe(true);
expect(getNextButton(fixture).getAttribute('tabindex')).toBe('-1');
expect(getFirstButton(fixture).hasAttribute('aria-disabled')).toBe(true);
expect(getFirstButton(fixture).getAttribute('tabindex')).toBe('-1');
expect(getLastButton(fixture).hasAttribute('aria-disabled')).toBe(true);
expect(getLastButton(fixture).getAttribute('tabindex')).toBe('-1');
});

it('should be able to configure the default options via a provider', () => {
Expand Down
Loading