Skip to content

Commit

Permalink
Merge pull request #1141 from atomiks/fix/relative-html
Browse files Browse the repository at this point in the history
fix: relative html
  • Loading branch information
FezVrasta committed Jun 27, 2020
2 parents 38914aa + d6a1b8f commit c595ef3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
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>

0 comments on commit c595ef3

Please sign in to comment.