Skip to content

Commit a9957dc

Browse files
authored
Merge pull request #978 from Patternslib/fix-bumper
Fix bumper
2 parents 6a7fda2 + edfec61 commit a9957dc

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/core/dom.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,13 @@ function get_css_value(el, property, as_pixels = false, as_float = false) {
177177
* @param {String} [direction=] - Not given: Search for any scrollable element up in the DOM tree.
178178
* ``x``: Search for a horizontally scrollable element.
179179
* ``y``: Search for a vertically scrollable element.
180+
* @param {(DOM Node|null)} [fallback=document.body] - Fallback, if no scroll container can be found.
181+
* The default is to use document.body.
180182
*
181183
* @returns {Node} - Return the first scrollable element.
182184
* If no other element could be found, document.body would be returned.
183185
*/
184-
const find_scroll_container = (el, direction) => {
186+
const find_scroll_container = (el, direction, fallback = document.body) => {
185187
while (el && el !== document.body) {
186188
if (!direction || direction === "y") {
187189
let overflow_y = get_css_value(el, "overflow-y");
@@ -197,7 +199,7 @@ const find_scroll_container = (el, direction) => {
197199
}
198200
el = el.parentElement;
199201
}
200-
return el;
202+
return fallback;
201203
};
202204

203205
const dom = {

src/core/dom.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ describe("core.dom tests", () => {
668668
expect(dom.find_scroll_container(div3, "x")).toBe(div1);
669669
done();
670670
});
671-
it("returns document.body if nothing else is found", function (done) {
671+
it("returns a fallback if given, if nothing else is found", function (done) {
672672
document.body.innerHTML = `
673673
<div
674674
id="div1"
@@ -679,7 +679,7 @@ describe("core.dom tests", () => {
679679
</div>
680680
`;
681681
const div2 = document.querySelector("#div2");
682-
expect(dom.find_scroll_container(div2, "y")).toBe(document.body);
682+
expect(dom.find_scroll_container(div2, "y", null)).toBe(null);
683683
done();
684684
});
685685
});

src/pat/bumper/bumper.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,45 @@ export default Base.extend({
4343
},
4444

4545
_init() {
46-
const scroll_container_y = dom.find_scroll_container(this.el.parentElement, "y");
47-
const scroll_container_x = dom.find_scroll_container(this.el.parentElement, "x");
46+
const scroll_container_y = dom.find_scroll_container(
47+
this.el.parentElement,
48+
"y",
49+
null
50+
);
51+
const scroll_container_x = dom.find_scroll_container(
52+
this.el.parentElement,
53+
"x",
54+
null
55+
);
4856

4957
const pos = {
5058
top: dom.get_css_value(this.el, "top", true),
5159
right: dom.get_css_value(this.el, "right", true),
5260
bottom: dom.get_css_value(this.el, "bottom", true),
5361
left: dom.get_css_value(this.el, "left", true),
5462
};
55-
const intersection_observer_config_y = {
63+
const intersection_observer_config = {
5664
threshold: [1, 0.99, 0.97, 0.96, 0.95, 0.94, 0.93, 0.92, 0.91, 0.9],
57-
root: scroll_container_y,
5865
// add margin as inverted sticky positions.
5966
rootMargin: `${-pos.top - 1}px ${-pos.right - 1}px ${-pos.bottom - 1}px ${-pos.left - 1}px`, // prettier-ignore
6067
};
6168

6269
const observer_y = new IntersectionObserver(
6370
this._intersection_observer_callback.bind(this),
64-
intersection_observer_config_y
71+
{
72+
...intersection_observer_config,
73+
root: scroll_container_y,
74+
}
6575
);
6676
observer_y.observe(this.el);
6777

6878
if (scroll_container_x !== scroll_container_y) {
69-
const intersection_observer_config_x = Object.assign(
70-
{},
71-
intersection_observer_config_y,
72-
{ root: scroll_container_x }
73-
);
7479
const observer_x = new IntersectionObserver(
7580
this._intersection_observer_callback.bind(this),
76-
intersection_observer_config_x
81+
{
82+
...intersection_observer_config,
83+
root: scroll_container_x,
84+
}
7785
);
7886
observer_x.observe(this.el);
7987
}

src/pat/bumper/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
<div id="spacer">
8989
Making some space to the left
9090
</div>
91-
<div class="pat-bumper bumper-2" data-pat-bumper="margin: 20; side: all">
91+
<div class="pat-bumper bumper-2">
9292
Sticky bar.
9393
</div>
9494
<p>

0 commit comments

Comments
 (0)