forked from frappe/studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDraggablePopup.vue
More file actions
189 lines (172 loc) · 4.97 KB
/
DraggablePopup.vue
File metadata and controls
189 lines (172 loc) · 4.97 KB
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
<template>
<teleport to="body">
<div class="relative" v-show="!isHidden" ref="popover">
<div class="fixed" @mousedown.stop>
<div
ref="popoverContent"
class="fixed flex flex-col gap-1 overflow-hidden rounded-lg border border-outline-gray-2 bg-surface-white shadow-2xl"
:style="{
width: width + 'px',
minHeight: height + 'px',
left: popupLeft + 'px',
top: popupTop + 'px',
}"
>
<div
class="flex cursor-grab select-none items-center justify-between px-4 py-2 pr-3 text-sm text-ink-gray-9"
:class="{ 'cursor-grabbing': isDragging }"
@mousedown="startDrag"
>
<slot name="header"></slot>
<div class="flex items-center gap-2">
<Button
v-if="actionLabel && actionHandler"
@click="actionHandler"
:label="actionLabel"
variant="solid"
></Button>
<Button @click="togglePopup" icon="x" variant="subtle"></Button>
</div>
</div>
<div class="flex-1 px-3 pb-3">
<slot name="content"></slot>
</div>
</div>
</div>
</div>
</teleport>
</template>
<script setup lang="ts">
import { useDomAttr } from "@/utils/useDomAttr"
import { useEventListener } from "@vueuse/core"
import { nextTick, onMounted, Ref, ref } from "vue"
const popover = ref(null) as Ref<HTMLElement | null>
const props = withDefaults(
defineProps<{
modelValue: boolean
width?: number
height?: number
placement?:
| "top-left"
| "top-right"
| "bottom-left"
| "bottom-right"
| "center"
| "top-middle"
| "bottom-middle"
| "middle-left"
| "middle-right"
placementOffset?: number
clickOutsideToClose?: boolean
container?: HTMLElement | null
actionLabel?: string
actionHandler?: () => void
}>(),
{
width: 300,
height: 200,
clickOutsideToClose: false,
placement: "top-left",
placementOffset: 0,
},
)
const isHidden = useDomAttr(popover, "data-aria-hidden", {
immediate: true,
transform: (v) => v === "true",
})
const emit = defineEmits(["update:modelValue"])
const popoverContent = ref(null) as Ref<HTMLElement | null>
const popupLeft = ref(1500)
const popupTop = ref(100)
let isDragging = ref(false)
let startX = 0
let startY = 0
let startLeft = 0
let startTop = 0
onMounted(async () => {
await nextTick()
setPosition()
})
const togglePopup = () => {
emit("update:modelValue", !props.modelValue)
}
const startDrag = (event: MouseEvent) => {
isDragging.value = true
startX = event.clientX
startY = event.clientY
startLeft = popupLeft.value
startTop = popupTop.value
document.addEventListener("mousemove", drag)
document.addEventListener("mouseup", stopDrag, { once: true })
}
const drag = (event: MouseEvent) => {
if (!isDragging.value) return
const dx = event.clientX - startX
const dy = event.clientY - startY
popupLeft.value = startLeft + dx
popupTop.value = startTop + dy
}
const stopDrag = () => {
isDragging.value = false
document.removeEventListener("mousemove", drag)
}
const handleClickOutside = (event: Event) => {
if (props.modelValue && popoverContent.value && !popoverContent.value.contains(event.target as Node)) {
emit("update:modelValue", false)
}
}
const setPosition = () => {
if (props.container) {
const { left, top, right, bottom } = props.container.getBoundingClientRect()
switch (props.placement) {
case "top-left":
popupLeft.value = left + props.placementOffset
popupTop.value = top + props.placementOffset
break
case "top-right":
popupLeft.value = right - props.width - props.placementOffset
popupTop.value = top + props.placementOffset
break
case "bottom-left":
popupLeft.value = left + props.placementOffset
popupTop.value = bottom - props.height - props.placementOffset
break
case "bottom-right":
popupLeft.value = right - props.width - props.placementOffset
popupTop.value = bottom - props.height - props.placementOffset
break
case "center":
popupLeft.value = left + (right - left) / 2 - props.width / 2
popupTop.value = top + (bottom - top) / 2 - props.height / 2
break
case "top-middle":
popupLeft.value = left + (right - left) / 2 - props.width / 2
popupTop.value = top + props.placementOffset
break
case "bottom-middle":
popupLeft.value = left + (right - left) / 2 - props.width / 2
popupTop.value = bottom - props.height - props.placementOffset
break
case "middle-left":
popupLeft.value = left + props.placementOffset
popupTop.value = top + (bottom - top) / 2 - props.height / 2
break
case "middle-right":
popupLeft.value = right - props.width - props.placementOffset
popupTop.value = top + (bottom - top) / 2 - props.height / 2
break
}
} else {
const { innerWidth, innerHeight } = window
popupLeft.value = innerWidth / 2 - props.width / 2
popupTop.value = innerHeight / 2 - props.height / 2
}
}
if (props.clickOutsideToClose) {
useEventListener(document, "click", handleClickOutside, {
capture: true,
passive: true,
})
}
useEventListener(window, "resize", setPosition)
</script>