-
Notifications
You must be signed in to change notification settings - Fork 7
/
DropdownButton.vue
114 lines (102 loc) · 2.81 KB
/
DropdownButton.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
<script setup lang="ts">
import { autoUpdate, useFloating } from "@floating-ui/vue";
import { onBeforeUnmount, onMounted, ref } from "vue";
import SimpleButton from "@/components/SimpleButton.vue";
interface DropdownProps {
label?: string;
icon?: string;
isPrimary?: boolean;
align?: "left" | "right";
isInline?: boolean;
}
const props = withDefaults(defineProps<DropdownProps>(), { isPrimary: false, align: "left" });
const isOpen = ref(false);
const el = ref<HTMLDivElement>();
const reference = ref<HTMLElement>();
const floating = ref<HTMLDivElement>();
const { floatingStyles } = useFloating(reference, floating, {
placement: props.align === "right" ? "bottom-end" : "bottom-start",
strategy: "fixed",
whileElementsMounted: autoUpdate,
});
function onClick(e: Event) {
if (el.value && floating.value) {
// is it a click outside or a click on an item ?
if (!el.value.contains(e.target as Node) || floating.value.contains(e.target as Node)) {
isOpen.value = false;
}
}
}
// Mouse move listener to close the dropdown when the mouse moves
function onMouseMove() {
if (
el.value &&
!el.value.checkVisibility({
opacityProperty: true,
contentVisibilityAuto: true,
visibilityProperty: true,
})
) {
isOpen.value = false;
}
}
// Intersection observer to close the dropdown when it is not visible
const intersectionObserver = new IntersectionObserver(
([entry]) => {
if (!entry.isIntersecting) {
isOpen.value = false;
}
},
{
root: document.body,
threshold: 0,
}
);
onMounted(() => {
document.addEventListener("click", onClick);
document.addEventListener("mousemove", onMouseMove);
if (el.value) {
intersectionObserver.observe(el.value);
}
});
onBeforeUnmount(() => {
document.removeEventListener("click", onClick);
document.removeEventListener("mousemove", onMouseMove);
intersectionObserver.disconnect();
});
</script>
<template>
<div class="dropdown" ref="el" :class="{ 'align-right': props.align === 'right' }">
<SimpleButton
:is-primary="props.isPrimary"
:label="props.label"
:icon="props.icon"
:is-inline="props.isInline"
@click="isOpen = !isOpen"
ref="reference"
/>
<Transition name="fade">
<div class="items" v-if="isOpen" ref="floating" :style="floatingStyles">
<slot></slot>
</div>
</Transition>
</div>
</template>
<style scoped>
.dropdown {
position: relative;
display: inline-block;
& .items {
position: fixed;
z-index: 9999;
display: flex;
overflow: visible;
width: max-content;
flex-direction: column;
border: solid 1px var(--border-color-normal);
border-radius: var(--border-radius);
background-color: var(--background-color-normal);
box-shadow: 4px 10px 20px var(--background-color-selected);
}
}
</style>