3
3
[ ![ npm] ( https://img.shields.io/npm/v/vue-context.svg )] ( https://www.npmjs.org/package/vue-context )
4
4
[ ![ npm] ( https://img.shields.io/npm/dt/vue-context.svg )] ( https://www.npmjs.org/package/vue-context )
5
5
6
- A simple yet flexible context menu component for Vue. It is styled for the standard ` ul ` tag, but any menu template can be used.
7
- The only dependency this package has is Vue, so the majority of styling is up to you, and any package styles for the menu
6
+ A simple yet flexible context menu for Vue. It is styled for the standard ` ul ` tag,
7
+ but any menu template can be used. The only dependency this package has is Vue,
8
+ so the majority of styling is up to you, and any package styles for the menu
8
9
can easily be overridden.
9
10
10
- The menu disappears when you expect by using the ` onblur ` event and also disappears when clicked on.
11
+ The menu disappears when you expect by using the ` onblur ` even and it also
12
+ disappears when clicked on.
13
+
14
+ ## Note
15
+ The API has changed. Check [ v2.0.1 documentation] ( https://github.com/rawilk/vue-context/blob/master/docs/2.0.1.md )
16
+ if you use the old version.
11
17
12
18
## Getting Started
13
19
14
- The following instructions will help you get the vue-context menu up and running on your project.
20
+ The following instructions will help you get the vue-context menu up and running on
21
+ your project.
15
22
16
23
### Installation
17
24
@@ -22,111 +29,117 @@ $ npm install vue-context --save
22
29
23
30
## Basic Usage
24
31
25
- Import the package and use in your Vue instance, and add a simple method for the click event.
32
+ Import the package and use it in your Vue instance, and add a simple method
33
+ for the click event.
26
34
27
35
``` js
28
36
import Vue from ' vue' ;
29
- import vContext from ' vue-context' ;
37
+ import { VueContext } from ' vue-context' ;
30
38
31
39
new Vue ({
32
- el : ' #app ' ,
33
- components : {
34
- vContext
35
- },
36
- methods: {
37
- /**
38
- * Alert the text of the menu item clicked on.
39
- *
40
- * @param text
41
- */
42
- onClick (text ) {
43
- alert (` You clicked ${ text} !` );
44
- }
45
- }
46
- });
40
+ components : {
41
+ VueContext
42
+ },
43
+
44
+ methods: {
45
+ /**
46
+ * Alert the text of the menu item that was clicked on.
47
+ *
48
+ * @param {string} text
49
+ */
50
+ onClick (text ) {
51
+ alert (` You clicked ${ text} !` );
52
+ }
53
+ }
54
+ }). $mount ( ' #app ' ) ;
47
55
```
48
56
49
- Add an element to the page that will trigger the context menu to appear, and also add the context menu to the page.
57
+ Add an element to the page that will trigger the context menu to appear,
58
+ and also add the context menu to the page.
50
59
51
60
``` html
52
61
<div id =" app" >
53
-
54
- <div >
55
- <p @contextmenu.prevent =" $refs.menu.open" >
56
- Right click on me
57
- </p >
58
- </div >
59
-
60
- < v -context ref =" menu" >
61
- <ul >
62
- <li @click =" onClick($event.target.innerText)" >Option 1</li >
63
- <li @click =" onClick($event.target.innerText)" >Option 2</li >
64
- </ul >
65
- </ v -context >
66
-
62
+
63
+ <div >
64
+ <p @contextmenu.prevent =" $refs.menu.open" >
65
+ Right click on me
66
+ </p >
67
+ </div >
68
+
69
+ < vue -context ref =" menu" >
70
+ <ul >
71
+ <li @click =" onClick($event.target.innerText)" >Option 1</li >
72
+ <li @click =" onClick($event.target.innerText)" >Option 2</li >
73
+ </ul >
74
+ </ vue -context >
75
+
67
76
</div >
68
77
```
69
78
70
- ` @contextmenu.prevent ` is the event listener needed to open the context menu. It is using ` .prevent ` as a modifier to prevent the
71
- the default behavior. It has a ref of ` menu ` , which is what ` $refs.menu ` is referring to. When each item is clicked on, the text of
72
- the item is sent to the ` onClick ` method on the Vue instance, which is then shown in an alert.
79
+ ` @contextmenu.prevent ` is the event listener needed to open the context menu. It is using
80
+ ` .prevent ` as a modifier to prevent the default behavior. It has a ref of ` menu ` , which
81
+ is what ` $refs.menu ` is referring to. When each item is clicked on, the text of the item
82
+ is sent to the ` onClick ` method on the Vue instance, which is then shown in an alert.
73
83
74
84
## Advanced Usage
75
85
76
- To pass any data from the ` contextmenu ` event to your template, you can pass it as the second parameter of ` open ` and
77
- access it within a [ scoped slot] ( https://vuejs.org/v2/guide/components.html#Scoped-Slots ) under the ` userData ` property.
86
+ To pass any data from the ` contextmenu ` event to your template, you can pass it as the second
87
+ parameter of ` open ` and access it within a [ scoped slot] ( https://vuejs.org/v2/guide/components.html#Scoped-Slots )
88
+ under the ` data ` property` . ` $event` must be passed as the first parameter,
89
+ otherwise the context menu will not function properly.
78
90
79
91
``` html
80
92
<div id =" app" >
81
-
82
- <p @contextmenu.prevent =" $refs.menu.open($event, 'foo')" >
83
- Right click on me
84
- </p >
85
-
86
- <v-context ref =" menu" >
87
- <template scope =" child" >
88
- <ul >
89
- <li @click =" onClick($event.target.innerText, child.userData)" >Option 1</li >
90
- <li @click =" onClick($event.target.innerText, child.userData)" >Option 2</li >
91
- </ul >
92
- </template >
93
- </v-context >
94
-
93
+
94
+ <p @contextmenu.prevent =" $refs.menu.open($event, { foo: 'bar' })" >
95
+ Right click on me
96
+ </p >
97
+
98
+ <vue-context ref =" menu" >
99
+ <ul slot-scope =" child" >
100
+ <li @click =" onClick($event.target.innerText, child.data)" >Option 1</li >
101
+ <li @click =" onClick($event.target.innerText, child.data)" >Option 2</li >
102
+ </ul >
103
+ </vue-context >
104
+
95
105
</div >
96
106
```
97
107
98
- Now the ` onClick ` method on the Vue instance has access to any user data passed to it, which in this case this the string ` foo ` . The
99
- ` userData ` can also be used to output dynamic content to the context menu.
108
+ Now the ` onClick ` method on the Vue instance has access to any user data passed to it,
109
+ which in this case is an object with a property named ` foo ` . The ` data ` slot scope
110
+ can also be used to output dynamic content to the context menu.
100
111
101
112
``` js
102
113
import Vue from ' vue' ;
103
- import vContext from ' vue-context' ;
114
+ import { VueContext } from ' vue-context' ;
104
115
105
116
new Vue ({
106
- el: ' #app' ,
107
- components: {
108
- vContext
109
- },
110
- methods: {
111
- /**
112
- * Alert the text of the menu item clicked on. Console log the data sent from the menu.
113
- *
114
- * @param text
115
- * @param userData
116
- */
117
- onClick (text , userData ) {
118
- alert (` You clicked ${ text} !` );
119
- console .log (userData);
120
- }
121
- }
122
- });
117
+ components: {
118
+ VueContext
119
+ },
120
+
121
+ methods: {
122
+ /**
123
+ * Alert the text of the menu item that was clicked on.
124
+ * Console log the data sent from the menu.
125
+ *
126
+ * @param {string} text
127
+ * @param {object} data
128
+ */
129
+ onClick (text , data ) {
130
+ alert (` You clicked ${ text} !` );
131
+ console .log (data);
132
+ // => { foo: 'bar' }
133
+ }
134
+ }
135
+ }).$mount (' #app' );
123
136
```
124
137
125
138
## Credits
126
139
127
- vue-context is inspired from [ vue-lil-context-menu] ( https://github.com/timwis/vue-lil-context-menu )
128
- and [ Vue.JS Right Click Menu] ( http://vuejsexamples.com/vue-js-right-click-menu/ ) . Ultimately vue-context is intended
129
- to provide a simple and lightweight context menu for Vue.
140
+ vue-context is inspired from [ vue-lil-context-menu] ( https://github.com/timwis/vue-lil-context-menu )
141
+ and [ Vue.JS Right Click Menu] ( http://vuejsexamples.com/vue-js-right-click-menu/ ) . Ultimately
142
+ vue-context is intended to provide a simple and lightweight context menu for Vue.
130
143
131
144
## License
132
145
0 commit comments