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

[Popover] Fix PaperProps.ref breaking positioning #26560

Merged
merged 3 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/material-ui/src/Popover/Popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import useThemeProps from '../styles/useThemeProps';
import debounce from '../utils/debounce';
import ownerDocument from '../utils/ownerDocument';
import ownerWindow from '../utils/ownerWindow';
import useForkRef from '../utils/useForkRef';
import Grow from '../Grow';
import Modal from '../Modal';
import Paper from '../Paper';
Expand Down Expand Up @@ -120,6 +121,7 @@ const Popover = React.forwardRef(function Popover(inProps, ref) {
...other
} = props;
const paperRef = React.useRef();
const handleRef = useForkRef(paperRef, PaperProps.ref);

const styleProps = {
...props,
Expand Down Expand Up @@ -369,8 +371,8 @@ const Popover = React.forwardRef(function Popover(inProps, ref) {
>
<PopoverPaper
elevation={elevation}
ref={paperRef}
{...PaperProps}
ref={handleRef}
className={clsx(classes.paper, PaperProps.className)}
>
{children}
Expand Down
16 changes: 14 additions & 2 deletions packages/material-ui/src/Popover/Popover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe('<Popover />', () => {
let expectPopover;

before(() => {
openPopover = (anchorOrigin) => {
openPopover = (anchorOrigin, mockPaperRef) => {
if (!anchorEl) {
anchorEl = document.createElement('div');
}
Expand All @@ -316,14 +316,18 @@ describe('<Popover />', () => {
});
document.body.appendChild(anchorEl);

const PaperProps = mockPaperRef
? { 'data-testid': 'Popover', ref: () => null }
: { 'data-testid': 'Popover' }

return new Promise((resolve) => {
const component = (
<Popover
{...defaultProps}
anchorEl={anchorEl}
anchorOrigin={anchorOrigin}
transitionDuration={0}
PaperProps={{ 'data-testid': 'Popover' }}
PaperProps={{...PaperProps}}
TransitionProps={{
onEntered: () => {
popoverEl = document.querySelector('[data-testid="Popover"]');
Expand Down Expand Up @@ -360,6 +364,14 @@ describe('<Popover />', () => {
expectPopover(expectedTop, expectedLeft);
});

it('should be positioned over the top left of the anchor even when `PaperProps.ref` is passed as prop', async () => {
await openPopover({ vertical: 'top', horizontal: 'left' }, true);
const anchorRect = anchorEl.getBoundingClientRect();
const expectedTop = anchorRect.top <= 16 ? 16 : anchorRect.top;
const expectedLeft = anchorRect.left <= 16 ? 16 : anchorRect.left;
expectPopover(expectedTop, expectedLeft);
});

it('should be positioned over the center left of the anchor', async () => {
await openPopover({ vertical: 'center', horizontal: 'left' });
const anchorRect = anchorEl.getBoundingClientRect();
Expand Down