Skip to content

Commit cc3f201

Browse files
committed
refactor: update tooltip position feature implementation
1 parent 08775e9 commit cc3f201

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const Tooltip = ({
2424
delayHide = 0,
2525
noArrow,
2626
style: externalStyles,
27-
type = 'fixed',
27+
position,
2828
// props handled by controller
2929
isHtmlContent = false,
3030
content,
@@ -101,21 +101,22 @@ const Tooltip = ({
101101
}
102102
}
103103

104-
const handleMouseMove = (event?: MouseEvent) => {
105-
if (!event) {
104+
const handleTooltipPosition = () => {
105+
if (!position?.x || !position?.y) {
106106
return
107107
}
108+
108109
const virtualElement = {
109110
getBoundingClientRect() {
110111
return {
111-
x: event.clientX,
112-
y: event.clientY,
112+
x: position.x,
113+
y: position.y,
113114
width: 0,
114115
height: 0,
115-
top: event.clientY,
116-
left: event.clientX,
117-
right: event.clientX,
118-
bottom: event.clientY,
116+
top: position.y,
117+
left: position.x,
118+
right: position.x,
119+
bottom: position.y,
119120
}
120121
},
121122
} as Element
@@ -138,11 +139,7 @@ const Tooltip = ({
138139
})
139140
}
140141

141-
const handleClickTooltipAnchor = (event: MouseEvent) => {
142-
if (type === 'free') {
143-
handleMouseMove(event)
144-
}
145-
142+
const handleClickTooltipAnchor = () => {
146143
if (setIsOpen) {
147144
setIsOpen(!isOpen)
148145
} else if (!setIsOpen && isOpen === undefined) {
@@ -184,13 +181,6 @@ const Tooltip = ({
184181
{ event: 'focus', listener: debouncedHandleShowTooltip },
185182
{ event: 'blur', listener: debouncedHandleHideTooltip },
186183
)
187-
188-
if (type === 'float') {
189-
enabledEvents.push({
190-
event: 'mousemove',
191-
listener: (event) => handleMouseMove(event as MouseEvent),
192-
})
193-
}
194184
}
195185

196186
enabledEvents.forEach(({ event, listener }) => {
@@ -209,7 +199,8 @@ const Tooltip = ({
209199
}, [anchorRefs, anchorId, events, delayHide, delayShow])
210200

211201
useEffect(() => {
212-
if (type === 'free') {
202+
if (position?.x && position?.y) {
203+
handleTooltipPosition()
213204
return () => null
214205
}
215206

@@ -243,7 +234,7 @@ const Tooltip = ({
243234
return () => {
244235
mounted = false
245236
}
246-
}, [show, isOpen, anchorId, activeAnchor, content, place, offset, positionStrategy, type])
237+
}, [show, isOpen, anchorId, activeAnchor, content, place, offset, positionStrategy, position])
247238

248239
useEffect(() => {
249240
return () => {

src/components/Tooltip/TooltipTypes.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ElementType, ReactNode, Element, CSSProperties } from 'react'
1+
import type { ElementType, ReactNode, CSSProperties } from 'react'
22

33
export type PlacesType = 'top' | 'right' | 'bottom' | 'left'
44

@@ -26,6 +26,11 @@ export type DataAttribute =
2626
| 'delay-show'
2727
| 'delay-hide'
2828

29+
export interface IPosition {
30+
x?: number
31+
y?: number
32+
}
33+
2934
export interface ITooltip {
3035
className?: string
3136
classNameArrow?: string
@@ -41,11 +46,11 @@ export interface ITooltip {
4146
children?: ChildrenType
4247
events?: EventsType[]
4348
positionStrategy?: PositionStrategy
44-
type?: Type
4549
delayShow?: number
4650
delayHide?: number
4751
noArrow?: boolean
4852
style?: CSSProperties
53+
position?: IPosition
4954
isOpen?: boolean
5055
setIsOpen?: (value: boolean) => void
5156
}

src/components/TooltipController/TooltipController.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const TooltipController = ({
3030
delayHide = 0,
3131
noArrow,
3232
style,
33-
type,
33+
position,
3434
isOpen,
3535
setIsOpen,
3636
}: ITooltipController) => {
@@ -180,7 +180,7 @@ const TooltipController = ({
180180
delayHide: tooltipDelayHide,
181181
noArrow,
182182
style,
183-
type,
183+
position,
184184
isOpen,
185185
setIsOpen,
186186
}

src/components/TooltipController/TooltipControllerTypes.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
ChildrenType,
88
EventsType,
99
PositionStrategy,
10-
Type,
10+
IPosition,
1111
} from 'components/Tooltip/TooltipTypes'
1212

1313
export interface ITooltipController {
@@ -28,7 +28,7 @@ export interface ITooltipController {
2828
delayHide?: number
2929
noArrow?: boolean
3030
style?: CSSProperties
31-
type?: Type
31+
position?: IPosition
3232
isOpen?: boolean
3333
setIsOpen?: (value: boolean) => void
3434
}

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
PositionStrategy,
88
VariantType,
99
WrapperType,
10+
IPosition,
1011
} from './components/Tooltip/TooltipTypes'
1112
import type { ITooltipController } from './components/TooltipController/TooltipControllerTypes'
1213
import type { ITooltipWrapper } from './components/TooltipProvider/TooltipProviderTypes'
@@ -23,4 +24,5 @@ export type {
2324
WrapperType,
2425
ITooltipController as ITooltip,
2526
ITooltipWrapper,
27+
IPosition,
2628
}

0 commit comments

Comments
 (0)