lazy-react 3.6.1 → 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/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,i]=u(e),[o,s]=(0,n.useState)({backgroundImage:`url(${l})`,...e.style});return(0,n.useEffect)((()=>{s({backgroundImage:`url(${l})`,...e.style})}),[l,i]),(0,t.jsx)("div",{className:e.className,style:o,ref:e=>{r(e)},children:e.children})}function i(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 o(e){const[n,r]=u(e);return(0,t.jsx)("iframe",{height:e.height||"500",scrolling:e.scrolling||"no",src:r,frameBorder:e.frameBorder||"no",allowFullScreen:e.allowFullScreen||!0,style:e.style||{},ref:e=>{n(e)},...e})}function s(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",(()=>i)),r(module.exports,"LazyFrame",(()=>o)),r(module.exports,"LazyComponent",(()=>s));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:function(e){a.add(e),0===c.isListenerAttached&&(c.isListenerAttached=window.requestAnimationFrame(c.eventHandler))},eventHandler:function(){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:function(){window.cancelAnimationFrame(c.isListenerAttached),c.isListenerAttached=0},removeElementFromList:function(e){a.delete(e)},isListenerAttached:0};function u(e){const[t,r]=(0,n.useState)(""),[l,i]=(0,n.useState)(!1),[o,s]=(0,n.useState)(null),a=(0,n.useMemo)((()=>({element:o,makeItVisible:d,offset:e.offset||100})),[o,e]),u=(0,n.useCallback)((e=>{null!==e&&s(e)}),[]);function d(){e.link&&r((()=>e.link)),i((()=>!0))}return(0,n.useEffect)((()=>(null!==o&&c.addElement(a),()=>c.removeElementFromList(a))),[o]),[u,t,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
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":"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","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":"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","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.1",
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": "eslint src/*",
11
- "lint:fix": "eslint --fix src/*",
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/*.js": [
23
- "prettier --write --single-quote --no-semi",
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
- "@types/jest": "^27.4.1",
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 { LazyImage, LazyBackgroundImage, LazyFrame, LazyComponent } from '../../dist'
2
+ import {
3
+ LazyImage,
4
+ LazyBackgroundImage,
5
+ LazyFrame,
6
+ LazyComponent,
7
+ } from '../../dist'
6
8
 
7
- function Experimental (){
8
- return <h1> I&rsquo;m lazy loaded!</h1>
9
+ function Experimental() {
10
+ return <h1> I&rsquo;m lazy loaded!</h1>
9
11
  }
10
12
 
11
13
  function Demo() {
12
- return <div>
13
- <h2>Just scroll down</h2>
14
- <div style={{
15
- minHeight: '100vh',
16
- minWidth: '100vw',
17
- backgroundColor: 'tomato'
18
- }}></div>
19
- <LazyImage
20
- link={'https://images.unsplash.com/photo-1462834026679-7c03bf571a67?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=6e160dc1e65511df7bf1c461f8a93c82'}
21
- offset={0}
22
- />
23
- <LazyImage
24
- link={'https://images.unsplash.com/5/unsplash-kitsune-4.jpg?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=44828cfc286e8eb8cd0ed2ca561f8cb9'}
25
- offset={100}
26
- className={'styledImage'}
27
- />
28
- <LazyImage
29
- link={'https://images.unsplash.com/photo-1472132858735-6313c7962473?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=f38318ed6adee6db020705bedf6d6e10'}
30
- offset={100}
31
- />
32
- <LazyImage
33
- link={'https://images.unsplash.com/photo-1469899324414-c72bfb4d4161?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=8d708d646e3b2d32c5faf67e01919872'}
34
- offset={100}
35
- className={'test'}
36
- preserveAspect={false}
37
- />
38
- <LazyImage
39
- link={'https://images.unsplash.com/photo-1462834026679-7c03bf571a67?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=6e160dc1e65511df7bf1c461f8a93c82'}
40
- offset={100}
41
- className={'test'}
42
- preserveAspect={false}
43
- />
44
- <LazyBackgroundImage
45
- link={'https://images.unsplash.com/photo-1462834026679-7c03bf571a67?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=6e160dc1e65511df7bf1c461f8a93c82'}
46
- className="fill"
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
- <LazyComponent>
50
- <Experimental />
51
- <Experimental />
52
- <Experimental />
53
- </LazyComponent>
66
+ <LazyComponent>
67
+ <Experimental />
68
+ <Experimental />
69
+ <Experimental />
70
+ </LazyComponent>
54
71
 
55
- <LazyFrame
56
- link={'http://jonathanobino.xyz'}
57
- scrolling={'true'}
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 React, { useState, useEffect } from 'react' // eslint-disable-line no-unused-vars
1
+ import { useState, useEffect, type ReactNode } from 'react'
2
2
  import useIsInViewPort from './index'
3
3
 
4
- export default function LazyBackgroundImage(props: any) {
5
- const [setRef, link, isViewable] = useIsInViewPort(props)
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
- const [style, setStyle] = useState({
8
- backgroundImage: `url(${link})`,
9
- ...props.style,
10
- })
15
+ const [style, setStyle] = useState({
16
+ backgroundImage: `url(${link})`,
17
+ ...props.style,
18
+ })
11
19
 
12
- useEffect(() => {
13
- setStyle({
14
- backgroundImage: `url(${link})`,
15
- ...props.style,
16
- })
17
- }, [link, isViewable])
20
+ useEffect(() => {
21
+ isViewable
22
+ ? setStyle({
23
+ backgroundImage: `url(${link})`,
24
+ ...props.style,
25
+ })
26
+ : null
27
+ }, [link, isViewable, props.style])
18
28
 
19
- return (
20
- <div
21
- className={props.className}
22
- style={style}
23
- ref={(node) => {
24
- setRef(node)
25
- }}
26
- >
27
- {props.children}
28
- </div>
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 React from 'react'
1
+ import type { ReactNode } from 'react'
2
2
  import useIsInViewport from './index'
3
3
 
4
- export default function LazyComponent(props: any) {
5
- const [setRef, link, isVisible] = useIsInViewport(props)
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
- if (!isVisible)
8
- return (
9
- <div
10
- ref={(node) => {
11
- setRef(node)
12
- }}
13
- style={{
14
- height: '300px',
15
- width: '300px',
16
- ...props.style,
17
- }}
18
- ></div>
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
- return <div>{props.children}</div>
28
+ return <div>{props.children}</div>
22
29
  }
@@ -1,21 +1,27 @@
1
- import React from 'react' // eslint-disable-line no-unused-vars
2
1
  import useIsInViewPort from './index'
3
2
 
4
- export default function LazyFrame(props: any) {
5
- const [setRef, link] = useIsInViewPort(props)
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
- return (
8
- <iframe
9
- height={props.height || '500'}
10
- scrolling={props.scrolling || 'no'}
11
- src={link}
12
- frameBorder={props.frameBorder || 'no'}
13
- allowFullScreen={props.allowFullScreen || true}
14
- style={props.style || {}}
15
- ref={(node) => {
16
- setRef(node)
17
- }}
18
- {...props}
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
  }
@@ -1,21 +1,28 @@
1
- import React from 'react' // eslint-disable-line no-unused-vars
2
1
  import useIsInViewPort from './index'
3
2
 
4
- export default function LazyImage(props: any) {
5
- const [setRef, link] = useIsInViewPort(props)
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
- return (
8
- <div
9
- ref={(node) => {
10
- setRef(node)
11
- }}
12
- >
13
- <img
14
- src={link}
15
- alt={props.alt}
16
- style={props.style}
17
- className={props.className}
18
- />
19
- </div>
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
  }
@@ -1,116 +1,117 @@
1
1
  import { useState, useEffect, useMemo, useCallback } from 'react'
2
2
 
3
3
  interface InstanceElement {
4
- makeItVisible: () => void
5
- element: HTMLElement | null
6
- offset: number
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
- // 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,
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
- calculateNewPosition: (elem: InstanceElement) => {
27
- const { top, left } = elem.element?.getBoundingClientRect() || {
28
- top: 0,
29
- left: 0,
30
- }
26
+ calculateNewPosition: (elem: InstanceElement) => {
27
+ const { top, left } = elem.element?.getBoundingClientRect() || {
28
+ top: 0,
29
+ left: 0,
30
+ }
31
31
 
32
- return {
33
- ...elem,
34
- top,
35
- left,
36
- }
37
- },
38
- addElement: function (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: function () {
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
- },
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
- removeScrollHandler: function () {
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: function (toRemove: InstanceElement) {
75
- elements.delete(toRemove)
76
- },
77
- isListenerAttached: 0, // intended as not set
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
- link: string
82
- offset: number
83
- }): [Function, string, boolean] {
84
- const [link, setLink] = useState('')
85
- const [visible, setVisible] = useState(false)
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
- const [ref, setRef] = useState(null)
87
+ const [ref, setRef] = useState<HTMLDivElement | null>(null)
88
88
 
89
- const thisInstance = useMemo(() => {
90
- return {
91
- element: ref,
92
- makeItVisible,
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
- const getRef = useCallback((node) => {
98
- if (node !== null) setRef(node)
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
- function makeItVisible() {
102
- if (props.link) setLink(() => props.link)
103
- setVisible(() => true)
104
- }
102
+ const getRef = useCallback((node: HTMLDivElement | null) => {
103
+ if (node !== null) setRef(node)
104
+ }, [])
105
105
 
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])
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
- return [getRef, link, visible]
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
- }