From e897d63cdc5ae530f59d0d7f12c677abaad2460e Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 12 Feb 2026 12:49:24 +0000 Subject: [PATCH 1/8] Improve tab keyboard nav --- .../src/tabs-menu-item/index.php | 1 + packages/block-library/src/tabs/index.php | 7 +- packages/block-library/src/tabs/view.js | 127 ++++++++++++++---- 3 files changed, 106 insertions(+), 29 deletions(-) diff --git a/packages/block-library/src/tabs-menu-item/index.php b/packages/block-library/src/tabs-menu-item/index.php index 47b26259da8277..d4a3eaed6997a6 100644 --- a/packages/block-library/src/tabs-menu-item/index.php +++ b/packages/block-library/src/tabs-menu-item/index.php @@ -42,6 +42,7 @@ function block_core_tabs_menu_item_render_callback( array $attributes, string $c // Add IAPI directives $tag_processor->set_attribute( 'data-wp-on--click', 'actions.handleTabClick' ); $tag_processor->set_attribute( 'data-wp-on--keydown', 'actions.handleTabKeyDown' ); + $tag_processor->set_attribute( 'data-wp-on--keyup', 'actions.handleTabKeyUp' ); $tag_processor->set_attribute( 'data-wp-bind--aria-selected', 'state.isActiveTab' ); $tag_processor->set_attribute( 'data-wp-bind--tabindex', 'state.tabIndexAttribute' ); diff --git a/packages/block-library/src/tabs/index.php b/packages/block-library/src/tabs/index.php index 2952d3a7334cd7..8b9a9e98ee04e4 100644 --- a/packages/block-library/src/tabs/index.php +++ b/packages/block-library/src/tabs/index.php @@ -118,9 +118,10 @@ function block_core_tabs_render_block_callback( array $attributes, string $conte 'data-wp-context', wp_json_encode( array( - 'tabsId' => $tabs_id, - 'activeTabIndex' => $active_tab_index, - 'isVertical' => $is_vertical, + 'tabsId' => $tabs_id, + 'activeTabIndex' => $active_tab_index, + 'focusedTabIndex' => $active_tab_index, + 'isVertical' => $is_vertical, ) ) ); diff --git a/packages/block-library/src/tabs/view.js b/packages/block-library/src/tabs/view.js index 8956290f0f8eff..4d07b7e707e324 100644 --- a/packages/block-library/src/tabs/view.js +++ b/packages/block-library/src/tabs/view.js @@ -85,13 +85,24 @@ const { actions: privateActions, state: privateState } = store( const { tabIndex } = privateState; return activeTabIndex === tabIndex; }, + /** + * Whether the tab has keyboard focus. + * + * @type {boolean} + */ + get isFocusedTab() { + const { focusedTabIndex } = getContext(); + const { tabIndex } = privateState; + return focusedTabIndex === tabIndex; + }, /** * The value of the tabindex attribute. + * Only the focused tab should be keyboard-focusable. * - * @type {false|string} + * @type {number} */ get tabIndexAttribute() { - return privateState.isActiveTab ? -1 : 0; + return privateState.isFocusedTab ? 0 : -1; }, }, actions: { @@ -101,33 +112,48 @@ const { actions: privateActions, state: privateState } = store( * @param {KeyboardEvent} event The keydown event. */ handleTabKeyDown: withSyncEvent( ( event ) => { - // If this is the enter key then lets get the tab index from context and set the active tab to that index. - const { isVertical } = getContext(); + const context = getContext(); + const { isVertical } = context; + const { tabIndex } = privateState; + + if ( tabIndex === null ) { + return; + } + if ( event.key === 'Enter' ) { - const { tabIndex } = privateState; - if ( tabIndex !== null ) { - privateActions.setActiveTab( tabIndex ); - } + event.preventDefault(); + privateActions.setActiveTab( tabIndex ); + } else if ( event.key === ' ' ) { + event.preventDefault(); } else if ( event.key === 'ArrowRight' && ! isVertical ) { - const { tabIndex } = privateState; - if ( tabIndex !== null ) { - privateActions.setActiveTab( tabIndex + 1 ); - } + event.preventDefault(); + privateActions.moveFocus( tabIndex + 1 ); } else if ( event.key === 'ArrowLeft' && ! isVertical ) { - const { tabIndex } = privateState; - if ( tabIndex !== null ) { - privateActions.setActiveTab( tabIndex - 1 ); - } + event.preventDefault(); + privateActions.moveFocus( tabIndex - 1 ); } else if ( event.key === 'ArrowDown' && isVertical ) { - const { tabIndex } = privateState; - if ( tabIndex !== null ) { - privateActions.setActiveTab( tabIndex + 1 ); - } + event.preventDefault(); + privateActions.moveFocus( tabIndex + 1 ); } else if ( event.key === 'ArrowUp' && isVertical ) { - const { tabIndex } = privateState; - if ( tabIndex !== null ) { - privateActions.setActiveTab( tabIndex - 1 ); - } + event.preventDefault(); + privateActions.moveFocus( tabIndex - 1 ); + } + } ), + /** + * Handles the keyup event for the tab label. + * + * @param {KeyboardEvent} event The keyup event. + */ + handleTabKeyUp: withSyncEvent( ( event ) => { + const { tabIndex } = privateState; + + if ( tabIndex === null ) { + return; + } + + if ( event.key === ' ' ) { + event.preventDefault(); + privateActions.setActiveTab( tabIndex ); } } ), /** @@ -143,6 +169,34 @@ const { actions: privateActions, state: privateState } = store( privateActions.setActiveTab( tabIndex ); } } ), + /** + * Moves focus to a specific tab without activating it. + * + * @param {number} tabIndex The index to move focus to. + */ + moveFocus: ( tabIndex ) => { + const { tabsList } = privateState; + + if ( ! tabsList || tabsList.length === 0 ) { + return; + } + + let newIndex = tabIndex; + if ( newIndex < 0 ) { + newIndex = tabsList.length - 1; + } else if ( newIndex >= tabsList.length ) { + newIndex = 0; + } + + const context = getContext(); + context.focusedTabIndex = newIndex; + + const tabId = tabsList[ newIndex ].id; + const tabElement = document.getElementById( 'tab__' + tabId ); + if ( tabElement ) { + tabElement.focus(); + } + }, /** * Sets the active tab index (internal implementation). * @@ -150,10 +204,25 @@ const { actions: privateActions, state: privateState } = store( * @param {boolean} scrollToTab Whether to scroll to the tab element. */ setActiveTab: ( tabIndex, scrollToTab = false ) => { + const { tabsList } = privateState; + + if ( ! tabsList || tabsList.length === 0 ) { + return; + } + + let newIndex = tabIndex; + if ( newIndex < 0 ) { + newIndex = 0; + } else if ( newIndex >= tabsList.length ) { + newIndex = tabsList.length - 1; + } + const context = getContext(); - context.activeTabIndex = tabIndex; + context.activeTabIndex = newIndex; + context.focusedTabIndex = newIndex; + if ( scrollToTab ) { - const tabId = privateState.tabsList[ tabIndex ].id; + const tabId = tabsList[ newIndex ].id; const tabElement = document.getElementById( tabId ); if ( tabElement ) { setTimeout( () => { @@ -173,6 +242,12 @@ const { actions: privateActions, state: privateState } = store( if ( tabsList.length === 0 ) { return; } + + const context = getContext(); + if ( context.focusedTabIndex === undefined ) { + context.focusedTabIndex = context.activeTabIndex ?? 0; + } + const { hash } = window.location; const tabId = hash.replace( '#', '' ); const tabIndex = tabsList.findIndex( ( t ) => t.id === tabId ); From e20ff41af17bb992ce21a333846bff6b933ac95f Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 12 Feb 2026 12:50:41 +0000 Subject: [PATCH 2/8] Make tabs button elements --- packages/block-library/src/tabs-menu-item/index.php | 8 ++++---- packages/block-library/src/tabs-menu-item/save.js | 4 ++-- packages/block-library/src/tabs-menu-item/style.scss | 6 ++++++ packages/block-library/src/tabs-menu/index.php | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/tabs-menu-item/index.php b/packages/block-library/src/tabs-menu-item/index.php index d4a3eaed6997a6..023d115aa8f619 100644 --- a/packages/block-library/src/tabs-menu-item/index.php +++ b/packages/block-library/src/tabs-menu-item/index.php @@ -34,8 +34,8 @@ function block_core_tabs_menu_item_render_callback( array $attributes, string $c $tag_processor->remove_attribute( 'hidden' ); // Set tab-specific attributes + $tag_processor->set_attribute( 'type', 'button' ); $tag_processor->set_attribute( 'id', 'tab__' . $tab_id ); - $tag_processor->set_attribute( 'href', '#' . $tab_id ); $tag_processor->set_attribute( 'role', 'tab' ); $tag_processor->set_attribute( 'aria-controls', $tab_id ); @@ -56,10 +56,10 @@ function block_core_tabs_menu_item_render_callback( array $attributes, string $c // Get updated HTML and inject the label $output = $tag_processor->get_updated_html(); - // The save.js outputs ... - // Replace the anchor content with the actual tab label + // The save.js outputs + // Replace the button content with the actual tab label $output = preg_replace( - '/(]*>).*?(<\/a>)/s', + '/(]*>).*?(<\/button>)/s', '$1' . '' . esc_html( html_entity_decode( $tab_label ) ) . '' . '$2', $output ); diff --git a/packages/block-library/src/tabs-menu-item/save.js b/packages/block-library/src/tabs-menu-item/save.js index 24ec02c4b08166..40dbc7f981dc50 100644 --- a/packages/block-library/src/tabs-menu-item/save.js +++ b/packages/block-library/src/tabs-menu-item/save.js @@ -35,10 +35,10 @@ export default function save( { attributes } ) { } ); return ( - + ); } diff --git a/packages/block-library/src/tabs-menu-item/style.scss b/packages/block-library/src/tabs-menu-item/style.scss index 7ae61399d8341f..13511201b948bf 100644 --- a/packages/block-library/src/tabs-menu-item/style.scss +++ b/packages/block-library/src/tabs-menu-item/style.scss @@ -7,6 +7,12 @@ flex-basis: inherit !important; flex-grow: inherit !important; + // Button reset + border: none; + background: none; + appearance: none; + -webkit-appearance: none; + margin: 0; padding-block: var(--tab-padding-block, var(--wp--preset--spacing--20, 0.5em)); padding-inline: var(--tab-padding-inline, var(--wp--preset--spacing--30, 1em)); diff --git a/packages/block-library/src/tabs-menu/index.php b/packages/block-library/src/tabs-menu/index.php index 70e16ccf6c4498..0f2a1037a3d230 100644 --- a/packages/block-library/src/tabs-menu/index.php +++ b/packages/block-library/src/tabs-menu/index.php @@ -50,7 +50,7 @@ function block_core_tabs_menu_render_callback( array $attributes, string $conten // Find the template block and replace it in $content with $tabs_markup $content = preg_replace( - '/]*\bwp-block-tabs-menu-item__template\b[^>]*>.*?<\/a>/si', + '/]*\bwp-block-tabs-menu-item__template\b[^>]*>.*?<\/button>/si', $tabs_markup, $content ); From 976c125e96781f3ba5c95237664d54f4f06e8a61 Mon Sep 17 00:00:00 2001 From: Sarah Norris Date: Thu, 12 Feb 2026 14:56:59 +0000 Subject: [PATCH 3/8] Consolidate block props --- packages/block-library/src/tabs-menu-item/save.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/tabs-menu-item/save.js b/packages/block-library/src/tabs-menu-item/save.js index 40dbc7f981dc50..d67dffd14e6a25 100644 --- a/packages/block-library/src/tabs-menu-item/save.js +++ b/packages/block-library/src/tabs-menu-item/save.js @@ -32,10 +32,12 @@ export default function save( { attributes } ) { className: 'wp-block-tabs-menu-item__template', style: customColorStyles, hidden: true, + type: 'button', + role: 'tab', } ); return ( -