lazy-react 3.1.1 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -2
- package/demo/index.0aef4497.js +25760 -0
- package/demo/index.0aef4497.js.map +1 -0
- package/demo/index.html +34 -30
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -0
- package/package.json +18 -20
- package/src/demo/index.html +39 -0
- package/{demo/demo.js → src/demo/index.js} +2 -2
- package/src/{index.js → index.tsx} +2 -8
- package/src/lazyLoadBackgroundImage.tsx +30 -0
- package/src/lazyLoadComponent.tsx +22 -0
- package/src/lazyLoadFrame.tsx +21 -0
- package/src/lazyLoadImage.tsx +21 -0
- package/src/useIsInViewport.tsx +119 -0
- package/.babelrc +0 -6
- package/demo/index.js +0 -219
- package/demo/index.js.LICENSE.txt +0 -32
- package/src/baseClass.js +0 -111
- package/src/lazyLoadBackgroundImage.js +0 -24
- package/src/lazyLoadComponent.js +0 -24
- package/src/lazyLoadFrame.js +0 -20
- package/src/lazyLoadImage.js +0 -17
- package/webpack.config.demo.js +0 -19
- package/webpack.config.js +0 -47
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
object-assign
|
|
3
|
-
(c) Sindre Sorhus
|
|
4
|
-
@license MIT
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/** @license React v0.20.2
|
|
8
|
-
* scheduler.production.min.js
|
|
9
|
-
*
|
|
10
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
11
|
-
*
|
|
12
|
-
* This source code is licensed under the MIT license found in the
|
|
13
|
-
* LICENSE file in the root directory of this source tree.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/** @license React v17.0.2
|
|
17
|
-
* react-dom.production.min.js
|
|
18
|
-
*
|
|
19
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
20
|
-
*
|
|
21
|
-
* This source code is licensed under the MIT license found in the
|
|
22
|
-
* LICENSE file in the root directory of this source tree.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
/** @license React v17.0.2
|
|
26
|
-
* react.production.min.js
|
|
27
|
-
*
|
|
28
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
29
|
-
*
|
|
30
|
-
* This source code is licensed under the MIT license found in the
|
|
31
|
-
* LICENSE file in the root directory of this source tree.
|
|
32
|
-
*/
|
package/src/baseClass.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import React, { useState, useEffect } from 'react' // eslint-disable-line no-unused-vars
|
|
2
|
-
|
|
3
|
-
export default function useRenderIfInViewPort(element, props) {
|
|
4
|
-
const [state, setState] = useState({
|
|
5
|
-
link: '',
|
|
6
|
-
visible: false,
|
|
7
|
-
})
|
|
8
|
-
|
|
9
|
-
function makeItVisible() {
|
|
10
|
-
setState({
|
|
11
|
-
visible: true,
|
|
12
|
-
link: props.link,
|
|
13
|
-
})
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
useEffect(() => {
|
|
17
|
-
if (element.current !== undefined)
|
|
18
|
-
CheckIfRender.addElement({
|
|
19
|
-
// add the element to the array of elements that are waiting to be lazy loaded
|
|
20
|
-
element: element.current,
|
|
21
|
-
props,
|
|
22
|
-
makeItVisible,
|
|
23
|
-
})
|
|
24
|
-
return () =>
|
|
25
|
-
CheckIfRender.removeElementFromList({
|
|
26
|
-
// if the element is unloaded remove the element from the list of elements that needs to be lazy loader
|
|
27
|
-
element: element.current,
|
|
28
|
-
props,
|
|
29
|
-
makeItVisible,
|
|
30
|
-
})
|
|
31
|
-
}, [])
|
|
32
|
-
|
|
33
|
-
return state
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// array with all the elements that are waiting to be shown in the viewport
|
|
37
|
-
const CheckIfRender = {
|
|
38
|
-
elements: [],
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// top: the position of the element in relation with the top of the browser
|
|
42
|
-
// left: the position of the element in relation with the left of the browser
|
|
43
|
-
// offset: the desired offset of the element in relation of the viewport
|
|
44
|
-
CheckIfRender.isInViewPort = ({ offset, top, left }) =>
|
|
45
|
-
top < window.innerHeight + offset && left < window.innerWidth + offset
|
|
46
|
-
|
|
47
|
-
CheckIfRender.calculateNewPosition = (elem) => {
|
|
48
|
-
const reference = elem.element
|
|
49
|
-
const { top, left } = reference.getBoundingClientRect()
|
|
50
|
-
return {
|
|
51
|
-
...elem,
|
|
52
|
-
top,
|
|
53
|
-
left,
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
CheckIfRender.addElement = function ({ element, props, makeItVisible }) {
|
|
58
|
-
//the distance from the pixel 0,0 and the top of the element
|
|
59
|
-
const { top, left } = element.getBoundingClientRect()
|
|
60
|
-
CheckIfRender.elements.push({
|
|
61
|
-
element,
|
|
62
|
-
top,
|
|
63
|
-
left,
|
|
64
|
-
offset: props.offset || 100,
|
|
65
|
-
makeItVisible,
|
|
66
|
-
})
|
|
67
|
-
//check if has already been started the rAF cycle
|
|
68
|
-
if (typeof CheckIfRender.isListenerAttached === 'boolean') {
|
|
69
|
-
CheckIfRender.isListenerAttached = window.requestAnimationFrame(
|
|
70
|
-
CheckIfRender.eventHandler
|
|
71
|
-
)
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
CheckIfRender.eventHandler = function () {
|
|
76
|
-
//if there is no more element to lazy load remove the listener/rAF
|
|
77
|
-
if (CheckIfRender.elements.length === 0) {
|
|
78
|
-
CheckIfRender.removeScrollHandler()
|
|
79
|
-
} else {
|
|
80
|
-
CheckIfRender.elements.forEach((elem, i) => {
|
|
81
|
-
// if element is in viewPort make it visible
|
|
82
|
-
if (CheckIfRender.isInViewPort(elem)) {
|
|
83
|
-
elem.makeItVisible()
|
|
84
|
-
// remove element from the list of elements to lazy load
|
|
85
|
-
CheckIfRender.removeElementFromList(elem)
|
|
86
|
-
} else {
|
|
87
|
-
// if the element is not shown update his position
|
|
88
|
-
CheckIfRender.elements[i] = CheckIfRender.calculateNewPosition(elem)
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
-
CheckIfRender.isListenerAttached = window.requestAnimationFrame(
|
|
92
|
-
CheckIfRender.eventHandler
|
|
93
|
-
)
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
CheckIfRender.removeScrollHandler = function () {
|
|
98
|
-
window.cancelAnimationFrame(CheckIfRender.isListenerAttached)
|
|
99
|
-
CheckIfRender.isListenerAttached = false
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
//When an element is unloaded remove it from the list of elements that are waiting to be lazy-loaded
|
|
103
|
-
CheckIfRender.removeElementFromList = function (toRemove) {
|
|
104
|
-
CheckIfRender.elements = CheckIfRender.elements.filter(
|
|
105
|
-
(elem) => elem !== toRemove
|
|
106
|
-
)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
CheckIfRender.isListenerAttached = false
|
|
110
|
-
|
|
111
|
-
export { CheckIfRender }
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import React, { useState, useEffect, useRef } from 'react' // eslint-disable-line no-unused-vars
|
|
2
|
-
import useIsInViewPort from './baseClass'
|
|
3
|
-
|
|
4
|
-
export default function LazyBackgroundImage(props) {
|
|
5
|
-
const ref = useRef()
|
|
6
|
-
let isViewable = useIsInViewPort(ref, props)
|
|
7
|
-
const [style, setStyle] = useState({
|
|
8
|
-
backgroundImage: `url(${isViewable.link})`,
|
|
9
|
-
...props.style,
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
useEffect(() => {
|
|
13
|
-
setStyle({
|
|
14
|
-
backgroundImage: `url(${isViewable.link})`,
|
|
15
|
-
...props.style,
|
|
16
|
-
})
|
|
17
|
-
}, [isViewable])
|
|
18
|
-
|
|
19
|
-
return (
|
|
20
|
-
<div className={props.className} style={style} ref={ref}>
|
|
21
|
-
{props.children}
|
|
22
|
-
</div>
|
|
23
|
-
)
|
|
24
|
-
}
|
package/src/lazyLoadComponent.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import React, { useRef } from 'react' // eslint-disable-line no-unused-vars
|
|
2
|
-
import useIsInViewPort from './baseClass'
|
|
3
|
-
|
|
4
|
-
export default function LazyComponent(props) {
|
|
5
|
-
const ref = useRef()
|
|
6
|
-
let isViewable = useIsInViewPort(ref, props)
|
|
7
|
-
|
|
8
|
-
const placeHolder = (
|
|
9
|
-
<div
|
|
10
|
-
style={
|
|
11
|
-
props.style
|
|
12
|
-
? props.style
|
|
13
|
-
: {
|
|
14
|
-
heigt: '300px',
|
|
15
|
-
width: '300px',
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
/>
|
|
19
|
-
)
|
|
20
|
-
|
|
21
|
-
return (
|
|
22
|
-
<div ref={ref}>{isViewable.visible ? props.children : placeHolder}</div>
|
|
23
|
-
)
|
|
24
|
-
}
|
package/src/lazyLoadFrame.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import React, { useRef } from 'react' // eslint-disable-line no-unused-vars
|
|
2
|
-
import useIsInViewPort from './baseClass'
|
|
3
|
-
|
|
4
|
-
export default function LazyFrame(props) {
|
|
5
|
-
const ref = useRef()
|
|
6
|
-
let isViewable = useIsInViewPort(ref, props)
|
|
7
|
-
|
|
8
|
-
return (
|
|
9
|
-
<iframe
|
|
10
|
-
height={props.height || '500'}
|
|
11
|
-
scrolling={props.scrolling || 'no'}
|
|
12
|
-
src={isViewable.link}
|
|
13
|
-
frameBorder={props.frameBorder || 'no'}
|
|
14
|
-
allowtransparency={props.allowTransparency || 'true'}
|
|
15
|
-
allowFullScreen={props.allowFullScreen || true}
|
|
16
|
-
style={props.style || {}}
|
|
17
|
-
ref={ref}
|
|
18
|
-
/>
|
|
19
|
-
)
|
|
20
|
-
}
|
package/src/lazyLoadImage.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import React, { useRef } from 'react' // eslint-disable-line no-unused-vars
|
|
2
|
-
import useIsInViewPort from './baseClass'
|
|
3
|
-
|
|
4
|
-
export default function LazyImage(props) {
|
|
5
|
-
const ref = useRef()
|
|
6
|
-
const isViewable = useIsInViewPort(ref, props)
|
|
7
|
-
|
|
8
|
-
return (
|
|
9
|
-
<img
|
|
10
|
-
src={isViewable.link}
|
|
11
|
-
alt={props.alt}
|
|
12
|
-
style={props.style}
|
|
13
|
-
className={props.className}
|
|
14
|
-
ref={ref}
|
|
15
|
-
/>
|
|
16
|
-
)
|
|
17
|
-
}
|
package/webpack.config.demo.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
var path = require('path')
|
|
2
|
-
|
|
3
|
-
module.exports = {
|
|
4
|
-
mode:'development',
|
|
5
|
-
entry: './demo/demo.js',
|
|
6
|
-
output: {
|
|
7
|
-
path: path.join(__dirname),
|
|
8
|
-
filename: './demo/index.js'
|
|
9
|
-
},
|
|
10
|
-
module: {
|
|
11
|
-
rules: [
|
|
12
|
-
{
|
|
13
|
-
test: /\.js$/,
|
|
14
|
-
use: 'babel-loader',
|
|
15
|
-
exclude: /node_modules/
|
|
16
|
-
}
|
|
17
|
-
]
|
|
18
|
-
}
|
|
19
|
-
}
|
package/webpack.config.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
var path = require('path')
|
|
2
|
-
var webpack = require('webpack')
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
mode:'production',
|
|
6
|
-
entry: './src/index.js',
|
|
7
|
-
output: {
|
|
8
|
-
library: 'LazyReact',
|
|
9
|
-
libraryTarget: 'umd',
|
|
10
|
-
path: path.join(__dirname),
|
|
11
|
-
filename: './dist/index.js'
|
|
12
|
-
},
|
|
13
|
-
plugins: [
|
|
14
|
-
new webpack.DefinePlugin({
|
|
15
|
-
'process.env': {
|
|
16
|
-
NODE_ENV: '"production"'
|
|
17
|
-
},
|
|
18
|
-
__DEVELOPMENT__: false
|
|
19
|
-
})],
|
|
20
|
-
externals: [
|
|
21
|
-
{
|
|
22
|
-
react: {
|
|
23
|
-
root: 'React',
|
|
24
|
-
commonjs2: 'react',
|
|
25
|
-
commonjs: 'react',
|
|
26
|
-
amd: 'react'
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
'react-dom': {
|
|
31
|
-
root: 'ReactDOM',
|
|
32
|
-
commonjs2: 'react-dom',
|
|
33
|
-
commonjs: 'react-dom',
|
|
34
|
-
amd: 'react-dom'
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
],
|
|
38
|
-
module: {
|
|
39
|
-
rules: [
|
|
40
|
-
{
|
|
41
|
-
test: /\.js$/,
|
|
42
|
-
use: 'babel-loader',
|
|
43
|
-
exclude: /node_modules/
|
|
44
|
-
}
|
|
45
|
-
]
|
|
46
|
-
}
|
|
47
|
-
}
|