Giter VIP home page Giter VIP logo

react-on-visible's Introduction

Build Status npm version

React OnVisible component

Live demo

Live example: https://dazld.github.io/react-on-visible/demo/

Description

This is a higher order react component that applies the css class visible to the container when the element becomes more than 50% visible in the browser viewport.

It only attaches to browser events on the client, so is also safe to use for server rendering.

Viewport visibility is calculated by listening to a debounced window scroll event (10ms), and once the element has been found to be visible, no further changes are applied unless the bounce prop is set to true.

API

percent - number

Percentage (100 based) of element that should be visible before triggering change. eg percent={80}

visibleClassName - string

ClassName that should be added to element once visible

onChange - function

Callback when visibility changes. Argument is a boolean, true for visible, false for not.

bounce - boolean

Whether visibility should be continuously calculated, or just until the element becomes visible. defaults to false. Go easy with this, don't expect amazing performance with many tens or hundreds of elements (getBoundingClientRect).

wrappingElement - string

The string representation of the html element used to wrap children. Defaults to a div

setDefaultProps - static function

The user can override default props via this function. If you want everything to bounce, for example, then can do this:

import OnVisible, { setDefaultProps } from 'react-on-visible';

setDefaultProps({
    bounce: true,
    visibleClassName: 'appear',
    percent: 10
});

and so on.

Example

import React from 'react';
import OnVisible from 'react-on-visible';

export default function someComponent(props) {
  return (
    <OnVisible className="my-container">
      <h1>I will be rendered inside a div that has class 'my-container' only until I become visible, at which point the container will have the classes 'my-container visible'</h1>
    </OnVisible>
  );
}

A further example is available in the demo folder of this project.

Changelog

1.5.1

  • Fixes onChange bug reported in dazld#27, and remove repeated invocation of onChange, only calling it when something actually changes.

1.5.0

  • Added an option to configure the wrappingElement, instead of using div always (thanks @TomiTakussaari - dazld#23)

1.4.0

  • Added an index.d.ts file for typescript users (thanks @KromDaniel)

1.3.0

  • support react 16+ (thanks @schwers - dazld#17)
  • change scheduler to use requestAnimationFrame instead of debounce (thanks @falconmick - dazld#14)
  • tweak dependencies, move react to peer deps

1.2.0

  • include a fix for this.holder being null in some situations #12

1.1.1

  • added react-props package instead of using them directly from react (thanks @renoiser )

1.1.0

  • Added bounce prop and function to set default props.

1.0.3

  • added onChange callback prop option (thanks @rdjpalmer)

react-on-visible's People

Contributors

dazld avatar falconmick avatar renoiser avatar schwers avatar

Watchers

James Cloos avatar Trending Technology avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.