@@ -37,22 +37,33 @@ const useStickyEvent = <T extends HTMLElement = HTMLDivElement>({
37
37
}
38
38
39
39
let previousStuckState : boolean
40
+ let hasSentinelLeftView : boolean
41
+ let hasHeaderLeftView : boolean
42
+
43
+ const sentinelId = `${ identifier } __sentinel`
40
44
41
45
const intersectionObserver = new IntersectionObserver (
42
46
( entries ) => {
43
47
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
48
54
}
49
55
} )
56
+
57
+ const isStuck = hasSentinelLeftView && ! hasHeaderLeftView
58
+
59
+ if ( isNullOrUndefined ( previousStuckState ) || isStuck !== previousStuckState ) {
60
+ callback ( isStuck )
61
+ previousStuckState = isStuck
62
+ }
50
63
} ,
51
64
{ root : stickyElementParent , threshold : OBSERVER_THRESHOLD , rootMargin : OBSERVER_ROOT_MARGIN } ,
52
65
)
53
66
54
- const sentinelId = `${ identifier } __sentinel`
55
-
56
67
let sentinelElement = document . getElementById ( sentinelId )
57
68
58
69
if ( ! sentinelElement ) {
@@ -76,6 +87,7 @@ const useStickyEvent = <T extends HTMLElement = HTMLDivElement>({
76
87
stickyElementRef . current . appendChild ( sentinelElement )
77
88
78
89
intersectionObserver . observe ( sentinelElement )
90
+ intersectionObserver . observe ( stickyElementRef . current )
79
91
80
92
return ( ) => {
81
93
intersectionObserver . disconnect ( )
0 commit comments