Skip to content

Commit d581085

Browse files
committed
Fix "origin" click triggering the away callback
1 parent 3679c80 commit d581085

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

index.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ export var directive = {
1212
acceptStatement: true,
1313
priority: 700,
1414

15+
bind: function() {
16+
var self = this;
17+
18+
// @NOTE: Vue binds directives in microtasks, while UI events are dispatched
19+
// in macrotasks. This causes the listener to be set up before
20+
// the "origin" click event (the event that lead to the binding of
21+
// the directive) arrives at the document root. To work around that,
22+
// we ignore events until the end of the "initial" macrotask.
23+
// @REFERENCE: https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/
24+
// @REFERENCE: https://github.com/simplesmiler/vue-clickaway/issues/8
25+
self.initialMacrotaskEnded = false;
26+
setTimeout(function() {
27+
self.initialMacrotaskEnded = true;
28+
});
29+
},
30+
1531
update: function(handler) {
1632
if (typeof handler !== 'function') {
1733
if (process.env.NODE_ENV !== 'production') {
@@ -26,13 +42,14 @@ export var directive = {
2642

2743
this.reset();
2844

45+
var self = this;
2946
var el = this.el;
3047
var scope = this._scope || this.vm;
3148

3249
this.handler = function(ev) {
3350
// @NOTE: IE 5.0+
3451
// @REFERENCE: https://developer.mozilla.org/en/docs/Web/API/Node/contains
35-
if (!el.contains(ev.target)) {
52+
if (self.initialMacrotaskEnded && !el.contains(ev.target)) {
3653
scope.$event = ev;
3754
var res = handler(ev);
3855
scope.$event = null;

0 commit comments

Comments
 (0)