smooth-loader 1.1.0 → 1.1.1

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/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  ----
4
4
 
5
+ ## v1.1.1 (2022-12-04)
6
+
7
+ - Code refactoring;
8
+ - Added ability to provide array of images instead of only NodeListOf;
9
+ - Improved documentation (README.md);
10
+
11
+ ----
12
+
5
13
  ## v1.1.0 (2022-12-01)
6
14
 
7
15
  - Moved from beta version;
package/README.md CHANGED
@@ -18,52 +18,75 @@ This package is a MUST-HAVE for every website containing multiple images on the
18
18
 
19
19
  ## 🎁 Features
20
20
 
21
- - ✅ Written in TypeScript;
22
- - ✅ Very small package;
23
- - ✅ Loads images with IMG tag and backgrounds;
24
- - ✅ Loads images with a smooth fade in animation;
25
- - ✅ Configurable;
21
+ - 😍 Well-documented;
22
+ - 😍 Written in TypeScript;
23
+ - 😍 Very small package;
24
+ - 😍 Loads images with IMG tag and backgrounds;
25
+ - 😍 Loads images with a smooth fade in animation;
26
+ - 😍 For image elements, you can show minified version of the image on initial page load;
27
+ - 😍 Configurable;
28
+ - 😍 Smooth loader for Vue 3 is coming soon;
26
29
 
27
30
  ## ✍️ Usage
28
31
 
29
- To make image or element Lazy loaded, replace `src` attribute with `data-src`
30
- on image element, and add `data-src` attribute to elements that have background
31
- image that you want to lazy load.
32
+ To make image element or background image lazy loaded:
33
+ - Add image URL to `data-src` attribute;
34
+ - Call `smoothLoader()` function;
32
35
 
33
- - Add **smooth-loader** css class to each image or element that you want or specify your own selector for images instead of **smooth-loader**.
34
- - Call `smoothLoader()` function when you want to lazy load images.
36
+ #### Look at several options to choose images which you want to lazy load:
35
37
 
36
38
  ```js
37
- // with .smooth-loader selector
39
+ import smoothLoader from 'smooth-loader'
40
+
41
+ // Default selector
42
+ // Selects all images with 'smooth-loader' class
38
43
  smoothLoader()
39
44
 
40
- // with custom string selector
45
+ // Custom selector
46
+ // Selects all images with 'lazy-image' class
41
47
  smoothLoader('.lazy-image')
42
48
 
43
- // with custom elements
44
- const myImages = document.querySelectorAll<HTMLImageElement>('img')
45
- smoothLoader(myImages)
49
+ // Elements
50
+ // Selects all the images
51
+ const images = document.querySelectorAll<HTMLImageElement>('img')
52
+ smoothLoader(images)
53
+
54
+ // Certain elements
55
+ // Selects all the images
56
+ const myPhoto = document.querySelector<HTMLImageElement>('#me')
57
+
58
+ if (myPhoto) {
59
+ smoothLoader([myPhoto])
60
+ }
46
61
  ```
47
62
 
48
63
  > Note, that lazy loading works only when your elements are already in the DOM. Meaning, if images are loaded after the `smoothLoader` was executed, lazy loading will not work.
49
64
 
50
65
  ```html
51
66
  <!-- Regular image -->
52
- <img data-src="./images/me.png" class="smooth-loader">
67
+ <img
68
+ src="./images/me-min.webp"
69
+ data-src="./images/me.webp"
70
+ class="smooth-loader"
71
+ alt="Photo of Serhii Cho"
72
+ />
53
73
 
54
74
  <!-- Background image -->
55
- <div data-src="./images/we.png" class="smooth-loader my-bg-image"></div>
75
+ <div
76
+ data-src="./images/we.png"
77
+ class="smooth-loader my-bg-image"
78
+ ></div>
56
79
  ```
57
80
 
58
81
  ## 🌄 Image preview
59
82
 
60
- You can add a small version of the image to `src` attribute on image element, so that users don't look at the empty place when image is loading. It can be useful if you have many images on the page or you have several large images which.
83
+ You can add a small version of the image to `src` attribute on image element `<img src="here">`, so that users don't look at the empty place when image is loading. It can be useful if you have many images on the page or you have several large images which.
61
84
 
62
85
  To use this feature, you'll need small versions of your images which will be loaded right away when the page loads. Let's say I have 2 same images with different sizes:
63
86
 
64
87
  ![Big and small images](https://serhii.io/storage/other/big-and-small-images.png)
65
88
 
66
- I want to load a small image on page load, and when person scrolls into the image view, the bigger version will be loaded and it will replace the small one. In my HTML I will simply add an `src` attribute with path to a small image.
89
+ I want to load a small image on the initial page load, and when person scrolls into the image view, the bigger version will be loaded and it will replace the small one. In my HTML I will simply add an `src` attribute with path to a small image.
67
90
 
68
91
  ```html
69
92
  <img
@@ -78,6 +101,8 @@ I want to load a small image on page load, and when person scrolls into the imag
78
101
 
79
102
  > It's important to add `width` and `height` attributes or have width and hight setup correctly with CSS. Because small image has to be filling all the space that large image is taking.
80
103
 
104
+ > This feature works only for image elements, background images do not support it yet!
105
+
81
106
  Instead of the small version of your image you can use just a small placeholder image, and it will work fine.
82
107
 
83
108
  ## ⚙️ Configurations
@@ -0,0 +1,8 @@
1
+ import { Config, LazyImage } from './types';
2
+ /**
3
+ * Creates image object, gets attributes from placeholder,
4
+ * sets them on image object, adds classes to image and
5
+ * when image is loaded, appends it to a placeholder
6
+ */
7
+ export default function (images: LazyImage[], config: Config): void;
8
+ //# sourceMappingURL=handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAG3C;;;;GAIG;AACH,MAAM,CAAC,OAAO,WAAW,MAAM,EAAE,SAAS,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAElE"}
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const imageLoader_1 = require("./imageLoader");
4
+ /**
5
+ * Creates image object, gets attributes from placeholder,
6
+ * sets them on image object, adds classes to image and
7
+ * when image is loaded, appends it to a placeholder
8
+ */
9
+ function default_1(images, config) {
10
+ images.forEach(img => window['IntersectionObserver'] ? createObserver(img, config) : (0, imageLoader_1.loadImage)(img));
11
+ }
12
+ exports.default = default_1;
13
+ /**
14
+ * Creates instance of IntersectionObserver and loads image in DOM
15
+ * as soon as image will be visible on the screen
16
+ */
17
+ function createObserver(lazyImage, config) {
18
+ const handleObserver = (entries, observer) => {
19
+ for (const entry of entries) {
20
+ if (!entry.isIntersecting) {
21
+ continue;
22
+ }
23
+ (0, imageLoader_1.loadImage)(lazyImage);
24
+ observer.unobserve(lazyImage);
25
+ }
26
+ };
27
+ const observer = new IntersectionObserver(handleObserver, config);
28
+ observer.observe(lazyImage);
29
+ }
30
+ //# sourceMappingURL=handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handler.js","sourceRoot":"","sources":["../../src/handler.ts"],"names":[],"mappings":";;AACA,+CAAyC;AAEzC;;;;GAIG;AACH,mBAAyB,MAAmB,EAAE,MAAc;IACxD,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,IAAA,uBAAS,EAAC,GAAG,CAAC,CAAC,CAAA;AACxG,CAAC;AAFD,4BAEC;AAED;;;GAGG;AACH,SAAS,cAAc,CAAC,SAAoB,EAAE,MAAc;IACxD,MAAM,cAAc,GAAG,CAAC,OAAoC,EAAE,QAA8B,EAAE,EAAE;QAC5F,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE;YACzB,IAAI,CAAC,KAAK,CAAC,cAAc,EAAE;gBACvB,SAAQ;aACX;YAED,IAAA,uBAAS,EAAC,SAAS,CAAC,CAAA;YACpB,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,CAAA;SAChC;IACL,CAAC,CAAA;IAED,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAC,cAAc,EAAE,MAAM,CAAC,CAAA;IAEjE,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;AAC/B,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { LazyImage } from './types';
2
+ /**
3
+ * If image has tag IMG then set the src attribute to img url,
4
+ * otherwise set the background of the element to given image url
5
+ */
6
+ export declare function loadImage(lazyImage: LazyImage): void;
7
+ //# sourceMappingURL=imageLoader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"imageLoader.d.ts","sourceRoot":"","sources":["../../src/imageLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC;;;GAGG;AACH,wBAAgB,SAAS,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI,CA0BpD"}
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadImage = void 0;
4
+ /**
5
+ * If image has tag IMG then set the src attribute to img url,
6
+ * otherwise set the background of the element to given image url
7
+ */
8
+ function loadImage(lazyImage) {
9
+ const imageUrl = getImageUrl(lazyImage);
10
+ if (!imageUrl) {
11
+ return;
12
+ }
13
+ if (!lazyImage.hasAttribute('src')) {
14
+ lazyImage.style.opacity = '0';
15
+ }
16
+ const ghostImage = new Image();
17
+ ghostImage.addEventListener('load', () => {
18
+ lazyImage.style.transition = 'opacity 777ms';
19
+ lazyImage.style.opacity = '1';
20
+ });
21
+ ghostImage.src = imageUrl;
22
+ if (isImageElement(lazyImage)) {
23
+ lazyImage.setAttribute('src', imageUrl);
24
+ return;
25
+ }
26
+ lazyImage.style.backgroundImage = `url(${imageUrl})`;
27
+ }
28
+ exports.loadImage = loadImage;
29
+ const isImageElement = (img) => img.tagName === 'IMG';
30
+ const getImageUrl = (img) => img.getAttribute('data-src');
31
+ //# sourceMappingURL=imageLoader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"imageLoader.js","sourceRoot":"","sources":["../../src/imageLoader.ts"],"names":[],"mappings":";;;AAEA;;;GAGG;AACH,SAAgB,SAAS,CAAC,SAAoB;IAC1C,MAAM,QAAQ,GAAG,WAAW,CAAC,SAAS,CAAC,CAAA;IAEvC,IAAI,CAAC,QAAQ,EAAE;QACX,OAAM;KACT;IAED,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;QAChC,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;KAChC;IAED,MAAM,UAAU,GAAG,IAAI,KAAK,EAAE,CAAA;IAE9B,UAAU,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;QACrC,SAAS,CAAC,KAAK,CAAC,UAAU,GAAG,eAAe,CAAA;QAC5C,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;IACjC,CAAC,CAAC,CAAA;IAEF,UAAU,CAAC,GAAG,GAAG,QAAQ,CAAA;IAEzB,IAAI,cAAc,CAAC,SAAS,CAAC,EAAE;QAC3B,SAAS,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QACvC,OAAM;KACT;IAED,SAAS,CAAC,KAAK,CAAC,eAAe,GAAG,OAAO,QAAQ,GAAG,CAAA;AACxD,CAAC;AA1BD,8BA0BC;AAED,MAAM,cAAc,GAAG,CAAC,GAAc,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,KAAK,KAAK,CAAA;AAChE,MAAM,WAAW,GAAG,CAAC,GAAc,EAAE,EAAE,CAAC,GAAG,CAAC,YAAY,CAAC,UAAU,CAAC,CAAA"}
package/lib/js/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Config, LazyImage } from './types';
2
- declare const _default: (selector?: NodeListOf<LazyImage> | string, config?: Config) => void;
1
+ import { Config, InputTypes } from './types';
2
+ declare const _default: (selector?: InputTypes, config?: Config) => void;
3
3
  export default _default;
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;oCAEhB,WAAW,SAAS,CAAC,GAAG,MAAM,WAAW,MAAM,KAAG,IAAI;AAAjF,wBAYC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAa,UAAU,EAAE,MAAM,SAAS,CAAA;oCAE5B,UAAU,WAAW,MAAM,KAAG,IAAI;AAA7D,wBAaC"}
package/lib/js/index.js CHANGED
@@ -3,22 +3,30 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const LazyLoader_1 = __importDefault(require("./LazyLoader"));
6
+ const handler_1 = __importDefault(require("./handler"));
7
7
  exports.default = (selector, config) => {
8
8
  const images = getImagesElements(selector);
9
- if (!images)
9
+ if (!images) {
10
10
  return;
11
+ }
11
12
  const configurations = config || {
12
13
  root: null,
13
14
  threshold: 0,
14
15
  };
15
- return new LazyLoader_1.default(images, configurations).execute();
16
+ return (0, handler_1.default)(images, configurations);
16
17
  };
17
18
  function getImagesElements(selector) {
18
- if (typeof selector === 'string' && selector !== '')
19
- return document.querySelectorAll(selector);
20
- if (selector instanceof NodeList)
21
- return selector;
22
- return document.querySelectorAll('.smooth-loader[data-src]');
19
+ if (!selector) {
20
+ const elements = document.querySelectorAll('.smooth-loader[data-src]');
21
+ return Array.from(elements);
22
+ }
23
+ if (typeof selector === 'string') {
24
+ const elements = document.querySelectorAll(selector);
25
+ return Array.from(elements);
26
+ }
27
+ if (selector instanceof NodeList) {
28
+ return Array.from(selector);
29
+ }
30
+ return selector;
23
31
  }
24
32
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAAA,8DAAqC;AAGrC,kBAAe,CAAC,QAAyC,EAAE,MAAe,EAAQ,EAAE;IAChF,MAAM,MAAM,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IAE1C,IAAI,CAAC,MAAM;QACP,OAAM;IAEV,MAAM,cAAc,GAAG,MAAM,IAAI;QAC7B,IAAI,EAAE,IAAI;QACV,SAAS,EAAE,CAAC;KACf,CAAA;IAED,OAAO,IAAI,oBAAU,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC,OAAO,EAAE,CAAA;AAC3D,CAAC,CAAA;AAED,SAAS,iBAAiB,CAAC,QAAyC;IAChE,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,EAAE;QAC/C,OAAO,QAAQ,CAAC,gBAAgB,CAAY,QAAQ,CAAC,CAAA;IAEzD,IAAI,QAAQ,YAAY,QAAQ;QAC5B,OAAO,QAAQ,CAAA;IAEnB,OAAO,QAAQ,CAAC,gBAAgB,CAAY,0BAA0B,CAAC,CAAA;AAC3E,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAAA,wDAA+B;AAG/B,kBAAe,CAAC,QAAqB,EAAE,MAAe,EAAQ,EAAE;IAC5D,MAAM,MAAM,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IAE1C,IAAI,CAAC,MAAM,EAAE;QACT,OAAM;KACT;IAED,MAAM,cAAc,GAAG,MAAM,IAAI;QAC7B,IAAI,EAAE,IAAI;QACV,SAAS,EAAE,CAAC;KACf,CAAA;IAED,OAAO,IAAA,iBAAO,EAAC,MAAM,EAAE,cAAc,CAAC,CAAA;AAC1C,CAAC,CAAA;AAED,SAAS,iBAAiB,CAAC,QAAqB;IAC5C,IAAI,CAAC,QAAQ,EAAE;QACX,MAAM,QAAQ,GAAG,QAAQ,CAAC,gBAAgB,CAAY,0BAA0B,CAAC,CAAA;QACjF,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;KAC9B;IAED,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;QAC9B,MAAM,QAAQ,GAAG,QAAQ,CAAC,gBAAgB,CAAY,QAAQ,CAAC,CAAA;QAC/D,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;KAC9B;IAED,IAAI,QAAQ,YAAY,QAAQ,EAAE;QAC9B,OAAO,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;KAC9B;IAED,OAAO,QAAQ,CAAA;AACnB,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { Config, LazyImage } from './types';
2
+ /**
3
+ * Creates image object, gets attributes from placeholder,
4
+ * sets them on image object, adds classes to image and
5
+ * when image is loaded, appends it to a placeholder
6
+ */
7
+ export default function (images: NodeListOf<LazyImage>, config: Config): void;
8
+ //# sourceMappingURL=loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../../src/loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAE3C;;;;GAIG;AACH,MAAM,CAAC,OAAO,WAAW,MAAM,EAAE,UAAU,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAI5E"}
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Creates image object, gets attributes from placeholder,
5
+ * sets them on image object, adds classes to image and
6
+ * when image is loaded, appends it to a placeholder
7
+ */
8
+ function default_1(images, config) {
9
+ for (const img of images) {
10
+ window['IntersectionObserver'] ? createObserver(img, config) : loadImage(img);
11
+ }
12
+ }
13
+ exports.default = default_1;
14
+ /**
15
+ * If image has tag IMG then set the src attribute to img url,
16
+ * otherwise set the background of the element to given image url
17
+ */
18
+ function loadImage(lazyImage) {
19
+ const imageUrl = lazyImage.getAttribute('data-src');
20
+ if (!imageUrl) {
21
+ return;
22
+ }
23
+ if (!lazyImage.hasAttribute('src')) {
24
+ lazyImage.style.opacity = '0';
25
+ }
26
+ const ghostImage = new Image();
27
+ ghostImage.addEventListener('load', () => {
28
+ lazyImage.style.transition = 'opacity 777ms';
29
+ lazyImage.style.opacity = '1';
30
+ });
31
+ ghostImage.src = imageUrl;
32
+ if (lazyImage.tagName === 'IMG') {
33
+ lazyImage.setAttribute('src', imageUrl);
34
+ return;
35
+ }
36
+ lazyImage.style.backgroundImage = `url(${imageUrl})`;
37
+ }
38
+ /**
39
+ * Creates instance of IntersectionObserver and loads image in DOM
40
+ * as soon as image will be visible on the screen
41
+ */
42
+ function createObserver(lazyImage, config) {
43
+ const handleObserver = (entries, observer) => {
44
+ for (const entry of entries) {
45
+ if (!entry.isIntersecting) {
46
+ continue;
47
+ }
48
+ loadImage(lazyImage);
49
+ observer.unobserve(lazyImage);
50
+ }
51
+ };
52
+ const observer = new IntersectionObserver(handleObserver, config);
53
+ observer.observe(lazyImage);
54
+ }
55
+ //# sourceMappingURL=loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loader.js","sourceRoot":"","sources":["../../src/loader.ts"],"names":[],"mappings":";;AAEA;;;;GAIG;AACH,mBAAyB,MAA6B,EAAE,MAAc;IAClE,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE;QACtB,MAAM,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;KAChF;AACL,CAAC;AAJD,4BAIC;AAED;;;GAGG;AACH,SAAS,SAAS,CAAC,SAAoB;IACnC,MAAM,QAAQ,GAAG,SAAS,CAAC,YAAY,CAAC,UAAU,CAAC,CAAA;IAEnD,IAAI,CAAC,QAAQ,EAAE;QACX,OAAM;KACT;IAED,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,KAAK,CAAC,EAAE;QAChC,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;KAChC;IAED,MAAM,UAAU,GAAG,IAAI,KAAK,EAAE,CAAA;IAE9B,UAAU,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;QACrC,SAAS,CAAC,KAAK,CAAC,UAAU,GAAG,eAAe,CAAA;QAC5C,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;IACjC,CAAC,CAAC,CAAA;IAEF,UAAU,CAAC,GAAG,GAAG,QAAQ,CAAA;IAEzB,IAAI,SAAS,CAAC,OAAO,KAAK,KAAK,EAAE;QAC7B,SAAS,CAAC,YAAY,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QACvC,OAAM;KACT;IAED,SAAS,CAAC,KAAK,CAAC,eAAe,GAAG,OAAO,QAAQ,GAAG,CAAA;AACxD,CAAC;AAGD;;;GAGG;AACH,SAAS,cAAc,CAAC,SAAoB,EAAE,MAAc;IACxD,MAAM,cAAc,GAAG,CAAC,OAAoC,EAAE,QAA8B,EAAE,EAAE;QAC5F,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE;YACzB,IAAI,CAAC,KAAK,CAAC,cAAc,EAAE;gBACvB,SAAQ;aACX;YAED,SAAS,CAAC,SAAS,CAAC,CAAA;YACpB,QAAQ,CAAC,SAAS,CAAC,SAAS,CAAC,CAAA;SAChC;IACL,CAAC,CAAA;IAED,MAAM,QAAQ,GAAG,IAAI,oBAAoB,CAAC,cAAc,EAAE,MAAM,CAAC,CAAA;IAEjE,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;AAC/B,CAAC"}
package/lib/js/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export declare type LazyImage = HTMLDivElement | HTMLImageElement;
2
+ export declare type InputTypes = NodeListOf<LazyImage> | LazyImage[] | string;
2
3
  export interface Config extends IntersectionObserverInit {
3
4
  }
4
5
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS,GAAG,cAAc,GAAG,gBAAgB,CAAA;AAEzD,MAAM,WAAW,MAAO,SAAQ,wBAAwB;CACvD"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS,GAAG,cAAc,GAAG,gBAAgB,CAAA;AACzD,oBAAY,UAAU,GAAG,UAAU,CAAC,SAAS,CAAC,GAAG,SAAS,EAAE,GAAG,MAAM,CAAA;AAErE,MAAM,WAAW,MAAO,SAAQ,wBAAwB;CAAI"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smooth-loader",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "This package allows you to lazy load your images using IntersectionObserver technology. It supports loading background images as well as the regular images with img tag.",
5
5
  "main": "lib/js/index.js",
6
6
  "types": "lib/js",
package/src/handler.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { Config, LazyImage } from './types'
2
+ import { loadImage } from './imageLoader'
3
+
4
+ /**
5
+ * Creates image object, gets attributes from placeholder,
6
+ * sets them on image object, adds classes to image and
7
+ * when image is loaded, appends it to a placeholder
8
+ */
9
+ export default function (images: LazyImage[], config: Config): void {
10
+ images.forEach(img => window['IntersectionObserver'] ? createObserver(img, config) : loadImage(img))
11
+ }
12
+
13
+ /**
14
+ * Creates instance of IntersectionObserver and loads image in DOM
15
+ * as soon as image will be visible on the screen
16
+ */
17
+ function createObserver(lazyImage: LazyImage, config: Config): void {
18
+ const handleObserver = (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
19
+ for (const entry of entries) {
20
+ if (!entry.isIntersecting) {
21
+ continue
22
+ }
23
+
24
+ loadImage(lazyImage)
25
+ observer.unobserve(lazyImage)
26
+ }
27
+ }
28
+
29
+ const observer = new IntersectionObserver(handleObserver, config)
30
+
31
+ observer.observe(lazyImage)
32
+ }
@@ -0,0 +1,36 @@
1
+ import { LazyImage } from './types'
2
+
3
+ /**
4
+ * If image has tag IMG then set the src attribute to img url,
5
+ * otherwise set the background of the element to given image url
6
+ */
7
+ export function loadImage(lazyImage: LazyImage): void {
8
+ const imageUrl = getImageUrl(lazyImage)
9
+
10
+ if (!imageUrl) {
11
+ return
12
+ }
13
+
14
+ if (!lazyImage.hasAttribute('src')) {
15
+ lazyImage.style.opacity = '0'
16
+ }
17
+
18
+ const ghostImage = new Image()
19
+
20
+ ghostImage.addEventListener('load', () => {
21
+ lazyImage.style.transition = 'opacity 777ms'
22
+ lazyImage.style.opacity = '1'
23
+ })
24
+
25
+ ghostImage.src = imageUrl
26
+
27
+ if (isImageElement(lazyImage)) {
28
+ lazyImage.setAttribute('src', imageUrl)
29
+ return
30
+ }
31
+
32
+ lazyImage.style.backgroundImage = `url(${imageUrl})`
33
+ }
34
+
35
+ const isImageElement = (img: LazyImage) => img.tagName === 'IMG'
36
+ const getImageUrl = (img: LazyImage) => img.getAttribute('data-src')
package/src/index.ts CHANGED
@@ -1,26 +1,35 @@
1
- import LazyLoader from "./LazyLoader"
2
- import { Config, LazyImage } from './types'
1
+ import handler from './handler'
2
+ import { Config, LazyImage, InputTypes } from './types'
3
3
 
4
- export default (selector?: NodeListOf<LazyImage> | string, config?: Config): void => {
4
+ export default (selector?: InputTypes, config?: Config): void => {
5
5
  const images = getImagesElements(selector)
6
6
 
7
- if (!images)
7
+ if (!images) {
8
8
  return
9
+ }
9
10
 
10
11
  const configurations = config || {
11
12
  root: null,
12
13
  threshold: 0,
13
14
  }
14
15
 
15
- return new LazyLoader(images, configurations).execute()
16
+ return handler(images, configurations)
16
17
  }
17
18
 
18
- function getImagesElements(selector?: NodeListOf<LazyImage> | string): NodeListOf<LazyImage> {
19
- if (typeof selector === 'string' && selector !== '')
20
- return document.querySelectorAll<LazyImage>(selector)
19
+ function getImagesElements(selector?: InputTypes): LazyImage[] {
20
+ if (!selector) {
21
+ const elements = document.querySelectorAll<LazyImage>('.smooth-loader[data-src]')
22
+ return Array.from(elements)
23
+ }
24
+
25
+ if (typeof selector === 'string') {
26
+ const elements = document.querySelectorAll<LazyImage>(selector)
27
+ return Array.from(elements)
28
+ }
21
29
 
22
- if (selector instanceof NodeList)
23
- return selector
30
+ if (selector instanceof NodeList) {
31
+ return Array.from(selector)
32
+ }
24
33
 
25
- return document.querySelectorAll<LazyImage>('.smooth-loader[data-src]')
26
- }
34
+ return selector
35
+ }
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export type LazyImage = HTMLDivElement | HTMLImageElement
2
+ export type InputTypes = NodeListOf<LazyImage> | LazyImage[] | string
2
3
 
3
- export interface Config extends IntersectionObserverInit {
4
- }
4
+ export interface Config extends IntersectionObserverInit { }
package/src/LazyLoader.ts DELETED
@@ -1,80 +0,0 @@
1
- import { Config, LazyImage } from './types'
2
-
3
- /**
4
- * Class that handles lazy loading images with
5
- * Intersection Observer
6
- */
7
- export default class {
8
- public constructor(
9
- private readonly images: NodeListOf<LazyImage>,
10
- private readonly config: Config
11
- ) {
12
- }
13
-
14
- /**
15
- * If image has tag IMG then set the src attribute to img url,
16
- * otherwise set the background of the element to given image url
17
- */
18
- private loadImage(lazyImage: LazyImage): void {
19
- const imageUrl = lazyImage.getAttribute('data-src')
20
-
21
- if (!imageUrl) {
22
- return
23
- }
24
-
25
- if (!lazyImage.hasAttribute('src')) {
26
- lazyImage.style.opacity = '0'
27
- }
28
-
29
- const ghostImage = new Image()
30
-
31
- ghostImage.addEventListener('load', () => {
32
- lazyImage.style.transition = 'opacity 777ms'
33
- lazyImage.style.opacity = '1'
34
- })
35
-
36
- ghostImage.src = imageUrl
37
-
38
- if (lazyImage.tagName === 'IMG') {
39
- lazyImage.setAttribute('src', imageUrl)
40
- return
41
- }
42
-
43
- lazyImage.style.backgroundImage = `url(${imageUrl})`
44
- }
45
-
46
-
47
- /**
48
- * Creates instance of IntersectionObserver and loads image in DOM
49
- * as soon as image will be visible on the screen
50
- */
51
- private createObserver(lazyImage: LazyImage): void {
52
- const handleObserver = (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
53
- for (const entry of entries) {
54
- if (!entry.isIntersecting) {
55
- continue
56
- }
57
-
58
- this.loadImage(lazyImage)
59
- observer.unobserve(lazyImage)
60
- }
61
- }
62
-
63
- const observer = new IntersectionObserver(handleObserver, this.config)
64
-
65
- observer.observe(lazyImage)
66
- }
67
-
68
- /**
69
- * Creates image object, gets attributes from placeholder,
70
- * sets them on image object, adds classes to image and
71
- * when image is loaded, appends it to a placeholder
72
- */
73
- public execute(): void {
74
- for (const img of this.images) {
75
- window['IntersectionObserver']
76
- ? this.createObserver(img)
77
- : this.loadImage(img)
78
- }
79
- }
80
- }