Skip to content

Commit aa82357

Browse files
committed
fix: useStickyEvent intersection handler callback
1 parent aef2048 commit aa82357

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/Shared/Hooks/useStickyEvent/useStickyEvent.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,33 @@ const useStickyEvent = <T extends HTMLElement = HTMLDivElement>({
3737
}
3838

3939
let previousStuckState: boolean
40+
let hasSentinelLeftView: boolean
41+
let hasHeaderLeftView: boolean
42+
43+
const sentinelId = `${identifier}__sentinel`
4044

4145
const intersectionObserver = new IntersectionObserver(
4246
(entries) => {
4347
entries.forEach((entry) => {
44-
const isStuck = !entry.isIntersecting
45-
if (isNullOrUndefined(previousStuckState) || isStuck !== previousStuckState) {
46-
callback(isStuck)
47-
previousStuckState = isStuck
48+
switch (entry.target.id) {
49+
case sentinelId:
50+
hasSentinelLeftView = !entry.isIntersecting
51+
break
52+
default:
53+
hasHeaderLeftView = !entry.isIntersecting
4854
}
4955
})
56+
57+
const isStuck = hasSentinelLeftView && !hasHeaderLeftView
58+
59+
if (isNullOrUndefined(previousStuckState) || isStuck !== previousStuckState) {
60+
callback(isStuck)
61+
previousStuckState = isStuck
62+
}
5063
},
5164
{ root: stickyElementParent, threshold: OBSERVER_THRESHOLD, rootMargin: OBSERVER_ROOT_MARGIN },
5265
)
5366

54-
const sentinelId = `${identifier}__sentinel`
55-
5667
let sentinelElement = document.getElementById(sentinelId)
5768

5869
if (!sentinelElement) {
@@ -76,6 +87,7 @@ const useStickyEvent = <T extends HTMLElement = HTMLDivElement>({
7687
stickyElementRef.current.appendChild(sentinelElement)
7788

7889
intersectionObserver.observe(sentinelElement)
90+
intersectionObserver.observe(stickyElementRef.current)
7991

8092
return () => {
8193
intersectionObserver.disconnect()

0 commit comments

Comments
 (0)