Skip to content

Commit a9c99bc

Browse files
committed
chore: dealing with palatable occlusion issues
1 parent 71e53bc commit a9c99bc

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"unocss": "^0.51.2",
8383
"vite": "^4.0.0",
8484
"vitest": "^0.13.1",
85-
"vue3-right-click-menu": "workspace:^0.0.3"
85+
"vue3-right-click-menu": "^0.0.3"
8686
},
8787
"lint-staged": {
8888
"*": [

pnpm-lock.yaml

Lines changed: 13 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rightClick.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import type { DefineComponent, PropType } from 'vue'
2-
import { type Ref, Teleport, Transition, computed, defineComponent, h, ref } from 'vue'
3-
import { useContextMenu } from './contextMenu'
1+
import type { DefineComponent, PropType, Ref } from 'vue'
2+
import { Teleport, Transition, computed, defineComponent, h, nextTick, ref, watch } from 'vue'
43
import type { Props } from './types'
4+
import { useContextMenu } from './contextMenu'
5+
import useViewport from './useViewport'
56

67
export const RightClick = defineComponent({
78
name: 'RightClick',
@@ -14,15 +15,39 @@ export const RightClick = defineComponent({
1415
emits: ['select'],
1516
setup(props, { emit, slots }) {
1617
const containerRef: Ref<HTMLElement | undefined> = ref()
18+
const menuRef: Ref<HTMLElement | undefined> = ref()
1719
const { x, y, showMenu } = useContextMenu(containerRef)
20+
const _w = ref(0)
21+
const _h = ref(0)
22+
const { vw, vh } = useViewport()
23+
24+
watch(() => showMenu.value, async (v) => {
25+
if (v) {
26+
await nextTick()
27+
const menuEl = menuRef.value
28+
if (!menuEl)
29+
return
30+
_w.value = menuEl.clientWidth
31+
_h.value = menuEl.clientHeight
32+
}
33+
})
1834
const handleClick = (item: Record<string, string>) => {
1935
showMenu.value = false
2036
emit('select', item)
2137
}
22-
const styleComputed = computed(() => ({
23-
left: `${x.value}px`,
24-
top: `${y.value}px`,
25-
}))
38+
39+
const styleComputed = computed(() => {
40+
let posX = x.value
41+
let posY = y.value
42+
if (x.value > vw.value - _w.value)
43+
posX -= _w.value
44+
if (y.value > vh.value - _h.value)
45+
posY -= y.value - vh.value + _h.value
46+
return {
47+
left: `${posX}px`,
48+
top: `${posY}px`,
49+
}
50+
})
2651

2752
return () => h('div', {
2853
'ref': containerRef,
@@ -60,6 +85,7 @@ export const RightClick = defineComponent({
6085
},
6186
h('div', {
6287
class: 'menu-list',
88+
ref: menuRef,
6389
}, props.menu.map(item => h('div', {
6490
key: item.label,
6591
class: 'menu-item',

src/useViewport.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ref } from 'vue'
2+
const vw = ref(document.documentElement.clientWidth)
3+
const vh = ref(document.documentElement.clientHeight)
4+
export default function () {
5+
window.addEventListener('resize', () => {
6+
vw.value = document.documentElement.clientWidth
7+
vh.value = document.documentElement.clientHeight
8+
})
9+
return {
10+
vw,
11+
vh,
12+
}
13+
}

0 commit comments

Comments
 (0)