-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[Popper][Base] Fix Tooltip Anchor Element Setter #35469
[Popper][Base] Fix Tooltip Anchor Element Setter #35469
Conversation
|
@mnajdova @michaldudak this is ready for review when you get a chance! Thanks! |
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.
Nice catch! Changes make sense to me. Here is the CodeSandbox after fix: https://codesandbox.io/s/anchorel-called-with-arg-gcgds2.
I think migration to TypeScript can prevent these type of issues. It's currently being done in #34771.
LGTM @michaldudak Can you take a final look? |
@@ -84,7 +84,7 @@ const PopperTooltip = React.forwardRef(function PopperTooltip(props, ref) { | |||
* modifiers.flip is essentially a flip for controlled/uncontrolled behavior | |||
*/ | |||
const [placement, setPlacement] = React.useState(rtlPlacement); | |||
const [tooltipAnchorEl, setTooltipAnchorEl] = React.useState(anchorEl); | |||
const [tooltipAnchorEl, setTooltipAnchorEl] = React.useState(resolveAnchorEl(anchorEl)); |
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.
Since this is always a resolved element, calling it resolvedAnchorEl
(or even resolvedAnchorElement
, to reduce abbreviations), would make the intents clearer
Looks good! I do have one remark about naming, but apart from that it's good to go. |
@@ -78,13 +78,14 @@ const PopperTooltip = React.forwardRef(function PopperTooltip(props, ref) { | |||
}, [handlePopperRef]); | |||
React.useImperativeHandle(popperRefProp, () => popperRef.current, []); | |||
|
|||
const resolvedAnchorElement = resolveAnchorEl(anchorEl); |
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.
@michaldudak Is this what you asked for?
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, I meant:
-const [tooltipAnchorEl, setTooltipAnchorEl] = React.useState(resolveAnchorEl(anchorEl));
+const [resolvedAnchorElement, setResolvedAnchorElement] = React.useState(resolveAnchorEl(anchorEl));
863d6d5
to
c7ab9d2
Compare
Problem
When
props.anchorEl
is a function, it sometimes gets called with one argument when it expects no arguments (see codesandbox example).Cause
This is happening because the state setter
setTooltipAnchorEl(anchorEl)
(recently added in #34714) calls a functionanchorEl
as if it were in the form ofsetTooltipAnchorEl((prev) => next)
.Solution
We should call
resolveAnchorEl
onanchorEl
before settingtooltipAnchorEl
.