Skip to content

Commit 1044539

Browse files
committed
fix(core): activate scroll spy link on connect
1 parent 3d48094 commit 1044539

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

core/pfe-core/controllers/scroll-spy-controller.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class ScrollSpyController implements ReactiveController {
4343
#root: ScrollSpyControllerOptions['root'];
4444
#rootMargin?: string;
4545
#threshold: number | number[];
46+
#intersectingElements: Element[] = [];
4647

4748
#getRootNode: () => Node;
4849
#getHash: (el: Element) => string | null;
@@ -100,7 +101,9 @@ export class ScrollSpyController implements ReactiveController {
100101
this.#initIo();
101102
}
102103

103-
#initIo() {
104+
#initializing = true;
105+
106+
async #initIo() {
104107
const rootNode = this.#getRootNode();
105108
if (rootNode instanceof Document || rootNode instanceof ShadowRoot) {
106109
const { rootMargin, threshold, root } = this;
@@ -151,6 +154,24 @@ export class ScrollSpyController implements ReactiveController {
151154
this.#setActive(last ?? this.#linkChildren.at(0));
152155
}
153156
this.#intersected = true;
157+
this.#intersectingElements =
158+
entries
159+
.filter(x => x.isIntersecting)
160+
.map(x => x.target);
161+
if (this.#initializing) {
162+
const ints = entries?.filter(x => x.isIntersecting) ?? [];
163+
if (this.#intersectingElements) {
164+
const [{ target = null } = {}] = ints;
165+
const { id } = target ?? {};
166+
if (id) {
167+
const link = this.#linkChildren.find(link => this.#getHash(link) === `#${id}`);
168+
if (link) {
169+
this.#setActive(link);
170+
}
171+
}
172+
}
173+
this.#initializing = false;
174+
}
154175
}
155176

156177
/**

0 commit comments

Comments
 (0)