-
Notifications
You must be signed in to change notification settings - Fork 0
/
Floating.vue
259 lines (234 loc) · 5.88 KB
/
Floating.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<script setup lang="ts">
// This component is used to render a floating tooltip by @floating-ui/vue.
// Usage is almost the same as vue-tippy.
//
// <Floating placement="top">
// <template #default>
// <button>
// Button with floating tooltip
// </button>
// </template>
// <template #content>
// <div style="max-width: 300px;">
// My Long Long Long Long Long Long Long Long Long Long Tooltip
// </div>
// </template>
// </Floating>
//
// props
//
// - placement: string, see https://floating-ui.com/docs/usefloating#placement
// - interactive: keeping the tooltip open when the user hovers over the tooltip, default false
// - delay: [delayEnter, delayLeave] for hover, default 0 (means [0, 0])
// - arrow: boolean, default true
// - theme: string as data-theme, default 'light'.
// - trigger: boolean to control the tooltip visibility.
// - onShow: () => void, callback when the tooltip is shown.
// - onHide: () => void, callback when the tooltip is hidden.
//
// theme
//
// <Floating theme="warning">
// ...
//
// <style>
// .floating-wrapper[data-theme~="warning"] {
// background-color: yellow;
// color: red;
// border-color: red;
// }
// .floating-arrow[data-theme~="warning"] { ... }
// .floating-content[data-theme~="warning"] { ... }
import { ref, watch, computed, toRefs } from "vue";
import { useElementHover } from "@vueuse/core";
import * as floating from "@floating-ui/vue";
import type { Placement } from "@floating-ui/vue";
// interface ----
type OnShowFn = () => void;
type OnHideFn = () => void;
type Props = {
placement?: Placement;
interactive?: boolean;
delay?: number | [number, number];
arrow?: boolean;
theme?: string;
trigger?: boolean;
onShow?: OnShowFn;
onHide?: OnHideFn;
};
const props = withDefaults(defineProps<Props>(), {
placement: "top",
interactive: false,
delay: 0,
arrow: true,
theme: "light",
trigger: undefined,
});
const slots = defineSlots();
// define varibales ----
const {
placement: placementProp,
interactive,
delay,
arrow: arrowProp,
theme: themeProp,
trigger: triggerRef,
} = toRefs(props);
const targetRef = ref();
const floatingRef = ref();
const arrowRef = ref();
// define floating
const {
floatingStyles,
middlewareData,
update,
placement: placed,
} = floating.useFloating(targetRef, floatingRef, {
whileElementsMounted: floating.autoUpdate,
placement: placementProp,
middleware: [
floating.offset(6),
floating.flip(),
floating.shift({ padding: 5 }),
floating.arrow({ element: arrowRef }),
],
});
const delayOptions = computed(() => {
const [delayEnter, delayLeave] = Array.isArray(delay.value)
? delay.value
: [delay.value, delay.value];
return { delayEnter, delayLeave };
});
// define hover states ----
const isTargetHovered = useElementHover(targetRef, {
delayEnter: delayOptions.value.delayEnter,
delayLeave: delayOptions.value.delayLeave + (interactive.value ? 100 : 0),
});
const isTooltipHovered = useElementHover(floatingRef, {
delayEnter: 0, // keep tooltip open when hovering over the tooltip
delayLeave: delayOptions.value.delayLeave,
});
const isTriggered = computed((): boolean => {
const triggered = triggerRef.value ?? isTargetHovered.value;
if (interactive.value) {
return triggered || isTooltipHovered.value;
} else {
return triggered;
}
});
watch(isTriggered, (show) => {
if (show) {
update();
props.onShow?.();
} else {
props.onHide?.();
}
});
// define styles ----
const placed0 = computed(() => {
return placed.value.split("-")[0];
});
const arrowStyles = computed(() => {
const arrow = middlewareData.value?.arrow;
if (!arrow || !arrowProp.value) {
return {};
}
return {
top: arrow.y != null ? `${arrow.y}px` : "",
left: arrow.x != null ? `${arrow.x}px` : "",
right: "",
bottom: "",
};
});
</script>
<template>
<span ref="targetRef">
<slot />
</span>
<Teleport v-if="slots.content" to="body">
<div
v-if="isTriggered"
ref="floatingRef"
class="floating-wrapper"
:data-theme="themeProp"
:data-placement="placed0"
:style="floatingStyles"
>
<div class="floating-content" :data-theme="themeProp">
<slot name="content" />
</div>
<div
v-if="arrowProp"
ref="arrowRef"
class="floating-arrow"
:data-theme="themeProp"
:style="arrowStyles"
/>
</div>
</Teleport>
</template>
<style>
/* Want to use global scope css, so not using `scoped`.
* And I want to define it near the component implementation instead of external css file.
*/
.floating-wrapper {
background-color: white;
border: 1px solid black;
border-radius: 3px;
z-index: 10000;
.floating-content {
display: grid;
background-color: white;
z-index: 10002;
}
.floating-arrow {
position: absolute;
background-color: white;
border: 0px solid black;
width: 8px;
height: 8px;
transform: rotate(45deg);
box-sizing: border-box;
z-index: 10001;
}
}
.floating-wrapper[data-theme~="light"] {
background-color: white;
border: 1px solid black;
.floating-content {
background-color: white;
}
.floating-arrow {
background-color: white;
border: 0px solid black;
}
}
.floating-wrapper[data-placement~="top"] {
box-shadow: 0 4px 4px rgba(0, 0, 0, 0.2);
.floating-arrow {
border-width: 0 1px 1px 0;
bottom: -5px !important;
}
}
.floating-wrapper[data-placement~="right"] {
box-shadow: -4px 0 4px rgba(0, 0, 0, 0.2);
.floating-arrow {
border-width: 0 0 1px 1px;
left: -5px !important;
}
}
.floating-wrapper[data-placement~="bottom"] {
box-shadow: 0 -4px 4px rgba(0, 0, 0, 0.2);
.floating-arrow {
border-width: 1px 0 0 1px;
top: -5px !important;
}
}
.floating-wrapper[data-placement~="left"] {
box-shadow: 4px 0 4px rgba(0, 0, 0, 0.2);
.floating-arrow {
border-width: 1px 1px 0 0;
right: -5px !important;
}
}
</style>