/
tooltip.tsx
175 lines (159 loc) · 4.45 KB
/
tooltip.tsx
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
import { popperCSSVars } from "@chakra-ui/popper"
import { Portal, PortalProps } from "@chakra-ui/portal"
import {
chakra,
forwardRef,
HTMLChakraProps,
omitThemingProps,
ThemingProps,
useStyleConfig,
useTheme,
} from "@chakra-ui/system"
import { isString, omit, pick, __DEV__, getCSSVar } from "@chakra-ui/utils"
import { VisuallyHidden } from "@chakra-ui/visually-hidden"
import { AnimatePresence, motion } from "framer-motion"
import * as React from "react"
import { scale } from "./tooltip.transition"
import { useTooltip, UseTooltipProps } from "./use-tooltip"
export interface TooltipProps
extends HTMLChakraProps<"div">,
ThemingProps<"Tooltip">,
UseTooltipProps {
/**
* The react component to use as the
* trigger for the tooltip
*/
children: React.ReactNode
/**
* The label of the tooltip
*/
label?: React.ReactNode
/**
* The accessible, human friendly label to use for
* screen readers.
*
* If passed, tooltip will show the content `label`
* but expose only `aria-label` to assistive technologies
*/
"aria-label"?: string
/**
* If `true`, the tooltip will wrap its children
* in a `<span/>` with `tabIndex=0`
*/
shouldWrapChildren?: boolean
/**
* If `true`, the tooltip will show an arrow tip
*/
hasArrow?: boolean
/**
* Props to be forwarded to the portal component
*/
portalProps?: Pick<PortalProps, "appendToParentPortal" | "containerRef">
}
const StyledTooltip = chakra(motion.div)
/**
* Tooltips display informative text when users hover, focus on, or tap an element.
*
* @see Docs https://chakra-ui.com/docs/overlay/tooltip
* @see WAI-ARIA https://www.w3.org/TR/wai-aria-practices/#tooltip
*/
export const Tooltip = forwardRef<TooltipProps, "div">((props, ref) => {
const styles = useStyleConfig("Tooltip", props)
const ownProps = omitThemingProps(props)
const theme = useTheme()
const {
children,
label,
shouldWrapChildren,
"aria-label": ariaLabel,
hasArrow,
bg,
portalProps,
...rest
} = ownProps
if (bg) {
styles.bg = bg
styles[popperCSSVars.arrowBg.var] = getCSSVar(theme, "colors", bg)
}
const tooltip = useTooltip({ ...rest, direction: theme.direction })
const shouldWrap = isString(children) || shouldWrapChildren
let trigger: React.ReactElement
if (shouldWrap) {
trigger = (
<chakra.span tabIndex={0} {...tooltip.getTriggerProps()}>
{children}
</chakra.span>
)
} else {
/**
* Ensure tooltip has only one child node
*/
const child = React.Children.only(children) as React.ReactElement & {
ref?: React.Ref<any>
}
trigger = React.cloneElement(
child,
tooltip.getTriggerProps(child.props, child.ref),
)
}
const hasAriaLabel = !!ariaLabel
const _tooltipProps = tooltip.getTooltipProps({}, ref)
const tooltipProps = hasAriaLabel
? omit(_tooltipProps, ["role", "id"])
: _tooltipProps
const hiddenProps = pick(_tooltipProps, ["role", "id"])
/**
* If the `label` is empty, there's no
* point showing the tooltip. Let's simply return back the children
*/
if (!label) {
return <>{children}</>
}
return (
<>
{trigger}
<AnimatePresence>
{tooltip.isOpen && (
<Portal {...portalProps}>
<chakra.div
{...tooltip.getTooltipPositionerProps()}
__css={{
zIndex: styles.zIndex,
pointerEvents: "none",
}}
>
<StyledTooltip
variants={scale}
{...(tooltipProps as any)}
initial="exit"
animate="enter"
exit="exit"
__css={styles}
>
{label}
{hasAriaLabel && (
<VisuallyHidden {...hiddenProps}>{ariaLabel}</VisuallyHidden>
)}
{hasArrow && (
<chakra.div
data-popper-arrow
className="chakra-tooltip__arrow-wrapper"
>
<chakra.div
data-popper-arrow-inner
className="chakra-tooltip__arrow"
__css={{ bg: styles.bg }}
/>
</chakra.div>
)}
</StyledTooltip>
</chakra.div>
</Portal>
)}
</AnimatePresence>
</>
)
})
if (__DEV__) {
Tooltip.displayName = "Tooltip"
}