lazy-react 3.6.0 → 3.6.2
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 +4 -0
- package/biome.json +18 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +6 -13
- package/src/demo/index.js +71 -56
- package/src/lazyLoadBackgroundImage.tsx +34 -24
- package/src/lazyLoadComponent.tsx +24 -17
- package/src/lazyLoadFrame.tsx +23 -17
- package/src/lazyLoadImage.tsx +24 -17
- package/src/useIsInViewport.tsx +95 -94
- package/.eslintrc +0 -28
package/README.md
CHANGED
package/biome.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"files": {
|
|
3
|
+
"includes": ["**", "!dist", "!**/dist", "!demo"]
|
|
4
|
+
},
|
|
5
|
+
"javascript": {
|
|
6
|
+
"formatter": {
|
|
7
|
+
"quoteStyle": "single",
|
|
8
|
+
"semicolons": "asNeeded"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"assist": {
|
|
12
|
+
"actions": {
|
|
13
|
+
"source": {
|
|
14
|
+
"organizeImports": "off"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e,t=require("react/jsx-runtime"),n=require("react");function r(e,t,n,r){Object.defineProperty(e,t,{get:n,set:r,enumerable:!0,configurable:!0})}function l(e){const[r,l,
|
|
1
|
+
var e,t=require("react/jsx-runtime"),n=require("react");function r(e,t,n,r){Object.defineProperty(e,t,{get:n,set:r,enumerable:!0,configurable:!0})}function l(e){const[r,l,s]=u(e),[i,o]=(0,n.useState)({backgroundImage:`url(${l})`,...e.style});return(0,n.useEffect)((()=>{s&&o({backgroundImage:`url(${l})`,...e.style})}),[l,s,e.style]),(0,t.jsx)("div",{className:e.className,style:i,ref:e=>{r(e)},children:e.children})}function s(e){const[n,r]=u(e);return(0,t.jsx)("div",{ref:e=>{n(e)},children:(0,t.jsx)("img",{src:r,alt:e.alt,style:e.style,className:e.className})})}function i(e){const[n,r]=u(e);return(0,t.jsx)("iframe",{src:r,ref:e=>{n(e)},...e,height:e.height||"500",allowFullScreen:e.allowFullScreen||!0})}function o(e){const[n,r,l]=u(e);return l?(0,t.jsx)("div",{children:e.children}):(0,t.jsx)("div",{ref:e=>{n(e)},style:{height:"300px",width:"300px",...e.style}})}e=module.exports,Object.defineProperty(e,"__esModule",{value:!0,configurable:!0}),r(module.exports,"default",(()=>u)),r(module.exports,"LazyBackgroundImage",(()=>l)),r(module.exports,"LazyImage",(()=>s)),r(module.exports,"LazyFrame",(()=>i)),r(module.exports,"LazyComponent",(()=>o));const a=new Set,c={isInViewPort:({offset:e,top:t,left:n})=>t<window.innerHeight+e&&n<window.innerWidth+e,calculateNewPosition:e=>{const{top:t,left:n}=e.element?.getBoundingClientRect()||{top:0,left:0};return{...e,top:t,left:n}},addElement:e=>{a.add(e),0===c.isListenerAttached&&(c.isListenerAttached=window.requestAnimationFrame(c.eventHandler))},eventHandler:()=>{0===a.size?c.removeScrollHandler():(a.forEach((e=>{c.isInViewPort(c.calculateNewPosition(e))&&(e.makeItVisible(),c.removeElementFromList(e))})),c.isListenerAttached=window.requestAnimationFrame(c.eventHandler))},removeScrollHandler:()=>{window.cancelAnimationFrame(c.isListenerAttached),c.isListenerAttached=0},removeElementFromList:e=>{a.delete(e)},isListenerAttached:0};function u(e){const[t,r]=(0,n.useState)(""),[l,s]=(0,n.useState)(!1),[i,o]=(0,n.useState)(null),a=(0,n.useCallback)((()=>{e.link&&r((()=>e.link)),s((()=>!0))}),[e.link]),u=(0,n.useMemo)((()=>({element:i,makeItVisible:a,offset:e.offset||100})),[i,e.offset,a]),d=(0,n.useCallback)((e=>{null!==e&&o(e)}),[]);return(0,n.useEffect)((()=>(null!==i&&c.addElement(u),()=>c.removeElementFromList(u))),[i,u]),[d,t,l]}
|
|
2
2
|
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Aa1C,IACd,OAATA,GAAed,EAAOc,EAAK,GAC7B,IAWH,OATA,EAAAR,EAAAG,YAAU,KACG,OAARI,GAEHe,EAAcY,WAAWiB,GACnB,IAEN7B,EAAcqB,sBAAsBQ,KACnC,CAAC5C,EAAK4C,IAEF,CAACE,EAAQ1D,EAAMqD,E","sources":["src/lazyLoadBackgroundImage.tsx","src/lazyLoadImage.tsx","src/lazyLoadFrame.tsx","src/lazyLoadComponent.tsx","src/useIsInViewport.tsx"],"sourcesContent":["import { useState, useEffect, type ReactNode } from 'react'\nimport useIsInViewPort from './index'\n\ninterface LazyBackgroundImageProps {\n\tlink: string\n\toffset: number\n\tstyle: object\n\tclassName: string\n\tchildren: ReactNode,\n}\n\nexport default function LazyBackgroundImage(props: LazyBackgroundImageProps) {\n\tconst [setRef, link, isViewable] = useIsInViewPort(props)\n\n\tconst [style, setStyle] = useState({\n\t\tbackgroundImage: `url(${link})`,\n\t\t...props.style,\n\t})\n\n\tuseEffect(() => {\n\t\tisViewable\n\t\t\t? setStyle({\n\t\t\t\t\tbackgroundImage: `url(${link})`,\n\t\t\t\t\t...props.style,\n\t\t\t\t})\n\t\t\t: null\n\t}, [link, isViewable, props.style])\n\n\treturn (\n\t\t<div\n\t\t\tclassName={props.className}\n\t\t\tstyle={style}\n\t\t\tref={(node) => {\n\t\t\t\tsetRef(node)\n\t\t\t}}\n\t\t>\n\t\t\t{props.children}\n\t\t</div>\n\t)\n}\n","import useIsInViewPort from './index'\n\ninterface LazyImageProps {\n link: string,\n offset: number,\n alt: string,\n style: object,\n className: string\n}\n\nexport default function LazyImage(props: LazyImageProps) {\n\tconst [setRef, link] = useIsInViewPort(props)\n\n\treturn (\n\t\t<div\n\t\t\tref={(node) => {\n\t\t\t\tsetRef(node)\n\t\t\t}}\n\t\t>\n\t\t\t<img\n\t\t\t\tsrc={link}\n\t\t\t\talt={props.alt}\n\t\t\t\tstyle={props.style}\n\t\t\t\tclassName={props.className}\n\t\t\t/>\n\t\t</div>\n\t)\n}\n","import useIsInViewPort from './index'\n\ninterface LazyFrameProps {\n\theight: number\n\tlink: string\n\toffset: number\n\tallowFullScreen: boolean,\n}\n\nexport default function LazyFrame(props: LazyFrameProps) {\n\tconst [setRef, link] = useIsInViewPort(props)\n\n\treturn (\n\t\t<iframe\n\t\t\t// scrolling={props.scrolling || 'no'}\n\t\t\tsrc={link}\n\t\t\t// frameBorder={props.frameBorder || 'no'}\n\t\t\t// style={props.style || {border: props.frameBorder}}\n\t\t\tref={(node) => {\n\t\t\t\tsetRef(node)\n\t\t\t}}\n\t\t\t{...props}\n\t\t\theight={props.height || '500'}\n\t\t\tallowFullScreen={props.allowFullScreen || true}\n\t\t/>\n\t)\n}\n","import type { ReactNode } from 'react'\nimport useIsInViewport from './index'\n\ninterface LazyComponentProps {\n link: string,\n offset: number,\n style: object,\n children: ReactNode\n}\n\nexport default function LazyComponent(props: LazyComponentProps) {\n\tconst [setRef, _, isVisible] = useIsInViewport(props)\n\n\tif (!isVisible)\n\t\treturn (\n\t\t\t<div\n\t\t\t\tref={(node) => {\n\t\t\t\t\tsetRef(node)\n\t\t\t\t}}\n\t\t\t\tstyle={{\n\t\t\t\t\theight: '300px',\n\t\t\t\t\twidth: '300px',\n\t\t\t\t\t...props.style,\n\t\t\t\t}}\n\t\t\t></div>\n\t\t)\n\n\treturn <div>{props.children}</div>\n}\n","import { useState, useEffect, useMemo, useCallback } from 'react'\n\ninterface InstanceElement {\n\tmakeItVisible: () => void\n\telement: HTMLElement | null\n\toffset: number\n}\n\n// array with all the elements that are waiting to be shown in the viewport\nconst elements = new Set<InstanceElement>()\n\nconst CheckIfRender = {\n\t// top: the position of the element in relation with the top of the browser\n\t// left: the position of the element in relation with the left of the browser\n\t// offset: the desired offset of the element in relation of the viewport\n\tisInViewPort: ({\n\t\toffset,\n\t\ttop,\n\t\tleft,\n\t}: {\n\t\toffset: number\n\t\ttop: number\n\t\tleft: number\n\t}) => top < window.innerHeight + offset && left < window.innerWidth + offset,\n\n\tcalculateNewPosition: (elem: InstanceElement) => {\n\t\tconst { top, left } = elem.element?.getBoundingClientRect() || {\n\t\t\ttop: 0,\n\t\t\tleft: 0,\n\t\t}\n\n\t\treturn {\n\t\t\t...elem,\n\t\t\ttop,\n\t\t\tleft,\n\t\t}\n\t},\n\taddElement: (element: InstanceElement) => {\n\t\t//the distance from the pixel 0,0 and the top of the element\n\t\telements.add(element)\n\t\t//check if has already been started the rAF cycle\n\t\tif (CheckIfRender.isListenerAttached === 0) {\n\t\t\tCheckIfRender.isListenerAttached = window.requestAnimationFrame(\n\t\t\t\tCheckIfRender.eventHandler,\n\t\t\t)\n\t\t}\n\t},\n\teventHandler: () => {\n\t\t//if there is no more element to lazy load remove the listener/rAF\n\t\tif (elements.size === 0) {\n\t\t\tCheckIfRender.removeScrollHandler()\n\t\t} else {\n\t\t\telements.forEach((elem) => {\n\t\t\t\tconst haveToShow = CheckIfRender.isInViewPort(\n\t\t\t\t\tCheckIfRender.calculateNewPosition(elem),\n\t\t\t\t)\n\t\t\t\tif (haveToShow) {\n\t\t\t\t\telem.makeItVisible()\n\t\t\t\t\t// remove element from the list of elements to lazy load\n\t\t\t\t\tCheckIfRender.removeElementFromList(elem)\n\t\t\t\t}\n\t\t\t})\n\t\t\tCheckIfRender.isListenerAttached = window.requestAnimationFrame(\n\t\t\t\tCheckIfRender.eventHandler,\n\t\t\t)\n\t\t}\n\t},\n\n\tremoveScrollHandler: () => {\n\t\twindow.cancelAnimationFrame(CheckIfRender.isListenerAttached)\n\t\tCheckIfRender.isListenerAttached = 0\n\t},\n\t//When an element is unloaded remove it from the list of elements that are waiting to be lazy-loaded\n\tremoveElementFromList: (toRemove: InstanceElement) => {\n\t\telements.delete(toRemove)\n\t},\n\tisListenerAttached: 0, // intended as not set\n}\n\nexport default function useRenderIfInViewPort(props: {\n\tlink: string\n\toffset: number\n}): [(node: HTMLDivElement | null) => void, string, boolean] {\n\tconst [link, setLink] = useState('')\n\tconst [visible, setVisible] = useState(false)\n\n\tconst [ref, setRef] = useState<HTMLDivElement | null>(null)\n\n\tconst makeItVisible = useCallback(() => {\n\t\tif (props.link) setLink(() => props.link)\n\t\tsetVisible(() => true)\n\t}, [props.link])\n\n\tconst thisInstance = useMemo(() => {\n\t\treturn {\n\t\t\telement: ref,\n\t\t\tmakeItVisible,\n\t\t\toffset: props.offset || 100,\n\t\t}\n\t}, [ref, props.offset, makeItVisible])\n\n\tconst getRef = useCallback((node: HTMLDivElement | null) => {\n\t\tif (node !== null) setRef(node)\n\t}, [])\n\n\tuseEffect(() => {\n\t\tif (ref !== null)\n\t\t\t// add the element to the array of elements that are waiting to be lazy loaded\n\t\t\tCheckIfRender.addElement(thisInstance)\n\t\treturn () =>\n\t\t\t// if the element is unloaded remove the element from the list of elements that needs to be lazy loader\n\t\t\tCheckIfRender.removeElementFromList(thisInstance)\n\t}, [ref, thisInstance])\n\n\treturn [getRef, link, visible]\n}\n\n"],"names":["$6e38eea936045ca0$export$2e2bcd8739ae039","props","setRef","link","isViewable","$03b515f3f8c77d8f$export$2e2bcd8739ae039","style","setStyle","$9w1Q4$react","useState","backgroundImage","useEffect","$9w1Q4$reactjsxruntime","jsx","className","ref","node","children","$f6f596272f209e0c$export$2e2bcd8739ae039","src","alt","$8e3d626f2d1eebd8$export$2e2bcd8739ae039","height","allowFullScreen","$c17687259f8b5ddf$export$2e2bcd8739ae039","_","isVisible","width","$03b515f3f8c77d8f$var$elements","Set","$03b515f3f8c77d8f$var$CheckIfRender","isInViewPort","offset","top","left","window","innerHeight","innerWidth","calculateNewPosition","elem","element","getBoundingClientRect","addElement","add","isListenerAttached","requestAnimationFrame","eventHandler","size","removeScrollHandler","forEach","makeItVisible","removeElementFromList","cancelAnimationFrame","toRemove","delete","setLink","visible","setVisible","useCallback","thisInstance","useMemo","getRef"],"version":3,"file":"index.js.map"}
|
package/package.json
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lazy-react",
|
|
3
|
-
"version": "3.6.
|
|
3
|
+
"version": "3.6.2",
|
|
4
4
|
"description": "components to lazy load iframes, images and images as backgrounds",
|
|
5
5
|
"source": "./src/index.tsx",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"start": "parcel serve ./src/demo/index.html --dist-dir ./demo",
|
|
9
9
|
"build": "parcel build",
|
|
10
|
-
"lint": "
|
|
11
|
-
"
|
|
12
|
-
"format": "prettier --write --single-quote --no-semi ./src/*.tsx",
|
|
10
|
+
"lint": "biome lint src/*",
|
|
11
|
+
"format": "biome format --write ./src/*.tsx",
|
|
13
12
|
"precommit": "lint-staged",
|
|
14
13
|
"test": "jest"
|
|
15
14
|
},
|
|
@@ -19,8 +18,8 @@
|
|
|
19
18
|
}
|
|
20
19
|
},
|
|
21
20
|
"lint-staged": {
|
|
22
|
-
"src/*.
|
|
23
|
-
"
|
|
21
|
+
"src/*.jsx": [
|
|
22
|
+
"biome format",
|
|
24
23
|
"git add"
|
|
25
24
|
]
|
|
26
25
|
},
|
|
@@ -35,20 +34,14 @@
|
|
|
35
34
|
"url": "https://github.com/jonathanobino/react-lazy"
|
|
36
35
|
},
|
|
37
36
|
"devDependencies": {
|
|
38
|
-
"@
|
|
37
|
+
"@biomejs/biome": "2.3.11",
|
|
39
38
|
"@types/node": "^17.0.21",
|
|
40
39
|
"@types/react": "^18.3.0",
|
|
41
40
|
"@types/react-dom": "^18.3.0",
|
|
42
|
-
"@typescript-eslint/eslint-plugin": "^5.35.1",
|
|
43
|
-
"@typescript-eslint/parser": "^5.35.1",
|
|
44
|
-
"eslint": "^7.32.0",
|
|
45
|
-
"eslint-config-prettier": "^8.5.0",
|
|
46
|
-
"eslint-plugin-react": "^7.30.1",
|
|
47
41
|
"husky": "^4.3.0",
|
|
48
42
|
"jest": "^27.5.1",
|
|
49
43
|
"lint-staged": "^10.4.0",
|
|
50
44
|
"parcel": "^2.7.0",
|
|
51
|
-
"prettier": "^2.1.2",
|
|
52
45
|
"process": "^0.11.10"
|
|
53
46
|
},
|
|
54
47
|
"peerDependencies": {
|
package/src/demo/index.js
CHANGED
|
@@ -1,66 +1,81 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
|
|
3
|
-
import React from 'react'
|
|
4
1
|
import { createRoot } from 'react-dom/client'
|
|
5
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
LazyImage,
|
|
4
|
+
LazyBackgroundImage,
|
|
5
|
+
LazyFrame,
|
|
6
|
+
LazyComponent,
|
|
7
|
+
} from '../../dist'
|
|
6
8
|
|
|
7
|
-
function Experimental
|
|
8
|
-
|
|
9
|
+
function Experimental() {
|
|
10
|
+
return <h1> I’m lazy loaded!</h1>
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
function Demo() {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
14
|
+
return (
|
|
15
|
+
<div>
|
|
16
|
+
<h2>Just scroll down</h2>
|
|
17
|
+
<div
|
|
18
|
+
style={{
|
|
19
|
+
minHeight: '100vh',
|
|
20
|
+
minWidth: '100vw',
|
|
21
|
+
backgroundColor: 'tomato',
|
|
22
|
+
}}
|
|
23
|
+
></div>
|
|
24
|
+
<LazyImage
|
|
25
|
+
link={
|
|
26
|
+
'https://images.unsplash.com/photo-1462834026679-7c03bf571a67?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=6e160dc1e65511df7bf1c461f8a93c82'
|
|
27
|
+
}
|
|
28
|
+
offset={0}
|
|
29
|
+
/>
|
|
30
|
+
<LazyImage
|
|
31
|
+
link={
|
|
32
|
+
'https://images.unsplash.com/5/unsplash-kitsune-4.jpg?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=44828cfc286e8eb8cd0ed2ca561f8cb9'
|
|
33
|
+
}
|
|
34
|
+
offset={100}
|
|
35
|
+
className={'styledImage'}
|
|
36
|
+
/>
|
|
37
|
+
<LazyImage
|
|
38
|
+
link={
|
|
39
|
+
'https://images.unsplash.com/photo-1472132858735-6313c7962473?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=f38318ed6adee6db020705bedf6d6e10'
|
|
40
|
+
}
|
|
41
|
+
offset={100}
|
|
42
|
+
/>
|
|
43
|
+
<LazyImage
|
|
44
|
+
link={
|
|
45
|
+
'https://images.unsplash.com/photo-1469899324414-c72bfb4d4161?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=8d708d646e3b2d32c5faf67e01919872'
|
|
46
|
+
}
|
|
47
|
+
offset={100}
|
|
48
|
+
className={'test'}
|
|
49
|
+
preserveAspect={false}
|
|
50
|
+
/>
|
|
51
|
+
<LazyImage
|
|
52
|
+
link={
|
|
53
|
+
'https://images.unsplash.com/photo-1462834026679-7c03bf571a67?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=6e160dc1e65511df7bf1c461f8a93c82'
|
|
54
|
+
}
|
|
55
|
+
offset={100}
|
|
56
|
+
className={'test'}
|
|
57
|
+
preserveAspect={false}
|
|
58
|
+
/>
|
|
59
|
+
<LazyBackgroundImage
|
|
60
|
+
link={
|
|
61
|
+
'https://images.unsplash.com/photo-1462834026679-7c03bf571a67?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=6e160dc1e65511df7bf1c461f8a93c82'
|
|
62
|
+
}
|
|
63
|
+
className="fill"
|
|
64
|
+
/>
|
|
48
65
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
66
|
+
<LazyComponent>
|
|
67
|
+
<Experimental />
|
|
68
|
+
<Experimental />
|
|
69
|
+
<Experimental />
|
|
70
|
+
</LazyComponent>
|
|
54
71
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
/>
|
|
59
|
-
</div>
|
|
72
|
+
<LazyFrame link={'http://jonathanobino.xyz'} scrolling={'true'} />
|
|
73
|
+
</div>
|
|
74
|
+
)
|
|
60
75
|
}
|
|
61
76
|
|
|
62
|
-
const container = document.getElementById('app')
|
|
63
|
-
const root = createRoot(container)
|
|
64
|
-
root.render(<Demo/>)
|
|
77
|
+
const container = document.getElementById('app')
|
|
78
|
+
const root = createRoot(container)
|
|
79
|
+
root.render(<Demo />)
|
|
65
80
|
|
|
66
|
-
// ReactDOM.render(<Demo/>, document.getElementById('app'))
|
|
81
|
+
// ReactDOM.render(<Demo/>, document.getElementById('app'))
|
|
@@ -1,30 +1,40 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { useState, useEffect, type ReactNode } from 'react'
|
|
2
2
|
import useIsInViewPort from './index'
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
interface LazyBackgroundImageProps {
|
|
5
|
+
link: string
|
|
6
|
+
offset: number
|
|
7
|
+
style: object
|
|
8
|
+
className: string
|
|
9
|
+
children: ReactNode,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default function LazyBackgroundImage(props: LazyBackgroundImageProps) {
|
|
13
|
+
const [setRef, link, isViewable] = useIsInViewPort(props)
|
|
6
14
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
const [style, setStyle] = useState({
|
|
16
|
+
backgroundImage: `url(${link})`,
|
|
17
|
+
...props.style,
|
|
18
|
+
})
|
|
11
19
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
isViewable
|
|
22
|
+
? setStyle({
|
|
23
|
+
backgroundImage: `url(${link})`,
|
|
24
|
+
...props.style,
|
|
25
|
+
})
|
|
26
|
+
: null
|
|
27
|
+
}, [link, isViewable, props.style])
|
|
18
28
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
return (
|
|
30
|
+
<div
|
|
31
|
+
className={props.className}
|
|
32
|
+
style={style}
|
|
33
|
+
ref={(node) => {
|
|
34
|
+
setRef(node)
|
|
35
|
+
}}
|
|
36
|
+
>
|
|
37
|
+
{props.children}
|
|
38
|
+
</div>
|
|
39
|
+
)
|
|
30
40
|
}
|
|
@@ -1,22 +1,29 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { ReactNode } from 'react'
|
|
2
2
|
import useIsInViewport from './index'
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
interface LazyComponentProps {
|
|
5
|
+
link: string,
|
|
6
|
+
offset: number,
|
|
7
|
+
style: object,
|
|
8
|
+
children: ReactNode
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default function LazyComponent(props: LazyComponentProps) {
|
|
12
|
+
const [setRef, _, isVisible] = useIsInViewport(props)
|
|
6
13
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
if (!isVisible)
|
|
15
|
+
return (
|
|
16
|
+
<div
|
|
17
|
+
ref={(node) => {
|
|
18
|
+
setRef(node)
|
|
19
|
+
}}
|
|
20
|
+
style={{
|
|
21
|
+
height: '300px',
|
|
22
|
+
width: '300px',
|
|
23
|
+
...props.style,
|
|
24
|
+
}}
|
|
25
|
+
></div>
|
|
26
|
+
)
|
|
20
27
|
|
|
21
|
-
|
|
28
|
+
return <div>{props.children}</div>
|
|
22
29
|
}
|
package/src/lazyLoadFrame.tsx
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
|
-
import React from 'react' // eslint-disable-line no-unused-vars
|
|
2
1
|
import useIsInViewPort from './index'
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
interface LazyFrameProps {
|
|
4
|
+
height: number
|
|
5
|
+
link: string
|
|
6
|
+
offset: number
|
|
7
|
+
allowFullScreen: boolean,
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default function LazyFrame(props: LazyFrameProps) {
|
|
11
|
+
const [setRef, link] = useIsInViewPort(props)
|
|
6
12
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
return (
|
|
14
|
+
<iframe
|
|
15
|
+
// scrolling={props.scrolling || 'no'}
|
|
16
|
+
src={link}
|
|
17
|
+
// frameBorder={props.frameBorder || 'no'}
|
|
18
|
+
// style={props.style || {border: props.frameBorder}}
|
|
19
|
+
ref={(node) => {
|
|
20
|
+
setRef(node)
|
|
21
|
+
}}
|
|
22
|
+
{...props}
|
|
23
|
+
height={props.height || '500'}
|
|
24
|
+
allowFullScreen={props.allowFullScreen || true}
|
|
25
|
+
/>
|
|
26
|
+
)
|
|
21
27
|
}
|
package/src/lazyLoadImage.tsx
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
|
-
import React from 'react' // eslint-disable-line no-unused-vars
|
|
2
1
|
import useIsInViewPort from './index'
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
interface LazyImageProps {
|
|
4
|
+
link: string,
|
|
5
|
+
offset: number,
|
|
6
|
+
alt: string,
|
|
7
|
+
style: object,
|
|
8
|
+
className: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default function LazyImage(props: LazyImageProps) {
|
|
12
|
+
const [setRef, link] = useIsInViewPort(props)
|
|
6
13
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
return (
|
|
15
|
+
<div
|
|
16
|
+
ref={(node) => {
|
|
17
|
+
setRef(node)
|
|
18
|
+
}}
|
|
19
|
+
>
|
|
20
|
+
<img
|
|
21
|
+
src={link}
|
|
22
|
+
alt={props.alt}
|
|
23
|
+
style={props.style}
|
|
24
|
+
className={props.className}
|
|
25
|
+
/>
|
|
26
|
+
</div>
|
|
27
|
+
)
|
|
21
28
|
}
|
package/src/useIsInViewport.tsx
CHANGED
|
@@ -1,116 +1,117 @@
|
|
|
1
1
|
import { useState, useEffect, useMemo, useCallback } from 'react'
|
|
2
2
|
|
|
3
3
|
interface InstanceElement {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
makeItVisible: () => void
|
|
5
|
+
element: HTMLElement | null
|
|
6
|
+
offset: number
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
// array with all the elements that are waiting to be shown in the viewport
|
|
10
10
|
const elements = new Set<InstanceElement>()
|
|
11
11
|
|
|
12
12
|
const CheckIfRender = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
13
|
+
// top: the position of the element in relation with the top of the browser
|
|
14
|
+
// left: the position of the element in relation with the left of the browser
|
|
15
|
+
// offset: the desired offset of the element in relation of the viewport
|
|
16
|
+
isInViewPort: ({
|
|
17
|
+
offset,
|
|
18
|
+
top,
|
|
19
|
+
left,
|
|
20
|
+
}: {
|
|
21
|
+
offset: number
|
|
22
|
+
top: number
|
|
23
|
+
left: number
|
|
24
|
+
}) => top < window.innerHeight + offset && left < window.innerWidth + offset,
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
26
|
+
calculateNewPosition: (elem: InstanceElement) => {
|
|
27
|
+
const { top, left } = elem.element?.getBoundingClientRect() || {
|
|
28
|
+
top: 0,
|
|
29
|
+
left: 0,
|
|
30
|
+
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
32
|
+
return {
|
|
33
|
+
...elem,
|
|
34
|
+
top,
|
|
35
|
+
left,
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
addElement: (element: InstanceElement) => {
|
|
39
|
+
//the distance from the pixel 0,0 and the top of the element
|
|
40
|
+
elements.add(element)
|
|
41
|
+
//check if has already been started the rAF cycle
|
|
42
|
+
if (CheckIfRender.isListenerAttached === 0) {
|
|
43
|
+
CheckIfRender.isListenerAttached = window.requestAnimationFrame(
|
|
44
|
+
CheckIfRender.eventHandler,
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
eventHandler: () => {
|
|
49
|
+
//if there is no more element to lazy load remove the listener/rAF
|
|
50
|
+
if (elements.size === 0) {
|
|
51
|
+
CheckIfRender.removeScrollHandler()
|
|
52
|
+
} else {
|
|
53
|
+
elements.forEach((elem) => {
|
|
54
|
+
const haveToShow = CheckIfRender.isInViewPort(
|
|
55
|
+
CheckIfRender.calculateNewPosition(elem),
|
|
56
|
+
)
|
|
57
|
+
if (haveToShow) {
|
|
58
|
+
elem.makeItVisible()
|
|
59
|
+
// remove element from the list of elements to lazy load
|
|
60
|
+
CheckIfRender.removeElementFromList(elem)
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
CheckIfRender.isListenerAttached = window.requestAnimationFrame(
|
|
64
|
+
CheckIfRender.eventHandler,
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
69
|
+
removeScrollHandler: () => {
|
|
70
|
+
window.cancelAnimationFrame(CheckIfRender.isListenerAttached)
|
|
71
|
+
CheckIfRender.isListenerAttached = 0
|
|
72
|
+
},
|
|
73
|
+
//When an element is unloaded remove it from the list of elements that are waiting to be lazy-loaded
|
|
74
|
+
removeElementFromList: (toRemove: InstanceElement) => {
|
|
75
|
+
elements.delete(toRemove)
|
|
76
|
+
},
|
|
77
|
+
isListenerAttached: 0, // intended as not set
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
export default function useRenderIfInViewPort(props: {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}): [
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
link: string
|
|
82
|
+
offset: number
|
|
83
|
+
}): [(node: HTMLDivElement | null) => void, string, boolean] {
|
|
84
|
+
const [link, setLink] = useState('')
|
|
85
|
+
const [visible, setVisible] = useState(false)
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
const [ref, setRef] = useState<HTMLDivElement | null>(null)
|
|
88
88
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
offset: props.offset || 100,
|
|
94
|
-
}
|
|
95
|
-
}, [ref, props])
|
|
89
|
+
const makeItVisible = useCallback(() => {
|
|
90
|
+
if (props.link) setLink(() => props.link)
|
|
91
|
+
setVisible(() => true)
|
|
92
|
+
}, [props.link])
|
|
96
93
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
94
|
+
const thisInstance = useMemo(() => {
|
|
95
|
+
return {
|
|
96
|
+
element: ref,
|
|
97
|
+
makeItVisible,
|
|
98
|
+
offset: props.offset || 100,
|
|
99
|
+
}
|
|
100
|
+
}, [ref, props.offset, makeItVisible])
|
|
100
101
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
102
|
+
const getRef = useCallback((node: HTMLDivElement | null) => {
|
|
103
|
+
if (node !== null) setRef(node)
|
|
104
|
+
}, [])
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
if (ref !== null)
|
|
108
|
+
// add the element to the array of elements that are waiting to be lazy loaded
|
|
109
|
+
CheckIfRender.addElement(thisInstance)
|
|
110
|
+
return () =>
|
|
111
|
+
// if the element is unloaded remove the element from the list of elements that needs to be lazy loader
|
|
112
|
+
CheckIfRender.removeElementFromList(thisInstance)
|
|
113
|
+
}, [ref, thisInstance])
|
|
114
114
|
|
|
115
|
-
|
|
115
|
+
return [getRef, link, visible]
|
|
116
116
|
}
|
|
117
|
+
|
package/.eslintrc
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"env": {
|
|
3
|
-
"browser": true,
|
|
4
|
-
"es6": true,
|
|
5
|
-
"node": true
|
|
6
|
-
},
|
|
7
|
-
"root": true,
|
|
8
|
-
"parser": "@typescript-eslint/parser",
|
|
9
|
-
"plugins": [
|
|
10
|
-
"@typescript-eslint"
|
|
11
|
-
],
|
|
12
|
-
"extends": [
|
|
13
|
-
"eslint:recommended",
|
|
14
|
-
"plugin:react/recommended",
|
|
15
|
-
"prettier",
|
|
16
|
-
"plugin:@typescript-eslint/eslint-recommended",
|
|
17
|
-
"plugin:@typescript-eslint/recommended"
|
|
18
|
-
],
|
|
19
|
-
"rules": {
|
|
20
|
-
"@typescript-eslint/ban-types": "off",
|
|
21
|
-
"@typescript-eslint/no-explicit-any": "off"
|
|
22
|
-
},
|
|
23
|
-
"settings": {
|
|
24
|
-
"react": {
|
|
25
|
-
"version": "detect"
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|