Skip to content

Commit 317922a

Browse files
committed
Tabs: update focus elements for accessibility
1 parent 73d0636 commit 317922a

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

ui/widgets/tabs.js

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ $.widget( "ui.tabs", {
176176
},
177177

178178
_tabKeydown: function( event ) {
179-
var focusedTab = $( this.document[ 0 ].activeElement ).closest( "li" ),
179+
var focusedAnchor = $( this.document[ 0 ].activeElement ).closest( "a" ),
180+
focusedTab = focusedAnchor.closest( "li" ),
180181
selectedIndex = this.tabs.index( focusedTab ),
181182
goingForward = true;
182183

@@ -202,14 +203,12 @@ $.widget( "ui.tabs", {
202203
break;
203204
case $.ui.keyCode.SPACE:
204205

205-
// Activate only, no collapsing
206206
event.preventDefault();
207207
clearTimeout( this.activating );
208208
this._activate( selectedIndex );
209209
return;
210210
case $.ui.keyCode.ENTER:
211211

212-
// Toggle (cancel delayed activation, allow collapsing)
213212
event.preventDefault();
214213
clearTimeout( this.activating );
215214

@@ -228,11 +227,8 @@ $.widget( "ui.tabs", {
228227
// Navigating with control/command key will prevent automatic activation
229228
if ( !event.ctrlKey && !event.metaKey ) {
230229

231-
// Update aria-selected immediately so that AT think the tab is already selected.
232-
// Otherwise AT may confuse the user by stating that they need to activate the tab,
233-
// but the tab will already be activated by the time the announcement finishes.
234-
focusedTab.attr( "aria-selected", "false" );
235-
this.tabs.eq( selectedIndex ).attr( "aria-selected", "true" );
230+
focusedAnchor.attr( "aria-selected", "false" );
231+
this.anchors.eq( selectedIndex ).attr( "aria-selected", "true" );
236232

237233
this.activating = this._delay( function() {
238234
this.option( "active", selectedIndex );
@@ -286,7 +282,7 @@ $.widget( "ui.tabs", {
286282

287283
_focusNextTab: function( index, goingForward ) {
288284
index = this._findNextTab( index, goingForward );
289-
this.tabs.eq( index ).trigger( "focus" );
285+
this.anchors.eq( index ).trigger( "focus" );
290286
return index;
291287
},
292288

@@ -413,15 +409,15 @@ $.widget( "ui.tabs", {
413409

414410
this.tabs = this.tablist.find( "> li:has(a[href])" )
415411
.attr( {
416-
role: "tab",
417-
tabIndex: -1
412+
role: "presentation"
418413
} );
419414
this._addClass( this.tabs, "ui-tabs-tab", "ui-state-default" );
420415

421416
this.anchors = this.tabs.map( function() {
422417
return $( "a", this )[ 0 ];
423418
} )
424419
.attr( {
420+
role: "tab",
425421
tabIndex: -1
426422
} );
427423
this._addClass( this.anchors, "ui-tabs-anchor" );
@@ -437,16 +433,6 @@ $.widget( "ui.tabs", {
437433
// Inline tab
438434
if ( that._isLocal( anchor ) ) {
439435

440-
// The "scrolling to a fragment" section of the HTML spec:
441-
// https://html.spec.whatwg.org/#scrolling-to-a-fragment
442-
// uses a concept of document's indicated part:
443-
// https://html.spec.whatwg.org/#the-indicated-part-of-the-document
444-
// Slightly below there's an algorithm to compute the indicated
445-
// part:
446-
// https://html.spec.whatwg.org/#the-indicated-part-of-the-document
447-
// First, the algorithm tries the hash as-is, without decoding.
448-
// Then, if one is not found, the same is attempted with a decoded
449-
// hash. Replicate this logic.
450436
selector = anchor.hash;
451437
panelId = selector.substring( 1 );
452438
panel = that.element.find( "#" + CSS.escape( panelId ) );
@@ -455,11 +441,8 @@ $.widget( "ui.tabs", {
455441
panel = that.element.find( "#" + CSS.escape( panelId ) );
456442
}
457443

458-
// remote tab
459444
} else {
460445

461-
// If the tab doesn't already have aria-controls,
462-
// generate an id by using a throw-away element
463446
panelId = tab.attr( "aria-controls" ) || $( {} ).uniqueId()[ 0 ].id;
464447
selector = "#" + panelId;
465448
panel = that.element.find( selector );
@@ -554,7 +537,7 @@ $.widget( "ui.tabs", {
554537
this._on( this.tabs, { keydown: "_tabKeydown" } );
555538
this._on( this.panels, { keydown: "_panelKeydown" } );
556539

557-
this._focusable( this.tabs );
540+
this._focusable( this.anchors );
558541
this._hoverable( this.tabs );
559542
},
560543

0 commit comments

Comments
 (0)