{"id":17141087,"url":"https://github.com/jscottsmith/react-observed","last_synced_at":"2025-04-13T10:31:18.186Z","repository":{"id":26088235,"uuid":"107307586","full_name":"jscottsmith/react-observed","owner":"jscottsmith","description":"👀 React component using the browser's Intersection Observer API to watch for when an element is within (or intersecting with) the viewport.","archived":false,"fork":false,"pushed_at":"2022-12-09T17:13:09.000Z","size":4042,"stargazers_count":22,"open_issues_count":23,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-27T01:51:34.295Z","etag":null,"topics":["component","intersection","observer","react","viewport","watch","waypoints"],"latest_commit_sha":null,"homepage":"http://react-observed.surge.sh/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jscottsmith.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-17T18:22:10.000Z","updated_at":"2023-06-11T01:04:42.000Z","dependencies_parsed_at":"2023-01-14T04:30:21.939Z","dependency_job_id":null,"html_url":"https://github.com/jscottsmith/react-observed","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jscottsmith%2Freact-observed","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jscottsmith%2Freact-observed/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jscottsmith%2Freact-observed/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jscottsmith%2Freact-observed/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jscottsmith","download_url":"https://codeload.github.com/jscottsmith/react-observed/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248698849,"owners_count":21147543,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["component","intersection","observer","react","viewport","watch","waypoints"],"created_at":"2024-10-14T20:24:19.865Z","updated_at":"2025-04-13T10:31:17.816Z","avatar_url":"https://github.com/jscottsmith.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# React Observed\n\n[![npm version](https://badge.fury.io/js/react-observed.svg)](https://badge.fury.io/js/react-observed) [![Build Status](https://travis-ci.org/jscottsmith/react-observed.svg?branch=master)](https://travis-ci.org/jscottsmith/react-observed) [![codecov](https://codecov.io/gh/jscottsmith/react-observed/branch/master/graph/badge.svg)](https://codecov.io/gh/jscottsmith/react-observed)\n\nReact component using the browser's Intersection Observer API to watch for when an element is within (or intersecting with) the viewport.\n\n## Example\n\n[Storybook Demos](http://react-observed.surge.sh/)\n\n## Install\n\n```\nnpm install react-observed --save\n```\n\nor with Yarn\n\n```\nyarn add react-observed\n```\n\n**Note:** For complete [browser support](#browser-support) you must also provide an [Intersection Observer polyfill](https://github.com/w3c/IntersectionObserver/tree/master/polyfill).\n\n```\nnpm install intersection-observer --save\n```\n\nor\n\n```\nyarn add intersection-observer\n```\n\n## Usage\n\n`\u003cObserved\u003e` takes a function as a child which gives you access to the `isInView` and `mapRef` properties.\n\n`mapRef` is the ref function callback that must be declared on the observed target element like `\u003cdiv ref={mapRef} /\u003e`. Then `isInView` will give you the current state of the observed element.\n\nHere's an example:\n\n```jsx\n\u003cObserved\u003e\n    {({ isInView, mapRef }) =\u003e (\n        \u003cdiv ref={mapRef}\u003e\n            {isInView ? (\n                \u003cspan\u003eI'm in view!\u003c/span\u003e\n            ) : (\n                \u003cspan\u003eI'm not in view\u003c/span\u003e\n            )}\n        \u003c/div\u003e\n    )}\n\u003c/Observed\u003e\n```\n\n## \\\u003cObserved\u003e Props\n\n`\u003cObserved\u003e` takes the props as shown by the following example:\n\n```jsx\n\u003cObserved\n    initialViewState // the initial view state state; defaults to `false`\n    intersectionRatio={0.5} // target's visibility must pass the 50% threshold to be considered visible\n    once // discontinue observing the target once it's become visible\n    onChange={isInView =\u003e {}} // handler called with the current `isInView` state whenever it changes\n    onEnter={() =\u003e {}} // handler called when the observed element enters\n    onExit={() =\u003e {}} // handler called when the observed element exits\n    onIntersect={entry =\u003e {}} // handler called when each threshold is met with the entry data\n    options={{\n        // IntersectionObserver constuctor options\n        root: null,\n        rootMargin: \"0px\",\n        threshold: 0.5\n    }}\n/\u003e\n```\n\n| Name                  |    Type    | Default     | Description                                                                                                                                                   |\n| --------------------- | :--------: | :---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| **initialViewState**  | `Boolean`  | `false`     | Optionally sets the initial `isInView` state. By default this is `false` until the observer updates the state.                                                |\n| **intersectionRatio** |  `Number`  | `0`         | The intersection ratio (a value between `0–1`) that when `\u003e=` to the target's intersect ratio and not equal to `0` will be considered in view.                |\n| **once**              | `Boolean`  | `false`     | If once is `true` the observer will disconnect after the target element has entered the view. Useful for triggering something when in view for a single time. |\n| **onChange**          | `Function` |             | Handler to be called with the current `isInView` state whenever it changes.                                                                                   |\n| **onEnter**           | `Function` |             | Handler to be called when the `isInView` state changes to `true`.                                                                                             |\n| **onExit**            | `Function` |             | Handler to be called when the `isInView` state changes to `false`.                                                                                            |\n| **onIntersect**       | `Function` |             | Handler to be called when each threshold is met with the current [entry](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry) data.    |\n| **options**           |  `Object`  | \\*see below | Options passed to the IntersectionObserver constructor.                                                                                                       |\n\n### \\*IntersectionObserver Options\n\nThese are the options passed to the [IntersectionObserver constructor](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Creating_an_intersection_observer). The default `options` to the `\u003cObserved\u003e` component are:\n\n```js\noptions: {\n    root: null,\n    rootMargin: '0px',\n    threshold: [0],\n}\n```\n\nThe following is as described on MDN:\n\n| Name           |        Type         | Default | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |\n| -------------- | :-----------------: | :------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| **root**       |      `Element`      | `null`  | The DOM element that is used as the viewport for checking visiblity of the target. Must be the ancestor of the target. Defaults to the browser viewport if not specified or if `null`.                                                                                                                                                                                                                                                                                                                                                                                              |\n| **rootMargin** |      `String`       | `0px`   | Margin around the root. Can have values similar to the CSS margin property, e.g. `10px 20px 30px 40px` (top, right, bottom, left). If the root element is specified, the values can be percentages. This set of values serves to grow or shrink each side of the root element's bounding box before computing intersections. Defaults to all zeros.                                                                                                                                                                                                                                 |\n| **threshold**  | `Number` or `Array` | `[0]`   | Either a single `Number` or an `Array` of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. If you only want to detect when visibility passes the 50% mark, you can use a value of `0.5`. If you want the callback run every time visibility passes another 25%, you would specify the array `[0, 0.25, 0.5, 0.75, 1]`. The default is `0` (meaning as soon as even one pixel is visible, the callback will be run). A value of `1.0` means that the threshold isn't considered passed until every pixel is visible. |\n\n## Render Callback\n\nThe child function takes one object parameter like such `{ isInView, mapRef }`. It's required to map a ref function to a DOM element otherwise `\u003cObserved\u003e` will throw an error.\n\nExample:\n\n```jsx\n\u003cObserved\u003e{({ mapRef }) =\u003e \u003cdiv ref={mapRef} /\u003e}\u003c/Observed\u003e\n```\n\nThe keys of which are:\n\n| Name         |    Type    | Description                                                                                                                             |\n| ------------ | :--------: | --------------------------------------------------------------------------------------------------------------------------------------- |\n| **isInView** | `Boolean`  | This is `true` when the observed element's intersection ratio is `\u003e=` to the `intersectionRatio` prop.                                  |\n| **mapRef**   | `Function` | This a `function` that is declared as the `ref` of the DOM element to be observed. Note that this is required for the observer to work. |\n\n## Browser Support\n\nIntersection Observer is [supported](https://caniuse.com/#feat=intersectionobserver) by the latest major browsers. For older browsers a [good polyfill](https://github.com/w3c/IntersectionObserver/tree/master/polyfill) is necessary.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjscottsmith%2Freact-observed","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjscottsmith%2Freact-observed","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjscottsmith%2Freact-observed/lists"}