forked from vueuse/vueuse
/
demo.vue
92 lines (87 loc) 路 1.92 KB
/
demo.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
<script setup lang="ts">
import { ref } from 'vue'
import type { OnClickOutsideHandler } from '@vueuse/core'
import { onClickOutside } from '@vueuse/core'
import { vOnClickOutside } from './directive'
const modal = ref(false)
const modalRef = ref(null)
onClickOutside(
modalRef,
(event) => {
console.log(event)
modal.value = false
},
)
const dropdown = ref(false)
const dropdownHandler: OnClickOutsideHandler = (event) => {
console.log(event)
dropdown.value = false
}
</script>
<template>
<button @click="modal = true">
Open Modal
</button>
<div class="ml-2 relative inline-block">
<button @click.stop="dropdown = !dropdown">
Toggle Dropdown
</button>
<div
v-if="dropdown"
v-on-click-outside.bubble="dropdownHandler"
class="dropdown-inner"
>
Click outside of the dropdown to close it.
</div>
</div>
<div v-if="modal" ref="modalRef" class="modal">
<div class="inner">
<button class="button small" title="Close" @click="modal = false">
饾柗
</button>
<p class="heading">
Demo Modal
</p>
<p>Click outside of the modal to close it.</p>
</div>
</div>
</template>
<style scoped>
.modal {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 420px;
max-width: 100%;
z-index: 10;
}
.inner {
background-color: var(--vp-c-bg);
padding: 0.4em 2em;
border-radius: 5px;
border: 1px solid var(--vp-c-divider-light);
box-shadow: 2px 2px 10px rgba(10, 10, 10, 0.1);
}
.dropdown-inner {
background-color: var(--vp-c-bg);
padding: 0.5em;
position: absolute;
left: 0;
z-index: 10;
border-radius: 5px;
border: 1px solid var(--vp-c-divider-light);
box-shadow: 2px 2px 5px rgba(10, 10, 10, 0.1);
}
.heading {
font-weight: bold;
font-size: 1.4rem;
margin-bottom: 2rem;
}
.button {
position: absolute;
top: -0.9rem;
right: -0.5rem;
font-weight: bold;
}
</style>