-
-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathTooltip.tsx
More file actions
66 lines (59 loc) · 1.81 KB
/
Tooltip.tsx
File metadata and controls
66 lines (59 loc) · 1.81 KB
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
import * as React from 'react'
import * as TooltipPrimitive from '@radix-ui/react-tooltip'
import { twMerge } from 'tailwind-merge'
interface TooltipProps {
children: React.ReactNode
content: React.ReactNode
side?: 'top' | 'right' | 'bottom' | 'left'
align?: 'start' | 'center' | 'end'
delayDuration?: number
className?: string
}
function useIsTouchDevice() {
const [isTouchDevice, setIsTouchDevice] = React.useState(false)
React.useEffect(() => {
setIsTouchDevice(
window.matchMedia('(hover: none)').matches ||
navigator.maxTouchPoints > 0,
)
}, [])
return isTouchDevice
}
export function Tooltip({
children,
content,
side = 'top',
align = 'center',
delayDuration = 200,
className,
}: TooltipProps) {
const isTouchDevice = useIsTouchDevice()
if (!content || isTouchDevice) {
return <>{children}</>
}
return (
<TooltipPrimitive.Provider delayDuration={delayDuration}>
<TooltipPrimitive.Root>
<TooltipPrimitive.Trigger asChild>{children}</TooltipPrimitive.Trigger>
<TooltipPrimitive.Portal>
<TooltipPrimitive.Content
side={side}
align={align}
sideOffset={5}
className={twMerge(
'z-50 rounded-lg px-3 py-2 text-xs',
'bg-gray-900 text-white dark:bg-gray-100 dark:text-gray-900',
'shadow-lg',
'animate-in fade-in-0 zoom-in-95',
'data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95',
className,
)}
>
{content}
<TooltipPrimitive.Arrow className="fill-gray-900 dark:fill-gray-100" />
</TooltipPrimitive.Content>
</TooltipPrimitive.Portal>
</TooltipPrimitive.Root>
</TooltipPrimitive.Provider>
)
}