Skip to content

Commit 3f64067

Browse files
committed
feat(core): scroll-spy-controller onIntersection
1 parent 1044539 commit 3f64067

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export interface ScrollSpyControllerOptions extends IntersectionObserverInit {
2323
* @default el => el.getAttribute('href');
2424
*/
2525
getHash?: (el: Element) => string | null;
26+
/**
27+
* Optional callback for when an intersection occurs
28+
*/
29+
onIntersection?(): void;
2630
}
2731

2832
export class ScrollSpyController implements ReactiveController {
@@ -47,6 +51,7 @@ export class ScrollSpyController implements ReactiveController {
4751

4852
#getRootNode: () => Node;
4953
#getHash: (el: Element) => string | null;
54+
#onIntersection?: () => void;
5055

5156
get #linkChildren(): Element[] {
5257
return Array.from(this.host.querySelectorAll(this.#tagNames.join(',')))
@@ -95,6 +100,7 @@ export class ScrollSpyController implements ReactiveController {
95100
this.#threshold = options.threshold ?? 0.85;
96101
this.#getRootNode = () => options.rootNode ?? host.getRootNode();
97102
this.#getHash = options?.getHash ?? ((el: Element) => el.getAttribute('href'));
103+
this.#onIntersection = options?.onIntersection;
98104
}
99105

100106
hostConnected(): void {
@@ -172,6 +178,7 @@ export class ScrollSpyController implements ReactiveController {
172178
}
173179
this.#initializing = false;
174180
}
181+
this.#onIntersection?.();
175182
}
176183

177184
/**

0 commit comments

Comments
 (0)