Skip to content

Commit 8aca3c1

Browse files
authored
Merge pull request #32 from ChromeQ/master
Add types for extra IntersectionObserverEntry props to reach the callback
2 parents 52a5b4c + 7be5360 commit 8aca3c1

File tree

6 files changed

+63
-7
lines changed

6 files changed

+63
-7
lines changed

example/intersection-observer/global-observer/GlobalObserver.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ import * as React from 'react';
22
import { useRef, ReactElement } from 'react';
33
import useIntersection from './useIntersection';
44

5-
export const Section = ({ number }: { number: number }): ReactElement => {
5+
export const Section = ({
6+
number,
7+
callback,
8+
}: {
9+
number: number;
10+
callback?: IntersectionObserverCallback;
11+
}): ReactElement => {
612
const ref = useRef(null);
7-
const isIntersecting = useIntersection(ref);
13+
const isIntersecting = useIntersection(ref, callback);
814

915
return (
1016
<section

example/intersection-observer/global-observer/useIntersection.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ function createObserver() {
2525
);
2626
}
2727

28-
const useIntersection = (ref: MutableRefObject<HTMLElement | null>) => {
28+
const useIntersection = (
29+
ref: MutableRefObject<HTMLElement | null>,
30+
callback?: IntersectionObserverCallback
31+
) => {
2932
const [isIntersecting, setIsIntersecting] = useState(false);
3033

3134
useEffect(() => {
@@ -39,6 +42,7 @@ const useIntersection = (ref: MutableRefObject<HTMLElement | null>) => {
3942

4043
entryCallbacks[domId.toString()] = entry => {
4144
setIsIntersecting(entry.isIntersecting);
45+
callback?.([entry], observer);
4246
};
4347

4448
node.dataset._ioid = domId.toString();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"ts-jest": "^26.5.4",
9292
"tsdx": "^0.14.1",
9393
"tslib": "^2.1.0",
94+
"type-fest": "^2.12.0",
9495
"typescript": "^4.2.3"
9596
},
9697
"resolutions": {

src/mocks/intersection-observer.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
type IntersectionDescription = {
2-
isIntersecting: boolean;
3-
target?: HTMLElement;
1+
import { Mutable, PartialDeep } from 'type-fest';
2+
3+
export type IntersectionDescription = Omit<
4+
PartialDeep<Mutable<IntersectionObserverEntry>>,
5+
'target'
6+
> & {
7+
target?: Element;
48
};
59

610
type State = {

test/intersection-observer.test.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import * as React from 'react';
22
import { render, act, screen } from '@testing-library/react';
33
import '@testing-library/jest-dom/extend-expect';
44

5-
import { mockIntersectionObserver } from '../src/mocks/intersection-observer';
5+
import {
6+
mockIntersectionObserver,
7+
IntersectionDescription,
8+
} from '../src/mocks/intersection-observer';
69

710
import App, {
811
Section,
@@ -200,4 +203,37 @@ describe('Section is intersecting', () => {
200203
'A section 9 - not intersecting',
201204
]);
202205
});
206+
207+
it('should receive intersection options to the callback', () => {
208+
const cb = jest.fn();
209+
const options: IntersectionDescription = {
210+
intersectionRatio: 1,
211+
rootBounds: {
212+
x: 0,
213+
y: 0,
214+
height: 100,
215+
width: 100,
216+
},
217+
};
218+
219+
render(<Section number={1} callback={cb} />);
220+
221+
expect(cb).not.toHaveBeenCalled();
222+
223+
act(() => {
224+
intersectionObserver.enterNode(screen.getByText(/A section 1/), options);
225+
});
226+
227+
const [entries, _observer] = cb.mock.calls[0];
228+
229+
expect(cb).toHaveBeenCalledTimes(1);
230+
expect(entries).toHaveLength(1); // Number of entries
231+
expect(entries[0]).toEqual(
232+
expect.objectContaining({
233+
...options,
234+
isIntersecting: true,
235+
})
236+
);
237+
expect(entries[0].target instanceof HTMLElement).toBe(true);
238+
});
203239
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8661,6 +8661,11 @@ type-fest@^0.8.1:
86618661
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"
86628662
integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==
86638663

8664+
type-fest@^2.12.0:
8665+
version "2.12.0"
8666+
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-2.12.0.tgz#ce342f58cab9114912f54b493d60ab39c3fc82b6"
8667+
integrity sha512-Qe5GRT+n/4GoqCNGGVp5Snapg1Omq3V7irBJB3EaKsp7HWDo5Gv2d/67gfNyV+d5EXD+x/RF5l1h4yJ7qNkcGA==
8668+
86648669
typedarray-to-buffer@^3.1.5:
86658670
version "3.1.5"
86668671
resolved "https://registry.yarnpkg.com/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080"

0 commit comments

Comments
 (0)