@@ -9,7 +9,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
9
9
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
10
*/
11
11
12
- import { entrySatisfiesRatio } from './utils' ;
12
+ import { calculateIsIntersecting } from './utils' ;
13
13
14
14
import { Frame , ElementScheduler , generateToken } from './metal/index' ;
15
15
@@ -63,7 +63,7 @@ export class SpanielIntersectionObserver implements IntersectionObserver {
63
63
public thresholds : number [ ] ;
64
64
private records : { [ index : string ] : EntryEvent } ;
65
65
66
- observe ( target : Element ) {
66
+ observe ( target : HTMLElement ) {
67
67
let trackedTarget = target as SpanielTrackedElement ;
68
68
69
69
let id = ( trackedTarget . __spanielId = trackedTarget . __spanielId || generateToken ( ) ) ;
@@ -77,7 +77,7 @@ export class SpanielIntersectionObserver implements IntersectionObserver {
77
77
) ;
78
78
return id ;
79
79
}
80
- private onTick ( frame : Frame , id : string , bcr : DOMRectReadOnly , el : Element ) {
80
+ private onTick ( frame : Frame , id : string , bcr : DOMRectReadOnly , el : SpanielTrackedElement ) {
81
81
let { numSatisfiedThresholds, entry } = this . generateEntryEvent ( frame , bcr , el ) ;
82
82
let record : EntryEvent =
83
83
this . records [ id ] ||
@@ -86,7 +86,10 @@ export class SpanielIntersectionObserver implements IntersectionObserver {
86
86
numSatisfiedThresholds : 0
87
87
} ) ;
88
88
89
- if ( numSatisfiedThresholds !== record . numSatisfiedThresholds ) {
89
+ if (
90
+ numSatisfiedThresholds !== record . numSatisfiedThresholds ||
91
+ entry . isIntersecting !== record . entry . isIntersecting
92
+ ) {
90
93
record . numSatisfiedThresholds = numSatisfiedThresholds ;
91
94
this . scheduler . scheduleWork ( ( ) => {
92
95
this . callback ( [ entry ] ) ;
@@ -104,13 +107,13 @@ export class SpanielIntersectionObserver implements IntersectionObserver {
104
107
takeRecords ( ) : IntersectionObserverEntry [ ] {
105
108
return [ ] ;
106
109
}
107
- private generateEntryEvent ( frame : Frame , bcr : DOMRectReadOnly , el : Element ) : EntryEvent {
110
+ private generateEntryEvent ( frame : Frame , bcr : DOMRectReadOnly , el : HTMLElement ) : EntryEvent {
108
111
let count : number = 0 ;
109
112
let entry = generateEntry ( frame , bcr , el , this . rootMarginObj ) ;
110
113
111
114
for ( let i = 0 ; i < this . thresholds . length ; i ++ ) {
112
115
let threshold = this . thresholds [ i ] ;
113
- if ( entrySatisfiesRatio ( entry , threshold ) ) {
116
+ if ( entry . intersectionRatio >= threshold ) {
114
117
count ++ ;
115
118
}
116
119
}
@@ -149,7 +152,7 @@ function addRatio(entryInit: SpanielIntersectionObserverEntryInit): Intersection
149
152
intersectionRect,
150
153
target,
151
154
intersectionRatio,
152
- isIntersecting : null
155
+ isIntersecting : calculateIsIntersecting ( { intersectionRect } )
153
156
} ;
154
157
}
155
158
@@ -179,12 +182,36 @@ export class IntersectionObserverEntry implements IntersectionObserverEntryInit
179
182
};
180
183
*/
181
184
185
+ function emptyRect ( ) : ClientRect | DOMRect {
186
+ return {
187
+ bottom : 0 ,
188
+ height : 0 ,
189
+ left : 0 ,
190
+ right : 0 ,
191
+ top : 0 ,
192
+ width : 0 ,
193
+ x : 0 ,
194
+ y : 0
195
+ } ;
196
+ }
197
+
182
198
export function generateEntry (
183
199
frame : Frame ,
184
200
bcr : DOMRectReadOnly ,
185
- el : Element ,
201
+ el : HTMLElement ,
186
202
rootMargin : DOMMargin
187
203
) : IntersectionObserverEntry {
204
+ if ( el . style . display === 'none' ) {
205
+ return {
206
+ boundingClientRect : emptyRect ( ) ,
207
+ intersectionRatio : 0 ,
208
+ intersectionRect : emptyRect ( ) ,
209
+ isIntersecting : false ,
210
+ rootBounds : emptyRect ( ) ,
211
+ target : el ,
212
+ time : frame . timestamp
213
+ } ;
214
+ }
188
215
let { bottom, right } = bcr ;
189
216
let rootBounds : ClientRect = {
190
217
left : frame . x - rootMargin . left ,
0 commit comments