This repository was archived by the owner on Jun 27, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathButtonWithDropdown.vue
89 lines (76 loc) · 2.08 KB
/
ButtonWithDropdown.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
<template>
<OnClickOutside :do="hide">
<div class="relative">
<button
ref="button"
type="button"
:dusk="dusk"
:disabled="disabled"
class="w-full bg-white border rounded-md shadow-sm px-4 py-2 inline-flex justify-center text-sm font-medium text-gray-700 hover:bg-gray-50 focus:ring-2 focus:ring-indigo-500 dark:bg-gray-700 dark:hover:bg-gray-600 dark:border-gray-500"
:class="{'border-green-300': active, 'border-gray-300': !active, 'cursor-not-allowed': disabled }"
aria-haspopup="true"
@click.prevent="toggle"
>
<slot name="button" />
</button>
<div
v-show="opened"
ref="tooltip"
class="absolute z-10"
>
<div class="mt-2 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5 dark:bg-gray-700">
<slot />
</div>
</div>
</div>
</OnClickOutside>
</template>
<script setup>
import OnClickOutside from "./OnClickOutside.vue";
import { createPopper } from "@popperjs/core/lib/popper-lite";
import preventOverflow from "@popperjs/core/lib/modifiers/preventOverflow";
import flip from "@popperjs/core/lib/modifiers/flip";
import { ref, watch, onMounted } from "vue";
const props = defineProps({
placement: {
type: String,
default: "bottom-start",
required: false,
},
active: {
type: Boolean,
default: false,
required: false,
},
dusk: {
type: String,
default: null,
required: false,
},
disabled: {
type: Boolean,
default: false,
required: false,
},
});
const opened = ref(false);
const popper = ref(null);
function toggle() {
opened.value = !opened.value;
}
function hide() {
opened.value = false;
}
watch(opened, () => {
popper.value.update();
});
const button = ref(null);
const tooltip = ref(null);
onMounted(() => {
popper.value = createPopper(button.value, tooltip.value, {
placement: props.placement,
modifiers: [flip, preventOverflow],
});
});
defineExpose({ hide });
</script>