Skip to content

Commit 83a9181

Browse files
refactor: improve internal provider api
also no longer using `useId()`
1 parent 377c29d commit 83a9181

File tree

5 files changed

+41
-57
lines changed

5 files changed

+41
-57
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const Tooltip = ({
4545
const wasShowing = useRef(false)
4646
const [calculatingPosition, setCalculatingPosition] = useState(false)
4747
const lastFloatPosition = useRef<IPosition | null>(null)
48-
const { anchorRefs, setActiveAnchor: setProviderActiveAnchor } = useTooltip()(id)
48+
const { anchorRefs, setActiveAnchor: setProviderActiveAnchor } = useTooltip(id)
4949
const [activeAnchor, setActiveAnchor] = useState<React.RefObject<HTMLElement>>({ current: null })
5050
const hoveringTooltip = useRef(false)
5151

src/components/TooltipController/TooltipController.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const TooltipController = ({
4949
const [tooltipWrapper, setTooltipWrapper] = useState<WrapperType>(wrapper)
5050
const [tooltipEvents, setTooltipEvents] = useState(events)
5151
const [tooltipPositionStrategy, setTooltipPositionStrategy] = useState(positionStrategy)
52-
const { anchorRefs, activeAnchor } = useTooltip()(id)
52+
const { anchorRefs, activeAnchor } = useTooltip(id)
5353

5454
const getDataAttributesFromAnchorElement = (elementReference: HTMLElement) => {
5555
const dataAttributes = elementReference?.getAttributeNames().reduce((acc, name) => {

src/components/TooltipProvider/TooltipProvider.tsx

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,38 @@ import React, {
33
PropsWithChildren,
44
useCallback,
55
useContext,
6-
useId,
76
useMemo,
87
useState,
98
} from 'react'
109

11-
import type {
12-
AnchorRef,
13-
TooltipContextData,
14-
TooltipContextDataWrapper,
15-
} from './TooltipProviderTypes'
10+
import type { AnchorRef, TooltipContextData } from './TooltipProviderTypes'
11+
12+
const DEFAULT_TOOLTIP_ID = 'DEFAULT_TOOLTIP_ID'
1613

1714
const defaultContextData: TooltipContextData = {
18-
anchorRefs: new Set(),
19-
activeAnchor: { current: null },
20-
attach: () => {
21-
/* attach anchor element */
22-
},
23-
detach: () => {
24-
/* detach anchor element */
25-
},
26-
setActiveAnchor: () => {
27-
/* set active anchor */
28-
},
15+
getTooltipData: () => ({
16+
anchorRefs: new Set(),
17+
activeAnchor: { current: null },
18+
attach: () => {
19+
/* attach anchor element */
20+
},
21+
detach: () => {
22+
/* detach anchor element */
23+
},
24+
setActiveAnchor: () => {
25+
/* set active anchor */
26+
},
27+
}),
2928
}
3029

31-
const defaultContextWrapper = Object.assign(() => defaultContextData, defaultContextData)
32-
const TooltipContext = createContext<TooltipContextDataWrapper>(defaultContextWrapper)
30+
const TooltipContext = createContext<TooltipContextData>(defaultContextData)
3331

3432
const TooltipProvider: React.FC<PropsWithChildren> = ({ children }) => {
35-
const defaultTooltipId = useId()
3633
const [anchorRefMap, setAnchorRefMap] = useState<Record<string, Set<AnchorRef>>>({
37-
[defaultTooltipId]: new Set(),
34+
[DEFAULT_TOOLTIP_ID]: new Set(),
3835
})
3936
const [activeAnchorMap, setActiveAnchorMap] = useState<Record<string, AnchorRef>>({
40-
[defaultTooltipId]: { current: null },
37+
[DEFAULT_TOOLTIP_ID]: { current: null },
4138
})
4239

4340
const attach = (tooltipId: string, ...refs: AnchorRef[]) => {
@@ -74,37 +71,27 @@ const TooltipProvider: React.FC<PropsWithChildren> = ({ children }) => {
7471
}
7572

7673
const getTooltipData = useCallback(
77-
(tooltipId?: string) => ({
78-
anchorRefs: anchorRefMap[tooltipId ?? defaultTooltipId] ?? new Set(),
79-
activeAnchor: activeAnchorMap[tooltipId ?? defaultTooltipId] ?? { current: null },
80-
attach: (...refs: AnchorRef[]) => attach(tooltipId ?? defaultTooltipId, ...refs),
81-
detach: (...refs: AnchorRef[]) => detach(tooltipId ?? defaultTooltipId, ...refs),
82-
setActiveAnchor: (ref: AnchorRef) => setActiveAnchor(tooltipId ?? defaultTooltipId, ref),
74+
(tooltipId = DEFAULT_TOOLTIP_ID) => ({
75+
anchorRefs: anchorRefMap[tooltipId] ?? new Set(),
76+
activeAnchor: activeAnchorMap[tooltipId] ?? { current: null },
77+
attach: (...refs: AnchorRef[]) => attach(tooltipId, ...refs),
78+
detach: (...refs: AnchorRef[]) => detach(tooltipId, ...refs),
79+
setActiveAnchor: (ref: AnchorRef) => setActiveAnchor(tooltipId, ref),
8380
}),
84-
[defaultTooltipId, anchorRefMap, activeAnchorMap, attach, detach],
81+
[anchorRefMap, activeAnchorMap, attach, detach],
8582
)
8683

8784
const context = useMemo(() => {
88-
const contextData: TooltipContextData = getTooltipData(defaultTooltipId)
89-
const contextWrapper = Object.assign(
90-
(tooltipId?: string) => getTooltipData(tooltipId),
91-
contextData,
92-
)
93-
return contextWrapper
85+
return {
86+
getTooltipData,
87+
}
9488
}, [getTooltipData])
9589

9690
return <TooltipContext.Provider value={context}>{children}</TooltipContext.Provider>
9791
}
9892

99-
/*
100-
// this will use the "global" tooltip (same as `useTooltip()()`)
101-
const { anchorRefs, attach, detach } = useTooltip()
102-
103-
// this will use the tooltip with id `tooltip-id`
104-
const { anchorRefs, attach, detach } = useTooltip()('tooltip-id')
105-
*/
106-
export function useTooltip() {
107-
return useContext(TooltipContext)
93+
export function useTooltip(tooltipId = DEFAULT_TOOLTIP_ID) {
94+
return useContext(TooltipContext).getTooltipData(tooltipId)
10895
}
10996

11097
export default TooltipProvider

src/components/TooltipProvider/TooltipProviderTypes.d.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ import type { ITooltipController } from 'components/TooltipController/TooltipCon
44
export type AnchorRef = RefObject<HTMLElement>
55

66
export interface TooltipContextData {
7-
anchorRefs: Set<AnchorRef>
8-
activeAnchor: AnchorRef
9-
attach: (...refs: AnchorRef[]) => void
10-
detach: (...refs: AnchorRef[]) => void
11-
setActiveAnchor: (ref: AnchorRef) => void
12-
}
13-
14-
export type TooltipContextDataWrapper = TooltipContextData & {
15-
// This means the context is a callable object
16-
(tooltipId?: string): TooltipContextData
7+
getTooltipData: (tooltipId?: string) => {
8+
anchorRefs: Set<AnchorRef>
9+
activeAnchor: AnchorRef
10+
attach: (...refs: AnchorRef[]) => void
11+
detach: (...refs: AnchorRef[]) => void
12+
setActiveAnchor: (ref: AnchorRef) => void
13+
}
1714
}
1815

1916
export interface ITooltipWrapper {

src/components/TooltipProvider/TooltipWrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const TooltipWrapper = ({
1818
delayShow,
1919
delayHide,
2020
}: ITooltipWrapper) => {
21-
const { attach, detach } = useTooltip()(tooltipId)
21+
const { attach, detach } = useTooltip(tooltipId)
2222
const anchorRef = useRef<HTMLElement | null>(null)
2323

2424
useEffect(() => {

0 commit comments

Comments
 (0)