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(isVisibleOnScreen): account for position: absolute elements inside overflow container #4405

Merged
merged 2 commits into from
Apr 15, 2024
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
40 changes: 37 additions & 3 deletions lib/commons/dom/visibility-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,33 @@ export function overflowHidden(vNode, { isAncestor } = {}) {
return false;
}

const rect = vNode.boundingClientRect;
const nodes = getOverflowHiddenAncestors(vNode);
// a node with position fixed cannot be hidden by an overflow
// ancestor, even when that ancestor uses a non-static position
const position = vNode.getComputedStylePropertyValue('position');
if (position === 'fixed') {
return false;
}

const nodes = getOverflowHiddenAncestors(vNode);
if (!nodes.length) {
return false;
}

const rect = vNode.boundingClientRect;
return nodes.some(node => {
const nodeRect = node.boundingClientRect;
// a node with position absolute will not be hidden by an
// overflow ancestor unless the ancestor uses a non-static
// position or a node in-between uses a position of relative
// or sticky
if (
position === 'absolute' &&
!hasPositionedAncestorBetween(vNode, node) &&
node.getComputedStylePropertyValue('position') === 'static'
) {
return false;
}

const nodeRect = node.boundingClientRect;
if (nodeRect.width < 2 || nodeRect.height < 2) {
return true;
}
Expand Down Expand Up @@ -238,3 +255,20 @@ export function detailsHidden(vNode) {

return !vNode.parent.hasAttr('open');
}

function hasPositionedAncestorBetween(child, ancestor) {
let node = child.parent;
while (node && node !== ancestor) {
if (
['relative', 'sticky'].includes(
node.getComputedStylePropertyValue('position')
)
) {
return true;
}

node = node.parent;
}

return false;
}
91 changes: 91 additions & 0 deletions test/commons/dom/visibility-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,97 @@ describe('dom.visibility-methods', () => {
);
assert.isFalse(overflowHidden(vNode));
});

it('should return false for ancestor with "overflow:hidden" and element with "position:fixed"', () => {
var vNode = queryFixture(
'<div style="overflow: hidden; width: 50px;">' +
'<div id="target" style="margin-left: 100px; position: fixed">Hello world</div>' +
'</div>'
);
assert.isFalse(overflowHidden(vNode));
});

it('should return false for ancestor with "overflow:hidden; position:relative" and element with "position:fixed"', () => {
var vNode = queryFixture(
'<div style="overflow: hidden; width: 50px; position: relative">' +
'<div id="target" style="margin-left: 100px; position: fixed">Hello world</div>' +
'</div>'
);
assert.isFalse(overflowHidden(vNode));
});

it('should return false for ancestor with "overflow:hidden" and element with "position:absolute"', () => {
var vNode = queryFixture(
'<div style="overflow: hidden; width: 50px;">' +
'<div id="target" style="margin-left: 100px; position: absolute">Hello world</div>' +
'</div>'
);
assert.isFalse(overflowHidden(vNode));
});

it('should return true for ancestor with "overflow:hidden; position:relative" and element with "position:absolute"', () => {
var vNode = queryFixture(
'<div style="overflow: hidden; width: 50px; position: relative">' +
'<div id="target" style="margin-left: 100px; position: absolute">Hello world</div>' +
'</div>'
);
assert.isTrue(overflowHidden(vNode));
});

it('should return true for ancestor with "overflow:hidden" and element with "position:absolute" if ancestor in-between uses "position:relative"', () => {
var vNode = queryFixture(
'<div style="overflow: hidden; width: 50px;">' +
'<div style="position: relative">' +
'<div id="target" style="margin-left: 100px; position: absolute">Hello world</div>' +
'</div>' +
'</div>'
);
assert.isTrue(overflowHidden(vNode));
});

it('should return true for ancestor with "overflow:hidden" and element with "position:absolute" if ancestor in-between uses "position:sticky"', () => {
var vNode = queryFixture(
'<div style="overflow: hidden; width: 50px;">' +
'<div style="position: sticky">' +
'<div id="target" style="margin-left: 100px; position: absolute">Hello world</div>' +
'</div>' +
'</div>'
);
assert.isTrue(overflowHidden(vNode));
});

it('should return false for ancestor with "overflow:hidden" and element with "position:absolute" if ancestor in-between uses "position:absolute"', () => {
var vNode = queryFixture(
'<div style="overflow: hidden; width: 50px;">' +
'<div style="position: absolute">' +
'<div id="target" style="margin-left: 100px; position: absolute">Hello world</div>' +
'</div>' +
'</div>'
);
assert.isFalse(overflowHidden(vNode));
});

it('should return false for ancestor with "overflow:hidden" and element with "position:absolute" if ancestor in-between uses "position:fixed"', () => {
var vNode = queryFixture(
'<div style="overflow: hidden; width: 50px;">' +
'<div style="position: fixed">' +
'<div id="target" style="margin-left: 100px; position: absolute">Hello world</div>' +
'</div>' +
'</div>'
);
assert.isFalse(overflowHidden(vNode));
});

it('should return false for ancestor with "overflow:hidden" and element with "position:absolute" if ancestor of overflow node uses position other than static', () => {
var vNode = queryFixture(
'<div style="position: relative">' +
'<div style="overflow: hidden; width: 50px;">' +
'<div id="target" style="margin-left: 100px; position: absolute">Hello world</div>' +
'</div>' +
'</div>'
);
assert.isFalse(overflowHidden(vNode));
});
});

describe('clipHidden', () => {
Expand Down
Loading