forked from vueuse/vueuse
/
index.ts
231 lines (199 loc) · 5.51 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import type { MaybeComputedRef } from '@vueuse/shared'
import { noop, resolveUnref } from '@vueuse/shared'
import type { Ref } from 'vue-demi'
import { computed, nextTick, reactive, ref, watch, watchEffect } from 'vue-demi'
import type { Position } from '../types'
import type { MaybeComputedElementRef } from '../unrefElement'
import { unrefElement } from '../unrefElement'
import { useEventListener } from '../useEventListener'
import { useElementSize } from '../useElementSize'
import { useWindowSize } from '../useWindowSize'
export interface UseContextMenuOptions {
/**
* Whether to prevent the context menu from hiding when clicking on it.
*
* @default true
*/
hideOnClick?: MaybeComputedRef<boolean>
/**
* Enable the context menu only when the mouse is clicking this element,
* enabled to the entire page if not specified.
*
* @default document
*/
target?: MaybeComputedElementRef
/**
* Fires when the visibility of the context menu changes.
*
* @param visible
*/
onVisibleChange?: (visible: boolean) => void
/**
* Fires when the `contextmenu` event triggers.
*
* Could be useful to know
* the exact DOM your're clicking on,
* or control whether this `menu` should be shown or not.
*
* @param event
* @returns `false` to prevent the `menu` from showing
*/
onContextMenu?: (event: MouseEvent) => boolean | void
}
export interface UseContextMenuReturn {
/**
* Whether the context menu is visible.
*/
visible: Ref<boolean>
/**
* The absolute position of the context menu to the viewport.
*/
position: Ref<Position>
/**
* Indicates whether the context menu is enabled,
* mutate to disable/enable it.
*/
enabled: Ref<boolean>
/**
* Hide the context menu.
*/
hide: () => void
/**
* Show the context menu.
*
* @param [position] absolute position to the viewport, default to the previous position.
*/
show: (position?: Position) => void
/**
* Stop the context menu permanently.
*/
stop: () => void
}
/**
*
* Add custom `contextMenu` to an Element.
*
* @see https://vueuse.org/useContextMenu
* @param menuElement
* @param options
* @returns
*/
export function useContextMenu(
menuElement: MaybeComputedElementRef,
options: UseContextMenuOptions = {},
): UseContextMenuReturn {
const {
hideOnClick = true,
onVisibleChange = noop,
onContextMenu = noop,
target,
} = options
const menuElementRef = computed(() => unrefElement(menuElement))
const targetRef = computed(() => unrefElement(target))
const menuElementSize = reactive(
useElementSize(
menuElementRef,
{ width: 0, height: 0 },
{ box: 'border-box' },
),
)
const windowSize = reactive(
useWindowSize({
includeScrollbar: false,
}),
)
const enabled = ref(true)
const visible = ref(false)
const position = ref<Position>({ x: 0, y: 0 })
// see: https://github.com/vueuse/vueuse/pull/2136#issuecomment-1230525648
const menuPositionOffset = computed(() => {
const { x, y } = position.value
let [left, top, right, bottom]: Array<`${number}px` | null> = [
`${x}px`,
`${y}px`,
null,
null,
]
const overflowX = x + menuElementSize.width > windowSize.width
const overflowY = y + menuElementSize.height > windowSize.height
if (overflowX)
[left, right] = [null, `${windowSize.width - x}px`]
if (overflowY)
[top, bottom] = [null, `${windowSize.height - y}px`]
return {
left,
top,
right,
bottom,
}
})
const show = () => visible.value = true
const hide = () => visible.value = false
// hide first when it changes
watch(enabled, hide)
watch(visible, onVisibleChange)
// automatically show/hide and update the position of the `MenuElement`
const stopUpdate = watchEffect(
() => {
const element = menuElementRef.value
if (!element)
return
element.style.position = 'fixed'
element.style.visibility = visible.value ? 'visible' : 'hidden'
// performance
if (visible.value) {
for (const [property, offset] of Object.entries(menuPositionOffset.value))
element.style.setProperty(property, offset)
}
})
const cleanups = [stopUpdate]
const stop = () => {
enabled.value = false
// wait till the `hide` effect is done.
nextTick(() => {
cleanups.forEach(fn => fn())
})
}
const contextMenuHandler = (e: MouseEvent) => {
if (!enabled.value)
return
if (onContextMenu(e) === false)
return
e.preventDefault()
position.value = {
x: e.clientX,
y: e.clientY,
}
show()
// prevent other(parent) context menu from showing
e.stopPropagation()
}
// with `target` specified, apply on it.
// otherwise, apply on the entire page.
if (target)
cleanups.push(useEventListener(targetRef, 'contextmenu', contextMenuHandler))
else
cleanups.push(useEventListener('contextmenu', contextMenuHandler))
cleanups.push(
useEventListener('scroll', hide),
useEventListener('click', hide),
useEventListener('resize', hide),
useEventListener('blur', hide),
useEventListener('contextmenu', hide, { capture: true }),
useEventListener(menuElementRef, 'click', (e) => {
if (!resolveUnref(hideOnClick)) {
// hide it only when clicking outside of the menu,
// so stopPropagation when clicking on itself.
e.stopPropagation()
}
}),
)
return {
visible,
position,
enabled,
hide,
show,
stop,
}
}