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

fix: relative html #1141

Merged
merged 1 commit into from
Jun 27, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/dom-utils/getOffsetParent.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import getComputedStyle from './getComputedStyle';
import { isHTMLElement } from './instanceOf';
import isTableElement from './isTableElement';
import getParentNode from './getParentNode';
import getDocumentElement from './getDocumentElement';

function getTrueOffsetParent(element: Element): ?Element {
if (
Expand All @@ -15,7 +16,21 @@ function getTrueOffsetParent(element: Element): ?Element {
return null;
}

return element.offsetParent;
const offsetParent = element.offsetParent;

if (offsetParent) {
const html = getDocumentElement(offsetParent);

if (
getNodeName(offsetParent) === 'body' &&
getComputedStyle(offsetParent).position === 'static' &&
getComputedStyle(html).position !== 'static'
) {
return html;
}
}

return offsetParent;
}

// `.offsetParent` reports `null` for fixed elements, while absolute elements
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions tests/functional/relative-html.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @jest-environment puppeteer
* @flow
*/
import { screenshot } from '../utils/puppeteer.js';

it('should position the popper on the right', async () => {
const page = await browser.newPage();
await page.goto(`${TEST_URL}/relative-html.html`);

expect(await screenshot(page)).toMatchImageSnapshot();
});
39 changes: 39 additions & 0 deletions tests/visual/relative-html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html> <title>Basic Visual Test</title>

<style>
@import '/reset.css';
html {
position: relative;
margin: 30px;
}

#reference {
position: absolute;
top: 150px;
left: 150px;
width: 200px;
height: 200px;
background-color: red;
box-shadow: inset 0 0 0 1px black;
}

#popper {
width: 100px;
height: 100px;
background-color: rebeccapurple;
box-shadow: inset 0 0 0 1px black;
}
</style>

<div id="reference">Reference Box</div>
<div id="popper">Popper Box</div>

<script type="module">
import { createPopper } from './dist/popper.js';
const reference = document.querySelector('#reference');
const popper = document.querySelector('#popper');

window.instance = createPopper(reference, popper, {
placement: 'right',
});
</script>