Socket
Socket
Sign inDemoInstall

react-intersection-observer

Package Overview
Dependencies
Maintainers
1
Versions
160
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-intersection-observer - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

example/favicon.ico

7

package.json
{
"name": "react-intersection-observer",
"version": "0.1.1",
"version": "0.1.2",
"description": "Monitor if a component is inside the viewport, using IntersectionObserver API",
"main": "lib/index.js",
"browser": "lib/Observer.js",
"author": "Daniel Schmidt",

@@ -20,3 +21,3 @@ "repository": {

"scripts": {
"build:lib": "NODE_ENV=development babel src --out-dir lib",
"build:lib": "rm -rf lib && NODE_ENV=development babel src --out-dir lib",
"build:storybook": "build-storybook --output-dir example",

@@ -51,3 +52,3 @@ "deploy": "npm run build:storybook && git-directory-deploy --directory example --branch gh-pages",

"jest": {
"testEnvironment": "jsdom",
"testEnvironment": "node",
"snapshotSerializers": [

@@ -54,0 +55,0 @@ "<rootDir>/node_modules/enzyme-to-json/serializer"

@@ -92,1 +92,9 @@ # react-intersection-observer

```
### Universal rendering
The IntersectionObserver polyfill requires `window` and `document`, and will crash if you try to import it outside the browser.
To allow the Observer to be rendered universally, it sets `global.window` and `global.document` to empty objects if they are not defined.
Ideally you would **noop** the IntersectionObserver when rendering outside the browser, but to make integration easier i've included the hack in the module.
It could potentially conflict with other modules that checks for the existence of `window` and `document`. You can always require `lib/Observer.js` directly, to skip the window check. `lib/Observer.js` is also set as the `"browser"` field.

@@ -1,100 +0,4 @@

import React, { Component, createElement } from 'react' // eslint-disable-line no-unused-vars
import PropTypes from 'prop-types'
import { observe, unobserve } from './intersection'
import './window'
import Observer from './Observer'
const isFunction = func => typeof func === 'function'
/**
* Monitors scroll, and triggers the children function with updated props
*
<Observer>
{inView => (
<h1>{`${inView}`}</h1>
)}
</Observer>
*/
class Observer extends Component {
static propTypes = {
/** Element tag to use for the wrapping */
tag: PropTypes.node,
/** Children should be either a function or a node */
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
/** Only trigger this method once */
triggerOnce: PropTypes.bool,
/** Number between 0 and 1 indicating the the percentage that should be visible before triggering */
threshold: PropTypes.number,
/** Call this function whenever the in view state changes */
onChange: PropTypes.func,
/** Use render method to only render content when inView */
render: PropTypes.func,
}
static defaultProps = {
tag: 'div',
threshold: 0,
triggerOnce: false,
}
state = {
inView: false,
}
componentWillUpdate(nextProps, nextState) {
if (!!this.props.onChange && nextState.inView !== this.state.inView) {
this.props.onChange(nextState.inView)
}
}
componentWillUnmount() {
if (this.node) {
unobserve(this.node)
this.node = null
}
}
node = null
handleNode = node => {
if (this.node) unobserve(this.node)
if (node) {
observe(
node,
this.handleChange,
this.props.triggerOnce,
this.props.threshold,
)
}
this.node = node
}
handleChange = inView => {
this.setState({ inView })
}
render() {
const {
children,
render,
tag,
triggerOnce,
threshold,
...props
} = this.props
const { inView } = this.state
return createElement(
tag,
{
...props,
ref: this.handleNode,
},
// If render is a function, use it to render content when in view
inView && isFunction(render) ? render() : null,
// If children is a function, render it with the current inView status.
// Otherwise always render children. Assume onChange is being used outside, to control the the state of children.
isFunction(children) ? children(inView) : children,
)
}
}
export default Observer

Sorry, the diff of this file is not supported yet

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