Gathering detailed insights and metrics for v-click-outside
Gathering detailed insights and metrics for v-click-outside
Gathering detailed insights and metrics for v-click-outside
Gathering detailed insights and metrics for v-click-outside
🔲 Vue directive to react on clicks outside an element without stopping the event propagation
npm install v-click-outside
99.5
Supply Chain
100
Quality
76.2
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
967 Stars
470 Commits
88 Forks
11 Watching
20 Branches
23 Contributors
Updated on 29 Oct 2024
JavaScript (87.61%)
Vue (10.3%)
HTML (2.08%)
Cumulative downloads
Total Downloads
Last day
-3.4%
22,105
Compared to previous day
Last week
3.6%
118,020
Compared to previous week
Last month
2.9%
503,564
Compared to previous month
Last year
-6.4%
6,130,633
Compared to previous year
Vue directive to react on clicks outside an element without stopping the event propagation. Great for closing dialogues and menus among other things.
1$ npm install --save v-click-outside
1$ yarn add v-click-outside
1import Vue from 'vue' 2import vClickOutside from 'v-click-outside' 3 4Vue.use(vClickOutside)
1<script> 2 export default { 3 data () { 4 vcoConfig: { 5 handler: this.handler, 6 middleware: this.middleware, 7 events: ['dblclick', 'click'], 8 // Note: The default value is true, but in case you want to activate / deactivate 9 // this directive dynamically use this attribute. 10 isActive: true, 11 // Note: The default value is true. See "Detecting Iframe Clicks" section 12 // to understand why this behavior is behind a flag. 13 detectIFrame: true, 14 // Note: The default value is false. Sets the capture option for EventTarget addEventListener method. 15 // Could be useful if some event's handler calls stopPropagation method preventing event bubbling. 16 capture: false 17 } 18 }, 19 methods: { 20 onClickOutside (event) { 21 console.log('Clicked outside. Event: ', event) 22 }, 23 24 handler (event) { 25 console.log('Clicked outside (Using config), middleware returned true :)') 26 }, 27 // Note: The middleware will be executed if the event was fired outside the element. 28 // It should have only sync functionality and it should return a boolean to 29 // define if the handler should be fire or not 30 middleware (event) { 31 return event.target.className !== 'modal' 32 } 33 } 34 }; 35</script> 36 37<template> 38 <div v-click-outside="onClickOutside"></div> 39 <div v-click-outside="vcoConfig"></div> 40</template>
Or use it as a directive
1import vClickOutside from 'v-click-outside' 2 3<script> 4 export default { 5 directives: { 6 clickOutside: vClickOutside.directive 7 }, 8 methods: { 9 onClickOutside (event) { 10 console.log('Clicked outside. Event: ', event) 11 } 12 } 13 }; 14</script> 15 16<template> 17 <div v-click-outside="onClickOutside"></div> 18</template>
Or use directive‘s hooks programmatically
1<script> 2import vClickOutside from 'v-click-outside' 3const { bind, unbind } = vClickOutside.directive 4 5export default { 6 name: 'RenderlessExample', 7 8 mounted() { 9 const this._el = document.querySelector('data-ref', 'some-uid') 10 // Note: v-click-outside config or handler needs to be passed to the 11 // "bind" function 2nd argument as object with a "value" key: 12 // same as Vue’s directives "binding" format. 13 // https://vuejs.org/v2/guide/custom-directive.html#Directive-Hook-Arguments 14 bind(this._el, { value: this.onOutsideClick }) 15 }, 16 beforeDestroy() { 17 unbind(this._el) 18 }, 19 20 methods: { 21 onClickOutside (event) { 22 console.log('Clicked outside. Event: ', event) 23 } 24 }, 25 26 render() { 27 return this.$scopedSlots.default({ 28 // Note: you can't pass vue's $refs (ref attribute) via slot-scope, 29 // and have this.$refs property populated as it will be 30 // interpreted as a regular html attribute. Workaround it 31 // with good old data-attr + querySelector combo. 32 props: { 'data-ref': 'some-uid' } 33 }) 34 } 35}; 36</script>
1<!-- SomeComponent.vue --> 2<template> 3 <renderless-example v-slot:default="slotScope"> 4 <div v-bind="slotScope.props"> 5 Transparently bound v-click-outside directive via slotScope 6 </div> 7 </renderless-example> 8</template>
See #220 for details or check-out this demo
To our knowledge, there isn't an idiomatic way to detect a click on a <iframe>
(HTMLIFrameElement
).
Clicks on iframes moves focus
to its contents’ window
but don't bubble
up to main window
, therefore not triggering our document.documentElement
listeners. On the other hand, the abovementioned focus
event does trigger a window.blur
event on main window
that we use in conjunction with document.activeElement
to detect if it came from an <iframe>
, and execute the provided handler
.
As with any workaround, this also has its caveats:
iframe
via keyboard
navigation also triggers window.blur
consequently the handler - no workaround found ATM;Because of these reasons, the detection mechanism is behind the detectIframe
flag that you can optionally set to false
if you find it conflicting with your use-case.
Any improvements or suggestions to this are welcomed.
The notouch
modifier is no longer supported, same functionality can be achieved using a middleware function
The HTML el
is not sent in the handler function argument any more. Review this issue for more details.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 3/7 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
63 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More