Skip to content

Commit 650e513

Browse files
authored
feat: export the observe method (#473)
1 parent b388555 commit 650e513

File tree

8 files changed

+65
-90
lines changed

8 files changed

+65
-90
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,31 @@ async function loadPolyfills() {
343343
}
344344
```
345345
346+
## Low level API
347+
348+
You can access the [`observe`](src/observe.ts) method, that
349+
`react-intersection-observer` uses internally to create and destroy
350+
IntersectionObserver instances. This allows you to handle more advanced use
351+
cases, where you need full control over when and how observers are created.
352+
353+
```js
354+
import { observe } from 'react-intersection-observer';
355+
const destroy = observe(element, callback, options);
356+
```
357+
358+
| Name | Type | Required | Description |
359+
| ------------ | -------------------------- | -------- | --------------------------------------------------------- |
360+
| **element** | `Element` | true | DOM element to observe |
361+
| **callback** | `ObserverInstanceCallback` | true | The callback function that IntersectionObserver will call |
362+
| **options** | `IntersectionObserverInit` | false | The options for the IntersectionObserver |
363+
364+
The `observe` method returns an `unobserve` function, that you must call in
365+
order to destroy the observer again.
366+
367+
> ⚠️ You most likely won't need this, but it can be useful if you need to handle
368+
> IntersectionObservers outside React, or need full control over how instances
369+
> are created.
370+
346371
[package-url]: https://npmjs.org/package/react-intersection-observer
347372
[npm-version-svg]: https://img.shields.io/npm/v/react-intersection-observer.svg
348373
[npm-minzip-svg]:

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@
177177
"postcss": "^7.0.35",
178178
"prettier": "^2.2.0",
179179
"prettier-plugin-pkg": "^0.8.0",
180-
"prettier-plugin-tailwind": "^2.2.8",
181180
"react": "^17.0.1",
182181
"react-dom": "^17.0.1",
183182
"react-test-renderer": "^17.0.1",

src/InView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import { IntersectionObserverProps, PlainChildrenProps } from './index';
3-
import { observe } from './observers';
3+
import { observe } from './observe';
44

55
type State = {
66
inView: boolean;

src/__tests__/observers.test.ts renamed to src/__tests__/observe.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
import { optionsToId } from '../observers';
1+
import { mockIsIntersecting, intersectionMockInstance } from '../test-utils';
2+
import { optionsToId } from '../observe';
3+
import { observe } from '../';
4+
5+
test('should be able to use observe', () => {
6+
const element = document.createElement('div');
7+
const cb = jest.fn();
8+
const unmount = observe(element, cb, { threshold: 0.1 });
9+
10+
mockIsIntersecting(element, true);
11+
expect(cb).toHaveBeenCalled();
12+
13+
// should be unmounted after unmount
14+
unmount();
15+
expect(() =>
16+
intersectionMockInstance(element),
17+
).toThrowErrorMatchingInlineSnapshot(
18+
`"Failed to find IntersectionObserver for element. Is it being observer?"`,
19+
);
20+
});
221

322
test('should convert options to id', () => {
423
expect(

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as React from 'react';
22
import { InView } from './InView';
33
export { InView } from './InView';
44
export { useInView } from './useInView';
5+
export { observe } from './observe';
6+
57
export default InView;
68

79
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

src/observers.ts renamed to src/observe.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ function createObserver(options: IntersectionObserverInit) {
9292
return instance;
9393
}
9494

95+
/**
96+
* @param element - DOM Element to observe
97+
* @param callback - Callback function to trigger when intersection status changes
98+
* @param options - Intersection Observer options
99+
* @return Function - Cleanup function that should be triggered to unregister the observer
100+
*/
95101
export function observe(
96102
element: Element,
97103
callback: ObserverInstanceCallback,

src/useInView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22
import { InViewHookResponse, IntersectionOptions } from './index';
33
import { useEffect } from 'react';
4-
import { observe } from './observers';
4+
import { observe } from './observe';
55

66
type State = {
77
inView: boolean;

yarn.lock

Lines changed: 10 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,14 +1441,6 @@
14411441
minimatch "^3.0.4"
14421442
strip-json-comments "^3.1.1"
14431443

1444-
"@fullhuman/postcss-purgecss@^2.1.2":
1445-
version "2.3.0"
1446-
resolved "https://registry.yarnpkg.com/@fullhuman/postcss-purgecss/-/postcss-purgecss-2.3.0.tgz#50a954757ec78696615d3e118e3fee2d9291882e"
1447-
integrity sha512-qnKm5dIOyPGJ70kPZ5jiz0I9foVOic0j+cOzNDoo8KoCf6HjicIZ99UfO2OmE7vCYSKAAepEwJtNzpiiZAh9xw==
1448-
dependencies:
1449-
postcss "7.0.32"
1450-
purgecss "^2.3.0"
1451-
14521444
"@fullhuman/postcss-purgecss@^3.0.0":
14531445
version "3.1.3"
14541446
resolved "https://registry.yarnpkg.com/@fullhuman/postcss-purgecss/-/postcss-purgecss-3.1.3.tgz#47af7b87c9bfb3de4bc94a38f875b928fffdf339"
@@ -3558,7 +3550,7 @@ autoprefixer@^10.1.0:
35583550
normalize-range "^0.1.2"
35593551
postcss-value-parser "^4.1.0"
35603552

3561-
autoprefixer@^9, autoprefixer@^9.4.5, autoprefixer@^9.7.2:
3553+
autoprefixer@^9, autoprefixer@^9.7.2:
35623554
version "9.8.6"
35633555
resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-9.8.6.tgz#3b73594ca1bf9266320c5acf1588d74dea74210f"
35643556
integrity sha512-XrvP4VVHdRBCdX1S3WXVD8+RyG9qeb1D5Sn1DeLiG2xfSpzellk5k54xbUERJ3M5DggQxes39UGOTP8CFrEGbg==
@@ -4469,7 +4461,7 @@ chalk@^3.0.0:
44694461
ansi-styles "^4.1.0"
44704462
supports-color "^7.1.0"
44714463

4472-
"chalk@^3.0.0 || ^4.0.0", chalk@^4.0.0, chalk@^4.1.0:
4464+
chalk@^4.0.0, chalk@^4.1.0:
44734465
version "4.1.0"
44744466
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a"
44754467
integrity sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==
@@ -4734,7 +4726,7 @@ color-string@^1.5.4:
47344726
color-name "^1.0.0"
47354727
simple-swizzle "^0.2.2"
47364728

4737-
color@^3.0.0, color@^3.1.2, color@^3.1.3:
4729+
color@^3.0.0, color@^3.1.3:
47384730
version "3.1.3"
47394731
resolved "https://registry.yarnpkg.com/color/-/color-3.1.3.tgz#ca67fb4e7b97d611dcde39eceed422067d91596e"
47404732
integrity sha512-xgXAcTHa2HeFCGLE9Xs/R82hujGtu9Jd9x4NW3T34+OMs7VoPsjwzRczKHvTAHeJwWFwX5j15+MgAppE8ztObQ==
@@ -6785,7 +6777,7 @@ from2@^2.1.0:
67856777
inherits "^2.0.1"
67866778
readable-stream "^2.0.0"
67876779

6788-
[email protected], fs-extra@^8.0.0:
6780+
67896781
version "8.1.0"
67906782
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0"
67916783
integrity sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==
@@ -9766,11 +9758,6 @@ normalize-url@^3.0.0:
97669758
resolved "https://registry.yarnpkg.com/normalize-url/-/normalize-url-3.3.0.tgz#b2e1c4dc4f7c6d57743df733a4f5978d18650559"
97679759
integrity sha512-U+JJi7duF1o+u2pynbp2zXDW2/PADgC30f0GsHZtRh+HOcXHnw137TrNlyxxRvWW5fjKd3bcLHPxofWuCjaeZg==
97689760

9769-
normalize.css@^8.0.1:
9770-
version "8.0.1"
9771-
resolved "https://registry.yarnpkg.com/normalize.css/-/normalize.css-8.0.1.tgz#9b98a208738b9cc2634caacbc42d131c97487bf3"
9772-
integrity sha512-qizSNPO93t1YUuUhP22btGOo3chcvDFqFaj2TRybP0DMxkHOCTYwp3n34fel4a31ORXy4m1Xq0Gyqpb5m33qIg==
9773-
97749761
npm-run-all@^4.1.5:
97759762
version "4.1.5"
97769763
resolved "https://registry.yarnpkg.com/npm-run-all/-/npm-run-all-4.1.5.tgz#04476202a15ee0e2e214080861bff12a51d98fba"
@@ -10487,7 +10474,7 @@ postcss-flexbugs-fixes@^4.1.0:
1048710474
dependencies:
1048810475
postcss "^7.0.26"
1048910476

10490-
postcss-functions@^3, postcss-functions@^3.0.0:
10477+
postcss-functions@^3:
1049110478
version "3.0.0"
1049210479
resolved "https://registry.yarnpkg.com/postcss-functions/-/postcss-functions-3.0.0.tgz#0e94d01444700a481de20de4d55fb2640564250e"
1049310480
integrity sha1-DpTQFERwCkgd4g3k1V+yZAVkJQ4=
@@ -10497,7 +10484,7 @@ postcss-functions@^3, postcss-functions@^3.0.0:
1049710484
postcss "^6.0.9"
1049810485
postcss-value-parser "^3.3.0"
1049910486

10500-
postcss-js@^2, postcss-js@^2.0.0:
10487+
postcss-js@^2:
1050110488
version "2.0.3"
1050210489
resolved "https://registry.yarnpkg.com/postcss-js/-/postcss-js-2.0.3.tgz#a96f0f23ff3d08cec7dc5b11bf11c5f8077cdab9"
1050310490
integrity sha512-zS59pAk3deu6dVHyrGqmC3oDXBdNdajk4k1RyxeVXCrcEDBUBHoIhE4QTsmhxgzXxsaqFDAkUZfmMa5f/N/79w==
@@ -10668,7 +10655,7 @@ postcss-modules@^4.0.0:
1066810655
postcss-modules-values "^4.0.0"
1066910656
string-hash "^1.1.1"
1067010657

10671-
postcss-nested@^4, postcss-nested@^4.1.1:
10658+
postcss-nested@^4:
1067210659
version "4.2.3"
1067310660
resolved "https://registry.yarnpkg.com/postcss-nested/-/postcss-nested-4.2.3.tgz#c6f255b0a720549776d220d00c4b70cd244136f6"
1067410661
integrity sha512-rOv0W1HquRCamWy2kFl3QazJMMe1ku6rCFoAAH+9AcxdbpDeBr6k968MLWuLjvjMcGEip01ak09hKOEgpK9hvw==
@@ -10834,15 +10821,6 @@ postcss-value-parser@^4.0.2, postcss-value-parser@^4.1.0:
1083410821
resolved "https://registry.yarnpkg.com/postcss-value-parser/-/postcss-value-parser-4.1.0.tgz#443f6a20ced6481a2bda4fa8532a6e55d789a2cb"
1083510822
integrity sha512-97DXOFbQJhk71ne5/Mt6cOu6yxsSfM0QGQyl0L25Gca4yGWEGJaig7l7gbCX623VqTBNGLRLaVUCnNkcedlRSQ==
1083610823

10837-
10838-
version "7.0.32"
10839-
resolved "https://registry.yarnpkg.com/postcss/-/postcss-7.0.32.tgz#4310d6ee347053da3433db2be492883d62cec59d"
10840-
integrity sha512-03eXong5NLnNCD05xscnGKGDZ98CyzoqPSMjOe6SuoQY7Z2hIj0Ld1g/O/UQRuOle2aRtiIRDg9tDcTGAkLfKw==
10841-
dependencies:
10842-
chalk "^2.4.2"
10843-
source-map "^0.6.1"
10844-
supports-color "^6.1.0"
10845-
1084610824
postcss@^6.0.9:
1084710825
version "6.0.23"
1084810826
resolved "https://registry.yarnpkg.com/postcss/-/postcss-6.0.23.tgz#61c82cc328ac60e677645f979054eb98bc0e3324"
@@ -10852,7 +10830,7 @@ postcss@^6.0.9:
1085210830
source-map "^0.6.1"
1085310831
supports-color "^5.4.0"
1085410832

10855-
postcss@^7, postcss@^7.0.0, postcss@^7.0.1, postcss@^7.0.11, postcss@^7.0.14, postcss@^7.0.18, postcss@^7.0.26, postcss@^7.0.27, postcss@^7.0.32, postcss@^7.0.35, postcss@^7.0.5, postcss@^7.0.6:
10833+
postcss@^7, postcss@^7.0.0, postcss@^7.0.1, postcss@^7.0.14, postcss@^7.0.18, postcss@^7.0.26, postcss@^7.0.27, postcss@^7.0.32, postcss@^7.0.35, postcss@^7.0.5, postcss@^7.0.6:
1085610834
version "7.0.35"
1085710835
resolved "https://registry.yarnpkg.com/postcss/-/postcss-7.0.35.tgz#d2be00b998f7f211d8a276974079f2e92b970e24"
1085810836
integrity sha512-3QT8bBJeX/S5zKTTjTCIjRF3If4avAT6kqxcASlTWEtAFCb9NH0OUxNDfgZSWdP5fJnBYCMEWkIFfWeugjzYMg==
@@ -10885,16 +10863,7 @@ prettier-plugin-pkg@^0.8.0:
1088510863
resolved "https://registry.yarnpkg.com/prettier-plugin-pkg/-/prettier-plugin-pkg-0.8.0.tgz#379ca5c21a08e8e978a9d0ee0220fe9db36908f7"
1088610864
integrity sha512-o0+gQDn7yQHFB4k667+aZLDuXlefCDzH00A+ExBlHx//CB9Wuj1lw935Hqu0PxGu+PXItsWlIr/wJZmdi8SGeg==
1088710865

10888-
prettier-plugin-tailwind@^2.2.8:
10889-
version "2.2.8"
10890-
resolved "https://registry.yarnpkg.com/prettier-plugin-tailwind/-/prettier-plugin-tailwind-2.2.8.tgz#6e25544a9ca94f671a1befc0133992a6f7f8f7ad"
10891-
integrity sha512-0ViHn1LnRrPj09FXa1bbZF49ybctmDfnZjPI3Iy9jIL3+ZpD/TnbSgAy18xmy8SYw1EsAq5kyOXmOghc8+WipQ==
10892-
dependencies:
10893-
prettier "^2.1.2"
10894-
tailwind-classes-sorter "^0.2.5"
10895-
tailwindcss "^1.8.13"
10896-
10897-
prettier@^2.1.2, prettier@^2.2.0:
10866+
prettier@^2.2.0:
1089810867
version "2.2.1"
1089910868
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
1090010869
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==
@@ -11085,16 +11054,6 @@ punycode@^2.1.0, punycode@^2.1.1:
1108511054
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
1108611055
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==
1108711056

11088-
purgecss@^2.3.0:
11089-
version "2.3.0"
11090-
resolved "https://registry.yarnpkg.com/purgecss/-/purgecss-2.3.0.tgz#5327587abf5795e6541517af8b190a6fb5488bb3"
11091-
integrity sha512-BE5CROfVGsx2XIhxGuZAT7rTH9lLeQx/6M0P7DTXQH4IUc3BBzs9JUzt4yzGf3JrH9enkeq6YJBe9CTtkm1WmQ==
11092-
dependencies:
11093-
commander "^5.0.0"
11094-
glob "^7.0.0"
11095-
postcss "7.0.32"
11096-
postcss-selector-parser "^6.0.2"
11097-
1109811057
purgecss@^3.1.3:
1109911058
version "3.1.3"
1110011059
resolved "https://registry.yarnpkg.com/purgecss/-/purgecss-3.1.3.tgz#26987ec09d12eeadc318e22f6e5a9eb0be094f41"
@@ -11818,7 +11777,7 @@ [email protected]:
1181811777
dependencies:
1181911778
path-parse "^1.0.6"
1182011779

11821-
resolve@^1.1.6, resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.14.2, resolve@^1.17.0, resolve@^1.18.1, resolve@^1.19.0, resolve@^1.3.2:
11780+
resolve@^1.1.6, resolve@^1.10.0, resolve@^1.12.0, resolve@^1.13.1, resolve@^1.17.0, resolve@^1.18.1, resolve@^1.19.0, resolve@^1.3.2:
1182211781
version "1.19.0"
1182311782
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.19.0.tgz#1af5bf630409734a067cae29318aac7fa29a267c"
1182411783
integrity sha512-rArEXAgsBG4UgRGcynxWIWKFvh/XZCcS8UJdHhwy91zwAvCZIbcs+vAbflgBnNjYMs/i/i+/Ux6IZhML1yPvxg==
@@ -12843,41 +12802,6 @@ table@^6.0.4:
1284312802
slice-ansi "^4.0.0"
1284412803
string-width "^4.2.0"
1284512804

12846-
tailwind-classes-sorter@^0.2.5:
12847-
version "0.2.5"
12848-
resolved "https://registry.yarnpkg.com/tailwind-classes-sorter/-/tailwind-classes-sorter-0.2.5.tgz#fa499fb5564783e5edd77259ce1a3ba9e0c8b3ad"
12849-
integrity sha512-7S0AaDitt+Fjy9m3v0GrQ/KZrCswNDD25SSXX9U/HIB5h9lk0ImSkq/VVcnzu78cRQxR0fFfvHYFdfcqN3dCGA==
12850-
dependencies:
12851-
find-up "^5.0.0"
12852-
12853-
tailwindcss@^1.8.13:
12854-
version "1.9.6"
12855-
resolved "https://registry.yarnpkg.com/tailwindcss/-/tailwindcss-1.9.6.tgz#0c5089911d24e1e98e592a31bfdb3d8f34ecf1a0"
12856-
integrity sha512-nY8WYM/RLPqGsPEGEV2z63riyQPcHYZUJpAwdyBzVpxQHOHqHE+F/fvbCeXhdF1+TA5l72vSkZrtYCB9hRcwkQ==
12857-
dependencies:
12858-
"@fullhuman/postcss-purgecss" "^2.1.2"
12859-
autoprefixer "^9.4.5"
12860-
browserslist "^4.12.0"
12861-
bytes "^3.0.0"
12862-
chalk "^3.0.0 || ^4.0.0"
12863-
color "^3.1.2"
12864-
detective "^5.2.0"
12865-
fs-extra "^8.0.0"
12866-
html-tags "^3.1.0"
12867-
lodash "^4.17.20"
12868-
node-emoji "^1.8.1"
12869-
normalize.css "^8.0.1"
12870-
object-hash "^2.0.3"
12871-
postcss "^7.0.11"
12872-
postcss-functions "^3.0.0"
12873-
postcss-js "^2.0.0"
12874-
postcss-nested "^4.1.1"
12875-
postcss-selector-parser "^6.0.0"
12876-
postcss-value-parser "^4.1.0"
12877-
pretty-hrtime "^1.0.3"
12878-
reduce-css-calc "^2.1.6"
12879-
resolve "^1.14.2"
12880-
1288112805
"tailwindcss@npm:@tailwindcss/postcss7-compat":
1288212806
version "2.0.2"
1288312807
resolved "https://registry.yarnpkg.com/@tailwindcss/postcss7-compat/-/postcss7-compat-2.0.2.tgz#49cb21703dfb4447620fceab5cef3285cff8c69d"

0 commit comments

Comments
 (0)