@@ -3,35 +3,58 @@ import Popover from "@material-ui/core/Popover"
3
3
import { makeStyles } from "@material-ui/core/styles"
4
4
import HelpIcon from "@material-ui/icons/HelpOutline"
5
5
import OpenInNewIcon from "@material-ui/icons/OpenInNew"
6
- import { useState } from "react"
6
+ import React , { createContext , useContext , useState } from "react"
7
7
import { Stack } from "../Stack/Stack"
8
8
9
+ type Icon = typeof HelpIcon
10
+
9
11
type Size = "small" | "medium"
10
12
export interface HelpTooltipProps {
11
13
// Useful to test on storybook
12
14
open ?: boolean
13
15
size ?: Size
14
16
}
15
17
18
+ const HelpTooltipContext = createContext < { open : boolean ; onClose : ( ) => void } | undefined > ( undefined )
19
+
20
+ const useHelpTooltip = ( ) => {
21
+ const helpTooltipContext = useContext ( HelpTooltipContext )
22
+
23
+ if ( ! helpTooltipContext ) {
24
+ throw new Error ( "This hook should be used in side of the HelpTooltipContext." )
25
+ }
26
+
27
+ return helpTooltipContext
28
+ }
29
+
16
30
export const HelpTooltip : React . FC < HelpTooltipProps > = ( { children, open, size = "medium" } ) => {
17
31
const styles = useStyles ( { size } )
18
32
const [ anchorEl , setAnchorEl ] = useState < HTMLButtonElement | null > ( null )
19
33
open = open ?? Boolean ( anchorEl )
20
34
const id = open ? "help-popover" : undefined
21
35
36
+ const onClose = ( ) => {
37
+ setAnchorEl ( null )
38
+ }
39
+
22
40
return (
23
41
< >
24
- < button aria-describedby = { id } className = { styles . button } onClick = { ( event ) => setAnchorEl ( event . currentTarget ) } >
42
+ < button
43
+ aria-describedby = { id }
44
+ className = { styles . button }
45
+ onClick = { ( event ) => {
46
+ event . stopPropagation ( )
47
+ setAnchorEl ( event . currentTarget )
48
+ } }
49
+ >
25
50
< HelpIcon className = { styles . icon } />
26
51
</ button >
27
52
< Popover
28
53
classes = { { paper : styles . popoverPaper } }
29
54
id = { id }
30
55
open = { open }
31
56
anchorEl = { anchorEl }
32
- onClose = { ( ) => {
33
- setAnchorEl ( null )
34
- } }
57
+ onClose = { onClose }
35
58
anchorOrigin = { {
36
59
vertical : "bottom" ,
37
60
horizontal : "left" ,
@@ -41,7 +64,7 @@ export const HelpTooltip: React.FC<HelpTooltipProps> = ({ children, open, size =
41
64
horizontal : "left" ,
42
65
} }
43
66
>
44
- { children }
67
+ < HelpTooltipContext . Provider value = { { open , onClose } } > { children } </ HelpTooltipContext . Provider >
45
68
</ Popover >
46
69
</ >
47
70
)
@@ -70,6 +93,25 @@ export const HelpTooltipLink: React.FC<{ href: string }> = ({ children, href })
70
93
)
71
94
}
72
95
96
+ export const HelpTooltipAction : React . FC < { icon : Icon ; onClick : ( ) => void } > = ( { children, icon : Icon , onClick } ) => {
97
+ const styles = useStyles ( )
98
+ const tooltip = useHelpTooltip ( )
99
+
100
+ return (
101
+ < button
102
+ className = { styles . action }
103
+ onClick = { ( event ) => {
104
+ event . stopPropagation ( )
105
+ onClick ( )
106
+ tooltip . onClose ( )
107
+ } }
108
+ >
109
+ < Icon className = { styles . actionIcon } />
110
+ { children }
111
+ </ button >
112
+ )
113
+ }
114
+
73
115
export const HelpTooltipLinksGroup : React . FC = ( { children } ) => {
74
116
const styles = useStyles ( )
75
117
@@ -110,11 +152,12 @@ const useStyles = makeStyles((theme) => ({
110
152
padding : 0 ,
111
153
border : 0 ,
112
154
background : "transparent" ,
113
- color : theme . palette . text . secondary ,
155
+ color : theme . palette . text . primary ,
156
+ opacity : 0.5 ,
114
157
cursor : "pointer" ,
115
158
116
159
"&:hover" : {
117
- color : theme . palette . text . primary ,
160
+ opacity : 0.75 ,
118
161
} ,
119
162
} ,
120
163
@@ -156,4 +199,22 @@ const useStyles = makeStyles((theme) => ({
156
199
linksGroup : {
157
200
marginTop : theme . spacing ( 2 ) ,
158
201
} ,
202
+
203
+ action : {
204
+ display : "flex" ,
205
+ alignItems : "center" ,
206
+ background : "none" ,
207
+ border : 0 ,
208
+ color : theme . palette . primary . light ,
209
+ padding : 0 ,
210
+ cursor : "pointer" ,
211
+ fontSize : 14 ,
212
+ } ,
213
+
214
+ actionIcon : {
215
+ color : "inherit" ,
216
+ width : 14 ,
217
+ height : 14 ,
218
+ marginRight : theme . spacing ( 1 ) ,
219
+ } ,
159
220
} ) )
0 commit comments