smooth-loader 1.0.6 → 1.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Release Notes
2
2
 
3
+ ## v1.1.0 (2022-11-29)
4
+
5
+ - Added missing information to the README.md file;
6
+ - Added ability to show small images as placeholder, so you don't look at empty screen but blurred mini version of the image;
7
+
8
+ ----
9
+
10
+ ## v1.0.7 (2022-06-22)
11
+
12
+ - Fixed appearing transition with background images. Now it works.
13
+ - Improved documentation `README.md` file.
14
+ - Added **configurations** for smooth loading images.
15
+
16
+ ----
17
+
3
18
  ## v1.0.6 (2022-01-2)
4
19
 
5
20
  - Changed default images selector to be more specific.
package/README.md CHANGED
@@ -1,35 +1,30 @@
1
- # Description
1
+ - Documentation menu
2
+ - [📄 Description](#-description)
3
+ - [🎁 Features](#-features)
4
+ - [✍️ Usage](#%EF%B8%8F-usage)
5
+ - [🌄 Image preview](#-image-preview)
6
+ - [⚙️ Configurations](#%EF%B8%8F-configurations)
7
+ - [🚀️ Quick start](#-quick-start)
8
+ - Useful links
9
+ - [NPM](https://www.npmjs.com/package/smooth-loader)
10
+ - [Usage on codesandbox](https://codesandbox.io/s/smooth-loader-example-usage-5xr6h)
11
+ - [Example usage in production](https://shobar.com.ua/category/photo-reports)
12
+
13
+ ## 📄 Description
2
14
 
3
15
  Smooth loader allows you smoothly lazy load images and background images. Lazy Loading defers the loading of an image that is not needed on the page immediately. An image, not visible to the user when the page loads, is loaded later when the user scrolls and the image actually becomes visible. If the user never scrolls, an image that is not visible to the user never gets loaded.
4
16
 
5
- - [NPM](https://www.npmjs.com/package/smooth-loader)
6
- - [Usage on codesandbox](https://codesandbox.io/s/smooth-loader-example-usage-5xr6h)
7
- - [Example usage in production](https://shobar.com.ua/category/photo-reports)
17
+ This package is a MUST-HAVE for every website containing multiple images on the page. It's surely the best lazy loading package on NPM that you can configure to your own needs.
8
18
 
9
- # Features
19
+ ## 🎁 Features
10
20
 
11
- - Written in TypeScript
12
- - Very small package
13
- - Loads images with IMG tag and backgrounds
14
- - Loads images with a smooth fade in animation
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;
15
26
 
16
- # Installation
17
-
18
- ```bash
19
- # npm
20
- npm i smooth-loader
21
-
22
- # yarn
23
- yarn add smooth-loader
24
- ```
25
-
26
- ```js
27
- import smoothLoader from 'smooth-loader'
28
-
29
- smoothLoader()
30
- ```
31
-
32
- # Usage
27
+ ## ✍️ Usage
33
28
 
34
29
  To make image or element Lazy loaded, replace `src` attribute with `data-src`
35
30
  on image element, and add `data-src` attribute to elements that have background
@@ -60,8 +55,57 @@ smoothLoader(myImages)
60
55
  <div data-src="./images/we.png" class="smooth-loader my-bg-image"></div>
61
56
  ```
62
57
 
63
- # Function types
58
+ ## 🌄 Image preview
59
+
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.
61
+
62
+ 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
+
64
+ ![Big and small images](https://serhii.io/storage/other/big-and-small-images.png)
65
+
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.
67
+
68
+ ```html
69
+ <img
70
+ alt="Beautiful girl"
71
+ class="smooth-loader"
72
+ src="images/1min.jpg"
73
+ data-src="images/1.jpg"
74
+ width="736"
75
+ height="725"
76
+ />
77
+ ```
78
+
79
+ > 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
+
81
+ ## ⚙️ Configurations
82
+
83
+ As the second argument, you can pass configurations. Some of the parameters extend [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
84
+
85
+ ```typescript
86
+ smoothLoader('.lazy-image', {
87
+ root: document.querySelector('#scrollArea'),
88
+ rootMargin: '0px',
89
+ threshold: 1.0,
90
+ })
91
+ ```
92
+
93
+ - `root`: The element that is used as the viewport for checking visibility of the target. Must be the ancestor of the target. Defaults to the browser viewport if not specified or if null.
94
+ - `rootMargin`: Margin around the root. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). The values can be percentages. This set of values serves to grow or shrink each side of the root element's bounding box before computing intersections. Defaults to all zeros.
95
+ - `threshold`: Either a single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. If you only want to detect when visibility passes the 50% mark, you can use a value of 0.5. If you want the callback to run every time visibility passes another 25%, you would specify the array [0, 0.25, 0.5, 0.75, 1]. The default is 0 (meaning as soon as even one pixel is visible, the callback will be run). A value of 1.0 means that the threshold isn't considered passed until every pixel is visible.
96
+
97
+ ## 🚀 Quick Start
98
+
99
+ ```bash
100
+ # npm
101
+ npm i smooth-loader
102
+
103
+ # yarn
104
+ yarn add smooth-loader
105
+ ```
64
106
 
65
107
  ```js
66
- function smoothLoader(selector?: NodeListOf<HTMLDivElement | HTMLImageElement> | string) => void
108
+ import smoothLoader from 'smooth-loader'
109
+
110
+ smoothLoader()
67
111
  ```
@@ -1,19 +1,20 @@
1
- import { LazyImage } from './types';
1
+ import { Config, LazyImage } from './types';
2
2
  /**
3
3
  * Class that handles lazy loading images with
4
4
  * Intersection Observer
5
5
  */
6
6
  export default class {
7
7
  private readonly images;
8
- private observerOptions;
9
- constructor(images: NodeListOf<LazyImage>);
8
+ private readonly config;
9
+ constructor(images: NodeListOf<LazyImage>, config: Config);
10
10
  /**
11
11
  * If image has tag IMG then set the src attribute to img url,
12
12
  * otherwise set the background of the element to given image url
13
13
  */
14
14
  private loadImage;
15
15
  /**
16
- * Create observer object
16
+ * Create observer object that will trigger loading image function
17
+ * when it's gonna be visible on the screen
17
18
  */
18
19
  private createObserver;
19
20
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"LazyLoader.d.ts","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAEnC;;;GAGG;AACH,MAAM,CAAC,OAAO;IACV,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuB;IAC9C,OAAO,CAAC,eAAe,CAGtB;gBAEkB,MAAM,EAAE,UAAU,CAAC,SAAS,CAAC;IAIhD;;;OAGG;IACH,OAAO,CAAC,SAAS;IAkBjB;;OAEG;IACH,OAAO,CAAC,cAAc;IAatB;;;;OAIG;IACI,OAAO,IAAI,IAAI;CAKzB"}
1
+ {"version":3,"file":"LazyLoader.d.ts","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAE3C;;;GAGG;AACH,MAAM,CAAC,OAAO;IAEN,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,MAAM,EAAE,UAAU,CAAC,SAAS,CAAC,EAC7B,MAAM,EAAE,MAAM;IAInC;;;OAGG;IACH,OAAO,CAAC,SAAS;IA4BjB;;;OAGG;IACH,OAAO,CAAC,cAAc;IAatB;;;;OAIG;IACI,OAAO,IAAI,IAAI;CAKzB"}
@@ -5,12 +5,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
5
5
  * Intersection Observer
6
6
  */
7
7
  class default_1 {
8
- constructor(images) {
9
- this.observerOptions = {
10
- root: null,
11
- threshold: 0,
12
- };
8
+ constructor(images, config) {
13
9
  this.images = images;
10
+ this.config = config;
14
11
  }
15
12
  /**
16
13
  * If image has tag IMG then set the src attribute to img url,
@@ -18,21 +15,28 @@ class default_1 {
18
15
  */
19
16
  loadImage(img) {
20
17
  const imageUrl = img.getAttribute('data-src');
21
- img.style.opacity = '0';
22
- img.style.transition = 'opacity 777ms';
23
18
  if (!imageUrl)
24
19
  return;
25
- if (img.tagName === 'IMG') {
26
- img.setAttribute('src', imageUrl);
27
- img.addEventListener('load', () => img.style.opacity = '1');
20
+ if (img.hasAttribute('src')) {
21
+ img.style.filter = 'blur(7px)';
28
22
  }
29
23
  else {
30
- img.style.backgroundImage = `url(${img.getAttribute('data-src')})`;
31
- setTimeout(() => img.style.opacity = '1', 0);
24
+ img.style.opacity = '0';
32
25
  }
26
+ const ghostImage = new Image();
27
+ ghostImage.addEventListener('load', () => {
28
+ img.style.transition = 'opacity 777ms';
29
+ img.style.opacity = '1';
30
+ img.style.filter = 'none';
31
+ });
32
+ ghostImage.src = imageUrl;
33
+ img.tagName === 'IMG'
34
+ ? img.setAttribute('src', imageUrl)
35
+ : img.style.backgroundImage = `url(${imageUrl})`;
33
36
  }
34
37
  /**
35
- * Create observer object
38
+ * Create observer object that will trigger loading image function
39
+ * when it's gonna be visible on the screen
36
40
  */
37
41
  createObserver(img) {
38
42
  const observer = new IntersectionObserver((entries, observer) => {
@@ -42,7 +46,7 @@ class default_1 {
42
46
  observer.unobserve(img);
43
47
  }
44
48
  });
45
- }, this.observerOptions);
49
+ }, this.config);
46
50
  observer.observe(img);
47
51
  }
48
52
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"LazyLoader.js","sourceRoot":"","sources":["../../src/LazyLoader.ts"],"names":[],"mappings":";;AAEA;;;GAGG;AACH;IAOI,YAAmB,MAA6B;QALxC,oBAAe,GAAG;YACtB,IAAI,EAAE,IAAI;YACV,SAAS,EAAE,CAAC;SACf,CAAA;QAGG,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACxB,CAAC;IAED;;;OAGG;IACK,SAAS,CAAC,GAAc;QAC5B,MAAM,QAAQ,GAAG,GAAG,CAAC,YAAY,CAAC,UAAU,CAAC,CAAA;QAE7C,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,GAAG,CAAA;QACvB,GAAG,CAAC,KAAK,CAAC,UAAU,GAAG,eAA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E;gBACpB,IAAI,KAAK,CAAC,cAAc,EAAE;oBACtB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;oBACnB,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;iBAC1B;YACL,CAAC,CAAC,CAAA;QACN,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;QAEf,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IACzB,CAAC;IAED;;;;OAIG;IACI,OAAO;QACV,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACtB,CAAC,MAAM,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAA;QACpF,CAAC,CAAC,CAAA;IACN,CAAC;CACJ;AAlED,4BAkEC"}
package/lib/js/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LazyImage } from './types';
2
- declare const _default: (selector?: string | NodeListOf<LazyImage> | undefined) => void;
1
+ import { Config, LazyImage } from './types';
2
+ declare const _default: (selector?: NodeListOf<LazyImage> | string, 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,SAAS,EAAE,MAAM,SAAS,CAAA;mFAEyB,IAAI;AAAhE,wBAOC"}
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"}
package/lib/js/index.js CHANGED
@@ -4,11 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const LazyLoader_1 = __importDefault(require("./LazyLoader"));
7
- exports.default = (selector) => {
7
+ exports.default = (selector, config) => {
8
8
  const images = getImagesElements(selector);
9
9
  if (!images)
10
10
  return;
11
- return new LazyLoader_1.default(images).execute();
11
+ const configurations = config || {
12
+ root: null,
13
+ threshold: 0,
14
+ };
15
+ return new LazyLoader_1.default(images, configurations).execute();
12
16
  };
13
17
  function getImagesElements(selector) {
14
18
  if (typeof selector === 'string' && selector !== '')
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAAA,8DAAqC;AAGrC,kBAAe,CAAC,QAAyC,EAAQ,EAAE;IAC/D,MAAM,MAAM,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IAE1C,IAAI,CAAC,MAAM;QACP,OAAM;IAEV,OAAO,IAAI,oBAAU,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAA;AAC3C,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,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"}
package/lib/js/types.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export declare type LazyImage = HTMLDivElement | HTMLImageElement;
2
+ export interface Config extends IntersectionObserverInit {
3
+ }
2
4
  //# 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"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smooth-loader",
3
- "version": "1.0.6",
3
+ "version": "1.1.0-beta.0",
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",
@@ -10,7 +10,7 @@
10
10
  "scripts": {
11
11
  "build": "tsc -p .",
12
12
  "watch": "tsc --watch",
13
- "publish": "npm publish"
13
+ "publish": "npm login && npm publish"
14
14
  },
15
15
  "keywords": [
16
16
  "lazy loader",
package/src/LazyLoader.ts CHANGED
@@ -1,18 +1,14 @@
1
- import { LazyImage } from './types'
1
+ import { Config, LazyImage } from './types'
2
2
 
3
3
  /**
4
4
  * Class that handles lazy loading images with
5
5
  * Intersection Observer
6
6
  */
7
7
  export default class {
8
- private readonly images: NodeListOf<LazyImage>
9
- private observerOptions = {
10
- root: null,
11
- threshold: 0,
12
- }
13
-
14
- public constructor(images: NodeListOf<LazyImage>) {
15
- this.images = images
8
+ public constructor(
9
+ private readonly images: NodeListOf<LazyImage>,
10
+ private readonly config: Config
11
+ ) {
16
12
  }
17
13
 
18
14
  /**
@@ -22,23 +18,34 @@ export default class {
22
18
  private loadImage(img: LazyImage): void {
23
19
  const imageUrl = img.getAttribute('data-src')
24
20
 
25
- img.style.opacity = '0'
26
- img.style.transition = 'opacity 777ms'
27
-
28
21
  if (!imageUrl)
29
22
  return
30
23
 
31
- if (img.tagName === 'IMG') {
32
- img.setAttribute('src', imageUrl)
33
- img.addEventListener('load', () => img.style.opacity = '1')
24
+ if (img.hasAttribute('src')) {
25
+ img.style.filter = 'blur(7px)'
34
26
  } else {
35
- img.style.backgroundImage = `url(${img.getAttribute('data-src')})`
36
- setTimeout(() => img.style.opacity = '1', 0)
27
+ img.style.opacity = '0'
37
28
  }
29
+
30
+ const ghostImage = new Image()
31
+
32
+ ghostImage.addEventListener('load', () => {
33
+ img.style.transition = 'opacity 777ms'
34
+ img.style.opacity = '1'
35
+ img.style.filter = 'none'
36
+ })
37
+
38
+ ghostImage.src = imageUrl
39
+
40
+ img.tagName === 'IMG'
41
+ ? img.setAttribute('src', imageUrl)
42
+ : img.style.backgroundImage = `url(${imageUrl})`
38
43
  }
39
44
 
45
+
40
46
  /**
41
- * Create observer object
47
+ * Create observer object that will trigger loading image function
48
+ * when it's gonna be visible on the screen
42
49
  */
43
50
  private createObserver(img: LazyImage): void {
44
51
  const observer = new IntersectionObserver((entries, observer) => {
@@ -48,7 +55,7 @@ export default class {
48
55
  observer.unobserve(img)
49
56
  }
50
57
  })
51
- }, this.observerOptions)
58
+ }, this.config)
52
59
 
53
60
  observer.observe(img)
54
61
  }
package/src/index.ts CHANGED
@@ -1,13 +1,18 @@
1
1
  import LazyLoader from "./LazyLoader"
2
- import { LazyImage } from './types'
2
+ import { Config, LazyImage } from './types'
3
3
 
4
- export default (selector?: NodeListOf<LazyImage> | string): void => {
4
+ export default (selector?: NodeListOf<LazyImage> | string, config?: Config): void => {
5
5
  const images = getImagesElements(selector)
6
6
 
7
7
  if (!images)
8
8
  return
9
9
 
10
- return new LazyLoader(images).execute()
10
+ const configurations = config || {
11
+ root: null,
12
+ threshold: 0,
13
+ }
14
+
15
+ return new LazyLoader(images, configurations).execute()
11
16
  }
12
17
 
13
18
  function getImagesElements(selector?: NodeListOf<LazyImage> | string): NodeListOf<LazyImage> {
package/src/types.ts CHANGED
@@ -1 +1,4 @@
1
- export type LazyImage = HTMLDivElement | HTMLImageElement
1
+ export type LazyImage = HTMLDivElement | HTMLImageElement
2
+
3
+ export interface Config extends IntersectionObserverInit {
4
+ }