Skip to content

Commit 86dbc36

Browse files
committed
Fix "origin" click triggering the away callback
1 parent 249455e commit 86dbc36

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,22 @@ function bind(el, binding) {
2828
return;
2929
}
3030

31+
// @NOTE: Vue binds directives in microtasks, while UI events are dispatched
32+
// in macrotasks. This causes the listener to be set up before
33+
// the "origin" click event (the event that lead to the binding of
34+
// the directive) arrives at the document root. To work around that,
35+
// we ignore events until the end of the "initial" macrotask.
36+
// @REFERENCE: https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/
37+
// @REFERENCE: https://github.com/simplesmiler/vue-clickaway/issues/8
38+
var initialMacrotaskEnded = false;
39+
setTimeout(function() {
40+
initialMacrotaskEnded = true;
41+
}, 0);
42+
3143
el[HANDLER] = function(ev) {
3244
// @NOTE: IE 5.0+
3345
// @REFERENCE: https://developer.mozilla.org/en/docs/Web/API/Node/contains
34-
if (!el.contains(ev.target)) {
46+
if (initialMacrotaskEnded && !el.contains(ev.target)) {
3547
return callback(ev);
3648
}
3749
};

0 commit comments

Comments
 (0)