Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ export default class IntersectionObserverEntry {
*/
// $FlowFixMe[unsafe-getters-setters]
get intersectionRatio(): number {
const intersectionRect = this.intersectionRect;
const boundingClientRect = this.boundingClientRect;
const intersectionRect = this._nativeEntry.intersectionRect;
const targetRect = this._nativeEntry.targetRect;
const width = targetRect[2];
const height = targetRect[3];

if (boundingClientRect.width === 0 || boundingClientRect.height === 0) {
if (width === 0 || height === 0) {
return 0;
}

const ratio =
(intersectionRect.width * intersectionRect.height) /
(boundingClientRect.width * boundingClientRect.height);
const intersectionArea =
intersectionRect == null ? 0 : intersectionRect[2] * intersectionRect[3];
const ratio = intersectionArea / (width * height);

// Prevent rounding errors from making this value greater than 1.
return Math.min(ratio, 1);
Expand All @@ -80,23 +82,18 @@ export default class IntersectionObserverEntry {
*/
// $FlowFixMe[unsafe-getters-setters]
get rnRootIntersectionRatio(): number {
const intersectionRect = this.intersectionRect;

const intersectionRect = this._nativeEntry.intersectionRect;
const rootRect = this._nativeEntry.rootRect;
const boundingRootRect = new DOMRectReadOnly(
rootRect[0],
rootRect[1],
rootRect[2],
rootRect[3],
);
const width = rootRect[2];
const height = rootRect[3];

if (boundingRootRect.width === 0 || boundingRootRect.height === 0) {
if (width === 0 || height === 0) {
return 0;
}

const ratio =
(intersectionRect.width * intersectionRect.height) /
(boundingRootRect.width * boundingRootRect.height);
const intersectionArea =
intersectionRect == null ? 0 : intersectionRect[2] * intersectionRect[3];
const ratio = intersectionArea / (width * height);

// Prevent rounding errors from making this value greater than 1.
return Math.min(ratio, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5164,6 +5164,37 @@ describe('IntersectionObserver', () => {
});
});

it('should keep entry ratios stable after later scrolling updates', () => {
const scrollRef = createRef<HostInstance>();
const targetRef = createRef<HostInstance>();
const root = Fantom.createRoot({viewportWidth: 100, viewportHeight: 200});
const callback = jest.fn();
Fantom.runTask(() => {
root.render(
<ScrollView ref={scrollRef}>
<View ref={targetRef} style={{width: 100, height: 100}} />
<View style={{width: 100, height: 1000}} />
</ScrollView>,
);
});
Fantom.runTask(() => {
observer = new IntersectionObserver(callback, {threshold: [0, 0.5, 1]});
observer.observe(nullthrows(targetRef.current));
});
const initialEntry = callback.mock.lastCall[0][0];
Fantom.scrollTo(nullthrows(scrollRef.current), {x: 0, y: 50});
const updatedEntry = callback.mock.lastCall[0][0];
expect(callback).toHaveBeenCalledTimes(2);
for (let i = 0; i < 2; i++) {
expect(initialEntry.intersectionRatio).toBe(1);
expect(initialEntry.rnRootIntersectionRatio).toBe(0.5);
expect(updatedEntry.intersectionRatio).toBe(0.5);
expect(updatedEntry.rnRootIntersectionRatio).toBe(0.25);
}
Fantom.runTask(() => observer.disconnect());
root.destroy();
});

describe('disconnect', () => {
it('should do nothing if no targets are observed (not fail)', () => {
const callback = jest.fn();
Expand Down
Loading