Skip to content
This repository was archived by the owner on Aug 14, 2021. It is now read-only.

Commit b13970c

Browse files
committed
New option autoFocus allows keyboard-only usage
1 parent b2c9466 commit b13970c

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# 1.8.3-0 / Unreleased
2+
* [FEATURE] New option `autoFocus`, defaults to *false*
23

34
# 1.8.2 / 2015-02-08
45

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ $(document).contextmenu({
242242
default: <code>"ui-contextmenu"</code><br>
243243
This class is added to the outer ul element.
244244
</dd>
245+
<dt>autoFocus</dt>
246+
<dd>
247+
Type: <code>Boolean</code>,
248+
default: <code>false</code><br>
249+
Set keyboard focus to first menu entry on open.
250+
</dd>
245251
<dt>autoTrigger</dt>
246252
<dd>
247253
Type: <code>Boolean</code>,
@@ -457,6 +463,13 @@ and make it right aligned via CSS:
457463
}
458464
```
459465
466+
### [Howto] Enable keyboard control
467+
468+
In order open a context menu with the keyboard, make sure the target elements
469+
are tabbable, for example by adding a `tabindex="0"` attribute.
470+
Also make sure the `autoFocus: true` option is set.
471+
This will allow to Use <kbd>Tab</key> and the Windows <key>Menu</key> keys.
472+
460473
461474
### [Howto] Modify the menu using an asynchronous request
462475

demo/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777

7878
$(document).contextmenu({
7979
delegate: ".hasmenu",
80+
autoFocus: true,
8081
preventContextMenuForPopup: true,
8182
preventSelect: true,
8283
taphold: true,
@@ -206,9 +207,9 @@ <h3>Sample 1</h3>
206207
</ul>
207208
<p>Right-click in an element to open the context menu:</p>
208209
<div>
209-
<span class="hasmenu">AAA</span>
210-
<span class="hasmenu">BBB</span>
211-
<span class="hasmenu">CCC</span>
210+
<span class="hasmenu" tabindex="0">AAA</span>
211+
<span class="hasmenu" tabindex="0">BBB</span>
212+
<span class="hasmenu" tabindex="0">CCC</span>
212213
</div>
213214

214215
<h3>Sample 2</h3>

jquery.ui-contextmenu.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ $.widget("moogle.contextmenu", {
3333
version: "@VERSION",
3434
options: {
3535
addClass: "ui-contextmenu", // Add this class to the outer <ul>
36+
autoFocus: false, // Set keyboard focus to first entry on open
3637
autoTrigger: true, // open menu on browser's `contextmenu` event
3738
delegate: null, // selector
3839
hide: { effect: "fadeOut", duration: "fast" },
@@ -64,6 +65,7 @@ $.widget("moogle.contextmenu", {
6465
this.$menu = null;
6566
this.menuIsTemp = false;
6667
this.currentTarget = null;
68+
this.previousFocus = null;
6769

6870
if (opts.preventSelect) {
6971
// Create a global style for all potential menu targets
@@ -261,7 +263,14 @@ $.widget("moogle.contextmenu", {
261263
event.preventDefault();
262264
});
263265
}
264-
this._show(this.$menu, this.options.show, function() {
266+
this._show(this.$menu, opts.show, function() {
267+
// Set focus to first active menu entry
268+
if ( opts.autoFocus ) {
269+
// var $first = self.$menu.children(".ui-menu-item:enabled:first");
270+
// self.$menu.menu("focus", event, $first).focus();
271+
self.$menu.focus();
272+
self.previousFocus = $(event.target);
273+
}
265274
self._trigger.call(self, "open", event, ui);
266275
});
267276
},
@@ -281,6 +290,10 @@ $.widget("moogle.contextmenu", {
281290
this.$menu
282291
.unbind("contextmenu" + this.eventNamespace);
283292
this._hide(this.$menu, hideOpts, function() {
293+
if ( self.previousFocus ) {
294+
self.previousFocus.focus();
295+
self.previousFocus = null;
296+
}
284297
self._trigger("close");
285298
});
286299
} else {

0 commit comments

Comments
 (0)