@@ -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,8 +86,12 @@ 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 ;
94
+ record . entry = entry ;
91
95
this . scheduler . scheduleWork ( ( ) => {
92
96
this . callback ( [ entry ] ) ;
93
97
} ) ;
@@ -104,13 +108,13 @@ export class SpanielIntersectionObserver implements IntersectionObserver {
104
108
takeRecords ( ) : IntersectionObserverEntry [ ] {
105
109
return [ ] ;
106
110
}
107
- private generateEntryEvent ( frame : Frame , bcr : DOMRectReadOnly , el : Element ) : EntryEvent {
111
+ private generateEntryEvent ( frame : Frame , bcr : DOMRectReadOnly , el : HTMLElement ) : EntryEvent {
108
112
let count : number = 0 ;
109
113
let entry = generateEntry ( frame , bcr , el , this . rootMarginObj ) ;
110
114
111
115
for ( let i = 0 ; i < this . thresholds . length ; i ++ ) {
112
116
let threshold = this . thresholds [ i ] ;
113
- if ( entrySatisfiesRatio ( entry , threshold ) ) {
117
+ if ( entry . intersectionRatio >= threshold ) {
114
118
count ++ ;
115
119
}
116
120
}
@@ -149,7 +153,7 @@ function addRatio(entryInit: SpanielIntersectionObserverEntryInit): Intersection
149
153
intersectionRect,
150
154
target,
151
155
intersectionRatio,
152
- isIntersecting : null
156
+ isIntersecting : calculateIsIntersecting ( { intersectionRect } )
153
157
} ;
154
158
}
155
159
@@ -179,12 +183,36 @@ export class IntersectionObserverEntry implements IntersectionObserverEntryInit
179
183
};
180
184
*/
181
185
186
+ function emptyRect ( ) : ClientRect | DOMRect {
187
+ return {
188
+ bottom : 0 ,
189
+ height : 0 ,
190
+ left : 0 ,
191
+ right : 0 ,
192
+ top : 0 ,
193
+ width : 0 ,
194
+ x : 0 ,
195
+ y : 0
196
+ } ;
197
+ }
198
+
182
199
export function generateEntry (
183
200
frame : Frame ,
184
201
bcr : DOMRectReadOnly ,
185
- el : Element ,
202
+ el : HTMLElement ,
186
203
rootMargin : DOMMargin
187
204
) : IntersectionObserverEntry {
205
+ if ( el . style . display === 'none' ) {
206
+ return {
207
+ boundingClientRect : emptyRect ( ) ,
208
+ intersectionRatio : 0 ,
209
+ intersectionRect : emptyRect ( ) ,
210
+ isIntersecting : false ,
211
+ rootBounds : emptyRect ( ) ,
212
+ target : el ,
213
+ time : frame . timestamp
214
+ } ;
215
+ }
188
216
let { bottom, right } = bcr ;
189
217
let rootBounds : ClientRect = {
190
218
left : frame . x - rootMargin . left ,
0 commit comments