Skip to content

Commit 2aca2e9

Browse files
committed
fix(core): prevent race condition on programmatic scroll
1 parent 6f49a79 commit 2aca2e9

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export class ScrollSpyController implements ReactiveController {
6666
/** Has the intersection observer found an element? */
6767
#intersected = false;
6868

69+
#destinationTarget: Element | null = null;
70+
6971
#root: ScrollSpyControllerOptions['root'];
7072

7173
#rootMargin?: string;
@@ -201,6 +203,12 @@ export class ScrollSpyController implements ReactiveController {
201203
}
202204

203205
async #onIo(entries: IntersectionObserverEntry[]) {
206+
if (this.#force && this.#destinationTarget) {
207+
if (entries.some(e => e.target === this.#destinationTarget && e.isIntersecting)) {
208+
this.#force = false;
209+
this.#destinationTarget = null;
210+
}
211+
}
204212
if (!this.#force) {
205213
for (const { target, boundingClientRect, intersectionRect } of entries) {
206214
const selector = `:is(${this.#tagNames.join(',')})[href="#${target.id}"]`;
@@ -244,14 +252,13 @@ export class ScrollSpyController implements ReactiveController {
244252
public async setActive(link: EventTarget | null): Promise<void> {
245253
this.#force = true;
246254
this.#setActive(link);
255+
this.#destinationTarget = this.#linkTargetMap.get(link as Element) ?? null;
247256
let sawActive = false;
248257
for (const child of this.#linkChildren) {
249258
this.#markPassed(child, !sawActive);
250259
if (child === link) {
251260
sawActive = true;
252261
}
253262
}
254-
await this.#nextIntersection();
255-
this.#force = false;
256263
}
257264
}

0 commit comments

Comments
 (0)