From 48eddcd1380088cad5fd90412462253061b08b9f Mon Sep 17 00:00:00 2001 From: Jan Sorgalla Date: Wed, 19 Sep 2018 22:58:39 +0200 Subject: [PATCH] Improve closeOnOutsideClick behavior --- dist/ctrly.esm.js | 42 +++++++++++----------- dist/ctrly.js | 42 +++++++++++----------- dist/ctrly.min.js | 2 +- src/ctrly.js | 56 +++++++++++++++-------------- test/test.close-on-outside-click.js | 36 ++----------------- 5 files changed, 73 insertions(+), 105 deletions(-) diff --git a/dist/ctrly.esm.js b/dist/ctrly.esm.js index 66a460c..e993d34 100644 --- a/dist/ctrly.esm.js +++ b/dist/ctrly.esm.js @@ -392,27 +392,6 @@ function ctrly() { } function addEventListeners(control, target) { var removeFuncs = []; - var active = false; - var activate = function activate() { - active = true; - }; - var deactivate = function deactivate() { - active = false; - }; - if (options.closeOnOutsideClick || options.closeOnScroll) { - removeFuncs.push(on(target, 'mouseenter', activate, { - passive: true - })); - removeFuncs.push(on(target, 'mouseleave', deactivate, { - passive: true - })); - removeFuncs.push(on(target, 'touchstart', activate, { - passive: true - })); - removeFuncs.push(on(target, 'touchend', deactivate, { - passive: true - })); - } if (options.closeOnBlur && !options.trapFocus) { removeFuncs.push(on(document, 'focusin', function (e) { if (!target.contains(e.target)) { @@ -432,7 +411,7 @@ function ctrly() { } if (options.closeOnOutsideClick) { removeFuncs.push(on(document, 'click', function (e) { - if (!active && keyCode(e) === 1 && !closest(e.target, controlSelector)) { + if (keyCode(e) === 1 && !target.contains(e.target) && !closest(e.target, controlSelector)) { close(target); } }, { @@ -440,6 +419,25 @@ function ctrly() { })); } if (options.closeOnScroll) { + var active = false; + var activate = function activate() { + active = true; + }; + var deactivate = function deactivate() { + active = false; + }; + removeFuncs.push(on(target, 'mouseenter', activate, { + passive: true + })); + removeFuncs.push(on(target, 'mouseleave', deactivate, { + passive: true + })); + removeFuncs.push(on(target, 'touchstart', activate, { + passive: true + })); + removeFuncs.push(on(target, 'touchend', deactivate, { + passive: true + })); removeFuncs.push(on(window, 'scroll', function () { if (!active) { close(target); diff --git a/dist/ctrly.js b/dist/ctrly.js index fa669fd..21413cd 100644 --- a/dist/ctrly.js +++ b/dist/ctrly.js @@ -398,27 +398,6 @@ } function addEventListeners(control, target) { var removeFuncs = []; - var active = false; - var activate = function activate() { - active = true; - }; - var deactivate = function deactivate() { - active = false; - }; - if (options.closeOnOutsideClick || options.closeOnScroll) { - removeFuncs.push(on(target, 'mouseenter', activate, { - passive: true - })); - removeFuncs.push(on(target, 'mouseleave', deactivate, { - passive: true - })); - removeFuncs.push(on(target, 'touchstart', activate, { - passive: true - })); - removeFuncs.push(on(target, 'touchend', deactivate, { - passive: true - })); - } if (options.closeOnBlur && !options.trapFocus) { removeFuncs.push(on(document, 'focusin', function (e) { if (!target.contains(e.target)) { @@ -438,7 +417,7 @@ } if (options.closeOnOutsideClick) { removeFuncs.push(on(document, 'click', function (e) { - if (!active && keyCode(e) === 1 && !closest(e.target, controlSelector)) { + if (keyCode(e) === 1 && !target.contains(e.target) && !closest(e.target, controlSelector)) { close(target); } }, { @@ -446,6 +425,25 @@ })); } if (options.closeOnScroll) { + var active = false; + var activate = function activate() { + active = true; + }; + var deactivate = function deactivate() { + active = false; + }; + removeFuncs.push(on(target, 'mouseenter', activate, { + passive: true + })); + removeFuncs.push(on(target, 'mouseleave', deactivate, { + passive: true + })); + removeFuncs.push(on(target, 'touchstart', activate, { + passive: true + })); + removeFuncs.push(on(target, 'touchend', deactivate, { + passive: true + })); removeFuncs.push(on(window, 'scroll', function () { if (!active) { close(target); diff --git a/dist/ctrly.min.js b/dist/ctrly.min.js index ee1be7d..375cb55 100644 --- a/dist/ctrly.min.js +++ b/dist/ctrly.min.js @@ -3,4 +3,4 @@ * Copyright (c) 2018 Jan Sorgalla * License: MIT */ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.ctrly=t()}(this,function(){"use strict";function f(){try{var e=document.activeElement;return e&&e.nodeName?e:document.body}catch(e){return document.body}}function r(e){for(var t=[];e&&e.parentNode&&1===e.parentNode.nodeType;)e=e.parentNode,t.push(e);return t}function p(e){var t,n;(1 { - active = true; - }; - - const deactivate = () => { - active = false; - }; - - if (options.closeOnOutsideClick || options.closeOnScroll) { - removeFuncs.push( - on(target, 'mouseenter', activate, {passive: true}) - ); - removeFuncs.push( - on(target, 'mouseleave', deactivate, {passive: true}) - ); - removeFuncs.push( - on(target, 'touchstart', activate, {passive: true}) - ); - removeFuncs.push( - on(target, 'touchend', deactivate, {passive: true}) - ); - } - if (options.closeOnBlur && !options.trapFocus) { removeFuncs.push( on(document, 'focusin', e => { @@ -213,10 +188,14 @@ export default function ctrly(opts = {}) { removeFuncs.push( on(document, 'click', e => { // Close only after click on document - // - if currently not interacting with the target // - if it's a left button mouse click + // - if currently not interacting with the target // - if the click did not originated from (within) a control - if (!active && keyCode(e) === 1 && !closest(e.target, controlSelector)) { + if ( + keyCode(e) === 1 && + !target.contains(e.target) && + !closest(e.target, controlSelector) + ) { close(target); } }, {passive: true}) @@ -224,6 +203,29 @@ export default function ctrly(opts = {}) { } if (options.closeOnScroll) { + let active = false; + + const activate = () => { + active = true; + }; + + const deactivate = () => { + active = false; + }; + + removeFuncs.push( + on(target, 'mouseenter', activate, {passive: true}) + ); + removeFuncs.push( + on(target, 'mouseleave', deactivate, {passive: true}) + ); + removeFuncs.push( + on(target, 'touchstart', activate, {passive: true}) + ); + removeFuncs.push( + on(target, 'touchend', deactivate, {passive: true}) + ); + removeFuncs.push( on(window, 'scroll', () => { if (!active) { diff --git a/test/test.close-on-outside-click.js b/test/test.close-on-outside-click.js index a57f819..ec050d2 100644 --- a/test/test.close-on-outside-click.js +++ b/test/test.close-on-outside-click.js @@ -44,30 +44,8 @@ describe('ctrly(closeOnOutsideClick)', () => { }); }); - it('does not close target on outside click after mouse enters target', done => { - const {control, target} = fixture.refs; - - ctrlyInstance = ctrly(); - - ready(() => { - assertClosed(control, target); - - simulant.fire(control, 'click', {which: 1, button: 0}); - - assertOpen(control, target); - - simulant.fire(target, 'mouseenter'); - - simulant.fire(document, 'click', {which: 1, button: 0}); - - assertOpen(control, target); - - done(); - }); - }); - - it('closes target on outside click after mouse leaves target', done => { - const {control, target} = fixture.refs; + it('does not close target on click from within target', done => { + const {control, target, targetLink} = fixture.refs; ctrlyInstance = ctrly(); @@ -78,18 +56,10 @@ describe('ctrly(closeOnOutsideClick)', () => { assertOpen(control, target); - simulant.fire(target, 'mouseenter'); - - simulant.fire(document, 'click', {which: 1, button: 0}); + simulant.fire(targetLink, 'click', {which: 1, button: 0}); assertOpen(control, target); - simulant.fire(target, 'mouseleave'); - - simulant.fire(document, 'click', {which: 1, button: 0}); - - assertClosed(control, target); - done(); }); });