-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathModal.vue
215 lines (193 loc) · 5.26 KB
/
Modal.vue
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
<template>
<Transition name="fade" appear @leave="onTransitionLeaveStart">
<div
ref="modal"
class="fixed inset-0 z-[1000] overflow-y-auto transition"
role="dialog"
>
<div
class="flex min-h-screen items-end justify-center text-center sm:!block"
>
<Transition name="fade" appear>
<div
class="fixed inset-0 transition-opacity"
@touchstart="!dialog ? close() : null"
@touchend="!dialog ? close() : null"
@mouseup="!dialog ? close() : null"
@mousedown="!dialog ? close() : null"
>
<div
class="absolute inset-0 bg-primaryLight opacity-80 focus:outline-none"
tabindex="0"
@click="!dialog ? close() : null"
></div>
</div>
</Transition>
<span
v-if="placement === 'center'"
class="sm:h-screen sm:align-middle <sm:hidden"
aria-hidden="true"
>​</span
>
<Transition name="bounce" appear>
<div
class="inline-block w-full overflow-hidden text-left align-bottom shadow-lg transform border-dividerDark bg-primary transition-all sm:rounded-xl sm:border sm:align-middle"
:class="[{ 'mt-16 md:mb-8': placement === 'top' }, styles]"
>
<div
v-if="title"
class="flex items-center justify-between border-b border-dividerLight"
:class="{ 'p-4': !fullWidth }"
>
<div class="flex items-center justify-start flex-1">
<slot name="actions"></slot>
</div>
<div class="flex items-center justify-center">
<h3 class="heading">
{{ title }}
</h3>
</div>
<div class="flex items-center justify-end flex-1">
<HoppButton
v-if="dimissible"
v-tippy="{ theme: 'tooltip', delay: [500, 20] }"
:title="closeText ?? t?.('action.close') ?? 'Close'"
:icon="IconX"
@click="close"
/>
</div>
</div>
<div
class="flex max-h-[60vh] flex-col overflow-y-auto"
:class="{ 'p-4': !fullWidth && !fullWidthBody }"
>
<slot name="body"></slot>
</div>
<div
v-if="hasFooterSlot"
class="flex items-center justify-between flex-1 border-t border-dividerLight bg-primaryContrast"
:class="{ 'p-4': !fullWidth }"
>
<slot name="footer"></slot>
</div>
</div>
</Transition>
</div>
</div>
</Transition>
</template>
<script lang="ts">
const PORTAL_DOM_ID = "hoppscotch-modal-portal"
// An ID ticker for generating consistently unique modal IDs
let stackIDTicker = 0
// Why ?
const stack = (() => {
const stack: number[] = []
return {
push: stack.push.bind(stack),
pop: stack.pop.bind(stack),
peek: () => (stack.length === 0 ? undefined : stack[stack.length - 1]),
}
})()
</script>
<script setup lang="ts">
import { HoppButton } from "../button"
import IconX from "~icons/lucide/x"
import {
ref,
computed,
useSlots,
onMounted,
onBeforeUnmount,
inject,
} from "vue"
import { HoppUIPluginOptions, HOPP_UI_OPTIONS } from "./../../plugin"
const { t, onModalOpen, onModalClose } =
inject<HoppUIPluginOptions>(HOPP_UI_OPTIONS) ?? {}
withDefaults(
defineProps<{
dialog: boolean
title: string
dimissible: boolean
placement: string
fullWidth: boolean
fullWidthBody: boolean
styles: string
closeText: string | null
}>(),
{
dialog: false,
title: "",
dimissible: true,
placement: "top",
fullWidth: false,
fullWidthBody: false,
styles: "sm:max-w-lg",
closeText: null,
},
)
const emit = defineEmits<{
(e: "close"): void
}>()
onBeforeUnmount(() => {
onModalClose?.()
})
const stackId = ref(stackIDTicker++)
const shouldCleanupDomOnUnmount = ref(true)
const slots = useSlots()
const hasFooterSlot = computed(() => {
return !!slots.footer
})
const modal = ref<Element>()
onMounted(() => {
const portal = getPortal()
portal.appendChild(modal.value!)
stack.push(stackId.value)
document.addEventListener("keydown", onKeyDown)
onModalOpen?.()
})
onBeforeUnmount(() => {
if (shouldCleanupDomOnUnmount.value && modal.value) {
getPortal().removeChild(modal.value)
}
stack.pop()
document.removeEventListener("keydown", onKeyDown)
})
const close = () => {
emit("close")
}
const onKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape" && stackId.value === stack.peek()) {
e.preventDefault()
close()
}
}
const onTransitionLeaveStart = () => {
close()
shouldCleanupDomOnUnmount.value = false
}
const getPortal = () => {
let el = document.querySelector("#" + PORTAL_DOM_ID)
if (el) {
return el
}
el = document.createElement("DIV")
el.id = PORTAL_DOM_ID
document.body.appendChild(el)
return el
}
</script>
<style lang="scss" scoped>
.bounce-enter-active {
@apply transition;
animation: bounce-in 150ms;
}
@keyframes bounce-in {
0% {
transform: scale(0.95);
}
100% {
transform: scale(1);
}
}
</style>