-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
TooltipProvider.vue
98 lines (89 loc) · 2.63 KB
/
TooltipProvider.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
<script lang="ts">
import type { Ref } from 'vue'
import { createContext, useForwardExpose } from '@/shared'
interface TooltipProviderContext {
isOpenDelayed: Ref<boolean>
delayDuration: Ref<number>
onOpen(): void
onClose(): void
isPointerInTransitRef: Ref<boolean>
disableHoverableContent: Ref<boolean>
disableClosingTrigger: Ref<boolean>
disabled: Ref<boolean>
ignoreNonKeyboardFocus: Ref<boolean>
}
export const [injectTooltipProviderContext, provideTooltipProviderContext]
= createContext<TooltipProviderContext>('TooltipProvider')
export interface TooltipProviderProps {
/**
* The duration from when the pointer enters the trigger until the tooltip gets opened.
* @defaultValue 700
*/
delayDuration?: number
/**
* How much time a user has to enter another trigger without incurring a delay again.
* @defaultValue 300
*/
skipDelayDuration?: number
/**
* When `true`, trying to hover the content will result in the tooltip closing as the pointer leaves the trigger.
* @defaultValue false
*/
disableHoverableContent?: boolean
/**
* When `true`, clicking on trigger will not close the content.
* @defaultValue false
*/
disableClosingTrigger?: boolean
/**
* When `true`, disable tooltip
* @defaultValue false
*/
disabled?: boolean
/**
* Prevent the tooltip from opening if the focus did not come from
* the keyboard by matching against the `:focus-visible` selector.
* This is useful if you want to avoid opening it when switching
* browser tabs or closing a dialog.
* @defaultValue false
*/
ignoreNonKeyboardFocus?: boolean
}
</script>
<script setup lang="ts">
import { useTimeoutFn } from '@vueuse/shared'
import { ref, toRefs } from 'vue'
const props = withDefaults(defineProps<TooltipProviderProps>(), {
delayDuration: 700,
skipDelayDuration: 300,
disableHoverableContent: false,
ignoreNonKeyboardFocus: false,
})
const { delayDuration, skipDelayDuration, disableHoverableContent, disableClosingTrigger, ignoreNonKeyboardFocus, disabled } = toRefs(props)
useForwardExpose()
const isOpenDelayed = ref(true)
// Reset the inTransit state if idle/scrolled.
const isPointerInTransitRef = ref(false)
const { start: startTimer, stop: clearTimer } = useTimeoutFn(() => {
isOpenDelayed.value = true
}, skipDelayDuration, { immediate: false })
provideTooltipProviderContext({
isOpenDelayed,
delayDuration,
onOpen() {
clearTimer()
isOpenDelayed.value = false
},
onClose() {
startTimer()
},
isPointerInTransitRef,
disableHoverableContent,
disableClosingTrigger,
disabled,
ignoreNonKeyboardFocus,
})
</script>
<template>
<slot />
</template>