Skip to content

Commit 6d16383

Browse files
committed
First attempt at PointerEvent polyfill trying to address #68
1 parent a9f63c1 commit 6d16383

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

bootstrap-hover-dropdown.js

+23-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Project: Bootstrap Hover Dropdown
44
* Author: Cameron Spear
55
* Version: v2.1.3
6-
* Contributors: Mattia Larentis
76
* Dependencies: Bootstrap's Dropdown plugin, jQuery
87
* Description: A simple plugin to enable Bootstrap dropdowns to active on hover and provide a nice user experience.
98
* License: MIT
@@ -17,9 +16,8 @@
1716
// if instantlyCloseOthers is true, then it will instantly
1817
// shut other nav items when a new one is hovered over
1918
$.fn.dropdownHover = function (options) {
20-
// don't do anything if touch is supported
21-
// (plugin causes some issues on mobile)
22-
if('ontouchstart' in document) return this; // don't want to affect chaining
19+
// this disabled touch actions on the elements so we can hook into Pointer Events
20+
this.attr('touch-action', 'pan-y');
2321

2422
// the element we really care about
2523
// is the dropdown-toggle's parent
@@ -45,7 +43,8 @@
4543
settings = $.extend(true, {}, defaults, options, data),
4644
timeout, timeoutHover;
4745

48-
$parent.hover(function (event) {
46+
$parent.on('pointerenter', function (event) {
47+
if (isPointerTouchEvent(event)) return;
4948
// so a neighbor can't open the dropdown
5049
if(!$parent.hasClass('open') && !$this.is(event.target)) {
5150
// stop this event, stop executing any code
@@ -54,9 +53,10 @@
5453
}
5554

5655
openDropdown(event);
57-
}, function () {
56+
}).on('pointerleave', function (event) {
57+
if (isPointerTouchEvent(event)) return;
5858
// clear timer for hover event
59-
window.clearTimeout(timeoutHover)
59+
window.clearTimeout(timeoutHover);
6060
timeout = window.setTimeout(function () {
6161
$this.attr('aria-expanded', 'false');
6262
$parent.removeClass('open');
@@ -65,7 +65,8 @@
6565
});
6666

6767
// this helps with button groups!
68-
$this.hover(function (event) {
68+
$this.on('pointerenter', function (event) {
69+
if (isPointerTouchEvent(event)) return;
6970
// this helps prevent a double event from firing.
7071
// see https://github.com/CWSpear/bootstrap-hover-dropdown/issues/55
7172
if(!$parent.hasClass('open') && !$parent.is(event.target)) {
@@ -81,12 +82,14 @@
8182
$parent.find('.dropdown-submenu').each(function (){
8283
var $this = $(this);
8384
var subTimeout;
84-
$this.hover(function () {
85+
$this.on('pointerenter', function (event) {
86+
if (isPointerTouchEvent(event)) return;
8587
window.clearTimeout(subTimeout);
8688
$this.children('.dropdown-menu').show();
8789
// always close submenu siblings instantly
8890
$this.siblings().children('.dropdown-menu').hide();
89-
}, function () {
91+
}).on('pointerleave', function (event) {
92+
if (isPointerTouchEvent(event)) return;
9093
var $submenu = $this.children('.dropdown-menu');
9194
subTimeout = window.setTimeout(function () {
9295
$submenu.hide();
@@ -104,8 +107,9 @@
104107
timeoutHover = window.setTimeout(function () {
105108
$allDropdowns.find(':focus').blur();
106109

107-
if(settings.instantlyCloseOthers === true)
110+
if(settings.instantlyCloseOthers === true) {
108111
$allDropdowns.removeClass('open');
112+
}
109113

110114
// clear timer for hover event
111115
window.clearTimeout(timeoutHover);
@@ -114,6 +118,14 @@
114118
$this.trigger(showEvent);
115119
}, settings.hoverDelay);
116120
}
121+
122+
function isPointerTouchEvent(event) {
123+
// need to get the originalEvent if jQuery was used to bind the event
124+
event = event.originalEvent || event;
125+
// I'm not sure we need to check for falsey pointerTypes or if it will cause issues...
126+
// In my testing, I found it sometimes be empty when I expected touch, but it was inconsistent
127+
return !event.pointerType || event.pointerType === 'touch';
128+
}
117129
});
118130
};
119131

bower.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"homepage": "https://github.com/CWSpear/bootstrap-hover-dropdown",
1313
"dependencies": {
1414
"bootstrap": "^3.0.0",
15-
"jquery": ">= 1.9.0"
15+
"jquery": ">= 1.9.0",
16+
"pepjs": ">= 0.3.0"
1617
},
1718
"author": {
1819
"name": "Cameron Spear",

demo.html

+10-9
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@
3232
<li class="dropdown">
3333
<a href="#" class="dropdown-toggle js-activated">Dropdown <b class="caret"></b></a>
3434
<ul class="dropdown-menu">
35-
<li><a href="#">Action</a></li>
36-
<li><a href="#">Another action</a></li>
37-
<li><a href="#">Something else here</a></li>
35+
<li><a href="#Action">Action</a></li>
36+
<li><a href="#Another action">Another action</a></li>
37+
<li><a href="#Something else here">Something else here</a></li>
3838
<li class="divider"></li>
3939
<li class="dropdown-header">Dropdown header</li>
40-
<li><a href="#">Separated link</a></li>
41-
<li><a href="#">One more separated link</a></li>
40+
<li><a href="#Separated link">Separated link</a></li>
41+
<li><a href="#One more separated link">One more separated link</a></li>
4242
</ul>
4343
</li>
4444
<li class="dropdown">
4545
<a href="#" class="dropdown-toggle js-activated" data-toggle="dropdown">I Have a Submenu <b class="caret"></b></a>
4646
<ul class="dropdown-menu">
47-
<li><a tabindex="-1" href="#">Menu Item</a></li>
48-
<li><a tabindex="-1" href="#">Bootstrap 3 Does Not Support Submenus</a></li>
47+
<li><a tabindex="-1" href="#Menu Item">Menu Item</a></li>
48+
<li><a tabindex="-1" href="#Bootstrap 3 Does Not Support Submenus">Bootstrap 3 Does Not Support Submenus</a></li>
4949
<!--
5050
BOOTSTRAP 3 DOES NOT SUPPORT SUBMENUS: https://github.com/twbs/bootstrap/pull/6342#issuecomment-11594010
5151
<li class="dropdown-submenu">
@@ -282,8 +282,9 @@ <h3>Works with button groups</h3>
282282
</div> <!-- .container -->
283283

284284
<!-- latest jQuery, Boostrap JS and hover dropdown plugin -->
285-
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
286-
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
285+
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
286+
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
287+
<script src="https://code.jquery.com/pep/0.3.0/pep.js"></script>
287288
<script src="bootstrap-hover-dropdown.js"></script>
288289

289290
<script>

0 commit comments

Comments
 (0)