-
-
Notifications
You must be signed in to change notification settings - Fork 534
Track window resize to reposition tooltip #1075
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
gabrieljablonski
merged 7 commits into
ReactTooltip:master
from
Tanmayshetty:feat/add-basic-resize-update
Aug 3, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f3f5ea9
feat: add update tooltip on resize
da955c8
feat: Use autoUpdate method from floating-ui/dom
6a88609
chore: Move the react type change back to dev dependecies
6c977bf
test: mock `ResizeObserver`
gabrieljablonski 810343a
fix: rearrange some `useEffect` deps
gabrieljablonski 9f5e6db
docs(troubleshooting): scrolling should not be an issue anymore
gabrieljablonski 0352afa
test: update snapshots
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import React, { useEffect, useState, useRef } from 'react' | ||
import React, { useEffect, useState, useRef, useCallback } from 'react' | ||
import { autoUpdate } from '@floating-ui/dom' | ||
import classNames from 'classnames' | ||
import debounce from 'utils/debounce' | ||
import { useTooltip } from 'components/TooltipProvider' | ||
|
@@ -290,6 +291,61 @@ const Tooltip = ({ | |
// mouse enter and focus events being triggered toggether | ||
const debouncedHandleShowTooltip = debounce(handleShowTooltip, 50, true) | ||
const debouncedHandleHideTooltip = debounce(handleHideTooltip, 50, true) | ||
const updateTooltipPosition = useCallback(() => { | ||
if (position) { | ||
// if `position` is set, override regular and `float` positioning | ||
handleTooltipPosition(position) | ||
return | ||
} | ||
|
||
if (float) { | ||
if (lastFloatPosition.current) { | ||
/* | ||
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.current) | ||
} | ||
// if `float` is set, override regular positioning | ||
return | ||
} | ||
|
||
computeTooltipPosition({ | ||
place, | ||
offset, | ||
elementReference: activeAnchor, | ||
tooltipReference: tooltipRef.current, | ||
tooltipArrowReference: tooltipArrowRef.current, | ||
strategy: positionStrategy, | ||
middlewares, | ||
border, | ||
}).then((computedStylesData) => { | ||
if (!mounted.current) { | ||
// invalidate computed positions after remount | ||
return | ||
} | ||
if (Object.keys(computedStylesData.tooltipStyles).length) { | ||
setInlineStyles(computedStylesData.tooltipStyles) | ||
} | ||
if (Object.keys(computedStylesData.tooltipArrowStyles).length) { | ||
setInlineArrowStyles(computedStylesData.tooltipArrowStyles) | ||
} | ||
setActualPlacement(computedStylesData.place as PlacesType) | ||
}) | ||
}, [ | ||
show, | ||
activeAnchor, | ||
content, | ||
externalStyles, | ||
place, | ||
offset, | ||
positionStrategy, | ||
position, | ||
float, | ||
]) | ||
Comment on lines
+338
to
+348
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. Had to update dependencies here and on the main After some testing it doesn't seem like anything broke, but there's a non-zero chance this will cause some random bugs we'll be running into soon. |
||
|
||
useEffect(() => { | ||
const elementRefs = new Set(anchorRefs) | ||
|
@@ -315,8 +371,20 @@ const Tooltip = ({ | |
anchorScrollParent?.addEventListener('scroll', handleScrollResize) | ||
tooltipScrollParent?.addEventListener('scroll', handleScrollResize) | ||
} | ||
let updateTooltipCleanup: null | (() => void) = null | ||
if (closeOnResize) { | ||
window.addEventListener('resize', handleScrollResize) | ||
} else if (activeAnchor && tooltipRef.current) { | ||
updateTooltipCleanup = autoUpdate( | ||
activeAnchor as HTMLElement, | ||
tooltipRef.current as HTMLElement, | ||
updateTooltipPosition, | ||
{ | ||
ancestorResize: true, | ||
elementResize: true, | ||
layoutShift: true, | ||
}, | ||
) | ||
} | ||
|
||
const handleEsc = (event: KeyboardEvent) => { | ||
|
@@ -377,6 +445,8 @@ const Tooltip = ({ | |
} | ||
if (closeOnResize) { | ||
window.removeEventListener('resize', handleScrollResize) | ||
} else { | ||
updateTooltipCleanup?.() | ||
} | ||
if (shouldOpenOnClick) { | ||
window.removeEventListener('click', handleClickOutsideAnchors) | ||
|
@@ -398,7 +468,15 @@ const Tooltip = ({ | |
* rendered is also a dependency to ensure anchor observers are re-registered | ||
* since `tooltipRef` becomes stale after removing/adding the tooltip to the DOM | ||
*/ | ||
}, [rendered, anchorRefs, anchorsBySelect, closeOnEsc, events]) | ||
}, [ | ||
activeAnchor, | ||
updateTooltipPosition, | ||
rendered, | ||
anchorRefs, | ||
anchorsBySelect, | ||
closeOnEsc, | ||
events, | ||
]) | ||
|
||
useEffect(() => { | ||
let selector = anchorSelect ?? '' | ||
|
@@ -476,55 +554,9 @@ const Tooltip = ({ | |
} | ||
}, [id, anchorSelect, activeAnchor]) | ||
|
||
const updateTooltipPosition = () => { | ||
if (position) { | ||
// if `position` is set, override regular and `float` positioning | ||
handleTooltipPosition(position) | ||
return | ||
} | ||
|
||
if (float) { | ||
if (lastFloatPosition.current) { | ||
/* | ||
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.current) | ||
} | ||
// if `float` is set, override regular positioning | ||
return | ||
} | ||
|
||
computeTooltipPosition({ | ||
place, | ||
offset, | ||
elementReference: activeAnchor, | ||
tooltipReference: tooltipRef.current, | ||
tooltipArrowReference: tooltipArrowRef.current, | ||
strategy: positionStrategy, | ||
middlewares, | ||
border, | ||
}).then((computedStylesData) => { | ||
if (!mounted.current) { | ||
// invalidate computed positions after remount | ||
return | ||
} | ||
if (Object.keys(computedStylesData.tooltipStyles).length) { | ||
setInlineStyles(computedStylesData.tooltipStyles) | ||
} | ||
if (Object.keys(computedStylesData.tooltipArrowStyles).length) { | ||
setInlineArrowStyles(computedStylesData.tooltipArrowStyles) | ||
} | ||
setActualPlacement(computedStylesData.place as PlacesType) | ||
}) | ||
} | ||
|
||
useEffect(() => { | ||
updateTooltipPosition() | ||
}, [show, activeAnchor, content, externalStyles, place, offset, positionStrategy, position]) | ||
}, [updateTooltipPosition]) | ||
|
||
useEffect(() => { | ||
if (!contentWrapperRef?.current) { | ||
|
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`compute positions all reference elements 1`] = ` | ||
{ | ||
"place": "top", | ||
"tooltipArrowStyles": { | ||
"bottom": "-4px", | ||
"left": "-1px", | ||
"right": "", | ||
"top": "", | ||
}, | ||
"tooltipStyles": { | ||
"border": undefined, | ||
"left": "5px", | ||
"top": "-10px", | ||
}, | ||
} | ||
`; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.