-
Notifications
You must be signed in to change notification settings - Fork 3k
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
fix chat-link in end of line displays tooltip over text and not on link #29134
Merged
pecanoro
merged 8 commits into
Expensify:main
from
teneeto:fix/27817-display-tooltip-over-link-and-not-on-text
Oct 12, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
179812c
display tooltip over links
teneeto f724dac
fix lint
teneeto be59886
move bounding box logic to base tooltip
teneeto 0ad6afd
destructure from props
teneeto eb8530a
merge and improve functions with a tolerance of 5
teneeto 1b90bb2
fix lint
teneeto 4a26dd7
Merge branch 'main' of github.com:Expensify/App into fix/27817-displa…
teneeto d6f8379
revert comment and use function destructure
teneeto 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 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 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 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ import _ from 'underscore'; | |
import React, {memo, useCallback, useEffect, useRef, useState} from 'react'; | ||
import {Animated} from 'react-native'; | ||
import {BoundsObserver} from '@react-ng/bounds-observer'; | ||
import Str from 'expensify-common/lib/str'; | ||
import TooltipRenderedOnPageBody from './TooltipRenderedOnPageBody'; | ||
import Hoverable from '../Hoverable'; | ||
import * as tooltipPropTypes from './tooltipPropTypes'; | ||
|
@@ -19,9 +20,41 @@ const hasHoverSupport = DeviceCapabilities.hasHoverSupport(); | |
* @param {propTypes} props | ||
* @returns {ReactNodeLike} | ||
*/ | ||
function Tooltip(props) { | ||
const {children, numberOfLines, maxWidth, text, renderTooltipContent, renderTooltipContentKey} = props; | ||
|
||
/** | ||
* Choose the correct bounding box for the tooltip to be positioned against. | ||
* This handles the case where the target is wrapped across two lines, and | ||
* so we need to find the correct part (the one that the user is hovering | ||
* over) and show the tooltip there. | ||
* | ||
* @param {Element} target The DOM element being hovered over. | ||
* @param {number} clientX The X position from the MouseEvent. | ||
* @param {number} clientY The Y position from the MouseEvent. | ||
* @return {DOMRect} The chosen bounding box. | ||
*/ | ||
|
||
function chooseBoundingBox(target, clientX, clientY) { | ||
const slop = 5; | ||
const bbs = target.getClientRects(); | ||
const clientXMin = clientX - slop; | ||
const clientXMax = clientX + slop; | ||
const clientYMin = clientY - slop; | ||
const clientYMax = clientY + slop; | ||
|
||
for (let i = 0; i < bbs.length; i++) { | ||
const bb = bbs[i]; | ||
if (clientXMin <= bb.right && clientXMax >= bb.left && clientYMin <= bb.bottom && clientYMax >= bb.top) { | ||
return bb; | ||
} | ||
} | ||
|
||
// If no matching bounding box is found, fall back to the first one. | ||
// This could only happen if the user is moving the mouse very quickly | ||
// and they got it outside our slop above. | ||
return bbs[0]; | ||
} | ||
|
||
function Tooltip({children, numberOfLines, maxWidth, text, renderTooltipContent, renderTooltipContentKey, shouldHandleScroll, shiftHorizontal, shiftVertical}) { | ||
const {preferredLocale} = useLocalize(); | ||
const {windowWidth} = useWindowDimensions(); | ||
|
||
|
@@ -43,6 +76,14 @@ function Tooltip(props) { | |
const isAnimationCanceled = useRef(false); | ||
const prevText = usePrevious(text); | ||
|
||
const target = useRef(null); | ||
const initialMousePosition = useRef({x: 0, y: 0}); | ||
|
||
const updateTargetAndMousePosition = useCallback((e) => { | ||
target.current = e.target; | ||
initialMousePosition.current = {x: e.clientX, y: e.clientY}; | ||
}, []); | ||
|
||
/** | ||
* Display the tooltip in an animation. | ||
*/ | ||
|
@@ -91,10 +132,15 @@ function Tooltip(props) { | |
if (bounds.width === 0) { | ||
setIsRendered(false); | ||
} | ||
setWrapperWidth(bounds.width); | ||
setWrapperHeight(bounds.height); | ||
setXOffset(bounds.x); | ||
setYOffset(bounds.y); | ||
// Choose a bounding box for the tooltip to target. | ||
// In the case when the target is a link that has wrapped onto | ||
// multiple lines, we want to show the tooltip over the part | ||
// of the link that the user is hovering over. | ||
const betterBounds = chooseBoundingBox(target.current, initialMousePosition.current.x, initialMousePosition.current.y); | ||
setWrapperWidth(betterBounds.width); | ||
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. 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. Yes it's, more info #29446 (comment) and PR fixing it #29468 |
||
setWrapperHeight(betterBounds.height); | ||
setXOffset(betterBounds.x); | ||
setYOffset(betterBounds.y); | ||
}; | ||
|
||
/** | ||
|
@@ -136,8 +182,8 @@ function Tooltip(props) { | |
yOffset={yOffset} | ||
targetWidth={wrapperWidth} | ||
targetHeight={wrapperHeight} | ||
shiftHorizontal={_.result(props, 'shiftHorizontal')} | ||
shiftVertical={_.result(props, 'shiftVertical')} | ||
shiftHorizontal={Str.result(shiftHorizontal)} | ||
shiftVertical={Str.result(shiftVertical)} | ||
text={text} | ||
maxWidth={maxWidth} | ||
numberOfLines={numberOfLines} | ||
|
@@ -152,9 +198,10 @@ function Tooltip(props) { | |
onBoundsChange={updateBounds} | ||
> | ||
<Hoverable | ||
onMouseEnter={updateTargetAndMousePosition} | ||
onHoverIn={showTooltip} | ||
onHoverOut={hideTooltip} | ||
shouldHandleScroll={props.shouldHandleScroll} | ||
shouldHandleScroll={shouldHandleScroll} | ||
> | ||
{children} | ||
</Hoverable> | ||
|
@@ -165,4 +212,6 @@ function Tooltip(props) { | |
|
||
Tooltip.propTypes = tooltipPropTypes.propTypes; | ||
Tooltip.defaultProps = tooltipPropTypes.defaultProps; | ||
Tooltip.displayName = 'Tooltip'; | ||
|
||
export default memo(Tooltip); |
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.
We should have used the
currentTarget
(the element that is listening to the mouse enter event) instead oftarget
(the element that caused the event to fire and bubble up). Coming from #29678