-
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 #27817
Conversation
… link Add a new prop to Tooltip called shouldUseMultilinePositioning. This enables a new algorithm for finding the bounding box target for the tooltip. In the case where the target has wrapped onto multiple lines (e.g. it is a link in a chat window) then the link has multiple bounding boxes, and we want to show the tooltip against the one that the user is hovering over. As part of this, extend Hoverable to pass the Event to its onHoverIn / onHoverOut callbacks. Enable this new algorithm from BaseAnchorForCommentsOnly, which is the base class for links that show up in chat.
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
I have read the CLA Document and I hereby sign the CLA |
recheck |
1 similar comment
recheck |
@@ -42,6 +59,7 @@ const defaultProps = { | |||
renderTooltipContent: undefined, | |||
renderTooltipContentKey: [], | |||
shouldHandleScroll: false, | |||
shouldUseMultilinePositioning: false, |
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.
Let's remove this prop. I think we want to have this feature enabled for all tooltips
src/components/Hoverable/index.js
Outdated
@@ -94,7 +95,7 @@ class Hoverable extends Component { | |||
if (this.isScrollingRef && this.props.shouldHandleScroll && !this.state.isHovered) return; | |||
|
|||
if (isHovered !== this.state.isHovered) { | |||
this.setState({isHovered}, isHovered ? this.props.onHoverIn : this.props.onHoverOut); | |||
this.setState({isHovered}, () => (isHovered ? this.props.onHoverIn : this.props.onHoverOut)(ev)); |
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.
Given that onHoverIn
may be called without events it's better to use onMouseEnter
. Add onMouseEnter and onMouseLeave props to Hoverable and use onMouseEnter to set the target and mouse position.
In Tooltip:
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};
}, []);
<Hoverable onMouseEnter={updateTargetAndMousePosition) ... />
src/components/Tooltip/index.js
Outdated
if (shouldUseMultilinePositioning) { | ||
if (ev) { | ||
const {clientX, clientY, target} = ev; | ||
const bb = chooseBoundingBox(target, clientX, clientY); | ||
updateBounds(bb); | ||
} | ||
} |
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.
Undo this. chooseBoundingBox
should be called from updateBounds
const updateBounds = (bounds) => {
if (bounds.width === 0) {
setIsRendered(false);
}
const betterBounds = chooseBoundingBox(target.current, initialMousePosition.current.x, initialMousePosition.current.y);
setWrapperWidth(betterBounds.width);
setWrapperHeight(betterBounds.height);
setXOffset(betterBounds.x);
setYOffset(betterBounds.y);
};
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.
@s77rt This doesn't make sense. If the user hovers over one part of the link and then moves to the other part, you need to move the tooltip. updateBounds
is only called when the element moves, but in that case the element itself has not moved, so we will miss the necessary update. That's why it needs to be in the hover event handler.
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.
I'm not sure if this is a valid case. if the user moves to the other part then we'd have two possible outcomes:
- User is still hovering the target, in this case the tooltip does not need to be updated as this case is only possible if the mouse stays at the same row (x position)
- User lost hover, in this case onMouseEnter will be called once the user hover the next part and the tooltip will be placed correctly
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.
@s77rt Right, but your proposed updateTargetAndMousePosition
doesn't set the tooltip position, it just saves the mouse position at that time. So when the onMouseEnter
is called, you remember the location but don't do anything with that. And you won't get a call to updateBounds
because the element hasn't moved.
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.
Yes, that's the intended use. updateBounds will be called once the tooltip is visible or on bounds change
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.
@s77rt "updateBounds will be called by BoundsObserver". This only happens if the element moves. If the user is moving the mouse between two parts of the same link, the element does not move and updateBounds is not called.
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.
That's correct. But the user can't move the mouse between two parts without firing the onMouseLeave and then onMouseEnter again on the second part
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.
@s77rt Yes, but again, if the user does onMouseLeave and onMouseEnter, then you have nothing calling updateBounds
. Your proposed updateTargetAndMousePosition
does not call updateBounds
. So the tooltip does not move. What am I missing from what you are saying?
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.
- onMouseLeave -> onHoverOut -> hideTooltip -> setIsVisible(false)
- onMouseEnter (1st callback) -> updateTargetAndMousePosition
- onMouseEnter (2nd callback) -> onHoverIn -> showTooltip -> setIsVisible(true)
Besides setting the target and mouse position, we have a change in the isVisible state which is passed to the enabled prop of BoundsObserver and per the docs https://socket.dev/npm/package/@react-ng/bounds-observer
Additionally, this callback will be invoked on enabling, i.e. when the enabled property changes to true
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.
@s77rt OK, I finally see what you are saying. I have updated this PR with all the changes mentioned.
src/components/Tooltip/index.js
Outdated
* enough then we go with that. | ||
* @return {DOMRect} The chosen bounding box. | ||
*/ | ||
function chooseBoundingBox(target, clientX, clientY, slop = 0) { |
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.
I think we'd need a safer algo without the recursive call.
src/components/Tooltip/index.js
Outdated
// Fall back to the full bounding box if we failed to find a matching one | ||
// (shouldn't happen). | ||
return target.getBoundingClientRect(); |
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.
Don't write code that can't be reached.
src/components/Tooltip/index.js
Outdated
* @return {DOMRect} The chosen bounding box. | ||
*/ | ||
function chooseBoundingBox(target, clientX, clientY, slop = 0) { | ||
const bbs = target.getClientRects(); |
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.
minor optimisation, if bbs contains only one element then just use it (that element would be same as getBoundingClientRect)
… link Changes from PR review: o Remove the shouldUseMultilinePositioning prop, and instead make the new algorithm the one to use in all situations. o Refactor chooseBoundingBox to avoid a recursive call. o Shortcut chooseBoundingBox in the case where the element only has one bounding box. o Revert the changes to Hoverable that pass the MouseEvent through onHoverIn / onHoverOut, and instead add direct pass-throughs for the onMouseEnter / onMouseLeave events. o Change the algorithm so that the onHoverIn event handler only records the mouse position, and rely on BoundsObserver being enabled as a side-effect of the isVisible change to move the tooltip.
recheck |
src/components/Hoverable/index.js
Outdated
if (_.isFunction(this.props.onMouseEnter)) { | ||
this.props.onMouseEnter(el); | ||
} |
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.
Move this to be the first callback (same for onMouseLeave)
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.
Done.
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.
This is still not the first, it needs to goes before setIsHovered
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.
Done.
src/components/Tooltip/index.js
Outdated
if (!t) { | ||
return; | ||
} |
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.
Is there a case where target is not defined?
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.
No, but the type system would not be able to see that, because it cannot tell that updateBounds
will never be called before a hover event makes the tooltip visible.
Regardless, this file is not TypeScript, so I've removed it.
src/components/Tooltip/index.js
Outdated
if (!t) { | ||
return; | ||
} | ||
const betterBounds = chooseBoundingBox(t, initialMousePosition.current.x, initialMousePosition.current.y); |
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.
Please add a comment here on why we need better bounding box
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.
Done.
… link Changes from PR review: o Reorder the callbacks from Hoverable. o Remove a null check. o Add a comment.
… link Changes from PR review: o Small tidyup.
recheck |
… link Change from PR review: move the onMouseEnter / onMouseLeave callbacks before the setIsHovered calls.
* @param {number} slop An allowed slop factor when searching for the bounding | ||
* box. If the user is moving the mouse quickly we can end up getting a | ||
* hover event with the position outside any of our bounding boxes. We retry | ||
* with a small slop factor in that case, so if we have a bounding box close | ||
* enough then we go with that. |
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.
I have been checking and I think the need for the tolerance is not related to moving the mouse quickly, I can reproduce the bug constantly and this seems due to the returned bbs values corresponding to the text fragments only and not the block. Let's have the tolerance always used and integrate this function inside chooseBoundingBox
// Fall back to the full bounding box if we failed to find a matching one. | ||
// This could only happen if the user is moving the mouse very quickly | ||
// and they got it outside our slop above. | ||
return target.getBoundingClientRect(); |
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.
return target.getBoundingClientRect(); | |
return bbs[0]; |
Details
Add a new prop to
Tooltip
calledshouldUseMultilinePositioning
. This enables a new algorithm for finding the bounding box target for the tooltip. In the case where the target has wrapped onto multiple lines (e.g. it is a link in a chat window) then the link has multiple bounding boxes, and we want to show the tooltip against the one that the user is hovering over.As part of this, extend
Hoverable
to pass the Event to itsonHoverIn
/onHoverOut
callbacks.Enable this new algorithm from
BaseAnchorForCommentsOnly
, which is the base class for links that show up in chat.Fixed Issues
$ #27585
PROPOSAL: #27585 (comment)
Tests
Action Performed:
On a platform with a mouse (i.e. not mobile):
Expected Result:
App should display tooltip over link on hover. The tooltip should appear over the same section of the link that the user is hovering over.
Offline tests
N/A. There is no network interaction in this change.
QA Steps
PR Author Checklist
### Fixed Issues
section aboveTests
sectionOffline steps
sectionQA steps
sectiontoggleReport
and notonIconClick
)myBool && <MyComponent />
.src/languages/*
files and using the translation methodWaiting for Copy
label for a copy review on the original GH to get the correct copy.STYLE.md
) were followedAvatar
, I verified the components usingAvatar
are working as expected)/** comment above it */
this
properly so there are no scoping issues (i.e. foronClick={this.submit}
the methodthis.submit
should be bound tothis
in the constructor)this
are necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);
ifthis.submit
is never passed to a component event handler likeonClick
)StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
)Avatar
is modified, I verified thatAvatar
is working as expected in all cases)ScrollView
component to make it scrollable when more elements are added to the page.main
branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTest
steps.Screenshots/Videos
Web
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android