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 - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

dist/peekaboo.d.ts

234

dist/index.esm.js

@@ -1,3 +0,2 @@

import { createRef, Component, createElement } from 'react';
import _slicedToArray from '@babel/runtime/helpers/esm/slicedToArray';
import { Component, createElement } from 'react';
import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';

@@ -8,4 +7,81 @@ import _createClass from '@babel/runtime/helpers/esm/createClass';

import _inherits from '@babel/runtime/helpers/esm/inherits';
import throttle from 'lodash.throttle';
import _slicedToArray from '@babel/runtime/helpers/esm/slicedToArray';
import throttleFunc from 'lodash.throttle';
var supported;
function intersectionObserverSupported() {
if (supported === undefined) {
supported = typeof window !== 'undefined' && 'IntersectionObserver' in window;
}
return supported;
}
function io(_ref) {
var element = _ref.element,
offsetBottom = _ref.offsetBottom,
offsetTop = _ref.offsetTop,
onChange = _ref.onChange;
var rootMargin = "".concat(offsetTop, "px 0px ").concat(offsetBottom, "px 0px");
var observer = new IntersectionObserver(function (_ref2) {
var _ref3 = _slicedToArray(_ref2, 1),
entry = _ref3[0];
onChange(entry.isIntersecting);
}, {
root: null,
rootMargin: rootMargin
});
observer.observe(element);
return function () {
observer.unobserve(element);
};
}
function isElementInDocument(element) {
return 'isConnected' in element ? element.isConnected : document.body.contains(element);
}
function isElementInViewport(element, offsetBottom, offsetTop) {
if (!isElementInDocument(element)) {
return false;
}
var rect = element.getBoundingClientRect();
var top = rect.top;
var bottom = rect.bottom;
var adjustedTop = top - offsetTop;
var adjustedBottom = bottom + offsetBottom;
var isTopEdgeAboveViewportBottom = adjustedTop <= window.innerHeight;
var isBottomEdgeBelowViewportTop = adjustedBottom >= 0;
var isIntersecting = isTopEdgeAboveViewportBottom && isBottomEdgeBelowViewportTop;
return isIntersecting;
}
function scroll(_ref4) {
var element = _ref4.element,
offsetBottom = _ref4.offsetBottom,
offsetTop = _ref4.offsetTop,
onChange = _ref4.onChange,
throttle = _ref4.throttle;
var prevInViewport;
var checkViewport = function checkViewport() {
var isInViewport = element ? isElementInViewport(element, offsetBottom, offsetTop) : false;
if (isInViewport !== prevInViewport) {
prevInViewport = isInViewport;
onChange(isInViewport);
}
};
var eventHandler = throttleFunc(checkViewport, throttle);
checkViewport();
window.addEventListener('scroll', eventHandler);
window.addEventListener('resize', eventHandler);
return function () {
window.removeEventListener('scroll', eventHandler);
window.removeEventListener('resize', eventHandler);
};
}
var IO =

@@ -22,14 +98,31 @@ /*#__PURE__*/

_this = _possibleConstructorReturn(this, _getPrototypeOf(IO).apply(this, arguments));
_this.observer = null;
_this.childRef = createRef();
_this.state = {
element: null
};
_this.handleChange = function (isInViewport) {
_this.props.onChange(isInViewport);
_this.childRef = function (element) {
_this.setState({
element: element
});
};
_this.updateEntry = function (_ref) {
var _ref2 = _slicedToArray(_ref, 1),
entry = _ref2[0];
_this.setup = function () {
if (_this.state.element) {
var _this$props = _this.props,
offsetBottom = _this$props.offsetBottom,
offsetTop = _this$props.offsetTop,
onChange = _this$props.onChange;
_this.teardown = io({
element: _this.state.element,
offsetBottom: offsetBottom,
offsetTop: offsetTop,
onChange: onChange
});
}
};
_this.handleChange(entry.isIntersecting);
_this.safeTeardown = function () {
if (_this.teardown) {
_this.teardown();
}
};

@@ -41,42 +134,13 @@

_createClass(IO, [{
key: "componentDidMount",
value: function componentDidMount() {
this.createObserver();
this.observeRef();
}
}, {
key: "createObserver",
value: function createObserver() {
var _this$props = this.props,
offsetBottom = _this$props.offsetBottom,
offsetTop = _this$props.offsetTop;
var rootMargin = "".concat(offsetTop, "px 0px ").concat(offsetBottom, "px 0px");
this.observer = new IntersectionObserver(this.updateEntry, {
root: null,
rootMargin: rootMargin
});
}
}, {
key: "observeRef",
value: function observeRef() {
if (this.childRef.current && this.observer) {
this.observer.observe(this.childRef.current);
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
if (prevProps.children !== this.props.children || prevProps.offsetBottom !== this.props.offsetTop || prevProps.offsetTop !== this.props.offsetTop || prevProps.onChange !== this.props.onChange || prevState.element !== this.state.element) {
this.safeTeardown();
this.setup();
}
}
}, {
key: "unobserveRef",
value: function unobserveRef() {
if (this.childRef.current && this.observer) {
this.observer.unobserve(this.childRef.current);
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.observeRef();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.unobserveRef();
this.safeTeardown();
}

@@ -97,14 +161,2 @@ }, {

function isElementInViewport(element, offsetBottom, offsetTop) {
var rect = element.getBoundingClientRect();
var top = rect.top;
var bottom = rect.bottom;
var adjustedTop = top - offsetTop;
var adjustedBottom = bottom + offsetBottom;
var isTopEdgeAboveViewportBottom = adjustedTop <= window.innerHeight;
var isBottomEdgeBelowViewportTop = adjustedBottom >= 0;
var isIntersecting = isTopEdgeAboveViewportBottom && isBottomEdgeBelowViewportTop;
return isIntersecting;
}
var Scroll =

@@ -121,26 +173,35 @@ /*#__PURE__*/

_this = _possibleConstructorReturn(this, _getPrototypeOf(Scroll).apply(this, arguments));
_this.childRef = createRef();
_this.state = {
element: null
};
_this.handleChange = function (isInViewport) {
_this.props.onChange(isInViewport);
_this.childRef = function (element) {
_this.setState({
element: element
});
};
_this.checkInViewport = function () {
var _this$props = _this.props,
offsetBottom = _this$props.offsetBottom,
offsetTop = _this$props.offsetTop;
return _this.childRef.current ? isElementInViewport(_this.childRef.current, offsetBottom, offsetTop) : false;
_this.setup = function () {
if (_this.state.element) {
var _this$props = _this.props,
offsetBottom = _this$props.offsetBottom,
offsetTop = _this$props.offsetTop,
onChange = _this$props.onChange,
throttle = _this$props.throttle;
_this.teardown = scroll({
offsetBottom: offsetBottom,
offsetTop: offsetTop,
onChange: onChange,
element: _this.state.element,
throttle: throttle
});
}
};
_this.checkVisibility = function () {
var isInViewport = _this.checkInViewport();
if (isInViewport !== _this.isInViewport) {
_this.isInViewport = isInViewport;
_this.handleChange(isInViewport);
_this.safeTeardown = function () {
if (_this.teardown) {
_this.teardown();
}
};
_this.eventHandler = throttle(_this.checkVisibility, _this.props.throttle);
return _this;

@@ -150,7 +211,8 @@ }

_createClass(Scroll, [{
key: "componentDidMount",
value: function componentDidMount() {
this.checkVisibility();
window.addEventListener('scroll', this.eventHandler);
window.addEventListener('resize', this.eventHandler);
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
if (prevProps.children !== this.props.children || prevProps.offsetBottom !== this.props.offsetTop || prevProps.offsetTop !== this.props.offsetTop || prevProps.onChange !== this.props.onChange || prevProps.throttle !== this.props.throttle || prevState.element !== this.state.element) {
this.safeTeardown();
this.setup();
}
}

@@ -160,4 +222,3 @@ }, {

value: function componentWillUnmount() {
window.removeEventListener('scroll', this.eventHandler);
window.removeEventListener('resize', this.eventHandler);
this.safeTeardown();
}

@@ -179,11 +240,2 @@ }, {

var supported;
function intersectionObserverSupported() {
if (supported === undefined) {
supported = typeof window !== 'undefined' && 'IntersectionObserver' in window;
}
return supported;
}
var InView = function InView(props) {

@@ -197,2 +249,2 @@ if (!intersectionObserverSupported()) {

export { IO, Scroll, InView };
export { IO, InView, Scroll };

@@ -8,3 +8,2 @@ 'use strict';

var react = require('react');
var _slicedToArray = _interopDefault(require('@babel/runtime/helpers/slicedToArray'));
var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));

@@ -15,4 +14,81 @@ var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));

var _inherits = _interopDefault(require('@babel/runtime/helpers/inherits'));
var throttle = _interopDefault(require('lodash.throttle'));
var _slicedToArray = _interopDefault(require('@babel/runtime/helpers/slicedToArray'));
var throttleFunc = _interopDefault(require('lodash.throttle'));
var supported;
function intersectionObserverSupported() {
if (supported === undefined) {
supported = typeof window !== 'undefined' && 'IntersectionObserver' in window;
}
return supported;
}
function io(_ref) {
var element = _ref.element,
offsetBottom = _ref.offsetBottom,
offsetTop = _ref.offsetTop,
onChange = _ref.onChange;
var rootMargin = "".concat(offsetTop, "px 0px ").concat(offsetBottom, "px 0px");
var observer = new IntersectionObserver(function (_ref2) {
var _ref3 = _slicedToArray(_ref2, 1),
entry = _ref3[0];
onChange(entry.isIntersecting);
}, {
root: null,
rootMargin: rootMargin
});
observer.observe(element);
return function () {
observer.unobserve(element);
};
}
function isElementInDocument(element) {
return 'isConnected' in element ? element.isConnected : document.body.contains(element);
}
function isElementInViewport(element, offsetBottom, offsetTop) {
if (!isElementInDocument(element)) {
return false;
}
var rect = element.getBoundingClientRect();
var top = rect.top;
var bottom = rect.bottom;
var adjustedTop = top - offsetTop;
var adjustedBottom = bottom + offsetBottom;
var isTopEdgeAboveViewportBottom = adjustedTop <= window.innerHeight;
var isBottomEdgeBelowViewportTop = adjustedBottom >= 0;
var isIntersecting = isTopEdgeAboveViewportBottom && isBottomEdgeBelowViewportTop;
return isIntersecting;
}
function scroll(_ref4) {
var element = _ref4.element,
offsetBottom = _ref4.offsetBottom,
offsetTop = _ref4.offsetTop,
onChange = _ref4.onChange,
throttle = _ref4.throttle;
var prevInViewport;
var checkViewport = function checkViewport() {
var isInViewport = element ? isElementInViewport(element, offsetBottom, offsetTop) : false;
if (isInViewport !== prevInViewport) {
prevInViewport = isInViewport;
onChange(isInViewport);
}
};
var eventHandler = throttleFunc(checkViewport, throttle);
checkViewport();
window.addEventListener('scroll', eventHandler);
window.addEventListener('resize', eventHandler);
return function () {
window.removeEventListener('scroll', eventHandler);
window.removeEventListener('resize', eventHandler);
};
}
var IO =

@@ -29,14 +105,31 @@ /*#__PURE__*/

_this = _possibleConstructorReturn(this, _getPrototypeOf(IO).apply(this, arguments));
_this.observer = null;
_this.childRef = react.createRef();
_this.state = {
element: null
};
_this.handleChange = function (isInViewport) {
_this.props.onChange(isInViewport);
_this.childRef = function (element) {
_this.setState({
element: element
});
};
_this.updateEntry = function (_ref) {
var _ref2 = _slicedToArray(_ref, 1),
entry = _ref2[0];
_this.setup = function () {
if (_this.state.element) {
var _this$props = _this.props,
offsetBottom = _this$props.offsetBottom,
offsetTop = _this$props.offsetTop,
onChange = _this$props.onChange;
_this.teardown = io({
element: _this.state.element,
offsetBottom: offsetBottom,
offsetTop: offsetTop,
onChange: onChange
});
}
};
_this.handleChange(entry.isIntersecting);
_this.safeTeardown = function () {
if (_this.teardown) {
_this.teardown();
}
};

@@ -48,42 +141,13 @@

_createClass(IO, [{
key: "componentDidMount",
value: function componentDidMount() {
this.createObserver();
this.observeRef();
}
}, {
key: "createObserver",
value: function createObserver() {
var _this$props = this.props,
offsetBottom = _this$props.offsetBottom,
offsetTop = _this$props.offsetTop;
var rootMargin = "".concat(offsetTop, "px 0px ").concat(offsetBottom, "px 0px");
this.observer = new IntersectionObserver(this.updateEntry, {
root: null,
rootMargin: rootMargin
});
}
}, {
key: "observeRef",
value: function observeRef() {
if (this.childRef.current && this.observer) {
this.observer.observe(this.childRef.current);
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
if (prevProps.children !== this.props.children || prevProps.offsetBottom !== this.props.offsetTop || prevProps.offsetTop !== this.props.offsetTop || prevProps.onChange !== this.props.onChange || prevState.element !== this.state.element) {
this.safeTeardown();
this.setup();
}
}
}, {
key: "unobserveRef",
value: function unobserveRef() {
if (this.childRef.current && this.observer) {
this.observer.unobserve(this.childRef.current);
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.observeRef();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.unobserveRef();
this.safeTeardown();
}

@@ -104,14 +168,2 @@ }, {

function isElementInViewport(element, offsetBottom, offsetTop) {
var rect = element.getBoundingClientRect();
var top = rect.top;
var bottom = rect.bottom;
var adjustedTop = top - offsetTop;
var adjustedBottom = bottom + offsetBottom;
var isTopEdgeAboveViewportBottom = adjustedTop <= window.innerHeight;
var isBottomEdgeBelowViewportTop = adjustedBottom >= 0;
var isIntersecting = isTopEdgeAboveViewportBottom && isBottomEdgeBelowViewportTop;
return isIntersecting;
}
var Scroll =

@@ -128,26 +180,35 @@ /*#__PURE__*/

_this = _possibleConstructorReturn(this, _getPrototypeOf(Scroll).apply(this, arguments));
_this.childRef = react.createRef();
_this.state = {
element: null
};
_this.handleChange = function (isInViewport) {
_this.props.onChange(isInViewport);
_this.childRef = function (element) {
_this.setState({
element: element
});
};
_this.checkInViewport = function () {
var _this$props = _this.props,
offsetBottom = _this$props.offsetBottom,
offsetTop = _this$props.offsetTop;
return _this.childRef.current ? isElementInViewport(_this.childRef.current, offsetBottom, offsetTop) : false;
_this.setup = function () {
if (_this.state.element) {
var _this$props = _this.props,
offsetBottom = _this$props.offsetBottom,
offsetTop = _this$props.offsetTop,
onChange = _this$props.onChange,
throttle = _this$props.throttle;
_this.teardown = scroll({
offsetBottom: offsetBottom,
offsetTop: offsetTop,
onChange: onChange,
element: _this.state.element,
throttle: throttle
});
}
};
_this.checkVisibility = function () {
var isInViewport = _this.checkInViewport();
if (isInViewport !== _this.isInViewport) {
_this.isInViewport = isInViewport;
_this.handleChange(isInViewport);
_this.safeTeardown = function () {
if (_this.teardown) {
_this.teardown();
}
};
_this.eventHandler = throttle(_this.checkVisibility, _this.props.throttle);
return _this;

@@ -157,7 +218,8 @@ }

_createClass(Scroll, [{
key: "componentDidMount",
value: function componentDidMount() {
this.checkVisibility();
window.addEventListener('scroll', this.eventHandler);
window.addEventListener('resize', this.eventHandler);
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
if (prevProps.children !== this.props.children || prevProps.offsetBottom !== this.props.offsetTop || prevProps.offsetTop !== this.props.offsetTop || prevProps.onChange !== this.props.onChange || prevProps.throttle !== this.props.throttle || prevState.element !== this.state.element) {
this.safeTeardown();
this.setup();
}
}

@@ -167,4 +229,3 @@ }, {

value: function componentWillUnmount() {
window.removeEventListener('scroll', this.eventHandler);
window.removeEventListener('resize', this.eventHandler);
this.safeTeardown();
}

@@ -186,11 +247,2 @@ }, {

var supported;
function intersectionObserverSupported() {
if (supported === undefined) {
supported = typeof window !== 'undefined' && 'IntersectionObserver' in window;
}
return supported;
}
var InView = function InView(props) {

@@ -205,3 +257,3 @@ if (!intersectionObserverSupported()) {

exports.IO = IO;
exports.InView = InView;
exports.Scroll = Scroll;
exports.InView = InView;
import { Component } from 'react';
import { Props } from './types';
declare type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export default class IO extends Component<Omit<Props, 'throttle'>> {
import { Omit, Props as SharedProps, Ref } from './types';
import { TeardownFunc } from './peekaboo';
declare type IOProps = Omit<SharedProps, 'throttle'>;
declare type State = {
element: Element | null;
};
export default class IO extends Component<IOProps, State> {
static defaultProps: {

@@ -9,11 +13,8 @@ offsetBottom: number;

};
observer: null | IntersectionObserver;
childRef: import("react").RefObject<Element>;
componentDidMount(): void;
createObserver(): void;
handleChange: (isInViewport: boolean) => void;
updateEntry: ([entry]: IntersectionObserverEntry[]) => void;
observeRef(): void;
unobserveRef(): void;
componentDidUpdate(): void;
state: State;
childRef: Ref;
teardown?: TeardownFunc;
setup: () => void;
safeTeardown: () => void;
componentDidUpdate(prevProps: IOProps, prevState: State): void;
componentWillUnmount(): void;

@@ -20,0 +21,0 @@ render(): JSX.Element;

@@ -1,5 +0,9 @@

/// <reference types="lodash" />
import { Component } from 'react';
import { Props } from './types';
export default class Scroll extends Component<Props> {
import { Props, Ref } from './types';
import { TeardownFunc } from './peekaboo';
declare type ScrollProps = Props;
declare type State = {
element: Element | null;
};
export default class Scroll extends Component<ScrollProps, State> {
static defaultProps: {

@@ -10,11 +14,11 @@ offsetBottom: number;

};
childRef: import("react").RefObject<Element>;
isInViewport?: boolean;
handleChange: (isInViewport: boolean) => void;
checkInViewport: () => boolean;
checkVisibility: () => void;
eventHandler: (() => void) & import("lodash").Cancelable;
componentDidMount(): void;
state: State;
childRef: Ref;
teardown?: TeardownFunc;
setup: () => void;
safeTeardown: () => void;
componentDidUpdate(prevProps: ScrollProps, prevState: State): void;
componentWillUnmount(): void;
render(): JSX.Element;
}
export {};

@@ -1,4 +0,6 @@

import { RefObject } from 'react';
import { Ref as ReactRef } from 'react';
export declare type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export declare type Ref = ReactRef<any>;
export interface Props {
children: (ref: RefObject<any> | ((node?: Element | null) => void)) => JSX.Element;
children: (ref: Ref) => JSX.Element;
offsetBottom: number;

@@ -5,0 +7,0 @@ offsetTop: number;

{
"name": "react-peekaboo",
"description": "React component that notifies when its child enters or exits the viewport",
"version": "0.1.3",
"version": "0.1.4",
"license": "MIT",
"author": "Wyatt Johnston <w@wyatt.page",
"author": "Wyatt Johnston <w@wyatt.page>",
"repository": {

@@ -43,4 +43,4 @@ "type": "git",

"test": "npm run test:e2e",
"test:e2e": "start-server-and-test dev \"http://localhost:3000/io|http://localhost:3000/scroll\" test:jest:e2e",
"test:e2e:ci": "start-server-and-test dev \"http://localhost:3000/io|http://localhost:3000/scroll\" test:jest:e2e:ci",
"test:e2e": "start-server-and-test dev \"http://localhost:3000/list|http://localhost:3000/ref-swap\" test:jest:e2e",
"test:e2e:ci": "start-server-and-test dev \"http://localhost:3000/list|http://localhost:3000/ref-swap\" test:jest:e2e:ci",
"test:jest:e2e": "jest -c jest.config.e2e.js",

@@ -51,6 +51,6 @@ "test:jest:e2e:ci": "cross-env CI=true jest -c jest.config.e2e.js --runInBand",

"devDependencies": {
"@babel/core": "^7.3.4",
"@babel/plugin-proposal-class-properties": "^7.3.4",
"@babel/plugin-transform-runtime": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"@babel/core": "^7.4.0",
"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/plugin-transform-runtime": "^7.4.0",
"@babel/preset-env": "^7.4.2",
"@babel/preset-react": "^7.0.0",

@@ -61,3 +61,3 @@ "@babel/preset-typescript": "^7.3.3",

"@types/react": "^16.8.8",
"@types/react-dom": "^16.8.2",
"@types/react-dom": "^16.8.3",
"@zeit/next-typescript": "^1.1.1",

@@ -73,6 +73,6 @@ "babel-jest": "^24.5.0",

"puppeteer": "^1.13.0",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"react": "^16.8.5",
"react-dom": "^16.8.5",
"rimraf": "^2.6.3",
"rollup": "^1.6.0",
"rollup": "^1.7.2",
"rollup-plugin-babel": "^4.3.2",

@@ -82,3 +82,3 @@ "rollup-plugin-commonjs": "^9.2.1",

"start-server-and-test": "^1.7.12",
"typescript": "^3.3.3333"
"typescript": "^3.3.4000"
},

@@ -85,0 +85,0 @@ "dependencies": {

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