Skip to content

Commit f45fc08

Browse files
committed
wrapping tab order for popup
1 parent 63b89b7 commit f45fc08

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

demos/popup/default.html

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,26 @@
5454
<body>
5555

5656
<div class="demo">
57-
<a href="#login-form">Log In</a>
58-
<div id="login-form" class="ui-widget-content" tabIndex="0">
57+
<a href="#login-form">Log In</a>
58+
<div class="ui-widget-content" id="login-form" aria-label="Login options">
5959
<form>
6060
<div>
61-
<label>Username</label>
62-
<input type="username" />
61+
<label for="un">Username</label>
62+
<input type="text" id="un" />
6363
</div>
6464
<div>
65-
<label>Password</label>
66-
<input type="password" />
65+
<label for="pw">Password</label>
66+
<input type="password" id="pw" />
6767
</div>
6868
<div>
69-
<input type="submit" class="submit" value="Login" />
69+
<input type="submit" value="Login" class="submit" />
7070
</div>
7171
</form>
7272
</div>
73+
74+
75+
76+
7377
</div>
7478

7579
<div class="demo-description">

ui/jquery.ui.popup.js

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ $.widget( "ui.popup", {
3535

3636
if ( !this.element.attr( "role" ) ) {
3737
// TODO alternatives to tooltip are dialog and menu, all three aren't generic popups
38-
this.element.attr( "role", "tooltip" );
38+
this.element.attr( "role", "dialog" );
3939
this.generatedRole = true;
4040
}
4141

@@ -80,19 +80,22 @@ $.widget( "ui.popup", {
8080
});
8181

8282
this._bind(this.element, {
83-
// TODO use focusout so that element itself doesn't need to be focussable
84-
blur: function( event ) {
83+
focusout: function( event ) {
8584
var that = this;
8685
// use a timer to allow click to clear it and letting that
8786
// handle the closing instead of opening again
8887
that.closeTimer = setTimeout( function() {
8988
that.close( event );
9089
}, 100);
91-
}
90+
},
91+
focusin: function( event ) {
92+
var that = this;
93+
clearTimeout( that.closeTimer );
94+
}
9295
});
9396

9497
this._bind({
95-
// TODO only triggerd on element if it can receive focus
98+
// TODO only triggered on element if it can receive focus
9699
// bind to document instead?
97100
// either element itself or a child should be focusable
98101
keyup: function( event ) {
@@ -141,14 +144,43 @@ $.widget( "ui.popup", {
141144
.show()
142145
.attr( "aria-hidden", false )
143146
.attr( "aria-expanded", true )
144-
.position( position )
145-
// TODO find a focussable child, otherwise put focus on element, add tabIndex=0 if not focussable
146-
.focus();
147-
148-
if (this.element.is(":ui-menu")) {
147+
.position( position );
148+
149+
if (this.element.is(":ui-menu")) { //popup is a menu
150+
this.element.focus();
149151
this.element.menu("focus", event, this.element.children( "li" ).first() );
152+
this.element.focus();
150153
}
151-
154+
// TODO add other special use cases that differ from the default dialog style keyboard mechanism
155+
else {
156+
//default use case, popup could be anything (e.g. a form)
157+
this.element
158+
.bind( "keypress.ui-popup", function( event ) {
159+
if ( event.keyCode !== $.ui.keyCode.TAB ) {
160+
return;
161+
}
162+
var tabbables = $( ":tabbable", this ),
163+
first = tabbables.filter( ":first" ),
164+
last = tabbables.filter( ":last" );
165+
if ( event.target === last[0] && !event.shiftKey ) {
166+
first.focus( 1 );
167+
return false;
168+
} else if ( event.target === first[0] && event.shiftKey ) {
169+
last.focus( 1 );
170+
return false;
171+
}
172+
});
173+
174+
// set focus to the first tabbable element in the popup container
175+
// if there are no tabbable elements, set focus on the popup itself
176+
var tabbables = this.element.find( ":tabbable" );
177+
if (!tabbables.length) {
178+
this.element.attr("tabindex", "0");
179+
tabbables.add(this.element);
180+
}
181+
tabbables.eq( 0 ).focus(1);
182+
}
183+
152184
// take trigger out of tab order to allow shift-tab to skip trigger
153185
this.options.trigger.attr("tabindex", -1);
154186

@@ -160,7 +192,8 @@ $.widget( "ui.popup", {
160192
this.element
161193
.hide()
162194
.attr( "aria-hidden", true )
163-
.attr( "aria-expanded", false );
195+
.attr( "aria-expanded", false )
196+
.unbind( "keypress.ui-popup");
164197

165198
this.options.trigger.attr("tabindex", 0);
166199

0 commit comments

Comments
 (0)