-
-
Notifications
You must be signed in to change notification settings - Fork 533
feat: add v4's float
effect (follow mouse position)
#861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4fb2db1
feat: effect float POC
gabrieljablonski 08775e9
feat[wip]: add type of tooltip as prop
danielbarion cc3f201
refactor: update tooltip position feature implementation
danielbarion eed1200
test: add tests to the new feature
danielbarion f028426
chore: add example into app.tsx about position feature
danielbarion 8815eed
docs: update project readme
danielbarion dc4d404
fix: pad arrow position to avoid overflow
gabrieljablonski c2c222a
refactor: avoid eslint `no-empty-function` warning
gabrieljablonski 9f5311a
chore: remove manual `float` example from dev demo
gabrieljablonski 88465f8
feat: close `click` tooltip on click outside
gabrieljablonski fae8796
feat: use `delayHide` on `click` tooltip
gabrieljablonski 7859ae3
refactor: `position` must have `x` and `y`
gabrieljablonski 49fdb26
feat: add `float` mode (follow mouse)
gabrieljablonski 7f290e4
test: account for added arrow padding
gabrieljablonski 1e9ddd0
refactor: var naming
gabrieljablonski e8058a6
docs: `position`/`float` props and general improvements
gabrieljablonski f579913
Merge branch 'master' into tooltip-effect-float
gabrieljablonski c1d9ca3
refactor: use let instead of state for lastFloatPosition variable
danielbarion 6a9a01d
refactor: `useRef()` for `lastFloatPosition`
gabrieljablonski c875241
chore: bump project version and docs package
gabrieljablonski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,9 @@ import { TooltipContent } from 'components/TooltipContent' | |
import { useTooltip } from 'components/TooltipProvider' | ||
import { computeTooltipPosition } from '../../utils/compute-positions' | ||
import styles from './styles.module.css' | ||
import type { ITooltip } from './TooltipTypes' | ||
import type { IPosition, ITooltip } from './TooltipTypes' | ||
|
||
let lastFloatPosition: IPosition | null = null | ||
|
||
const Tooltip = ({ | ||
// props | ||
|
@@ -22,8 +24,10 @@ const Tooltip = ({ | |
children = null, | ||
delayShow = 0, | ||
delayHide = 0, | ||
float = false, | ||
noArrow, | ||
style: externalStyles, | ||
position, | ||
// props handled by controller | ||
isHtmlContent = false, | ||
content, | ||
|
@@ -100,14 +104,71 @@ const Tooltip = ({ | |
} | ||
} | ||
|
||
const handleTooltipPosition = ({ x, y }: IPosition) => { | ||
const virtualElement = { | ||
getBoundingClientRect() { | ||
return { | ||
x, | ||
y, | ||
width: 0, | ||
height: 0, | ||
top: y, | ||
left: x, | ||
right: x, | ||
bottom: y, | ||
} | ||
}, | ||
} as Element | ||
setCalculatingPosition(true) | ||
computeTooltipPosition({ | ||
place, | ||
offset, | ||
elementReference: virtualElement, | ||
tooltipReference: tooltipRef.current, | ||
tooltipArrowReference: tooltipArrowRef.current, | ||
strategy: positionStrategy, | ||
}).then((computedStylesData) => { | ||
setCalculatingPosition(false) | ||
if (Object.keys(computedStylesData.tooltipStyles).length) { | ||
setInlineStyles(computedStylesData.tooltipStyles) | ||
} | ||
if (Object.keys(computedStylesData.tooltipArrowStyles).length) { | ||
setInlineArrowStyles(computedStylesData.tooltipArrowStyles) | ||
} | ||
}) | ||
} | ||
|
||
const handleMouseMove = (event?: Event) => { | ||
if (!event) { | ||
return | ||
} | ||
const mouseEvent = event as MouseEvent | ||
const mousePosition = { | ||
x: mouseEvent.clientX, | ||
y: mouseEvent.clientY, | ||
} | ||
handleTooltipPosition(mousePosition) | ||
lastFloatPosition = mousePosition | ||
} | ||
|
||
const handleClickTooltipAnchor = () => { | ||
if (setIsOpen) { | ||
setIsOpen(!isOpen) | ||
} else if (isOpen === undefined) { | ||
setShow((currentValue) => !currentValue) | ||
} else if (!setIsOpen && isOpen === undefined) { | ||
setShow(true) | ||
if (delayHide) { | ||
handleHideTooltipDelayed() | ||
} | ||
} | ||
} | ||
|
||
const handleClickOutsideAnchor = (event: MouseEvent) => { | ||
if (event.target === activeAnchor.current) { | ||
return | ||
} | ||
setShow(false) | ||
} | ||
|
||
// debounce handler to prevent call twice when | ||
// mouse enter and focus events being triggered toggether | ||
const debouncedHandleShowTooltip = debounce(handleShowTooltip, 50) | ||
|
@@ -125,13 +186,13 @@ const Tooltip = ({ | |
} | ||
|
||
if (!elementRefs.size) { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
return () => {} | ||
return () => null | ||
} | ||
|
||
const enabledEvents: { event: string; listener: (event?: Event) => void }[] = [] | ||
|
||
if (events.find((event: string) => event === 'click')) { | ||
window.addEventListener('click', handleClickOutsideAnchor) | ||
enabledEvents.push({ event: 'click', listener: handleClickTooltipAnchor }) | ||
} | ||
|
||
|
@@ -142,6 +203,12 @@ const Tooltip = ({ | |
{ event: 'focus', listener: debouncedHandleShowTooltip }, | ||
{ event: 'blur', listener: debouncedHandleHideTooltip }, | ||
) | ||
if (float) { | ||
enabledEvents.push({ | ||
event: 'mousemove', | ||
listener: handleMouseMove, | ||
}) | ||
} | ||
} | ||
|
||
enabledEvents.forEach(({ event, listener }) => { | ||
|
@@ -151,15 +218,37 @@ const Tooltip = ({ | |
}) | ||
|
||
return () => { | ||
window.removeEventListener('click', handleClickOutsideAnchor) | ||
enabledEvents.forEach(({ event, listener }) => { | ||
elementRefs.forEach((ref) => { | ||
ref.current?.removeEventListener(event, listener) | ||
}) | ||
}) | ||
} | ||
}, [anchorRefs, anchorId, events, delayHide, delayShow]) | ||
}, [anchorRefs, activeAnchor, anchorId, events, delayHide, delayShow]) | ||
|
||
useEffect(() => { | ||
if (position) { | ||
// if `position` is set, override regular and `float` positioning | ||
handleTooltipPosition(position) | ||
return () => null | ||
} | ||
|
||
if (float) { | ||
if (lastFloatPosition) { | ||
/* | ||
Without this, changes to `content`, `place`, `offset`, ..., will only | ||
trigger a position calculation after a `mousemove` event. | ||
|
||
To see why this matters, comment this line, run `yarn dev` and click the | ||
"Hover me!" anchor. | ||
*/ | ||
handleTooltipPosition(lastFloatPosition) | ||
} | ||
// if `float` is set, override regular positioning | ||
return () => null | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For future reference! |
||
|
||
let elementReference = activeAnchor.current | ||
if (anchorId) { | ||
// `anchorId` element takes precedence | ||
|
@@ -190,7 +279,7 @@ const Tooltip = ({ | |
return () => { | ||
mounted = false | ||
} | ||
}, [show, isOpen, anchorId, activeAnchor, content, place, offset, positionStrategy]) | ||
}, [show, isOpen, anchorId, activeAnchor, content, place, offset, positionStrategy, position]) | ||
|
||
useEffect(() => { | ||
return () => { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having both the empty brackets
[ ]
and**Deprecated**
seemed redundant.