Skip to content
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

Merged

Conversation

sydneyjodon-wk
Copy link
Contributor

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 function anchorEl as if it were in the form of setTooltipAnchorEl((prev) => next).

Solution

We should call resolveAnchorEl on anchorEl before setting tooltipAnchorEl.

@mui-bot
Copy link

mui-bot commented Dec 13, 2022

Messages
📖 Netlify deploy preview: https://deploy-preview-35469--material-ui.netlify.app/

Details of bundle changes

Generated by 🚫 dangerJS against c7ab9d2

@sydneyjodon-wk
Copy link
Contributor Author

@mnajdova @michaldudak this is ready for review when you get a chance! Thanks!

@zannager zannager added the component: Popper The React component. See <Popup> for the latest version. label Dec 14, 2022
@ZeeshanTamboli ZeeshanTamboli added the package: base-ui Specific to @mui/base label Dec 15, 2022
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a 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.

@ZeeshanTamboli ZeeshanTamboli added the bug 🐛 Something doesn't work label Dec 15, 2022
@ZeeshanTamboli ZeeshanTamboli changed the title [PopperUnstyled] Fix Tooltip Anchor Element Setter [Popper][Base] Fix Tooltip Anchor Element Setter Dec 15, 2022
@siriwatknp
Copy link
Member

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));
Copy link
Member

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

@michaldudak
Copy link
Member

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);
Copy link
Member

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?

Copy link
Member

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));

@siriwatknp siriwatknp force-pushed the fix-popper-anchor-el-tooltip-setter branch from 863d6d5 to c7ab9d2 Compare December 20, 2022 08:38
@siriwatknp siriwatknp merged commit 9e140a7 into mui:master Dec 20, 2022
@sydneyjodon-wk sydneyjodon-wk deleted the fix-popper-anchor-el-tooltip-setter branch December 21, 2022 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: Popper The React component. See <Popup> for the latest version. package: base-ui Specific to @mui/base
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants