Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-peekaboo

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-peekaboo

React component that notifies when its child enters or exits the viewport

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.5K
decreased by-27.76%
Maintainers
1
Weekly downloads
 
Created
Source

react-peekaboo

React component that notifies you when its child enters or exits the viewport. Under the hood, it uses the IntersectionObserver API in supported environments and falls back to listening for scroll and resize events in combination with getBoundingClientRect in unsupported environments.

Installation

yarn add react-peekaboo

Or

npm install --save react-peekaboo

Usage

import React, { useState } from 'react';
import { InView } from 'react-peekaboo';

function Example() {
  const [isInViewport, setIsInViewport] = useState(false);

  return (
    <InView onChange={setIsInViewport}>
      {ref => (
        <div ref={ref}>I am {isInViewport ? 'visible' : 'not visible'}.</div>
      )}
    </InView>
  );
}

API

InView

Props
children: (ref: React.Ref<any>) => JSX.Element

Render prop that accepts a ref as its parameter. You must apply the ref to a DOM element.

offsetBottom?: number

Number of pixels to add to the bottom of the area checked against when computing in view elements.

Default: 0

offsetTop?: number

Number of pixels to add to the top of the area checked against when computing in view elements.

Default: 0

onChange: (isInviewPort: boolean) => void

Callback that's invoked each time the wrapped element enters or exits the viewport.

throttle?: number

Number of ms to throttle scroll events (only applies in environments that don't support IntersectionObserver).

Default: 100

IO

Props
children: (ref: React.Ref<any>) => JSX.Element

Render prop that accepts a ref as its parameter. You must apply the ref to a DOM element.

offsetBottom?: number

Number of pixels to add to the bottom of the area checked against when computing in view elements.

Default: 0

offsetTop?: number

Number of pixels to add to the top of the area checked against when computing in view elements.

Default: 0

onChange: (isInviewPort: boolean) => void

Callback that's invoked each time the wrapped element enters or exits the viewport.

Scroll

children: (ref: React.Ref<any>) => JSX.Element

Render prop that accepts a ref as its parameter. You must apply the ref to a DOM element.

offsetBottom?: number

Number of pixels to add to the bottom of the area checked against when computing in view elements.

Default: 0

offsetTop?: number

Number of pixels to add to the top of the area checked against when computing in view elements.

Default: 0

onChange: (isInviewPort: boolean) => void

Callback that's invoked each time the wrapped element enters or exits the viewport.

throttle?: number

Number of ms to throttle scroll events.

Default: 100

Caveats

  • This module considers edge-adjacent intersections (when the target element is directly above or below the viewport) to be in viewport. If you only want to consider elements with pixels in the viewport as visible, you can configure offsetTop/offsetBottom to be -1.
  • IntersectionObserver ignores rootMargin in iframe contexts, which means that offsetTop and offsetBottom will be ignored.

Keywords

FAQs

Package last updated on 20 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc