|
1
|
"use client" |
|
2
|
|
|
3
|
import { Tooltip as RadixTooltip } from "radix-ui" |
|
4
|
import * as React from "react" |
|
5
|
|
|
6
|
import { clx } from "@/utils/clx" |
|
7
|
|
|
8
|
interface TooltipProps |
|
9
|
extends Omit<RadixTooltip.TooltipContentProps, "content" | "onClick">, |
|
10
|
Pick< |
|
11
|
RadixTooltip.TooltipProps, |
|
12
|
"open" | "defaultOpen" | "onOpenChange" | "delayDuration" |
|
13
|
> { |
|
14
|
content: React.ReactNode |
|
15
|
onClick?: React.MouseEventHandler<HTMLButtonElement> |
|
16
|
side?: "bottom" | "left" | "top" | "right" |
|
17
|
maxWidth?: number |
|
18
|
} |
|
19
|
|
|
20
|
/** |
|
21
|
* This component is based on the [Radix UI Tooltip](https://www.radix-ui.com/primitives/docs/components/tooltip) primitive. |
|
22
|
* |
|
23
|
* @excludeExternal |
|
24
|
*/ |
|
25
|
const Tooltip = ({ |
|
26
|
/** |
|
27
|
* The element to trigger the tooltip. |
|
28
|
* |
|
29
|
* @keep |
|
30
|
*/ |
|
31
|
children, |
|
32
|
/** |
|
33
|
* The content to display in the tooltip. |
|
34
|
*/ |
|
35
|
content, |
|
36
|
/** |
|
37
|
* Whether the tooltip is currently open. |
|
38
|
* |
|
39
|
* @keep |
|
40
|
*/ |
|
41
|
open, |
|
42
|
/** |
|
43
|
* Whether the tooltip is open by default. |
|
44
|
* |
|
45
|
* @keep |
|
46
|
*/ |
|
47
|
defaultOpen, |
|
48
|
/** |
|
49
|
* A function that is called when the tooltip's open state changes. |
|
50
|
* |
|
51
|
* @keep |
|
52
|
*/ |
|
53
|
onOpenChange, |
|
54
|
/** |
|
55
|
* The time in milliseconds to delay the tooltip's appearance. |
|
56
|
* |
|
57
|
* @keep |
|
58
|
*/ |
|
59
|
delayDuration, |
|
60
|
/** |
|
61
|
* The maximum width of the tooltip. |
|
62
|
*/ |
|
63
|
maxWidth = 220, |
|
64
|
className, |
|
65
|
/** |
|
66
|
* The side to position the tooltip. |
|
67
|
* |
|
68
|
* @defaultValue top |
|
69
|
*/ |
|
70
|
side, |
|
71
|
/** |
|
72
|
* The distance in pixels between the tooltip and its trigger. |
|
73
|
* |
|
74
|
* @keep |
|
75
|
*/ |
|
76
|
sideOffset = 8, |
|
77
|
/** |
|
78
|
* A function that is triggered when the tooltip is clicked. |
|
79
|
*/ |
|
80
|
onClick, |
|
81
|
...props |
|
82
|
}: TooltipProps) => { |
|
83
|
return ( |
|
84
|
<RadixTooltip.Root |
|
85
|
open={open} |
|
86
|
defaultOpen={defaultOpen} |
|
87
|
onOpenChange={onOpenChange} |
|
88
|
delayDuration={delayDuration} |
|
89
|
> |
|
90
|
<RadixTooltip.Trigger onClick={onClick} asChild> |
|
91
|
{children} |
|
92
|
</RadixTooltip.Trigger> |
|
93
|
<RadixTooltip.Portal> |
|
94
|
<RadixTooltip.Content |
|
95
|
side={side} |
|
96
|
sideOffset={sideOffset} |
|
97
|
align="center" |
|
98
|
className={clx( |
|
99
|
"txt-compact-xsmall text-ui-fg-subtle bg-ui-bg-base shadow-elevation-tooltip rounded-lg px-2.5 py-1", |
|
100
|
"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 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2", |
|
101
|
className |
|
102
|
)} |
|
103
|
{...props} |
|
104
|
style={{ ...props.style, maxWidth }} |
|
105
|
> |
|
106
|
{content} |
|
107
|
</RadixTooltip.Content> |
|
108
|
</RadixTooltip.Portal> |
|
109
|
</RadixTooltip.Root> |
|
110
|
) |
|
111
|
} |
|
112
|
|
|
113
|
interface TooltipProviderProps extends RadixTooltip.TooltipProviderProps {} |
|
114
|
|
|
115
|
const TooltipProvider = ({ children, delayDuration = 100, skipDelayDuration = 300, ...props }: TooltipProviderProps) => { |
|
116
|
return ( |
|
117
|
<RadixTooltip.TooltipProvider delayDuration={delayDuration} skipDelayDuration={skipDelayDuration} {...props}> |
|
118
|
{children} |
|
119
|
</RadixTooltip.TooltipProvider> |
|
120
|
) |
|
121
|
} |
|
122
|
|
|
123
|
|
|
124
|
export { Tooltip, TooltipProvider } |