# react-visibility-observer

> React component using the Intersection Observer API for watching when an element is visible in the viewport.

Latest version **1.0.8** (published 2023-01-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-visibility-observer
pnpm add react-visibility-observer
yarn add react-visibility-observer
bun add react-visibility-observer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2023-01-16 |
| First published | 2019-07-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 20 |
| Author | Joni Kanerva |
| Maintainers | jonikanerva |
| Keywords | react, react-component, component, viewport, visible, track, screen, hooks, context |

## Links

- npm: https://www.npmjs.com/package/react-visibility-observer
- Repository: https://github.com/jonikanerva/react-visibility-observer
- Homepage: https://github.com/jonikanerva/react-visibility-observer#readme
- Issues: https://github.com/jonikanerva/react-visibility-observer/issues
- npm.io page: https://npm.io/package/react-visibility-observer

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.8 (latest) — 2023-01-16
- 1.0.7 — 2023-01-16
- 1.0.6 — 2022-12-30
- 1.0.5 — 2022-12-30
- 1.0.4 — 2019-09-14
- 1.0.3 — 2019-07-16
- 1.0.2 — 2019-07-16
- 1.0.1 — 2019-07-15
- 1.0.0 — 2019-07-15

## README

# Description

React component using the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) for watching when an element is visible in the viewport.

Written in TypeScript using React Hooks and Context. No dependencies.

[![github license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://github.com/jonikanerva/react-visibility-observer/blob/master/license) [![npm version](https://img.shields.io/npm/v/react-visibility-observer.svg?color=brightgreen)](https://www.npmjs.com/package/react-visibility-observer) [![npm bundle size](https://img.shields.io/bundlephobia/min/react-visibility-observer.svg?color=brightgreen)](https://www.npmjs.com/package/react-visibility-observer) [![downloads](https://img.shields.io/npm/dw/react-visibility-observer.svg)](https://www.npmjs.com/package/react-visibility-observer) [![react](https://img.shields.io/npm/dependency-version/react-visibility-observer/peer/react.svg?color=brightgreen)](https://www.npmjs.com/package/react-visibility-observer)

# Install

with Yarn

```
yarn add react-visibility-observer
```

or with NPM

```
npm install --save react-visibility-observer
```

# Usage

```jsx
// Component.tsx

import VisibilityObserver from 'react-visibility-observer'

const Component: React.FC = () => (
  <VisibilityObserver>
    <ChildComponent />
  </VisibilityObserver>
)
```

```jsx
// ChildComponent.tsx

import { useVisibilityObserver } from 'react-visibility-observer'

const ChildComponent: React.FC = () => {
  const { isVisible } = useVisibilityObserver()

  return <div>{isVisible ? "I'm visible!" : "I'm hidden!"}</div>
}
```

## Component

You can configure the `VisibilityObserver` component by passing in the following props.

```jsx
const Component: React.FC = () => (
  <VisibilityObserver
    className="myClass"
    triggerOnce={true}
    root={document.querySelector('#scrollArea')}
    rootMargin="50px 50px 50px 50px"
    threshold={0.2}
  >
    <ChildComponent />
  </VisibilityObserver>
)
```

| Prop          |  Type                  | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| ------------- | ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `className`   | `string`               | Custom class for the tracked `div`. Defaults to no class name.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `triggerOnce` | `boolean`              | Trigger observer only once when the element becomes available. Defaults to `false`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `root`        | `Element` or `null`    | The element that is used as the viewport for checking visiblity of the target. Must be the ancestor of the target. Defaults to `null` which is the browser viewport.                                                                                                                                                                                                                                                                                                                                                                                                               |
| `rootMargin`  | `string`               | Margin around the root. Can have values similar to the CSS margin property, e.g. `"10px 20px 30px 40px"` (top, right, bottom, left). 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 `"0 0 0 0"`.                                                                                                                                                                                                                                                              |
| `threshold`   | `number` or `number[]` | 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 to 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. |

## Hook

You can get info from the observer by using the `useVisibilityObserver` hook in the child components.

```ts
const { isVisible, entries } = useVisibilityObserver()
```

|  Variable    |  Type                                        | Description                                                                                                                                           |
| ------------ | -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
|  `isVisible` | `boolean`                                    | Is the observed element in view.                                                                                                                      |
|  `entries`   | `IntersectionObserverEntry[]` or `undefined` | Array of [IntersectionObserverEntry](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserverEntry) objects being observed if available. |

# Browser Support

Intersection Observer API is [compatible with all modern browsers](https://caniuse.com/#search=IntersectionObserver).
If you need support for older browsers, use the [polyfill by W3C](https://github.com/w3c/IntersectionObserver/tree/master/polyfill).

Install the polyfill with Yarn

```
yarn add intersection-observer
```

or with NPM

```
npm install --save intersection-observer
```

and add to the top of your client's entry point

```ts
// Browser.tsx

import 'intersection-observer'
```

# Contribute

Use GitHub [issues](https://github.com/jonikanerva/react-visibility-observer/issues) and [Pull Requests](https://github.com/jonikanerva/react-visibility-observer/pulls).

---
_Source: https://npm.io/package/react-visibility-observer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
