Skip to content

Commit

Permalink
Update composed path implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelvalle committed Feb 2, 2020
1 parent cda66cb commit dd17cd4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 63 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ Vue.use(vClickOutside)
}
},
methods: {
onClickOutside (event, el) {
onClickOutside (event) {
console.log('Clicked outside. Event: ', event)
},

handler (event, el) {
handler (event) {
console.log('Clicked outside (Using config), middleware returned true :)')
},
// Note: The middleware will be executed if the event was fired outside the element.
// It should have only sync functionality and it should return a boolean to
// define if the handler should be fire or not
middleware (event, el) {
middleware (event) {
return event.target.className !== 'modal'
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ The `notouch` modifier is no longer supported, same functionality can be achieve

## Migrate from version 2

The HTML `el` is not sent in the handler function argument any more. See https://github.com/ndelvalle/v-click-outside/issues/137 for more details.
The HTML `el` is not sent in the handler function argument any more. Review [this issue](https://github.com/ndelvalle/v-click-outside/issues/137) for more details.

## License

Expand Down
93 changes: 48 additions & 45 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,5 @@
},
"engines": {
"node": ">=6"
},
"dependencies": {
"event-propagation-path": "^1.0.5"
}
}
17 changes: 11 additions & 6 deletions src/v-click-outside.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'event-propagation-path'

const HANDLERS_PROPERTY = '__v-click-outside'
const HAS_WINDOWS = typeof window !== 'undefined'
const HAS_NAVIGATOR = typeof navigator !== 'undefined'
Expand All @@ -26,9 +24,12 @@ function processDirectiveArguments(bindingValue) {
}

function onEvent({ el, event, handler, middleware }) {
const outsideCheck = event.propagationPath
? event.propagationPath().indexOf(el) < 0
: !el.contains(event.target)
// Note: composedPath is not supported on IE and Edge, more information here:
// https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath
// In the meanwhile, we are using el.contains for those browsers, not
// the ideal solution, but using IE or EDGE is not ideal either.
const path = event.path || (event.composedPath && event.composedPath())
const outsideCheck = path ? path.indexOf(el) < 0 : !el.contains(event.target)

const isClickOutside = event.target !== el && outsideCheck

Expand Down Expand Up @@ -56,6 +57,8 @@ function bind(el, { value }) {

el[HANDLERS_PROPERTY].forEach(({ event, handler }) =>
setTimeout(() => {
// Note: More info about this implementation can be found here:
// https://github.com/ndelvalle/v-click-outside/issues/137
if (!el[HANDLERS_PROPERTY]) {
return
}
Expand All @@ -80,8 +83,10 @@ function update(el, { value, oldValue }) {
bind(el, { value })
}

export default {
const directive = {
bind,
update,
unbind,
}

export default HAS_WINDOWS ? directive : {}

0 comments on commit dd17cd4

Please sign in to comment.