[Popper] Use ownerDocument of anchorEl #24550
Open
Comments
|
@simplecommerce Thanks for reporting this behavior. Yes, it's unfortunate, the Popover has a different default container logic than the Modal. It uses the owner document of the anchor. In your case, I would recommend customizing the default prop of the Popover. Actually, it looks like the Popper component should do the same. We forgot to port this part of the logic: material-ui/packages/material-ui/src/Popover/Popover.js Lines 64 to 66 in 1f70579 You can reproduce the bug on https://codesandbox.io/s/tender-lumiere-mhk0g?file=/src/App.tsx. I think that we can apply this fix: diff --git a/packages/material-ui/src/Popper/Popper.js b/packages/material-ui/src/Popper/Popper.js
index efb1aa2042..6f8bea8f80 100644
--- a/packages/material-ui/src/Popper/Popper.js
+++ b/packages/material-ui/src/Popper/Popper.js
@@ -4,6 +4,7 @@ import { createPopper } from '@popperjs/core';
import { chainPropTypes, refType, HTMLElementType } from '@material-ui/utils';
import { useTheme } from '@material-ui/styles';
import Portal from '../Portal';
+import ownerDocument from '../utils/ownerDocument';
import setRef from '../utils/setRef';
import useForkRef from '../utils/useForkRef';
import useEnhancedEffect from '../utils/useEnhancedEffect';
@@ -42,7 +43,7 @@ const Popper = React.forwardRef(function Popper(props, ref) {
const {
anchorEl,
children,
- container,
+ container: containerProp,
disablePortal = false,
keepMounted = false,
modifiers,
@@ -211,6 +212,12 @@ const Popper = React.forwardRef(function Popper(props, ref) {
};
}
+ // If the container prop is provided, use that
+ // If the anchorEl prop is provided, use its parent body element as the container
+ // If neither are provided let the Modal take care of choosing the container
+ const container =
+ containerProp || (anchorEl ? ownerDocument(getAnchorEl(anchorEl)).body : undefined);
+
return (
<Portal disablePortal={disablePortal} container={container}>
<div |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I have a question in regards to passing default theme props to a component.
My example is using the
TablePaginationcomponent.Underlying it uses the
Selectwhich uses theMenu,PopoverandModal.I am trying to set a default
containerto theModalcomponent, but it seems that it only works if I use theModaldirectly.If I use the
TablePaginationcomponent, I need to do it differently, which seems odd to me.Here is a sandbox to demonstrate the issue.
https://codesandbox.io/s/material-demo-forked-p06py?file=/demo.js
Current Behavior😯
In order to pass a
containerprop to theModalinside of theSelectI have to useExpected Behavior🤔
I was expecting to be able to simply pass the prop to the
Modalcomponent by doing:Steps to Reproduce🕹
In order to reproduce the issue, do the following.
On the page, simply click on the button to toggle the modal and then click on the paging option in the pagination.
You should see two console logs.
containerandselect container.Then comment out the
MuiSelectprops and try to toggle the select in the pagination again, and you should only seecontainerwhen toggling the modal.Context🔦
My use case, was to simply pass a default prop to any component using the
Modalas the underlying component which access theme props.If my understanding of it is wrong please let me know. And sorry if I am being unclear.
Thanks!
The text was updated successfully, but these errors were encountered: