Case study · Plugin

A11y Toolkit

Accessible interactive blocks for the WordPress editor. First up: a Tabs block built to the WAI-ARIA tabs pattern that still reads as plain headings and sections without JavaScript.

At a glance

Problem
Tabs need roles, a keyboard model and careful focus handling to work for keyboard and screen reader users, and they shouldn’t hide content if their script fails.
What I did
A Tabs block that saves plain headings and sections, then upgrades them in the browser to the WAI-ARIA tabs pattern.
Result
Arrow, Home and End keys, one Tab stop, manual or automatic activation, right-to-left and high-contrast support, checked by keyboard in a live WordPress install. An Accordion block is next.

01 Context and challenge

The problem

Tabs look simple but carry a lot of behaviour. Screen readers need to hear “tab, 2 of 3” and which tab is selected. Keyboard users expect one Tab stop with arrow keys inside it. Focus has to land in the open panel. And if the script never loads, the content must still be there.

Constraints

  • Built for the block editor in WordPress 6.8 and later, using block.json and a front-end script module.
  • Content has to stay readable, in order, when JavaScript doesn’t run.
  • It has to behave in right-to-left languages and in Windows high-contrast (forced colours) mode.

Who it’s for

The same widget has to work for very different people: keyboard users need one Tab stop and arrow keys; screen reader users need clear names, roles and selected states; people in high-contrast mode need tab shapes and focus that don’t rely on colour; and editors need tabs that simply work without settings to get right.

02 Research and design decisions

Accessibility decisions

  • The selected tab is shown by a thick bottom border and bold text, not colour alone (WCAG 1.4.1).
  • Focus is a 3px outline in the text colour rather than a box-shadow, because box-shadows disappear in forced-colours mode.
  • Tab borders are transparent rather than removed, so the tab shapes still show in Windows high contrast, where borders are painted in system colours.
  • Transitions only run when the user hasn’t asked for reduced motion.
  • A tab with an empty title falls back to its number, so no tab is ever unnamed.
  • Headings moved into tabs are hidden, so titles aren’t announced twice.
  • In right-to-left layouts, the arrow keys follow what you see.

03 Technical implementation

How it works

Each Tab block saves a heading and a <section>. The parent Tabs block saves a plain wrapper, with its label and activation mode as data attributes, because aria-label isn’t allowed on a generic <div>.

On the front end, a small script module builds a role="tablist" of real buttons, moves each heading’s text into its tab, links tabs and panels with aria-controls and aria-labelledby, and manages selection with a roving tabindex. Nested Tabs blocks only manage their own children.

Tab styles only apply once the script adds an is-enhanced class, so a failed script never leaves a half-styled widget.

From the code

Progressive enhancement: each tab saves as a real heading and section with no ARIA, so without JavaScript it reads as a normal document. View source for: Progressive enhancement: each tab saves as a real heading and section with no ARIA, so without JavaScript it reads as a normal document. (opens in a new tab)
/**
 * Saves a real heading + <section>, with no ARIA roles. Without JavaScript this
 * is a normal document: screen reader users can jump between tabs with heading
 * navigation and every panel is visible. view.js (on the parent Tabs block)
 * upgrades this to the APG tab pattern at runtime.
 *
 * @param {Object} props
 * @param {Object} props.attributes
 */
export default function save( { attributes } ) {
    const { title } = attributes;

    return (
        <div { ...useBlockProps.save() }>
            <RichText.Content
                tagName="h3"
                className="tab-title"
                value={ title }
            />
            <section
                { ...useInnerBlocksProps.save( { className: 'tab-panel' } ) }
            />
        </div>
    );
}
Selection state: aria-selected on every tab, a roving tabindex so the tab list is one Tab stop, and hidden on inactive panels. View source for: Selection state: aria-selected on every tab, a roving tabindex so the tab list is one Tab stop, and hidden on inactive panels. (opens in a new tab)
/**
 * Select a tab: update aria-selected, roving tabindex and panel visibility.
 *
 * @param {number}  index
 * @param {boolean} moveFocus
 */
function select( index, moveFocus ) {
    tabs.forEach( ( tab, i ) => {
        const selected = i === index;

        // aria-selected is how AT knows which tab is current. Set it on
        // every tab explicitly ("false" rather than absent) so the state is
        // announced unambiguously.
        tab.setAttribute( 'aria-selected', String( selected ) );

        // Roving tabindex: only the selected tab is in the Tab sequence, so
        // the whole tablist is a single Tab stop and arrow keys move within
        // it. Tab from the tablist then goes straight to the active panel.
        tab.tabIndex = selected ? 0 : -1;

        // `hidden` hides inactive panels from sighted users, AT and the Tab
        // order in one go.
        panels[ i ].hidden = ! selected;
    } );

    if ( moveFocus ) {
        tabs[ index ].focus();
    }
}
Keyboard: arrows follow the visual direction in right-to-left layouts, wrap at the ends, and only move focus in manual activation. View source for: Keyboard: arrows follow the visual direction in right-to-left layouts, wrap at the ends, and only move focus in manual activation. (opens in a new tab)
// In RTL layouts the first tab is on the right, so "Right" visually
// means "previous". Swap so arrows follow what the user sees.
const step =
    window.getComputedStyle( tablist ).direction === 'rtl' ? -1 : 1;
let next;

switch ( event.key ) {
    case 'ArrowRight':
        next = current + step;
        break;
    case 'ArrowLeft':
        next = current - step;
        break;
    case 'Home':
        next = 0;
        break;
    case 'End':
        next = tabs.length - 1;
        break;
    default:
        return;
}

// APG: Left/Right wrap from last to first and vice versa.
next = ( next + tabs.length ) % tabs.length;
event.preventDefault();

if ( isManual ) {
    // Manual activation: arrows only move focus. The user presses
    // Enter/Space (native button → click) to show the panel. The roving
    // tabindex stays on the *selected* tab, so tabbing away and back
    // returns to the tab whose panel is visible.
    tabs[ next ].focus();
} else {
    // Automatic activation: focusing a tab also shows its panel.
    select( next, true );
}

Testing

Checked in a live WordPress Playground install with a scripted keyboard test: the arrow keys select the next tab and wrap around, End jumps to the last tab, Tab moves into the open panel, and in manual mode the arrows move focus while Enter selects. axe-core reported no issues in the Tabs markup.

The repo doesn’t have automated tests yet.

How AI was used

Built mostly by prompting an AI coding tool. I set the requirements: the WAI-ARIA tabs pattern, content that still reads without JavaScript, and support for high-contrast mode and right-to-left layouts. I reviewed the code through pull requests, sent it back when something wasn’t right, and tested the result myself.

Code

View the repository on GitHub (opens in a new tab)

04 Final experience

Try it live

Opens a disposable WordPress site in your browser with the plugin installed and a demo page ready. Nothing to sign up for; it takes a few seconds to start.

Try it live : A11y Toolkit in WordPress Playground (opens in a new tab)

Screenshots

05 Outcomes and reflections

What’s next

An Accordion block is scaffolded in the repo but not built yet.