lazy-react 3.6.1 → 3.6.3
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/LICENSE +1 -1
- package/biome.json +18 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/module.js +186 -0
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +38 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +34 -29
- 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 +26 -17
- package/src/useIsInViewport.tsx +95 -94
- package/.eslintrc +0 -28
package/LICENSE
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
|
|
1
|
+
var $9w1Q4$reactjsxruntime=require("react/jsx-runtime"),$9w1Q4$react=require("react");function $parcel$defineInteropFlag(e){Object.defineProperty(e,"__esModule",{value:!0,configurable:!0})}function $parcel$export(e,t,r,$){Object.defineProperty(e,t,{get:r,set:$,enumerable:!0,configurable:!0})}function $6e38eea936045ca0$export$2e2bcd8739ae039(e){let[t,r,$]=$03b515f3f8c77d8f$export$2e2bcd8739ae039(e),[f,c]=(0,$9w1Q4$react.useState)({backgroundImage:`url(${r})`,...e.style});return(0,$9w1Q4$react.useEffect)(()=>{$&&c({backgroundImage:`url(${r})`,...e.style})},[r,$,e.style]),(0,$9w1Q4$reactjsxruntime.jsx)("div",{className:e.className,style:f,ref:e=>{t(e)},children:e.children})}function $f6f596272f209e0c$export$2e2bcd8739ae039(e){let[t,r]=$03b515f3f8c77d8f$export$2e2bcd8739ae039(e);return(0,$9w1Q4$reactjsxruntime.jsx)("div",{ref:e=>{t(e)},children:(0,$9w1Q4$reactjsxruntime.jsx)("img",{src:r,alt:e.alt,style:e.style,className:e.className,title:e.title})})}function $8e3d626f2d1eebd8$export$2e2bcd8739ae039(e){let[t,r]=$03b515f3f8c77d8f$export$2e2bcd8739ae039(e);return(0,$9w1Q4$reactjsxruntime.jsx)("iframe",{src:r,ref:e=>{t(e)},...e,height:e.height||"500",allowFullScreen:e.allowFullScreen||!0})}function $c17687259f8b5ddf$export$2e2bcd8739ae039(e){let[t,r,$]=$03b515f3f8c77d8f$export$2e2bcd8739ae039(e);return $?(0,$9w1Q4$reactjsxruntime.jsx)("div",{children:e.children}):(0,$9w1Q4$reactjsxruntime.jsx)("div",{ref:e=>{t(e)},style:{height:"300px",width:"300px",...e.style}})}$parcel$defineInteropFlag(module.exports),$parcel$export(module.exports,"default",()=>$03b515f3f8c77d8f$export$2e2bcd8739ae039),$parcel$export(module.exports,"LazyBackgroundImage",()=>$6e38eea936045ca0$export$2e2bcd8739ae039),$parcel$export(module.exports,"LazyImage",()=>$f6f596272f209e0c$export$2e2bcd8739ae039),$parcel$export(module.exports,"LazyFrame",()=>$8e3d626f2d1eebd8$export$2e2bcd8739ae039),$parcel$export(module.exports,"LazyComponent",()=>$c17687259f8b5ddf$export$2e2bcd8739ae039);const $03b515f3f8c77d8f$var$elements=new Set,$03b515f3f8c77d8f$var$CheckIfRender={isInViewPort:({offset:e,top:t,left:r})=>t<window.innerHeight+e&&r<window.innerWidth+e,calculateNewPosition:e=>{let{top:t,left:r}=e.element?.getBoundingClientRect()||{top:0,left:0};return{...e,top:t,left:r}},addElement:e=>{$03b515f3f8c77d8f$var$elements.add(e),0===$03b515f3f8c77d8f$var$CheckIfRender.isListenerAttached&&($03b515f3f8c77d8f$var$CheckIfRender.isListenerAttached=window.requestAnimationFrame($03b515f3f8c77d8f$var$CheckIfRender.eventHandler))},eventHandler:()=>{0===$03b515f3f8c77d8f$var$elements.size?$03b515f3f8c77d8f$var$CheckIfRender.removeScrollHandler():($03b515f3f8c77d8f$var$elements.forEach(e=>{$03b515f3f8c77d8f$var$CheckIfRender.isInViewPort($03b515f3f8c77d8f$var$CheckIfRender.calculateNewPosition(e))&&(e.makeItVisible(),$03b515f3f8c77d8f$var$CheckIfRender.removeElementFromList(e))}),$03b515f3f8c77d8f$var$CheckIfRender.isListenerAttached=window.requestAnimationFrame($03b515f3f8c77d8f$var$CheckIfRender.eventHandler))},removeScrollHandler:()=>{window.cancelAnimationFrame($03b515f3f8c77d8f$var$CheckIfRender.isListenerAttached),$03b515f3f8c77d8f$var$CheckIfRender.isListenerAttached=0},removeElementFromList:e=>{$03b515f3f8c77d8f$var$elements.delete(e)},isListenerAttached:0};function $03b515f3f8c77d8f$export$2e2bcd8739ae039(e){let[t,r]=(0,$9w1Q4$react.useState)(""),[$,f]=(0,$9w1Q4$react.useState)(!1),[c,a]=(0,$9w1Q4$react.useState)(null),n=(0,$9w1Q4$react.useCallback)(()=>{e.link&&r(()=>e.link),f(()=>!0)},[e.link]),d=(0,$9w1Q4$react.useMemo)(()=>({element:c,makeItVisible:n,offset:e.offset||100}),[c,e.offset,n]),l=(0,$9w1Q4$react.useCallback)(e=>{null!==e&&a(e)},[]);return(0,$9w1Q4$react.useEffect)(()=>(null!==c&&$03b515f3f8c77d8f$var$CheckIfRender.addElement(d),()=>$03b515f3f8c77d8f$var$CheckIfRender.removeElementFromList(d)),[c,d]),[l,t,$]}
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|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ACI,OAARI,GAEFgB,EAAcY,WAAWgB,GACpB,IAEL5B,EAAcqB,sBAAsBO,KACrC,CAAC5C,IAEG,CAAC8C,EAAQ1D,EAAMsD,E","sources":["src/lazyLoadBackgroundImage.tsx","src/lazyLoadImage.tsx","src/lazyLoadFrame.tsx","src/lazyLoadComponent.tsx","src/useIsInViewport.tsx"],"sourcesContent":["import React, { useState, useEffect } from 'react' // eslint-disable-line no-unused-vars\nimport useIsInViewPort from './index'\n\nexport default function LazyBackgroundImage(props: any) {\n const [setRef, link, isViewable] = useIsInViewPort(props)\n\n const [style, setStyle] = useState({\n backgroundImage: `url(${link})`,\n ...props.style,\n })\n\n useEffect(() => {\n setStyle({\n backgroundImage: `url(${link})`,\n ...props.style,\n })\n }, [link, isViewable])\n\n return (\n <div\n className={props.className}\n style={style}\n ref={(node) => {\n setRef(node)\n }}\n >\n {props.children}\n </div>\n )\n}\n","import React from 'react' // eslint-disable-line no-unused-vars\nimport useIsInViewPort from './index'\n\nexport default function LazyImage(props: any) {\n const [setRef, link] = useIsInViewPort(props)\n\n return (\n <div\n ref={(node) => {\n setRef(node)\n }}\n >\n <img\n src={link}\n alt={props.alt}\n style={props.style}\n className={props.className}\n />\n </div>\n )\n}\n","import React from 'react' // eslint-disable-line no-unused-vars\nimport useIsInViewPort from './index'\n\nexport default function LazyFrame(props: any) {\n const [setRef, link] = useIsInViewPort(props)\n\n return (\n <iframe\n height={props.height || '500'}\n scrolling={props.scrolling || 'no'}\n src={link}\n frameBorder={props.frameBorder || 'no'}\n allowFullScreen={props.allowFullScreen || true}\n style={props.style || {}}\n ref={(node) => {\n setRef(node)\n }}\n {...props}\n />\n )\n}\n","import React from 'react'\nimport useIsInViewport from './index'\n\nexport default function LazyComponent(props: any) {\n const [setRef, link, isVisible] = useIsInViewport(props)\n\n if (!isVisible)\n return (\n <div\n ref={(node) => {\n setRef(node)\n }}\n style={{\n height: '300px',\n width: '300px',\n ...props.style,\n }}\n ></div>\n )\n\n return <div>{props.children}</div>\n}\n","import { useState, useEffect, useMemo, useCallback } from 'react'\n\ninterface InstanceElement {\n makeItVisible: () => void\n element: HTMLElement | null\n offset: 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 // top: the position of the element in relation with the top of the browser\n // left: the position of the element in relation with the left of the browser\n // offset: the desired offset of the element in relation of the viewport\n isInViewPort: ({\n offset,\n top,\n left,\n }: {\n offset: number\n top: number\n left: number\n }) => top < window.innerHeight + offset && left < window.innerWidth + offset,\n\n calculateNewPosition: (elem: InstanceElement) => {\n const { top, left } = elem.element?.getBoundingClientRect() || {\n top: 0,\n left: 0,\n }\n\n return {\n ...elem,\n top,\n left,\n }\n },\n addElement: function (element: InstanceElement) {\n //the distance from the pixel 0,0 and the top of the element\n elements.add(element)\n //check if has already been started the rAF cycle\n if (CheckIfRender.isListenerAttached === 0) {\n CheckIfRender.isListenerAttached = window.requestAnimationFrame(\n CheckIfRender.eventHandler\n )\n }\n },\n eventHandler: function () {\n //if there is no more element to lazy load remove the listener/rAF\n if (elements.size === 0) {\n CheckIfRender.removeScrollHandler()\n } else {\n elements.forEach((elem) => {\n const haveToShow = CheckIfRender.isInViewPort(\n CheckIfRender.calculateNewPosition(elem)\n )\n if (haveToShow) {\n elem.makeItVisible()\n // remove element from the list of elements to lazy load\n CheckIfRender.removeElementFromList(elem)\n }\n })\n CheckIfRender.isListenerAttached = window.requestAnimationFrame(\n CheckIfRender.eventHandler\n )\n }\n },\n\n removeScrollHandler: function () {\n window.cancelAnimationFrame(CheckIfRender.isListenerAttached)\n CheckIfRender.isListenerAttached = 0\n },\n //When an element is unloaded remove it from the list of elements that are waiting to be lazy-loaded\n removeElementFromList: function (toRemove: InstanceElement) {\n elements.delete(toRemove)\n },\n isListenerAttached: 0, // intended as not set\n}\n\nexport default function useRenderIfInViewPort(props: {\n link: string\n offset: number\n}): [Function, string, boolean] {\n const [link, setLink] = useState('')\n const [visible, setVisible] = useState(false)\n\n const [ref, setRef] = useState(null)\n\n const thisInstance = useMemo(() => {\n return {\n element: ref,\n makeItVisible,\n offset: props.offset || 100,\n }\n }, [ref, props])\n\n const getRef = useCallback((node) => {\n if (node !== null) setRef(node)\n }, [])\n\n function makeItVisible() {\n if (props.link) setLink(() => props.link)\n setVisible(() => true)\n }\n\n useEffect(() => {\n if (ref !== null)\n // add the element to the array of elements that are waiting to be lazy loaded\n CheckIfRender.addElement(thisInstance)\n return () =>\n // if the element is unloaded remove the element from the list of elements that needs to be lazy loader\n CheckIfRender.removeElementFromList(thisInstance)\n }, [ref])\n\n return [getRef, link, visible]\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","scrolling","frameBorder","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","thisInstance","useMemo","getRef","useCallback"],"version":3,"file":"index.js.map"}
|
|
1
|
+
{"mappings":"A,I,uB,Q,qB,a,Q,S,S,0B,C,E,O,c,C,E,a,C,M,C,E,a,C,C,E,C,S,e,C,C,C,C,C,C,C,E,O,c,C,E,E,C,I,E,I,E,W,C,E,a,C,C,E,CEWe,SAAA,yCAA6B,CAA+B,EAC1E,GAAM,CAAC,EAAQ,EAAM,EAAW,CAAG,AAAA,yCAAgB,GAE7C,CAAC,EAAO,EAAS,CAAG,A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AAuB,AAAC,IACvB,+BAAS,MAAM,CAAC,EACjB,EACA,mBAAoB,CACrB,EAEe,SAAA,yCAA+B,CAG7C,EACA,GAAM,CAAC,EAAM,EAAQ,CAAG,AAAA,CAAA,EAAA,aAAA,QAAO,AAAP,EAAS,IAC3B,CAAC,EAAS,EAAW,CAAG,AAAA,CAAA,EAAA,aAAA,QAAO,AAAP,EAAS,CAAA,GAEjC,CAAC,EAAK,EAAO,CAAG,AAAA,CAAA,EAAA,aAAA,QAAO,AAAP,EAAgC,MAEhD,EAAgB,AAAA,CAAA,EAAA,aAAA,WAAU,AAAV,EAAY,KAC7B,EAAM,IAAI,EAAE,EAAQ,IAAM,EAAM,IAAI,EACxC,EAAW,IAAM,CAAA,EAClB,EAAG,CAAC,EAAM,IAAI,CAAC,EAET,EAAe,AAAA,CAAA,EAAA,aAAA,OAAM,AAAN,EAAQ,IACrB,CAAA,CACN,QAAS,EACT,cAAA,EACA,OAAQ,EAAM,MAAM,EAAI,GACzB,CAAA,EACE,CAAC,EAAK,EAAM,MAAM,CAAE,EAAc,EAE/B,EAAS,AAAA,CAAA,EAAA,aAAA,WAAU,AAAV,EAAY,AAAC,IACvB,AAAS,OAAT,GAAe,EAAO,EAC3B,EAAG,EAAE,EAWL,MATA,AAAA,CAAA,EAAA,aAAA,SAAQ,AAAR,EAAU,KACL,AAAQ,OAAR,GAEH,oCAAc,UAAU,CAAC,GACnB,IAEN,oCAAc,qBAAqB,CAAC,IACnC,CAAC,EAAK,EAAa,EAEf,CAAC,EAAQ,EAAM,EAAQ,AAC/B","sources":["<anon>","src/index.tsx","src/lazyLoadBackgroundImage.tsx","src/lazyLoadImage.tsx","src/lazyLoadFrame.tsx","src/lazyLoadComponent.tsx","src/useIsInViewport.tsx"],"sourcesContent":["var $9w1Q4$reactjsxruntime = require(\"react/jsx-runtime\");\nvar $9w1Q4$react = require(\"react\");\n\n\nfunction $parcel$defineInteropFlag(a) {\n Object.defineProperty(a, '__esModule', {value: true, configurable: true});\n}\n\nfunction $parcel$export(e, n, v, s) {\n Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});\n}\n\n$parcel$defineInteropFlag(module.exports);\n\n$parcel$export(module.exports, \"default\", () => $03b515f3f8c77d8f$export$2e2bcd8739ae039);\n$parcel$export(module.exports, \"LazyBackgroundImage\", () => $6e38eea936045ca0$export$2e2bcd8739ae039);\n$parcel$export(module.exports, \"LazyImage\", () => $f6f596272f209e0c$export$2e2bcd8739ae039);\n$parcel$export(module.exports, \"LazyFrame\", () => $8e3d626f2d1eebd8$export$2e2bcd8739ae039);\n$parcel$export(module.exports, \"LazyComponent\", () => $c17687259f8b5ddf$export$2e2bcd8739ae039);\n\n\n\nfunction $6e38eea936045ca0$export$2e2bcd8739ae039(props) {\n const [setRef, link, isViewable] = (0, $03b515f3f8c77d8f$export$2e2bcd8739ae039)(props);\n const [style, setStyle] = (0, $9w1Q4$react.useState)({\n backgroundImage: `url(${link})`,\n ...props.style\n });\n (0, $9w1Q4$react.useEffect)(()=>{\n isViewable && setStyle({\n backgroundImage: `url(${link})`,\n ...props.style\n });\n }, [\n link,\n isViewable,\n props.style\n ]);\n return /*#__PURE__*/ (0, $9w1Q4$reactjsxruntime.jsx)(\"div\", {\n className: props.className,\n style: style,\n ref: (node)=>{\n setRef(node);\n },\n children: props.children\n });\n}\n\n\n\n\nfunction $f6f596272f209e0c$export$2e2bcd8739ae039(props) {\n const [setRef, link] = (0, $03b515f3f8c77d8f$export$2e2bcd8739ae039)(props);\n return /*#__PURE__*/ (0, $9w1Q4$reactjsxruntime.jsx)(\"div\", {\n ref: (node)=>{\n setRef(node);\n },\n children: /*#__PURE__*/ (0, $9w1Q4$reactjsxruntime.jsx)(\"img\", {\n src: link,\n alt: props.alt,\n style: props.style,\n className: props.className,\n title: props.title\n })\n });\n}\n\n\n\n\nfunction $8e3d626f2d1eebd8$export$2e2bcd8739ae039(props) {\n const [setRef, link] = (0, $03b515f3f8c77d8f$export$2e2bcd8739ae039)(props);\n return /*#__PURE__*/ (0, $9w1Q4$reactjsxruntime.jsx)(\"iframe\", {\n // scrolling={props.scrolling || 'no'}\n src: link,\n // frameBorder={props.frameBorder || 'no'}\n // style={props.style || {border: props.frameBorder}}\n ref: (node)=>{\n setRef(node);\n },\n ...props,\n height: props.height || '500',\n allowFullScreen: props.allowFullScreen || true\n });\n}\n\n\n\n\nfunction $c17687259f8b5ddf$export$2e2bcd8739ae039(props) {\n const [setRef, _, isVisible] = (0, $03b515f3f8c77d8f$export$2e2bcd8739ae039)(props);\n if (!isVisible) return /*#__PURE__*/ (0, $9w1Q4$reactjsxruntime.jsx)(\"div\", {\n ref: (node)=>{\n setRef(node);\n },\n style: {\n height: '300px',\n width: '300px',\n ...props.style\n }\n });\n return /*#__PURE__*/ (0, $9w1Q4$reactjsxruntime.jsx)(\"div\", {\n children: props.children\n });\n}\n\n\n\n// array with all the elements that are waiting to be shown in the viewport\nconst $03b515f3f8c77d8f$var$elements = new Set();\nconst $03b515f3f8c77d8f$var$CheckIfRender = {\n // top: the position of the element in relation with the top of the browser\n // left: the position of the element in relation with the left of the browser\n // offset: the desired offset of the element in relation of the viewport\n isInViewPort: ({ offset: offset, top: top, left: left })=>top < window.innerHeight + offset && left < window.innerWidth + offset,\n calculateNewPosition: (elem)=>{\n const { top: top, left: left } = elem.element?.getBoundingClientRect() || {\n top: 0,\n left: 0\n };\n return {\n ...elem,\n top: top,\n left: left\n };\n },\n addElement: (element)=>{\n //the distance from the pixel 0,0 and the top of the element\n $03b515f3f8c77d8f$var$elements.add(element);\n //check if has already been started the rAF cycle\n if ($03b515f3f8c77d8f$var$CheckIfRender.isListenerAttached === 0) $03b515f3f8c77d8f$var$CheckIfRender.isListenerAttached = window.requestAnimationFrame($03b515f3f8c77d8f$var$CheckIfRender.eventHandler);\n },\n eventHandler: ()=>{\n //if there is no more element to lazy load remove the listener/rAF\n if ($03b515f3f8c77d8f$var$elements.size === 0) $03b515f3f8c77d8f$var$CheckIfRender.removeScrollHandler();\n else {\n $03b515f3f8c77d8f$var$elements.forEach((elem)=>{\n const haveToShow = $03b515f3f8c77d8f$var$CheckIfRender.isInViewPort($03b515f3f8c77d8f$var$CheckIfRender.calculateNewPosition(elem));\n if (haveToShow) {\n elem.makeItVisible();\n // remove element from the list of elements to lazy load\n $03b515f3f8c77d8f$var$CheckIfRender.removeElementFromList(elem);\n }\n });\n $03b515f3f8c77d8f$var$CheckIfRender.isListenerAttached = window.requestAnimationFrame($03b515f3f8c77d8f$var$CheckIfRender.eventHandler);\n }\n },\n removeScrollHandler: ()=>{\n window.cancelAnimationFrame($03b515f3f8c77d8f$var$CheckIfRender.isListenerAttached);\n $03b515f3f8c77d8f$var$CheckIfRender.isListenerAttached = 0;\n },\n //When an element is unloaded remove it from the list of elements that are waiting to be lazy-loaded\n removeElementFromList: (toRemove)=>{\n $03b515f3f8c77d8f$var$elements.delete(toRemove);\n },\n isListenerAttached: 0\n};\nfunction $03b515f3f8c77d8f$export$2e2bcd8739ae039(props) {\n const [link, setLink] = (0, $9w1Q4$react.useState)('');\n const [visible, setVisible] = (0, $9w1Q4$react.useState)(false);\n const [ref, setRef] = (0, $9w1Q4$react.useState)(null);\n const makeItVisible = (0, $9w1Q4$react.useCallback)(()=>{\n if (props.link) setLink(()=>props.link);\n setVisible(()=>true);\n }, [\n props.link\n ]);\n const thisInstance = (0, $9w1Q4$react.useMemo)(()=>{\n return {\n element: ref,\n makeItVisible: makeItVisible,\n offset: props.offset || 100\n };\n }, [\n ref,\n props.offset,\n makeItVisible\n ]);\n const getRef = (0, $9w1Q4$react.useCallback)((node)=>{\n if (node !== null) setRef(node);\n }, []);\n (0, $9w1Q4$react.useEffect)(()=>{\n if (ref !== null) // add the element to the array of elements that are waiting to be lazy loaded\n $03b515f3f8c77d8f$var$CheckIfRender.addElement(thisInstance);\n return ()=>// if the element is unloaded remove the element from the list of elements that needs to be lazy loader\n $03b515f3f8c77d8f$var$CheckIfRender.removeElementFromList(thisInstance);\n }, [\n ref,\n thisInstance\n ]);\n return [\n getRef,\n link,\n visible\n ];\n}\n\n\n\n\n//# sourceMappingURL=index.js.map\n","import LazyBackgroundImage from './lazyLoadBackgroundImage'\nimport LazyImage from './lazyLoadImage'\nimport LazyFrame from './lazyLoadFrame'\nimport LazyComponent from './lazyLoadComponent'\n\nexport { default } from './useIsInViewport'\nexport { LazyBackgroundImage, LazyImage, LazyFrame, LazyComponent }\n","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\ttitle: 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\ttitle={props.title}\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":["$9w1Q4$reactjsxruntime","require","$9w1Q4$react","$parcel$defineInteropFlag","a","Object","defineProperty","value","configurable","$parcel$export","e","n","v","s","get","set","enumerable","$6e38eea936045ca0$export$2e2bcd8739ae039","props","setRef","link","isViewable","$03b515f3f8c77d8f$export$2e2bcd8739ae039","style","setStyle","useState","backgroundImage","useEffect","jsx","className","ref","node","children","$f6f596272f209e0c$export$2e2bcd8739ae039","src","alt","title","$8e3d626f2d1eebd8$export$2e2bcd8739ae039","height","allowFullScreen","$c17687259f8b5ddf$export$2e2bcd8739ae039","_","isVisible","width","module","exports","$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/dist/module.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import {jsx as $4MPRY$jsx} from "react/jsx-runtime";
|
|
2
|
+
import {useState as $4MPRY$useState, useEffect as $4MPRY$useEffect, useCallback as $4MPRY$useCallback, useMemo as $4MPRY$useMemo} from "react";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
function $d53282a0f683afba$export$2e2bcd8739ae039(props) {
|
|
8
|
+
const [setRef, link, isViewable] = (0, $cf52ec504e3068a6$export$2e2bcd8739ae039)(props);
|
|
9
|
+
const [style, setStyle] = (0, $4MPRY$useState)({
|
|
10
|
+
backgroundImage: `url(${link})`,
|
|
11
|
+
...props.style
|
|
12
|
+
});
|
|
13
|
+
(0, $4MPRY$useEffect)(()=>{
|
|
14
|
+
isViewable && setStyle({
|
|
15
|
+
backgroundImage: `url(${link})`,
|
|
16
|
+
...props.style
|
|
17
|
+
});
|
|
18
|
+
}, [
|
|
19
|
+
link,
|
|
20
|
+
isViewable,
|
|
21
|
+
props.style
|
|
22
|
+
]);
|
|
23
|
+
return /*#__PURE__*/ (0, $4MPRY$jsx)("div", {
|
|
24
|
+
className: props.className,
|
|
25
|
+
style: style,
|
|
26
|
+
ref: (node)=>{
|
|
27
|
+
setRef(node);
|
|
28
|
+
},
|
|
29
|
+
children: props.children
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
function $c76f9bf8c035eea9$export$2e2bcd8739ae039(props) {
|
|
37
|
+
const [setRef, link] = (0, $cf52ec504e3068a6$export$2e2bcd8739ae039)(props);
|
|
38
|
+
return /*#__PURE__*/ (0, $4MPRY$jsx)("div", {
|
|
39
|
+
ref: (node)=>{
|
|
40
|
+
setRef(node);
|
|
41
|
+
},
|
|
42
|
+
children: /*#__PURE__*/ (0, $4MPRY$jsx)("img", {
|
|
43
|
+
src: link,
|
|
44
|
+
alt: props.alt,
|
|
45
|
+
style: props.style,
|
|
46
|
+
className: props.className,
|
|
47
|
+
title: props.title
|
|
48
|
+
})
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
function $6154e4cf378cce38$export$2e2bcd8739ae039(props) {
|
|
56
|
+
const [setRef, link] = (0, $cf52ec504e3068a6$export$2e2bcd8739ae039)(props);
|
|
57
|
+
return /*#__PURE__*/ (0, $4MPRY$jsx)("iframe", {
|
|
58
|
+
// scrolling={props.scrolling || 'no'}
|
|
59
|
+
src: link,
|
|
60
|
+
// frameBorder={props.frameBorder || 'no'}
|
|
61
|
+
// style={props.style || {border: props.frameBorder}}
|
|
62
|
+
ref: (node)=>{
|
|
63
|
+
setRef(node);
|
|
64
|
+
},
|
|
65
|
+
...props,
|
|
66
|
+
height: props.height || '500',
|
|
67
|
+
allowFullScreen: props.allowFullScreen || true
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
function $bff95282d9e2e246$export$2e2bcd8739ae039(props) {
|
|
75
|
+
const [setRef, _, isVisible] = (0, $cf52ec504e3068a6$export$2e2bcd8739ae039)(props);
|
|
76
|
+
if (!isVisible) return /*#__PURE__*/ (0, $4MPRY$jsx)("div", {
|
|
77
|
+
ref: (node)=>{
|
|
78
|
+
setRef(node);
|
|
79
|
+
},
|
|
80
|
+
style: {
|
|
81
|
+
height: '300px',
|
|
82
|
+
width: '300px',
|
|
83
|
+
...props.style
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
return /*#__PURE__*/ (0, $4MPRY$jsx)("div", {
|
|
87
|
+
children: props.children
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
// array with all the elements that are waiting to be shown in the viewport
|
|
94
|
+
const $cf52ec504e3068a6$var$elements = new Set();
|
|
95
|
+
const $cf52ec504e3068a6$var$CheckIfRender = {
|
|
96
|
+
// top: the position of the element in relation with the top of the browser
|
|
97
|
+
// left: the position of the element in relation with the left of the browser
|
|
98
|
+
// offset: the desired offset of the element in relation of the viewport
|
|
99
|
+
isInViewPort: ({ offset: offset, top: top, left: left })=>top < window.innerHeight + offset && left < window.innerWidth + offset,
|
|
100
|
+
calculateNewPosition: (elem)=>{
|
|
101
|
+
const { top: top, left: left } = elem.element?.getBoundingClientRect() || {
|
|
102
|
+
top: 0,
|
|
103
|
+
left: 0
|
|
104
|
+
};
|
|
105
|
+
return {
|
|
106
|
+
...elem,
|
|
107
|
+
top: top,
|
|
108
|
+
left: left
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
addElement: (element)=>{
|
|
112
|
+
//the distance from the pixel 0,0 and the top of the element
|
|
113
|
+
$cf52ec504e3068a6$var$elements.add(element);
|
|
114
|
+
//check if has already been started the rAF cycle
|
|
115
|
+
if ($cf52ec504e3068a6$var$CheckIfRender.isListenerAttached === 0) $cf52ec504e3068a6$var$CheckIfRender.isListenerAttached = window.requestAnimationFrame($cf52ec504e3068a6$var$CheckIfRender.eventHandler);
|
|
116
|
+
},
|
|
117
|
+
eventHandler: ()=>{
|
|
118
|
+
//if there is no more element to lazy load remove the listener/rAF
|
|
119
|
+
if ($cf52ec504e3068a6$var$elements.size === 0) $cf52ec504e3068a6$var$CheckIfRender.removeScrollHandler();
|
|
120
|
+
else {
|
|
121
|
+
$cf52ec504e3068a6$var$elements.forEach((elem)=>{
|
|
122
|
+
const haveToShow = $cf52ec504e3068a6$var$CheckIfRender.isInViewPort($cf52ec504e3068a6$var$CheckIfRender.calculateNewPosition(elem));
|
|
123
|
+
if (haveToShow) {
|
|
124
|
+
elem.makeItVisible();
|
|
125
|
+
// remove element from the list of elements to lazy load
|
|
126
|
+
$cf52ec504e3068a6$var$CheckIfRender.removeElementFromList(elem);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
$cf52ec504e3068a6$var$CheckIfRender.isListenerAttached = window.requestAnimationFrame($cf52ec504e3068a6$var$CheckIfRender.eventHandler);
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
removeScrollHandler: ()=>{
|
|
133
|
+
window.cancelAnimationFrame($cf52ec504e3068a6$var$CheckIfRender.isListenerAttached);
|
|
134
|
+
$cf52ec504e3068a6$var$CheckIfRender.isListenerAttached = 0;
|
|
135
|
+
},
|
|
136
|
+
//When an element is unloaded remove it from the list of elements that are waiting to be lazy-loaded
|
|
137
|
+
removeElementFromList: (toRemove)=>{
|
|
138
|
+
$cf52ec504e3068a6$var$elements.delete(toRemove);
|
|
139
|
+
},
|
|
140
|
+
isListenerAttached: 0
|
|
141
|
+
};
|
|
142
|
+
function $cf52ec504e3068a6$export$2e2bcd8739ae039(props) {
|
|
143
|
+
const [link, setLink] = (0, $4MPRY$useState)('');
|
|
144
|
+
const [visible, setVisible] = (0, $4MPRY$useState)(false);
|
|
145
|
+
const [ref, setRef] = (0, $4MPRY$useState)(null);
|
|
146
|
+
const makeItVisible = (0, $4MPRY$useCallback)(()=>{
|
|
147
|
+
if (props.link) setLink(()=>props.link);
|
|
148
|
+
setVisible(()=>true);
|
|
149
|
+
}, [
|
|
150
|
+
props.link
|
|
151
|
+
]);
|
|
152
|
+
const thisInstance = (0, $4MPRY$useMemo)(()=>{
|
|
153
|
+
return {
|
|
154
|
+
element: ref,
|
|
155
|
+
makeItVisible: makeItVisible,
|
|
156
|
+
offset: props.offset || 100
|
|
157
|
+
};
|
|
158
|
+
}, [
|
|
159
|
+
ref,
|
|
160
|
+
props.offset,
|
|
161
|
+
makeItVisible
|
|
162
|
+
]);
|
|
163
|
+
const getRef = (0, $4MPRY$useCallback)((node)=>{
|
|
164
|
+
if (node !== null) setRef(node);
|
|
165
|
+
}, []);
|
|
166
|
+
(0, $4MPRY$useEffect)(()=>{
|
|
167
|
+
if (ref !== null) // add the element to the array of elements that are waiting to be lazy loaded
|
|
168
|
+
$cf52ec504e3068a6$var$CheckIfRender.addElement(thisInstance);
|
|
169
|
+
return ()=>// if the element is unloaded remove the element from the list of elements that needs to be lazy loader
|
|
170
|
+
$cf52ec504e3068a6$var$CheckIfRender.removeElementFromList(thisInstance);
|
|
171
|
+
}, [
|
|
172
|
+
ref,
|
|
173
|
+
thisInstance
|
|
174
|
+
]);
|
|
175
|
+
return [
|
|
176
|
+
getRef,
|
|
177
|
+
link,
|
|
178
|
+
visible
|
|
179
|
+
];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
export {$cf52ec504e3068a6$export$2e2bcd8739ae039 as default, $d53282a0f683afba$export$2e2bcd8739ae039 as LazyBackgroundImage, $c76f9bf8c035eea9$export$2e2bcd8739ae039 as LazyImage, $6154e4cf378cce38$export$2e2bcd8739ae039 as LazyFrame, $bff95282d9e2e246$export$2e2bcd8739ae039 as LazyComponent};
|
|
186
|
+
//# sourceMappingURL=module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;ACWe,kDAA6B,KAA+B;IAC1E,MAAM,CAAC,QAAQ,MAAM,WAAW,GAAG,CAAA,GAAA,wCAAc,EAAE;IAEnD,MAAM,CAAC,OAAO,SAAS,GAAG,CAAA,GAAA,eAAO,EAAE;QAClC,iBAAiB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAC/B,GAAG,MAAM,KAAK;IACf;IAEA,CAAA,GAAA,gBAAQ,EAAE;QACT,cACG,SAAS;YACT,iBAAiB,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YAC/B,GAAG,MAAM,KAAK;QACf;IAEH,GAAG;QAAC;QAAM;QAAY,MAAM,KAAK;KAAC;IAElC,qBACC,gBAAC;QACA,WAAW,MAAM,SAAS;QAC1B,OAAO;QACP,KAAK,CAAC;YACL,OAAO;QACR;kBAEC,MAAM,QAAQ;;AAGlB;;;;;AC5Be,kDAAmB,KAAqB;IACtD,MAAM,CAAC,QAAQ,KAAK,GAAG,CAAA,GAAA,wCAAc,EAAE;IAEvC,qBACC,gBAAC;QACA,KAAK,CAAC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;QACT,IAAI,QAAQ,MACX,8EAA8E;QAC9E,oCAAc,UAAU,CAAC;QAC1B,OAAO,IACN,uGAAuG;YACvG,oCAAc,qBAAqB,CAAC;IACtC,GAAG;QAAC;QAAK;KAAa;IAEtB,OAAO;QAAC;QAAQ;QAAM;KAAQ;AAC/B;","sources":["src/index.tsx","src/lazyLoadBackgroundImage.tsx","src/lazyLoadImage.tsx","src/lazyLoadFrame.tsx","src/lazyLoadComponent.tsx","src/useIsInViewport.tsx"],"sourcesContent":["import LazyBackgroundImage from './lazyLoadBackgroundImage'\nimport LazyImage from './lazyLoadImage'\nimport LazyFrame from './lazyLoadFrame'\nimport LazyComponent from './lazyLoadComponent'\n\nexport { default } from './useIsInViewport'\nexport { LazyBackgroundImage, LazyImage, LazyFrame, LazyComponent }\n","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\ttitle: 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\ttitle={props.title}\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":[],"version":3,"file":"module.js.map"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ReactNode, JSX } from "react";
|
|
2
|
+
interface LazyBackgroundImageProps {
|
|
3
|
+
link: string;
|
|
4
|
+
offset: number;
|
|
5
|
+
style: object;
|
|
6
|
+
className: string;
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
}
|
|
9
|
+
export function LazyBackgroundImage(props: LazyBackgroundImageProps): JSX.Element;
|
|
10
|
+
interface LazyImageProps {
|
|
11
|
+
link: string;
|
|
12
|
+
offset: number;
|
|
13
|
+
alt: string;
|
|
14
|
+
style: object;
|
|
15
|
+
className: string;
|
|
16
|
+
title: string;
|
|
17
|
+
}
|
|
18
|
+
export function LazyImage(props: LazyImageProps): JSX.Element;
|
|
19
|
+
interface LazyFrameProps {
|
|
20
|
+
height: number;
|
|
21
|
+
link: string;
|
|
22
|
+
offset: number;
|
|
23
|
+
allowFullScreen: boolean;
|
|
24
|
+
}
|
|
25
|
+
export function LazyFrame(props: LazyFrameProps): JSX.Element;
|
|
26
|
+
interface LazyComponentProps {
|
|
27
|
+
link: string;
|
|
28
|
+
offset: number;
|
|
29
|
+
style: object;
|
|
30
|
+
children: ReactNode;
|
|
31
|
+
}
|
|
32
|
+
export function LazyComponent(props: LazyComponentProps): JSX.Element;
|
|
33
|
+
export default function useRenderIfInViewPort(props: {
|
|
34
|
+
link: string;
|
|
35
|
+
offset: number;
|
|
36
|
+
}): [(node: HTMLDivElement | null) => void, string, boolean];
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";AAGA;IACC,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,MAAM,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,SAAS,CAAC;CACpB;AAED,oCAA4C,KAAK,EAAE,wBAAwB,eA4B1E;ACrCD;IACE,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAA;CACb;AAED,0BAAkC,KAAK,EAAE,cAAc,eAkBtD;AC3BD;IACC,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,MAAM,CAAA;IACd,eAAe,EAAE,OAAO,CAAC;CACzB;AAED,0BAAkC,KAAK,EAAE,cAAc,eAiBtD;ACvBD;IACE,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,SAAS,CAAA;CACpB;AAED,8BAAsC,KAAK,EAAE,kBAAkB,eAkB9D;ACmDD,8CAA8C,KAAK,EAAE;IACpD,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,MAAM,CAAA;CACd,GAAG,CAAC,CAAC,IAAI,EAAE,cAAc,GAAG,IAAI,KAAK,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,CAiC3D","sources":["src/src/lazyLoadBackgroundImage.tsx","src/src/lazyLoadImage.tsx","src/src/lazyLoadFrame.tsx","src/src/lazyLoadComponent.tsx","src/src/useIsInViewport.tsx","src/src/index.tsx","src/index.tsx"],"sourcesContent":[null,null,null,null,null,null,"import LazyBackgroundImage from './lazyLoadBackgroundImage'\nimport LazyImage from './lazyLoadImage'\nimport LazyFrame from './lazyLoadFrame'\nimport LazyComponent from './lazyLoadComponent'\n\nexport { default } from './useIsInViewport'\nexport { LazyBackgroundImage, LazyImage, LazyFrame, LazyComponent }\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
package/package.json
CHANGED
|
@@ -1,15 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lazy-react",
|
|
3
|
-
"version": "3.6.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "3.6.3",
|
|
4
|
+
"description": "React utility for lazy resource loading and lazy component execution. Engineered with requestAnimationFrame to ensure 60fps scrolling, it minimizes both network payload and CPU overhead by deferring resource fetching and component mounting until the viewport entry.",
|
|
5
|
+
"author": "Jonathan Obino",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/jonathanobino/react-lazy"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"react",
|
|
13
|
+
"lazy-loading",
|
|
14
|
+
"typescript",
|
|
15
|
+
"javascript",
|
|
16
|
+
"performance",
|
|
17
|
+
"react-component",
|
|
18
|
+
"web-performance",
|
|
19
|
+
"frontend",
|
|
20
|
+
"library"
|
|
21
|
+
],
|
|
5
22
|
"source": "./src/index.tsx",
|
|
6
23
|
"main": "./dist/index.js",
|
|
24
|
+
"module": "dist/module.js",
|
|
25
|
+
"types": "./dist/types.d.ts",
|
|
7
26
|
"scripts": {
|
|
8
27
|
"start": "parcel serve ./src/demo/index.html --dist-dir ./demo",
|
|
9
28
|
"build": "parcel build",
|
|
10
|
-
"lint": "
|
|
11
|
-
"
|
|
12
|
-
"format": "prettier --write --single-quote --no-semi ./src/*.tsx",
|
|
29
|
+
"lint": "biome lint src/*",
|
|
30
|
+
"format": "biome format --write ./src/*.tsx",
|
|
13
31
|
"precommit": "lint-staged",
|
|
14
32
|
"test": "jest"
|
|
15
33
|
},
|
|
@@ -19,37 +37,24 @@
|
|
|
19
37
|
}
|
|
20
38
|
},
|
|
21
39
|
"lint-staged": {
|
|
22
|
-
"src/*.
|
|
23
|
-
"
|
|
40
|
+
"src/*.jsx": [
|
|
41
|
+
"biome format",
|
|
24
42
|
"git add"
|
|
25
43
|
]
|
|
26
44
|
},
|
|
27
|
-
"keywords": [
|
|
28
|
-
"react",
|
|
29
|
-
"lazy-load"
|
|
30
|
-
],
|
|
31
|
-
"author": "Jonathan Obino",
|
|
32
|
-
"license": "ISC",
|
|
33
|
-
"repository": {
|
|
34
|
-
"type": "git",
|
|
35
|
-
"url": "https://github.com/jonathanobino/react-lazy"
|
|
36
|
-
},
|
|
37
45
|
"devDependencies": {
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
40
|
-
"@types
|
|
41
|
-
"@types/
|
|
42
|
-
"@
|
|
43
|
-
"@
|
|
44
|
-
"eslint": "^7.32.0",
|
|
45
|
-
"eslint-config-prettier": "^8.5.0",
|
|
46
|
-
"eslint-plugin-react": "^7.30.1",
|
|
46
|
+
"@biomejs/biome": "2.3.11",
|
|
47
|
+
"@parcel/packager-ts": "^2.16.3",
|
|
48
|
+
"@parcel/transformer-typescript-types": "^2.9.0",
|
|
49
|
+
"@types/node": "^25.0.0",
|
|
50
|
+
"@types/react": "^19.2.0",
|
|
51
|
+
"@types/react-dom": "^19.2.0",
|
|
47
52
|
"husky": "^4.3.0",
|
|
48
53
|
"jest": "^27.5.1",
|
|
49
54
|
"lint-staged": "^10.4.0",
|
|
50
|
-
"parcel": "^2.
|
|
51
|
-
"
|
|
52
|
-
"
|
|
55
|
+
"parcel": "^2.9.0",
|
|
56
|
+
"process": "^0.11.10",
|
|
57
|
+
"typescript": "^5.9.3"
|
|
53
58
|
},
|
|
54
59
|
"peerDependencies": {
|
|
55
60
|
"react": "^18.3.0",
|
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,30 @@
|
|
|
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
|
+
title: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export default function LazyImage(props: LazyImageProps) {
|
|
13
|
+
const [setRef, link] = useIsInViewPort(props)
|
|
6
14
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
return (
|
|
16
|
+
<div
|
|
17
|
+
ref={(node) => {
|
|
18
|
+
setRef(node)
|
|
19
|
+
}}
|
|
20
|
+
>
|
|
21
|
+
<img
|
|
22
|
+
src={link}
|
|
23
|
+
alt={props.alt}
|
|
24
|
+
style={props.style}
|
|
25
|
+
className={props.className}
|
|
26
|
+
title={props.title}
|
|
27
|
+
/>
|
|
28
|
+
</div>
|
|
29
|
+
)
|
|
21
30
|
}
|
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
|
-
}
|