Skip to content

Commit 088203c

Browse files
gabrieljablonskidanielbarion
authored andcommitted
improve examples in dev demo
1 parent f8e99df commit 088203c

File tree

1 file changed

+41
-54
lines changed

1 file changed

+41
-54
lines changed

src/App.tsx

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import { TooltipController as Tooltip } from 'components/TooltipController'
2-
import { TooltipProvider, TooltipWrapper, useTooltip } from 'components/TooltipProvider'
2+
import { TooltipProvider, TooltipWrapper } from 'components/TooltipProvider'
33
import { useEffect, useRef, useState } from 'react'
44
import styles from './styles.module.css'
55

66
function WithProviderMinimal() {
7-
const ref = useRef<HTMLButtonElement>(null)
8-
97
return (
108
<section style={{ marginTop: '100px' }}>
119
<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>
1912
</TooltipWrapper>
2013
<TooltipWrapper place="right" content="Shared Global Tooltip">
2114
<button>Minimal 2</button>
@@ -26,57 +19,48 @@ function WithProviderMinimal() {
2619
)
2720
}
2821

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)
3942

4043
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+
})
5047

5148
return (
5249
<section style={{ marginTop: '100px' }}>
5350
<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>
7662
</p>
7763
<Tooltip />
78-
<Tooltip id="tooltip-1" />
79-
<Tooltip id="tooltip-2" />
8064
</section>
8165
)
8266
}
@@ -155,7 +139,10 @@ function App() {
155139
<WithProviderMinimal />
156140
</TooltipProvider>
157141
<TooltipProvider>
158-
<WithProviderFullControl />
142+
<WithProviderMultiple />
143+
</TooltipProvider>
144+
<TooltipProvider>
145+
<WithProviderForwardRef />
159146
</TooltipProvider>
160147
</main>
161148
)

0 commit comments

Comments
 (0)