Skip to content

Commit 6d315d3

Browse files
authored
Merge pull request #159 from boscop-fr/fix-collision-resolution
Fixed collision resolution
2 parents e13e3d3 + 68fa85d commit 6d315d3

4 files changed

Lines changed: 44 additions & 5 deletions

File tree

e2e/orejime.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ test.describe('Orejime', () => {
4343
<script id="mandatory"></script>
4444
</template>
4545
46+
<button
47+
id="obscuring"
48+
style="position: absolute; bottom: 6rem; right: 3rem; z-index: 9999;"
49+
>
50+
Obscuring
51+
</button>
52+
4653
<button
4754
id="obscured"
4855
style="position: absolute; bottom: 3rem; right: 3rem;"
@@ -254,6 +261,14 @@ test.describe('Orejime', () => {
254261

255262
const position2 = await orejimePage.banner.boundingBox();
256263
await expect(position2).toEqual(initalPosition);
264+
265+
// When a button above the banner takes focus, the
266+
// banner shouldn't be displaced either.
267+
const obscuring = orejimePage.locator('#obscuring');
268+
await obscuring.focus();
269+
270+
const position3 = await orejimePage.banner.boundingBox();
271+
await expect(position3).toEqual(initalPosition);
257272
});
258273

259274
test('should clear consents', async () => {

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"jest": "^28.1.3",
6262
"jest-environment-jsdom": "^28.1.0",
6363
"js-cookie": "^3.0.1",
64-
"micromodal": "^0.4.10",
64+
"micromodal": "^0.6.1",
6565
"postcss-loader": "^8.1.1",
6666
"postcss-preset-env": "^10.1.5",
6767
"preact": "^10.23.2",

src/ui/utils/dom.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ const getPaddedBoundingBox = (element: DOMRect, padding: number) => ({
8989
left: element.left + padding
9090
});
9191

92+
// A naive way to estimate the z-index of an element.
93+
// This does not take each and every stacking context rules
94+
// into account but merely adds up every z-indexes set on
95+
// the element's ancestors.
96+
const getZIndex = (element: HTMLElement): number => {
97+
const base =
98+
element.offsetParent instanceof HTMLElement
99+
? getZIndex(element.offsetParent)
100+
: 0;
101+
102+
const style = window.getComputedStyle(element);
103+
const z = parseInt(style.zIndex, 10) || 0;
104+
105+
return base + z;
106+
};
107+
92108
// Resolves a visual collision between two elements, either
93109
// by scrolling the page or moving one of them.
94110
// We're only resolving collisions on the vertical axis, as
@@ -99,6 +115,14 @@ export const resolveCollision = (fixed: HTMLElement, mobile: HTMLElement) => {
99115
return;
100116
}
101117

118+
// If the fixed element is meant to obscure the mobile
119+
// one (i.e. positionned absolutely above), there is no
120+
// need to move anything.
121+
if (getZIndex(fixed) > getZIndex(mobile)) {
122+
translateElementY(mobile, 0);
123+
return;
124+
}
125+
102126
// We're padding the fixed element's bounding box to
103127
// avoid snapping the mobile one right on its border.
104128
const fixedRect = getPaddedBoundingBox(

0 commit comments

Comments
 (0)