npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lskjs/react-on-screen

v2.0.21

Published

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

Downloads

23

Readme

React on screen npm npm license Coverage Status

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

react-on-screen-demo

Demo

View the demo.

Installation

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

A UMD build is also available :

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

Usage

Simple

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 !

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

Using custom container

const YourApp = () => {
    return (
        <TrackVisibility container={refContainer}>
            <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 :

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.

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.

const YourApp = () => {
    return (
        <TrackVisibility partialVisibility>
            {({ 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| |children |React Components| - |Can be on or many react components| |style |object | - |Style attributes| |className |string | - |Css classes| |container |React Ref |window|Custom ref container| |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|

Contributions

Any contributions is welcome !

  • Lint : $ npm run lint
  • Test : $ npm run test
  • Build : $ npm run build // will lint and run test before

Release Notes

v2.0.4

  • Recalculate on props change.

v2.0.2

  • Add check for component bottom up to stop whole page being visible
  • Push componentDidMount isComponentVisible call into next tick

v2.0.1

  • Fix issue #9. The component was not updated on prop changes

v2.0.0

  • Added support for partial visibility. Thanks to audiolion
  • Internal rewrite
  • CI intregration
  • Better Test suite
  • Typescript definition !

v1.1.4

  • React and react-dom are now peer dependencies

v1.1.1

  • switched to webpack2
  • Upgraded to stand alone prop-types

License

Licensed under MIT