1
1
import { TooltipController as Tooltip } from 'components/TooltipController'
2
- import { TooltipProvider , TooltipWrapper , useTooltip } from 'components/TooltipProvider'
2
+ import { TooltipProvider , TooltipWrapper } from 'components/TooltipProvider'
3
3
import { useEffect , useRef , useState } from 'react'
4
4
import styles from './styles.module.css'
5
5
6
6
function WithProviderMinimal ( ) {
7
- const ref = useRef < HTMLButtonElement > ( null )
8
-
9
7
return (
10
8
< section style = { { marginTop : '100px' } } >
11
9
< p >
12
- < TooltipWrapper forwardRef = { ref } place = "bottom" content = "Shared Global Tooltip" >
13
- < button
14
- // this will not work, must use wrapper `forwardRef`
15
- ref = { ref }
16
- >
17
- Minimal 1
18
- </ button >
10
+ < TooltipWrapper place = "bottom" content = "Shared Global Tooltip" >
11
+ < button > Minimal 1</ button >
19
12
</ TooltipWrapper >
20
13
< TooltipWrapper place = "right" content = "Shared Global Tooltip" >
21
14
< button > Minimal 2</ button >
@@ -26,57 +19,48 @@ function WithProviderMinimal() {
26
19
)
27
20
}
28
21
29
- function WithProviderFullControl ( ) {
30
- const { attach, detach } = useTooltip ( )
31
- const { attach : attach1 , detach : detach1 } = useTooltip ( ) ( 'tooltip-1' )
32
- const { attach : attach2 , detach : detach2 } = useTooltip ( ) ( 'tooltip-2' )
33
- const buttonRef1 = useRef < HTMLButtonElement > ( null )
34
- const buttonRef2 = useRef < HTMLButtonElement > ( null )
35
- const buttonRef3 = useRef < HTMLButtonElement > ( null )
36
- const buttonRef4 = useRef < HTMLButtonElement > ( null )
37
- const buttonRef5 = useRef < HTMLButtonElement > ( null )
38
- const buttonRef6 = useRef < HTMLButtonElement > ( null )
22
+ function WithProviderMultiple ( ) {
23
+ return (
24
+ < section style = { { marginTop : '100px' } } >
25
+ < p >
26
+ < TooltipWrapper tooltipId = "tooltip-1" place = "bottom" >
27
+ < button > Multiple 1</ button >
28
+ </ TooltipWrapper >
29
+ < TooltipWrapper tooltipId = "tooltip-2" place = "right" >
30
+ < button > Multiple 2</ button >
31
+ </ TooltipWrapper >
32
+ </ p >
33
+ < Tooltip id = "tooltip-1" content = "Tooltip 1" />
34
+ < Tooltip id = "tooltip-2" content = "Tooltip 2" />
35
+ </ section >
36
+ )
37
+ }
38
+
39
+ function WithProviderForwardRef ( ) {
40
+ // if access to the element's ref is needed, `TooltipWrapper.forwardRef` must be used
41
+ const ref = useRef < HTMLButtonElement > ( null )
39
42
40
43
useEffect ( ( ) => {
41
- attach ( buttonRef1 , buttonRef2 )
42
- attach1 ( buttonRef3 , buttonRef4 )
43
- attach2 ( buttonRef5 , buttonRef6 )
44
- return ( ) => {
45
- detach ( buttonRef1 , buttonRef2 )
46
- detach1 ( buttonRef3 , buttonRef4 )
47
- detach2 ( buttonRef5 , buttonRef6 )
48
- }
49
- } , [ ] )
44
+ // eslint-disable-next-line no-console
45
+ console . log ( 'Ref can still be accessed: ' , ref . current )
46
+ } )
50
47
51
48
return (
52
49
< section style = { { marginTop : '100px' } } >
53
50
< p >
54
- < button ref = { buttonRef1 } data-tooltip-content = "Shared Global Tooltip" >
55
- Provider 1
56
- </ button >
57
- < button ref = { buttonRef2 } data-tooltip-content = "Shared Global Tooltip" >
58
- Provider 2
59
- </ button >
60
- </ p >
61
- < p >
62
- < button ref = { buttonRef3 } data-tooltip-content = "Shared Tooltip 1" >
63
- Provider 3
64
- </ button >
65
- < button ref = { buttonRef4 } data-tooltip-content = "Shared Tooltip 1" >
66
- Provider 4
67
- </ button >
68
- </ p >
69
- < p >
70
- < button ref = { buttonRef5 } data-tooltip-content = "Shared Tooltip 2" >
71
- Provider 5
72
- </ button >
73
- < button ref = { buttonRef6 } data-tooltip-content = "Shared Tooltip 2" >
74
- Provider 6
75
- </ button >
51
+ < TooltipWrapper forwardRef = { ref } place = "bottom" content = "Shared Global Tooltip" >
52
+ < button
53
+ // this will not work, must use `forwardRef` from wrapper
54
+ ref = { ref }
55
+ >
56
+ Forward ref 1
57
+ </ button >
58
+ </ TooltipWrapper >
59
+ < TooltipWrapper place = "right" content = "Shared Global Tooltip" >
60
+ < button > Forward ref 2</ button >
61
+ </ TooltipWrapper >
76
62
</ p >
77
63
< Tooltip />
78
- < Tooltip id = "tooltip-1" />
79
- < Tooltip id = "tooltip-2" />
80
64
</ section >
81
65
)
82
66
}
@@ -155,7 +139,10 @@ function App() {
155
139
< WithProviderMinimal />
156
140
</ TooltipProvider >
157
141
< TooltipProvider >
158
- < WithProviderFullControl />
142
+ < WithProviderMultiple />
143
+ </ TooltipProvider >
144
+ < TooltipProvider >
145
+ < WithProviderForwardRef />
159
146
</ TooltipProvider >
160
147
</ main >
161
148
)
0 commit comments