Skip to content

Commit

Permalink
Merge pull request #2 from timwis/user-data
Browse files Browse the repository at this point in the history
Support passing user data via scoped slots
  • Loading branch information
timwis authored Jun 5, 2017
2 parents 8415d4f + 35c2da4 commit f8a84b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,23 @@ module.exports = {
</style>
```

If you'd like to pass any context from the original `oncontextmenu`
event down to your menu template, you can pass it as the second
param of `open` and access it within a [scoped slot](https://vuejs.org/v2/guide/components.html#Scoped-Slots)
under the `userData` property. For example:

```html
<div @contextmenu.prevent="$refs.menu.open($event, 'foo')">
...
</div>

<context-menu ref="menu">
<template scope="child">
<li @click="onClick('A', child.userData)">Option A</li>
<li @click="onClick('B', child.userData)">Option B</li>
</template>
</context-menu>
```

## Related
- [vue-context-menu](https://github.com/vmaimone/vue-context-menu)
11 changes: 7 additions & 4 deletions index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
:style="style"
tabindex="-1"
@blur="close"
@click.capture="close"
@click="close"
@contextmenu.capture.prevent>
<slot></slot>
<slot :user-data="userData"></slot>
</div>
</template>

Expand All @@ -19,7 +19,8 @@ module.exports = {
data () {
return {
x: null,
y: null
y: null,
userData: null
}
},
computed: {
Expand All @@ -34,14 +35,16 @@ module.exports = {
}
},
methods: {
open (evt) {
open (evt, userData) {
this.x = evt.pageX || evt.clientX
this.y = evt.pageY || evt.clientY
this.userData = userData
Vue.nextTick(() => this.$el.focus())
},
close (evt) {
this.x = null
this.y = null
this.userData = null
}
}
}
Expand Down

0 comments on commit f8a84b8

Please sign in to comment.