Skip to content

Commit

Permalink
feat: export the observe method (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
thebuilder authored May 14, 2021
1 parent b388555 commit 650e513
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 90 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,31 @@ async function loadPolyfills() {
}
```
## Low level API
You can access the [`observe`](src/observe.ts) method, that
`react-intersection-observer` uses internally to create and destroy
IntersectionObserver instances. This allows you to handle more advanced use
cases, where you need full control over when and how observers are created.
```js
import { observe } from 'react-intersection-observer';
const destroy = observe(element, callback, options);
```
| Name | Type | Required | Description |
| ------------ | -------------------------- | -------- | --------------------------------------------------------- |
| **element** | `Element` | true | DOM element to observe |
| **callback** | `ObserverInstanceCallback` | true | The callback function that IntersectionObserver will call |
| **options** | `IntersectionObserverInit` | false | The options for the IntersectionObserver |
The `observe` method returns an `unobserve` function, that you must call in
order to destroy the observer again.
> ⚠️ You most likely won't need this, but it can be useful if you need to handle
> IntersectionObservers outside React, or need full control over how instances
> are created.
[package-url]: https://npmjs.org/package/react-intersection-observer
[npm-version-svg]: https://img.shields.io/npm/v/react-intersection-observer.svg
[npm-minzip-svg]:
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@
"postcss": "^7.0.35",
"prettier": "^2.2.0",
"prettier-plugin-pkg": "^0.8.0",
"prettier-plugin-tailwind": "^2.2.8",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-test-renderer": "^17.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/InView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { IntersectionObserverProps, PlainChildrenProps } from './index';
import { observe } from './observers';
import { observe } from './observe';

type State = {
inView: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import { optionsToId } from '../observers';
import { mockIsIntersecting, intersectionMockInstance } from '../test-utils';
import { optionsToId } from '../observe';
import { observe } from '../';

test('should be able to use observe', () => {
const element = document.createElement('div');
const cb = jest.fn();
const unmount = observe(element, cb, { threshold: 0.1 });

mockIsIntersecting(element, true);
expect(cb).toHaveBeenCalled();

// should be unmounted after unmount
unmount();
expect(() =>
intersectionMockInstance(element),
).toThrowErrorMatchingInlineSnapshot(
`"Failed to find IntersectionObserver for element. Is it being observer?"`,
);
});

test('should convert options to id', () => {
expect(
Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as React from 'react';
import { InView } from './InView';
export { InView } from './InView';
export { useInView } from './useInView';
export { observe } from './observe';

export default InView;

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
Expand Down
6 changes: 6 additions & 0 deletions src/observers.ts → src/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ function createObserver(options: IntersectionObserverInit) {
return instance;
}

/**
* @param element - DOM Element to observe
* @param callback - Callback function to trigger when intersection status changes
* @param options - Intersection Observer options
* @return Function - Cleanup function that should be triggered to unregister the observer
*/
export function observe(
element: Element,
callback: ObserverInstanceCallback,
Expand Down
2 changes: 1 addition & 1 deletion src/useInView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { InViewHookResponse, IntersectionOptions } from './index';
import { useEffect } from 'react';
import { observe } from './observers';
import { observe } from './observe';

type State = {
inView: boolean;
Expand Down
96 changes: 10 additions & 86 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1441,14 +1441,6 @@
minimatch "^3.0.4"
strip-json-comments "^3.1.1"

"@fullhuman/postcss-purgecss@^2.1.2":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@fullhuman/postcss-purgecss/-/postcss-purgecss-2.3.0.tgz#50a954757ec78696615d3e118e3fee2d9291882e"
integrity sha512-qnKm5dIOyPGJ70kPZ5jiz0I9foVOic0j+cOzNDoo8KoCf6HjicIZ99UfO2OmE7vCYSKAAepEwJtNzpiiZAh9xw==
dependencies:
postcss "7.0.32"
purgecss "^2.3.0"

"@fullhuman/postcss-purgecss@^3.0.0":
version "3.1.3"
resolved "https://registry.yarnpkg.com/@fullhuman/postcss-purgecss/-/postcss-purgecss-3.1.3.tgz#47af7b87c9bfb3de4bc94a38f875b928fffdf339"
Expand Down Expand Up @@ -3558,7 +3550,7 @@ autoprefixer@^10.1.0:
normalize-range "^0.1.2"
postcss-value-parser "^4.1.0"

autoprefixer@^9, autoprefixer@^9.4.5, autoprefixer@^9.7.2:
autoprefixer@^9, autoprefixer@^9.7.2:
version "9.8.6"
resolved "https://registry.yarnpkg.com/autoprefixer/-/autoprefixer-9.8.6.tgz#3b73594ca1bf9266320c5acf1588d74dea74210f"
integrity sha512-XrvP4VVHdRBCdX1S3WXVD8+RyG9qeb1D5Sn1DeLiG2xfSpzellk5k54xbUERJ3M5DggQxes39UGOTP8CFrEGbg==
Expand Down Expand Up @@ -4469,7 +4461,7 @@ chalk@^3.0.0:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

"chalk@^3.0.0 || ^4.0.0", chalk@^4.0.0, chalk@^4.1.0:
chalk@^4.0.0, chalk@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a"
integrity sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==
Expand Down Expand Up @@ -4734,7 +4726,7 @@ color-string@^1.5.4:
color-name "^1.0.0"
simple-swizzle "^0.2.2"

color@^3.0.0, color@^3.1.2, color@^3.1.3:
color@^3.0.0, color@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/color/-/color-3.1.3.tgz#ca67fb4e7b97d611dcde39eceed422067d91596e"
integrity sha512-xgXAcTHa2HeFCGLE9Xs/R82hujGtu9Jd9x4NW3T34+OMs7VoPsjwzRczKHvTAHeJwWFwX5j15+MgAppE8ztObQ==
Expand Down Expand Up @@ -6785,7 +6777,7 @@ from2@^2.1.0:
inherits "^2.0.1"
readable-stream "^2.0.0"

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

normalize.css@^8.0.1:
version "8.0.1"
resolved "https://registry.yarnpkg.com/normalize.css/-/normalize.css-8.0.1.tgz#9b98a208738b9cc2634caacbc42d131c97487bf3"
integrity sha512-qizSNPO93t1YUuUhP22btGOo3chcvDFqFaj2TRybP0DMxkHOCTYwp3n34fel4a31ORXy4m1Xq0Gyqpb5m33qIg==

npm-run-all@^4.1.5:
version "4.1.5"
resolved "https://registry.yarnpkg.com/npm-run-all/-/npm-run-all-4.1.5.tgz#04476202a15ee0e2e214080861bff12a51d98fba"
Expand Down Expand Up @@ -10487,7 +10474,7 @@ postcss-flexbugs-fixes@^4.1.0:
dependencies:
postcss "^7.0.26"

postcss-functions@^3, postcss-functions@^3.0.0:
postcss-functions@^3:
version "3.0.0"
resolved "https://registry.yarnpkg.com/postcss-functions/-/postcss-functions-3.0.0.tgz#0e94d01444700a481de20de4d55fb2640564250e"
integrity sha1-DpTQFERwCkgd4g3k1V+yZAVkJQ4=
Expand All @@ -10497,7 +10484,7 @@ postcss-functions@^3, postcss-functions@^3.0.0:
postcss "^6.0.9"
postcss-value-parser "^3.3.0"

postcss-js@^2, postcss-js@^2.0.0:
postcss-js@^2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/postcss-js/-/postcss-js-2.0.3.tgz#a96f0f23ff3d08cec7dc5b11bf11c5f8077cdab9"
integrity sha512-zS59pAk3deu6dVHyrGqmC3oDXBdNdajk4k1RyxeVXCrcEDBUBHoIhE4QTsmhxgzXxsaqFDAkUZfmMa5f/N/79w==
Expand Down Expand Up @@ -10668,7 +10655,7 @@ postcss-modules@^4.0.0:
postcss-modules-values "^4.0.0"
string-hash "^1.1.1"

postcss-nested@^4, postcss-nested@^4.1.1:
postcss-nested@^4:
version "4.2.3"
resolved "https://registry.yarnpkg.com/postcss-nested/-/postcss-nested-4.2.3.tgz#c6f255b0a720549776d220d00c4b70cd244136f6"
integrity sha512-rOv0W1HquRCamWy2kFl3QazJMMe1ku6rCFoAAH+9AcxdbpDeBr6k968MLWuLjvjMcGEip01ak09hKOEgpK9hvw==
Expand Down Expand Up @@ -10834,15 +10821,6 @@ postcss-value-parser@^4.0.2, postcss-value-parser@^4.1.0:
resolved "https://registry.yarnpkg.com/postcss-value-parser/-/postcss-value-parser-4.1.0.tgz#443f6a20ced6481a2bda4fa8532a6e55d789a2cb"
integrity sha512-97DXOFbQJhk71ne5/Mt6cOu6yxsSfM0QGQyl0L25Gca4yGWEGJaig7l7gbCX623VqTBNGLRLaVUCnNkcedlRSQ==

[email protected]:
version "7.0.32"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-7.0.32.tgz#4310d6ee347053da3433db2be492883d62cec59d"
integrity sha512-03eXong5NLnNCD05xscnGKGDZ98CyzoqPSMjOe6SuoQY7Z2hIj0Ld1g/O/UQRuOle2aRtiIRDg9tDcTGAkLfKw==
dependencies:
chalk "^2.4.2"
source-map "^0.6.1"
supports-color "^6.1.0"

postcss@^6.0.9:
version "6.0.23"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-6.0.23.tgz#61c82cc328ac60e677645f979054eb98bc0e3324"
Expand All @@ -10852,7 +10830,7 @@ postcss@^6.0.9:
source-map "^0.6.1"
supports-color "^5.4.0"

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:
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:
version "7.0.35"
resolved "https://registry.yarnpkg.com/postcss/-/postcss-7.0.35.tgz#d2be00b998f7f211d8a276974079f2e92b970e24"
integrity sha512-3QT8bBJeX/S5zKTTjTCIjRF3If4avAT6kqxcASlTWEtAFCb9NH0OUxNDfgZSWdP5fJnBYCMEWkIFfWeugjzYMg==
Expand Down Expand Up @@ -10885,16 +10863,7 @@ prettier-plugin-pkg@^0.8.0:
resolved "https://registry.yarnpkg.com/prettier-plugin-pkg/-/prettier-plugin-pkg-0.8.0.tgz#379ca5c21a08e8e978a9d0ee0220fe9db36908f7"
integrity sha512-o0+gQDn7yQHFB4k667+aZLDuXlefCDzH00A+ExBlHx//CB9Wuj1lw935Hqu0PxGu+PXItsWlIr/wJZmdi8SGeg==

prettier-plugin-tailwind@^2.2.8:
version "2.2.8"
resolved "https://registry.yarnpkg.com/prettier-plugin-tailwind/-/prettier-plugin-tailwind-2.2.8.tgz#6e25544a9ca94f671a1befc0133992a6f7f8f7ad"
integrity sha512-0ViHn1LnRrPj09FXa1bbZF49ybctmDfnZjPI3Iy9jIL3+ZpD/TnbSgAy18xmy8SYw1EsAq5kyOXmOghc8+WipQ==
dependencies:
prettier "^2.1.2"
tailwind-classes-sorter "^0.2.5"
tailwindcss "^1.8.13"

prettier@^2.1.2, prettier@^2.2.0:
prettier@^2.2.0:
version "2.2.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==
Expand Down Expand Up @@ -11085,16 +11054,6 @@ punycode@^2.1.0, punycode@^2.1.1:
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"
integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==

purgecss@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/purgecss/-/purgecss-2.3.0.tgz#5327587abf5795e6541517af8b190a6fb5488bb3"
integrity sha512-BE5CROfVGsx2XIhxGuZAT7rTH9lLeQx/6M0P7DTXQH4IUc3BBzs9JUzt4yzGf3JrH9enkeq6YJBe9CTtkm1WmQ==
dependencies:
commander "^5.0.0"
glob "^7.0.0"
postcss "7.0.32"
postcss-selector-parser "^6.0.2"

purgecss@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/purgecss/-/purgecss-3.1.3.tgz#26987ec09d12eeadc318e22f6e5a9eb0be094f41"
Expand Down Expand Up @@ -11818,7 +11777,7 @@ [email protected]:
dependencies:
path-parse "^1.0.6"

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:
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:
version "1.19.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.19.0.tgz#1af5bf630409734a067cae29318aac7fa29a267c"
integrity sha512-rArEXAgsBG4UgRGcynxWIWKFvh/XZCcS8UJdHhwy91zwAvCZIbcs+vAbflgBnNjYMs/i/i+/Ux6IZhML1yPvxg==
Expand Down Expand Up @@ -12843,41 +12802,6 @@ table@^6.0.4:
slice-ansi "^4.0.0"
string-width "^4.2.0"

tailwind-classes-sorter@^0.2.5:
version "0.2.5"
resolved "https://registry.yarnpkg.com/tailwind-classes-sorter/-/tailwind-classes-sorter-0.2.5.tgz#fa499fb5564783e5edd77259ce1a3ba9e0c8b3ad"
integrity sha512-7S0AaDitt+Fjy9m3v0GrQ/KZrCswNDD25SSXX9U/HIB5h9lk0ImSkq/VVcnzu78cRQxR0fFfvHYFdfcqN3dCGA==
dependencies:
find-up "^5.0.0"

tailwindcss@^1.8.13:
version "1.9.6"
resolved "https://registry.yarnpkg.com/tailwindcss/-/tailwindcss-1.9.6.tgz#0c5089911d24e1e98e592a31bfdb3d8f34ecf1a0"
integrity sha512-nY8WYM/RLPqGsPEGEV2z63riyQPcHYZUJpAwdyBzVpxQHOHqHE+F/fvbCeXhdF1+TA5l72vSkZrtYCB9hRcwkQ==
dependencies:
"@fullhuman/postcss-purgecss" "^2.1.2"
autoprefixer "^9.4.5"
browserslist "^4.12.0"
bytes "^3.0.0"
chalk "^3.0.0 || ^4.0.0"
color "^3.1.2"
detective "^5.2.0"
fs-extra "^8.0.0"
html-tags "^3.1.0"
lodash "^4.17.20"
node-emoji "^1.8.1"
normalize.css "^8.0.1"
object-hash "^2.0.3"
postcss "^7.0.11"
postcss-functions "^3.0.0"
postcss-js "^2.0.0"
postcss-nested "^4.1.1"
postcss-selector-parser "^6.0.0"
postcss-value-parser "^4.1.0"
pretty-hrtime "^1.0.3"
reduce-css-calc "^2.1.6"
resolve "^1.14.2"

"tailwindcss@npm:@tailwindcss/postcss7-compat":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@tailwindcss/postcss7-compat/-/postcss7-compat-2.0.2.tgz#49cb21703dfb4447620fceab5cef3285cff8c69d"
Expand Down

1 comment on commit 650e513

@vercel
Copy link

@vercel vercel bot commented on 650e513 May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.