Giter Site home page Giter Site logo

iamadamjowett / angular-click-outside Goto Github PK

View Code? Open in Web Editor NEW
203.0 9.0 69.0 607 KB

An angular directive to detect a click outside of an elements scope, great for clicking outside of slide in and drop down menus amongst other things.

License: MIT License

JavaScript 72.39% HTML 24.32% CSS 3.28%

angular-click-outside's Introduction

angular-click-outside

An Angular directive to detect a click outside of an elements scope. Great for closing dialogues, drawers and off screen menu's etc.

Recent changes

  • Shortened Bower description to remove Bower warning on install (thanks @jcubic)
  • Thanks to @Lorelei for the pull request to pass the event back in the callback function
  • Thanks to @CosticaPuntaru for the improvement to now allow the directive to no longer need an id on the element for this to work
  • Added basic ngdocs documentation
  • Removed the addition of the element id to the classes array as now it no longer needs to be checked

Roadmap

  • Addition of outside-if attribute. Ability to restrict click outside registering to defined elements (opposite of outside-if-not) rather than anywhere outside an element
  • Look into the worth of converting directive to Angular 1 component
  • Conversion of directive to Angular 2 component

Installation

There are two easy ways to install the clickoutside directive:

Manual download

Download the clickoutside.directive.js file, and include it in your index.html file with something like:

<script type="text/javascript" src="/path/to/clickoutside.directive.js"></script>

Also be sure to include the module in your app.js file with:

npm

=======

angular.module('yourAppName', ['angular-click-outside'])

Bower

=======

npm install @iamadamjowett/angular-click-outside

Usage

=======

bower install angular-click-outside --save

Usage

The directive will work with either ID's or classes, however, be wary of using classes as quite often some unwanted elements may have the same class, and so will be excluded/included unintentionally.

If you are sure that you want to exclude/include all elements with a class however the directive will work just fine as it looks through the classNames as well as looking at the given ID list.

General though ID's will suffice, but instances of dynamically inserted list items may require the use of classes.

Add the directive via the click-outside attribute, and give exceptions via the outside-if-not attribute.

Basic example:

<div class="menu" click-outside="closeThis">
    ...
</div>

This is of little use though without a callback function to do something with that click:

<div class="menu" click-outside="closeThis()">
    ...
</div>

Where closeThis() is the function assigned to the scope via the controller such as:

angular
    .module('myApp')
    .controller('MenuController', ['$scope', MenuController]);

function MenuController($scope) {
    $scope.closeThis = function () {
        console.log('closing');
    }
}
<button id="my-button">Menu Trigger Button</button>
<div ng-controller="MenuController">
    <div class="menu" click-outside="closeThis()" outside-if-not="my-button">
        ...
    </div>
</div>

Adding Exceptions

You can also add exceptions via the outside-if-not tag, which executes the callback function, but only if the ID's or classes listed aren't clicked.

In this case closeThis() will be called only if clicked outside and #my-button wasn't clicked as well (note .my-button also would be an exception). This can be great for things like slide in menus that might have a button outside of the menu scope that triggers it:

<button id="my-button">Menu Trigger Button</button>
<div class="menu" id="main-menu" click-outside="closeThis()" outside-if-not="my-button">
    ...
</div>

You can have more than one exception by comma delimiting a list such as:

<button id="my-button">Menu Trigger Button</button>
<div class="menu" id="main-menu" click-outside="closeThis()" outside-if-not="my-button, another-button">
    ...
</div>
<button id="another-button">A second trigger button</button>

angular-click-outside's People

Contributors

adammettro avatar aderjaan avatar anatejms avatar apansky avatar ascripnic avatar bryant1410 avatar costicapuntaru avatar fela98 avatar holm avatar iamadamjowett avatar koningskristof avatar lorelei avatar marcin-wosinek avatar mariomc avatar ppalladino avatar quentin-sommer avatar tobiasbueschel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-click-outside's Issues

Possible bug with outside-if-not

Awesome directive, I appreciate you putting this together. It works great, however i'm having one small issue with it. I'm using this on a "fake" dropdown ( aka a styled div that drops down with selectable options). Using this directive, the div closes if you click outside unless the clicks are within an ngDialog modal. (outside-if-not="ngdialog1, ngdialog-overlay, ngdialog-close, form-buttons, form-actions, input")

My issue is.. This works on the initial ngDialog modal open, however if you close the modal then open it again, any clicks inside of the modal will close the dropdown. Its as if the "outside-if-not" directive is not being respected.

Comma Separated Values

You should add to the ReadMe that the values that are comma separated cannot contain any spaces. I didn't realize that you weren't doing a trim on the directives and spent hours just realized that the spaces were be added to the class names, which of course won't work.

Publish to NPM registry

Could you please publish your module to NPM registry for npm/webpack users? I'm not using bower.

Label for

<div class="c-checkbox">
    <input type="checkbox" class="c-checkbox__input" id="photoLayoutyCheckbox" name="photoLayoutyCheckbox"/>
    <label for="photoLayoutyCheckbox" class="c-checkbox__label">PhotoLayout</label>
</div>

this simple layout doesn't work (label doesn't toogle checkbox) if your module included in angular app

Document outside-if-not with classes

Hello,

I don't know if this was intended, but for me is perfect. outside-if-not worksp with classes as it works with IDs. It's a very great feature, because all my buttons has the class btn. I just tried and it worked, but there is nothing on the documentation that teaches about this.

Regards

Console logs

When I use the plugin, it logs some tests results in the console:

passed the hide test
passed the event check
about to check
about to check

Is it normal? How can I disable this behaviour?
Thanks, and very useful package by the way!

Bower warning

There is error (or warning) when you install from bower:

bower angular-click-outside#*     invalid-meta The "description" is too long, the limit is 140 characters

and the length is 149 characters.

Could be used on a SELECT with it's options ?

Hi,

Do you know if this can work with a SELECT and its options.

I am using the ng-options to fill the contents.

I would like to get some kind of visual feedback and expand / collapse. Actually, expand isn't needed as i can use a click of the button above. BUT collapse is, currently i have been able to use ng-blur but you have to completely click off the element for it to be called.

Is this something that is supported ?

I did check the issues here to see if anyone else was trying to achieve the same thing but couldn't find anything

Thanks

webpack issue

trying to build this module with npm and webpack

import '@iamadamjowett/angular-click-outside';

and getting error:

Module not found: Error: Cannot resolve module '@iamadamjowett/angular-click-outside'

Release with updated module name

Hello;
Thanks a lot for sharing the module.

I've just noticed that the version installed by default by bower has a different module name. Would it be possible to release the updated version?

Prevents inner href's from working

If you put click-outside on something that (for example) opens a dropdown, and that dropdown contains links with hrefs, those hrefs don't navigate (unless they only change the hash). E.g.

<li id="something-in-menu" click-outside="showDropdown = false">
  <a href="#" ng-click="showDropdown = true">Click here</a>
  <div ng-show="showDropdown">
    <ul id="submenu">
      <li><a href="/this/link/no/worky">Doesn't work</a></li>
      <li><a href="/blah#!/this/does">But only if you're already on /blah#!</a></li>
    </ul>
  </div>
</li>

Why is ID required on the element?

Awesome directive! It was exactly what I was looking for.

I noticed that it won't work properly unless you assign an id to the element. Is there a reason you made this a requirement? Any way to remove this restriction.

Cheers

[BUG] Input type file will not trigger

Using this directive, if you have an input of type file on your document, the file browser won't open.
As a workaround, I added the following lines in the beginning of the eventHandler function (it can be improved) :
if(e.target.type === "file") { return; }

Update the module name

The readme file said - add 'angular-click-outside' module, which not work.

instead, the updated readme file after install it and watch in the node_modules, said to include the work module name - 'tw.directives.clickOutside'

please update this for not confuse some people.
tnx

Click inside dropdown menu

Hi,
When I click inside the dropdown menu that has the click-outside directive, it closes the menu, and also, any links that are in that dropdown don't work (neither with href or ui-sref). Actually the click I execute on an inside the dropdown, gets executed on whatever is under that menu.... strange behaviour

Thanks

Not compatible IPad touch

Works prefect but not iPad safari. Obviously it not recognizes the $document.on('click') event. My solution was detect it iPad then 'touchstart'. As below:

var ua = navigator.userAgent,
event = (ua.match(/iPad/i)) ? "touchstart" : "click";
$document.on(event, eventHandler);

Click to href doesn't catch click outside

I use click outside to persist one form, but problem is if user is in input field and click on any kind or navigation link in my app which redirects page to other URL the attached function to click outside on that input is never entered.

Outside if not doesn't work with class if element has several classes

L54:
if ((id !== undefined && id === classList[i]) || (classNames && classNames === classList[i])) { // now let's exit out as it is an element that has been defined as being ignored for clicking outside return; }

ClassNames is a string with all the classes of an element while classList represents only the class you want to exclude. First you have to split classNames :
classNames.split(' ')

And after you have to compare each element of classList with each element of this new list.

Example:

var listClassNames = classNames.split(' '); for(var j = 0; j< listClassNames.length; j++){ if(classList[i] === listClassNames[j]){ return; } }

Tests?

No tests is a warning flag for me. Would you be open to adding some tests?

Let me know, thanks!

close if inside?

I just use this library and it is working correctly with me.
My question here is:
if I have a side bar menu and I want to close it if I click any link inside the side bar (not outside) how can I implement something like this?

Outside-if-not working with version 2.2.1, not anymore with newer versions

When I update the plugin to a version > 2.2.1, the outside-if-not attribute stopped working. Small code snippet:

<div  data-click-outside="deselectFormElement()" 
data-outside-if-not="editor__sidebar,option__button">

Downgrading to version 2.2.1 fixes the issue. Could you please fix it in a new version?

Please consider adding unit tests/e2e tests to prevent regressions. Other than that, I really appreciate your plugin!

outside-if-not possiblities

Is it possible to use 'outside-if-not' with more complex class selectors like:
".outerclass .innerclass"?
Can't get this to work.
Thanks!

Version 2 - laggy

There an delay about 1 or 2 seconds on big project then clicking no mater where. Looks like its freeze on click.
Please review and optimize your script.

Click outside glitch

I have a popup with a click-outside callback like this.

<popup click-outside="closePopup()"></popup>

It works fine in almost all cases, until I click an element that has bootstraps data-toggle="dropdown" on it. Like this.

<button data-toggle="dropdown"></button>

Any ideas would be helpful

Disable closing when mouse go out on scrolling

Hi there,

I'm facing a trouble,

Just imagine a facebook notification application, people click on a the bell and we have the notifications in a div that we can scroll.

When i click outside. OK.
When i scroll inside the div. OK
When i scroll and finish the scroll outside the div it trigger a click outside. So how to prevent this one ?

Thank's for all,

Jérémy

Bug

<div id="J_div"></div>
<div id="J_div_1"></div>
<div id="J_div_2"></div>
(id !== undefined && id.indexOf(classList[i]) > -1) || (classNames && classNames.indexOf(classList[i]) > -1)

Fix
(id !== undefined && id === classList[i]) || (classNames && classNames === classList[i])

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.