-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDropdownButton.vue
125 lines (112 loc) · 3.07 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
115
116
117
118
119
120
121
122
123
124
125
<script lang="ts">
export interface DropdownProps {
label?: string;
icon?: string;
isPrimary?: boolean;
align?: "left" | "right";
isInline?: boolean;
}
</script>
<script setup lang="ts">
import { autoUpdate, useFloating } from "@floating-ui/vue";
import { computed, onBeforeUnmount, onMounted, ref } from "vue";
import SimpleButton from "@/components/SimpleButton.vue";
const { label, icon, isPrimary = false, align = "left", isInline } = defineProps<DropdownProps>();
const isOpen = ref(false);
const el = ref<HTMLDivElement>();
const reference = ref<HTMLElement>();
const floating = ref<HTMLDivElement>();
const { floatingStyles } = useFloating(reference, floating, {
placement: 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,
}
);
const transitionName = computed(() => {
const isVisible = el.value?.checkVisibility({
opacityProperty: true,
contentVisibilityAuto: true,
visibilityProperty: true,
});
return isVisible ? "fade" : "";
});
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': align === 'right' }">
<SimpleButton
:is-primary="isPrimary"
:label="label"
:icon="icon"
:is-inline="isInline"
@click="isOpen = !isOpen"
ref="reference"
/>
<Transition :name="transitionName">
<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 var(--stroke-width-md) var(--color-stroke-background-primary);
border-radius: var(--radius-xs);
background-color: var(--color-background-primary);
box-shadow: 4px 10px 20px var(--color-shadow);
}
}
</style>