Skip to content

Commit e0a6bef

Browse files
authored
Merge pull request #1146 from Patternslib/scroll-marker--fix-scroll-container
fix(pat scroll-marker): Use the correct scroll container
2 parents b118834 + be6723f commit e0a6bef

File tree

3 files changed

+280
-75
lines changed

3 files changed

+280
-75
lines changed

src/core/dom.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ const querySelectorAllAndMe = (el, selector) => {
4747
/**
4848
* Wrap a element with a wrapper element.
4949
*
50+
* The element to be wrapped will be moved into the wrapper element and the
51+
* wrapper element is placed just before the old element was.
52+
*
5053
* @param {Node} el - The DOM node to wrap.
54+
* @param {Node} wrapper - The wrapper element.
5155
*/
5256
const wrap = (el, wrapper) => {
5357
// See: https://stackoverflow.com/a/13169465/1337474

src/pat/scroll-marker/scroll-marker.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Pattern extends BasePattern {
3333

3434
async init() {
3535
// Get all elements that are referenced by links in the current page.
36-
this.observeables = new Map(
36+
this.observables = new Map(
3737
[...dom.querySelectorAllAndMe(this.el, "a[href^='#']")]
3838
.map(
3939
// Create the structure:
@@ -50,7 +50,19 @@ class Pattern extends BasePattern {
5050
)
5151
);
5252

53-
this.scroll_container = dom.find_scroll_container(this.el, "y", window);
53+
// Find a possible scroll container based on the first target/content
54+
// element in the observables map.
55+
// Note, a Map's values method returns an iterator.
56+
const first_observable = this.observables.values().next();
57+
if (!first_observable.value) {
58+
// No targets found.
59+
return;
60+
}
61+
this.scroll_container = dom.find_scroll_container(
62+
first_observable.value.target,
63+
"y",
64+
window
65+
);
5466
// window.innerHeight or el.clientHeight
5567
const scroll_container_height =
5668
typeof this.scroll_container.innerHeight !== "undefined"
@@ -85,7 +97,7 @@ class Pattern extends BasePattern {
8597
threshold: utils.threshold_list(10),
8698
}
8799
);
88-
for (const it of this.observeables.values()) {
100+
for (const it of this.observables.values()) {
89101
observer.observe(it.target);
90102
}
91103
}
@@ -97,7 +109,7 @@ class Pattern extends BasePattern {
97109
for (const entry of entries) {
98110
// Set the in-view class on the link.
99111
const id = entry.target.getAttribute("id");
100-
const item = this.observeables.get(id);
112+
const item = this.observables.get(id);
101113
if (!item) {
102114
continue;
103115
}
@@ -122,13 +134,13 @@ class Pattern extends BasePattern {
122134
// Set the item which is nearest to the scroll container's middle to current.
123135

124136
// First, set all to non-current.
125-
for (const item of this.observeables.values()) {
137+
for (const item of this.observables.values()) {
126138
item.link.classList.remove(this.options["current-class"]);
127139
item.target.classList.remove(this.options["current-content-class"]);
128140
}
129141

130142
// Sort by distance to the middle of the scroll container.
131-
let items_by_weight = [...this.observeables.values()].map((it) => it.target);
143+
let items_by_weight = [...this.observables.values()].map((it) => it.target);
132144
log.debug("items_by_weight initial: ", items_by_weight);
133145

134146
if (this.options.visibility === "most-visible") {
@@ -216,7 +228,7 @@ class Pattern extends BasePattern {
216228

217229
// Finally, set the nearest to current.
218230
const nearest = items_by_weight[0];
219-
const nearest_link = this.observeables.get(nearest?.getAttribute("id"))?.link;
231+
const nearest_link = this.observables.get(nearest?.getAttribute("id"))?.link;
220232
log.debug("nearest: ", nearest);
221233
log.debug("nearest_link: ", nearest_link);
222234
if (nearest_link) {

0 commit comments

Comments
 (0)