# react-on-screen

> A wrapper component to track if your react component is visible on screen

Latest version **2.1.1** (published 2018-12-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-on-screen
pnpm add react-on-screen
yarn add react-on-screen
bun add react-on-screen
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2018-12-15 |
| First published | 2016-08-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 276.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 401 |
| Author | Fadi Khadra |
| Maintainers | sniphpet |
| Keywords | react, viewport, react-component, visible, track, react-on-screen, screen |

## Links

- npm: https://www.npmjs.com/package/react-on-screen
- Repository: https://github.com/fkhadra/react-on-screen
- Homepage: https://github.com/fkhadra/react-on-screen#readme
- Issues: https://github.com/fkhadra/react-on-screen/issues
- npm.io page: https://npm.io/package/react-on-screen

## Dependencies (3)

- [prop-types](https://npm.io/package/prop-types.md) ^15.6.0
- [shallowequal](https://npm.io/package/shallowequal.md) ^1.0.2
- [lodash.throttle](https://npm.io/package/lodash.throttle.md) ^4.1.1

## 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

- 2.1.1 (latest) — 2018-12-15
- 2.1.0 — 2018-10-30
- 2.0.4 — 2018-04-03
- 2.0.3 — 2018-02-07
- 2.0.2 — 2018-02-04
- 2.0.1 — 2017-11-12
- 2.0.0 — 2017-10-29
- 1.1.4 — 2017-05-11
- 1.1.3 — 2017-05-08
- 1.1.2 — 2017-05-01
- 1.1.1 — 2017-04-23
- 1.1.0 — 2016-11-03
- 1.0.2 — 2016-09-15
- 1.0.1 — 2016-08-17
- 1.0.0 — 2016-08-14

## README

# React on screen [![npm](https://img.shields.io/npm/dt/react-on-screen.svg)]() [![npm](https://img.shields.io/npm/v/react-on-screen.svg)]() [![license](https://img.shields.io/github/license/fkhadra/react-on-screen.svg?maxAge=2592000)]() [![Coverage Status](https://coveralls.io/repos/github/fkhadra/react-on-screen/badge.svg?branch=master)](https://coveralls.io/github/fkhadra/react-on-screen?branch=master)

😎 Check if your react component are visible on the screen without pain and with performance in mind 😎!

![react-on-screen-demo](https://user-images.githubusercontent.com/5574267/32147681-74918d80-bceb-11e7-98d4-1cbc04b29eb4.gif)

- [React on screen ![npm]() ![npm]() ![license]() ![Coverage Status](https://coveralls.io/github/fkhadra/react-on-screen?branch=master)](#react-on-screen-npm-npm-license-coverage-statushttpscoverallsiogithubfkhadrareact-on-screenbranchmaster)
    - [Demo](#demo)
    - [Installation](#installation)
    - [Usage](#usage)
        - [Simple](#simple)
        - [Using a render props](#using-a-render-props)
        - [Track the visibility only once](#track-the-visibility-only-once)
        - [Defining offset](#defining-offset)
        - [Partial visibility](#partial-visibility)
        - [Use the html tag of your choice](#use-the-html-tag-of-your-choice)
    - [Api](#api)
    - [Contributions](#contributions)
    - [License](#license)

## Demo

View the [demo](https://fkhadra.github.io/react-on-screen/demo-react-on-screen.html).

## Installation

```
$ npm install --save react-on-screen
$ yarn add react-on-screen
```

A UMD build is also available :

```html
<script src="./dist/ReactOnScreen.min.js"></script>
```

## Usage

### Simple

```javascript
import React from 'react';
import TrackVisibility from 'react-on-screen';

const ComponentToTrack = ({ isVisible }) => {
    const style = {
        background: isVisible ? 'red' : 'blue'
    };
    return <div style={style}>Hello</div>;
}

const YourApp = () => {
    return (
       {/* Some Stuff */}
        <TrackVisibility>
            <ComponentToTrack />
        </TrackVisibility>
       {/* Some Stuff */}
    );
}
```

### Using a render props

You can use a render props is you want to !

```js
const YourApp = () => {
    return (
        <TrackVisibility>
            {({ isVisible }) => isVisible && <ComponentToTrack />}
        </TrackVisibility>
    );
}
```

### Track the visibility only once

For many cases you may want to track the visibility only once. This can be done simply as follow :

```js
const YourApp = () => {
    return (
        <TrackVisibility once>
            <ComponentToTrack />
        </TrackVisibility>
    );
}
```

### Defining offset

Using `offset` props can be usefull if you want to lazy load an image for instance.

```js
const YourApp = () => {
    return (
        <TrackVisibility offset={1000}>
            {({ isVisible }) => isVisible ? <ComponentToTrack /> : <Loading />}
        </TrackVisibility>
    );
}
```

### Partial visibility

You may want to consider that a component is visible as soon as a part of the component is visible on screen. You can use the `partialVisibility` props for that.

```js
const YourApp = () => {
    return (
        <TrackVisibility partialVisibility>
            {({ isVisible }) => <ComponentToTrack />}
        </TrackVisibility>
    );
}
```

### Use the html tag of your choice

```js
const YourApp = () => {
    return (
        <TrackVisibility partialVisibility tag="h1">
            {({ isVisible }) => <ComponentToTrack />}
        </TrackVisibility>
    );
}
```

## Api

|props           |type            |default|description|
|----------------|----------------|-------|-----------|
|once            |bool            |false|If set to true track the visibility only once and remove the event listeners|
|throttleInterval|int             |150|Tweak the event listeners. See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/)|
|children        |React Components|  -  |Can be one or many react components|
|style           |object          |  -  |Style attributes|
|className       |string          |  -  |Css classes|
|offset          |number          |  0  |Allows you to specify how far left or above of the viewport you want to set isVisible to `true`|
|partialVisibility|bool           |false|Set isVisible to true on element as soon as any part is in the viewport|
|tag             |string|div  |Allows specifying html tag of your choice|

## Contributions

Any contributions is welcome !

- Develop: ``` $ yarn start ```
- Lint : ``` $ yarn lint ```
- Test : ``` $ yarn test ```
- Build : ``` $ yarn build // will lint and run test before ```

## License

Licensed under MIT

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