Skip to content

fix(tab-button): restore keyboard focus - #31568

Open
cpruijsen wants to merge 3 commits into
ionic-team:mainfrom
cpruijsen:fix/issue-30648
Open

cpruijsen wants to merge 3 commits into
ionic-team:mainfrom
cpruijsen:fix/issue-30648

Conversation

@cpruijsen

@cpruijsen cpruijsen commented Oct 13, 2026 •

Copy link
Copy Markdown

Enabled ion-tab-button controls can now receive keyboard focus, with or without href.

Issue number: resolves #30648


What is the current behavior?

Tab navigation skips enabled ion-tab-button controls without href because the native anchor has no tabindex. Safari also skips controls with href, because it skips <a href> on Tab unless it has an explicit tabindex.

What is the new behavior?

  • core/src/components/tab-button/tab-button.tsx sets tabindex="0" on the native anchor of an enabled tab button when the host has no tabindex. A tabindex set on the host still wins, and disabled controls keep tabindex="-1".
  • Spec tests in the existing tab button: tabindex describe cover a tab button with and without href.
  • An e2e test in test/a11y/tab-button.e2e.ts presses Tab through a tab bar: it reaches a button without href, skips one with a host tabindex="-1", and selects the next with Enter.

Does this introduce a breaking change?

  • Yes
  • No

Other information

Other browsers already focus an anchor with href, so the explicit tabindex="0" changes nothing there.

The new spec tests fail against the base source and pass with the change. The new e2e test fails against the base source in Mobile Chrome and passes with the change.

@cpruijsen
cpruijsen requested a review from a team as a code owner October 13, 2026 19:57
@vercel

vercel Bot commented Oct 13, 2026

Copy link
Copy Markdown

@cpruijsen is attempting to deploy a commit to the Ionic Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the package: core @ionic/core package label Oct 13, 2026
# Conflicts:
#	core/src/components/tab-button/tab-button.tsx
#	core/src/components/tab-button/test/tab-button.spec.ts
@vercel

vercel Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
ionic-framework Ready Ready Preview Nov 18, 2026 6:33pm UTC

Request Review

@thetaPC thetaPC left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congrats on your first PR to Ionic Framework, and thanks for picking this up!

The approach looks right: a tabindex set on the host still wins, and disabled still takes the button out of the tab order. I left a suggestion to extend the fix to tab buttons with an href.

Comment on lines 5 to 45
describe('tab-button: rendering', () => {
it('should render tabindex="0" on the native element when href is not defined', async () => {
const page = await newSpecPage({
components: [TabButton],
html: `<ion-tab-button tab="schedule"></ion-tab-button>`,
});

const nativeEl = page.body.querySelector('ion-tab-button')!.shadowRoot!.querySelector('a')!;

expect(nativeEl.getAttribute('tabindex')).toBe('0');
});

it('should update tabindex when href is removed', async () => {
const page = await newSpecPage({
components: [TabButton],
html: `<ion-tab-button tab="schedule" href="/schedule"></ion-tab-button>`,
});

const nativeEl = page.body.querySelector('ion-tab-button')!.shadowRoot!.querySelector('a')!;

expect(nativeEl.getAttribute('tabindex')).toBe(null);

page.root!.removeAttribute('href');
await page.waitForChanges();

expect(nativeEl.getAttribute('tabindex')).toBe('0');
});

it('should render tabindex="-1" on the native element when disabled', async () => {
const page = await newSpecPage({
components: [TabButton],
html: `<ion-tab-button tab="schedule" disabled="true"></ion-tab-button>`,
});

const nativeEl = page.body.querySelector('ion-tab-button')!.shadowRoot!.querySelector('a')!;

expect(nativeEl.getAttribute('tabindex')).toBe('-1');
});
});

describe('tab button: tabindex', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These belong in the existing tab button: tabindex describe:

  • Keeping a separate tab-button: rendering block splits the tabindex tests across two describes with different name spellings.
  • The disabled test repeats "should keep a disabled tab button out of the tab order" below, and this PR doesn't change the disabled branch.
  • With the '0' fallback, removing href no longer changes the result. A test with an href covers the Safari case instead.
  • The tests verify bug: ion-tab-button isn't focusable when no href #30648, so they should link it, the way other spec files do.
Suggested change
describe('tab-button: rendering', () => {
it('should render tabindex="0" on the native element when href is not defined', async () => {
const page = await newSpecPage({
components: [TabButton],
html: `<ion-tab-button tab="schedule"></ion-tab-button>`,
});
const nativeEl = page.body.querySelector('ion-tab-button')!.shadowRoot!.querySelector('a')!;
expect(nativeEl.getAttribute('tabindex')).toBe('0');
});
it('should update tabindex when href is removed', async () => {
const page = await newSpecPage({
components: [TabButton],
html: `<ion-tab-button tab="schedule" href="/schedule"></ion-tab-button>`,
});
const nativeEl = page.body.querySelector('ion-tab-button')!.shadowRoot!.querySelector('a')!;
expect(nativeEl.getAttribute('tabindex')).toBe(null);
page.root!.removeAttribute('href');
await page.waitForChanges();
expect(nativeEl.getAttribute('tabindex')).toBe('0');
});
it('should render tabindex="-1" on the native element when disabled', async () => {
const page = await newSpecPage({
components: [TabButton],
html: `<ion-tab-button tab="schedule" disabled="true"></ion-tab-button>`,
});
const nativeEl = page.body.querySelector('ion-tab-button')!.shadowRoot!.querySelector('a')!;
expect(nativeEl.getAttribute('tabindex')).toBe('-1');
});
});
describe('tab button: tabindex', () => {
describe('tab button: tabindex', () => {
// https://github.com/ionic-team/ionic-framework/issues/30648
it('should render tabindex="0" on the native element when href is not defined', async () => {
const page = await newSpecPage({
components: [TabButton],
html: `<ion-tab-button tab="schedule"></ion-tab-button>`,
});
const nativeEl = page.body.querySelector('ion-tab-button')!.shadowRoot!.querySelector('a')!;
expect(nativeEl.getAttribute('tabindex')).toBe('0');
});
// https://github.com/ionic-team/ionic-framework/issues/30648
it('should render tabindex="0" on the native element when href is defined', async () => {
const page = await newSpecPage({
components: [TabButton],
html: `<ion-tab-button tab="schedule" href="/schedule"></ion-tab-button>`,
});
const nativeEl = page.body.querySelector('ion-tab-button')!.shadowRoot!.querySelector('a')!;
expect(nativeEl.getAttribute('tabindex')).toBe('0');
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One request: the spec tests only check the attribute on the anchor, and the button added to the a11y page only runs through axe, which passes whether or not the button can take focus. Please add a test to test/a11y/tab-button.e2e.ts that presses Tab. Something like this, modeled on the button: tabindex test in button/test/a11y/button.e2e.ts:

/**
 * Tab order does not vary across modes or directions
 */
configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
  test.describe(title('tab-button: tabindex'), () => {
    test('should reach tab buttons without href and skip a host tabindex of -1', async ({ page, pageUtils }) => {
      test.info().annotations.push({
        type: 'issue',
        description: 'https://github.com/ionic-team/ionic-framework/issues/30648',
      });

      await page.setContent(
        `
        <input id="before" />
        <ion-tabs>
          <ion-tab tab="one">One</ion-tab>
          <ion-tab tab="two">Two</ion-tab>
          <ion-tab tab="three">Three</ion-tab>
          <ion-tab-bar slot="bottom">
            <ion-tab-button tab="one">One</ion-tab-button>
            <ion-tab-button tab="two" tabindex="-1">Two</ion-tab-button>
            <ion-tab-button tab="three">Three</ion-tab-button>
          </ion-tab-bar>
        </ion-tabs>
      `,
        config
      );

      const three = page.locator('ion-tab-button[tab="three"]');

      await page.locator('#before').focus();

      await pageUtils.pressKeys('Tab');
      await expect(page.locator('ion-tab-button[tab="one"] a')).toBeFocused();

      await pageUtils.pressKeys('Tab');
      await expect(three.locator('a')).toBeFocused();

      await page.keyboard.press('Enter');
      await expect(three).toHaveClass(/tab-selected/);
    });
  });
});

aria-selected={selected ? 'true' : null}
aria-disabled={disabled ? 'true' : null}
tabindex={disabled ? '-1' : tabindex}
tabindex={disabled ? '-1' : tabindex ?? (href === undefined || href === null ? '0' : undefined)}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Keeping the href check leaves tab buttons with an href out of the tab order in Safari, which skips <a href> on Tab unless it has an explicit tabindex. Apps that route their tab bar would keep the regression from bug: ion-tab-button isn't focusable when no href #30648 there.
  • An anchor with an href is already focusable in other browsers, so an explicit tabindex="0" changes nothing for them.
Suggested change
tabindex={disabled ? '-1' : tabindex ?? (href === undefined || href === null ? '0' : undefined)}
tabindex={disabled ? '-1' : tabindex ?? '0'}

@cpruijsen

Copy link
Copy Markdown
Author

Thanks for the review! I applied all three: the tests are in the existing describe with the issue link, there's a Tab-press e2e test, and tab buttons with an href now get tabindex="0" too.

This branch was successfully deployed

1 active (outdated) deployment
Preview — 79b3e864 Deployed Nov 18, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: core @ionic/core package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: ion-tab-button isn't focusable when no href

2 participants