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

[core] Warn if anchor element is not visible #17599

Merged
merged 4 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/material-ui/src/Popover/Popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,27 @@ const Popover = React.forwardRef(function Popover(props, ref) {
? resolvedAnchorEl
: ownerDocument(paperRef.current).body;
const anchorRect = anchorElement.getBoundingClientRect();

if (process.env.NODE_ENV !== 'production') {
const box = anchorElement.getBoundingClientRect();

if (
process.env.NODE_ENV !== 'test' &&
box.top === 0 &&
box.left === 0 &&
box.right === 0 &&
box.bottom === 0
) {
console.warn(
[
'Material-UI: the `anchorEl` prop provided to the component is invalid.',
'The anchor element should be part of the document layout.',
"Make sure the element is present in the document or that it's not display none.",
].join('\n'),
);
}
}

const anchorVertical = contentAnchorOffset === 0 ? anchorOrigin.vertical : 'center';

return {
Expand Down
26 changes: 26 additions & 0 deletions packages/material-ui/src/Popper/Popper.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ const Popper = React.forwardRef(function Popper(props, ref) {
setPlacement(data.placement);
};

const resolvedAnchorEl = getAnchorEl(anchorEl);

if (process.env.NODE_ENV !== 'production') {
const containerWindow = ownerWindow(resolvedAnchorEl);

if (resolvedAnchorEl instanceof containerWindow.Element) {
const box = resolvedAnchorEl.getBoundingClientRect();

if (
process.env.NODE_ENV !== 'test' &&
box.top === 0 &&
box.left === 0 &&
box.right === 0 &&
box.bottom === 0
) {
console.warn(
[
'Material-UI: the `anchorEl` prop provided to the component is invalid.',
'The anchor element should be part of the document layout.',
"Make sure the element is present in the document or that it's not display none.",
].join('\n'),
);
}
}
}

const popper = new PopperJS(getAnchorEl(anchorEl), popperNode, {
placement: rtlPlacement,
...popperOptions,
Expand Down