masonry-simple 2.2.0 → 3.0.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/README.md CHANGED
@@ -7,8 +7,8 @@
7
7
  [![GitHub package version](https://img.shields.io/github/package-json/v/ux-ui-pro/masonry-simple.svg)](https://github.com/ux-ui-pro/masonry-simple)
8
8
  [![NPM Downloads](https://img.shields.io/npm/dm/masonry-simple.svg?style=flat)](https://www.npmjs.org/package/masonry-simple)
9
9
 
10
- <sup>500B gzipped</sup>
11
- <h3><a href="https://l6nln6.csb.app/">Demo</a></h3>
10
+ <sup>600B gzipped</sup>
11
+ <p align="center"><a href="https://l6nln6.csb.app/">codesandbox</a> / <a href="https://codepen.io/ux-ui/pen/poxGEqX">codepen</a></p>
12
12
 
13
13
  </div>
14
14
  <br>
@@ -21,15 +21,17 @@ $ yarn add masonry-simple
21
21
 
22
22
  ### Import
23
23
  ```javascript
24
- import MasonrySimple from 'masonry-simple'
24
+ import MasonrySimple from 'masonry-simple';
25
25
  ```
26
26
  <br>
27
27
 
28
28
  ### Usage
29
29
  ```javascript
30
- const masonry = MasonrySimple.init({
31
- container: '.masonry'
32
- })
30
+ const masonry = new MasonrySimple({
31
+ container: '.masonry',
32
+ });
33
+
34
+ masonry.init();
33
35
  ```
34
36
  ```HTML
35
37
  <div class="masonry">
@@ -54,9 +56,9 @@ const masonry = MasonrySimple.init({
54
56
 
55
57
  ### Destroy
56
58
  ```javascript
57
- masonry.destroy()
59
+ masonry.destroy();
58
60
  ```
59
61
  <br>
60
62
 
61
63
  ### License
62
- <sup>masonry-simple is released under MIT license</sup>
64
+ <sup>masonry-simple is released under MIT license</sup>
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
1
+
1
2
  function $parcel$defineInteropFlag(a) {
2
3
  Object.defineProperty(a, '__esModule', {value: true, configurable: true});
3
4
  }
5
+
4
6
  function $parcel$export(e, n, v, s) {
5
7
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
6
8
  }
@@ -9,40 +11,43 @@ $parcel$defineInteropFlag(module.exports);
9
11
 
10
12
  $parcel$export(module.exports, "default", () => $4fa36e821943b400$export$2e2bcd8739ae039);
11
13
  class $4fa36e821943b400$var$MasonrySimple {
12
- constructor(){
13
- this.container = null;
14
- this.gridItems = [];
15
- this.rowHeight = 1;
16
- this.rowGap = 0;
17
- this.resizeObserver = new ResizeObserver(this.resizeAllItems.bind(this));
14
+ container = null;
15
+ gridItems = [];
16
+ rowHeight = 1;
17
+ rowGap = 0;
18
+ resizeTimeout = null;
19
+ resizeObserver = new ResizeObserver(this.handleResize.bind(this));
20
+ constructor(options = {}){
21
+ this.container = options.container instanceof HTMLElement ? options.container : document.querySelector(options.container || ".masonry");
22
+ }
23
+ handleResize() {
24
+ if (this.resizeTimeout) window.cancelAnimationFrame(this.resizeTimeout);
25
+ this.resizeTimeout = window.requestAnimationFrame(()=>{
26
+ this.resizeAllItems();
27
+ });
18
28
  }
19
29
  resizeAllItems() {
20
30
  this.container.style.alignItems = "start";
21
31
  this.gridItems.forEach((item)=>{
22
32
  const rowSpan = Math.ceil((item.clientHeight + this.rowGap) / (this.rowHeight + this.rowGap));
23
- const newItem = item;
24
- newItem.style.gridRowEnd = `span ${rowSpan}`;
33
+ item.style.gridRowEnd = `span ${rowSpan}`;
25
34
  });
26
35
  }
27
- static init(options = {}) {
28
- const { container: container = ".masonry" } = options;
29
- const masonry = new $4fa36e821943b400$var$MasonrySimple();
30
- masonry.container = container instanceof HTMLElement ? container : document.querySelector(container);
31
- if (!masonry.container) return;
32
- masonry.gridItems = Array.from(masonry.container.children);
33
- masonry.container.style.contain = "layout";
34
- masonry.rowGap = parseInt(getComputedStyle(masonry.container).rowGap, 10);
35
- masonry.resizeObserver.observe(masonry.container);
36
- masonry.resizeAllItems();
37
- return masonry;
36
+ init() {
37
+ if (!this.container) return;
38
+ const { rowGap: rowGap } = getComputedStyle(this.container);
39
+ this.gridItems = Array.from(this.container.children);
40
+ this.container.style.contain = "layout";
41
+ this.rowGap = parseInt(rowGap, 10);
42
+ this.resizeObserver.observe(this.container);
43
+ this.resizeAllItems();
38
44
  }
39
45
  destroy() {
40
46
  this.resizeObserver.unobserve(this.container);
41
47
  this.container.style.contain = "";
42
48
  this.container.style.alignItems = "";
43
49
  this.gridItems.forEach((item)=>{
44
- const newItem = item;
45
- newItem.style.gridRowEnd = "";
50
+ item.style.gridRowEnd = "";
46
51
  });
47
52
  }
48
53
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;AAAA,MAAM;IACJ,aAAc;QACZ,IAAI,CAAC,YAAY;QACjB,IAAI,CAAC,YAAY,EAAE;QACnB,IAAI,CAAC,YAAY;QACjB,IAAI,CAAC,SAAS;QACd,IAAI,CAAC,iBAAiB,IAAI,eAAe,IAAI,CAAC,eAAe,KAAK,IAAI;IACxE;IAEA,iBAAiB;QACf,IAAI,CAAC,UAAU,MAAM,aAAa;QAClC,IAAI,CAAC,UAAU,QAAQ,CAAC;YACtB,MAAM,UAAU,KAAK,KACnB,AAAC,CAAA,KAAK,eAAe,IAAI,CAAC,MAAK,IAAM,CAAA,IAAI,CAAC,YAAY,IAAI,CAAC,MAAK;YAGlE,MAAM,UAAU;YAEhB,QAAQ,MAAM,aAAa,CAAC,KAAK,EAAE,QAAQ,CAAC;QAC9C;IACF;IAEA,OAAO,KAAK,UAAU,CAAC,CAAC,EAAE;QACxB,MAAM,aAAE,YAAY,YAAY,GAAG;QACnC,MAAM,UAAU,IAAI;QAEpB,QAAQ,YAAY,qBAAqB,cACrC,YACA,SAAS,cAAc;QAE3B,IAAI,CAAC,QAAQ,WAAW;QAExB,QAAQ,YAAY,MAAM,KAAK,QAAQ,UAAU;QACjD,QAAQ,UAAU,MAAM,UAAU;QAElC,QAAQ,SAAS,SAAS,iBAAiB,QAAQ,WAAW,QAAQ;QAEtE,QAAQ,eAAe,QAAQ,QAAQ;QACvC,QAAQ;QAER,OAAO;IACT;IAEA,UAAU;QACR,IAAI,CAAC,eAAe,UAAU,IAAI,CAAC;QAEnC,IAAI,CAAC,UAAU,MAAM,UAAU;QAC/B,IAAI,CAAC,UAAU,MAAM,aAAa;QAClC,IAAI,CAAC,UAAU,QAAQ,CAAC;YACtB,MAAM,UAAU;YAEhB,QAAQ,MAAM,aAAa;QAC7B;IACF;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class MasonrySimple {\n constructor() {\n this.container = null;\n this.gridItems = [];\n this.rowHeight = 1;\n this.rowGap = 0;\n this.resizeObserver = new ResizeObserver(this.resizeAllItems.bind(this));\n }\n\n resizeAllItems() {\n this.container.style.alignItems = 'start';\n this.gridItems.forEach((item) => {\n const rowSpan = Math.ceil(\n (item.clientHeight + this.rowGap) / (this.rowHeight + this.rowGap),\n );\n\n const newItem = item;\n\n newItem.style.gridRowEnd = `span ${rowSpan}`;\n });\n }\n\n static init(options = {}) {\n const { container = '.masonry' } = options;\n const masonry = new MasonrySimple();\n\n masonry.container = container instanceof HTMLElement\n ? container\n : document.querySelector(container);\n\n if (!masonry.container) return;\n\n masonry.gridItems = Array.from(masonry.container.children);\n masonry.container.style.contain = 'layout';\n\n masonry.rowGap = parseInt(getComputedStyle(masonry.container).rowGap, 10);\n\n masonry.resizeObserver.observe(masonry.container);\n masonry.resizeAllItems();\n\n return masonry;\n }\n\n destroy() {\n this.resizeObserver.unobserve(this.container);\n\n this.container.style.contain = '';\n this.container.style.alignItems = '';\n this.gridItems.forEach((item) => {\n const newItem = item;\n\n newItem.style.gridRowEnd = '';\n });\n }\n}\n\nexport default MasonrySimple;\n"],"names":[],"version":3,"file":"index.js.map"}
1
+ {"mappings":";;;;;;;;;;;;AAAA,MAAM;IACJ,YAAY,KAAK;IACjB,YAAY,EAAE,CAAC;IACf,YAAY,EAAE;IACd,SAAS,EAAE;IACX,gBAAgB,KAAK;IACrB,iBAAiB,IAAI,eAAe,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,GAAG;IAElE,YAAY,UAAU,CAAC,CAAC,CAAE;QACxB,IAAI,CAAC,SAAS,GACZ,QAAQ,SAAS,YAAY,cACzB,QAAQ,SAAS,GACjB,SAAS,aAAa,CAAC,QAAQ,SAAS,IAAI;IACpD;IAEA,eAAe;QACb,IAAI,IAAI,CAAC,aAAa,EACpB,OAAO,oBAAoB,CAAC,IAAI,CAAC,aAAa;QAGhD,IAAI,CAAC,aAAa,GAAG,OAAO,qBAAqB,CAAC;YAChD,IAAI,CAAC,cAAc;QACrB;IACF;IAEA,iBAAiB;QACf,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,GAAG;QAClC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;YACtB,MAAM,UAAU,KAAK,IAAI,CACvB,AAAC,CAAA,KAAK,YAAY,GAAG,IAAI,CAAC,MAAM,AAAD,IAAM,CAAA,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,MAAM,AAAD;YAGlE,KAAK,KAAK,CAAC,UAAU,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC;QAC3C;IACF;IAEA,OAAO;QACL,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;QAErB,MAAM,UAAE,MAAM,EAAE,GAAG,iBAAiB,IAAI,CAAC,SAAS;QAElD,IAAI,CAAC,SAAS,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ;QACnD,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG;QAC/B,IAAI,CAAC,MAAM,GAAG,SAAS,QAAQ;QAC/B,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS;QAC1C,IAAI,CAAC,cAAc;IACrB;IAEA,UAAU;QACR,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS;QAC5C,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG;QAC/B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,GAAG;QAClC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;YACtB,KAAK,KAAK,CAAC,UAAU,GAAG;QAC1B;IACF;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class MasonrySimple {\n container = null;\n gridItems = [];\n rowHeight = 1;\n rowGap = 0;\n resizeTimeout = null;\n resizeObserver = new ResizeObserver(this.handleResize.bind(this));\n\n constructor(options = {}) {\n this.container =\n options.container instanceof HTMLElement\n ? options.container\n : document.querySelector(options.container || '.masonry');\n }\n\n handleResize() {\n if (this.resizeTimeout) {\n window.cancelAnimationFrame(this.resizeTimeout);\n }\n\n this.resizeTimeout = window.requestAnimationFrame(() => {\n this.resizeAllItems();\n });\n }\n\n resizeAllItems() {\n this.container.style.alignItems = 'start';\n this.gridItems.forEach((item) => {\n const rowSpan = Math.ceil(\n (item.clientHeight + this.rowGap) / (this.rowHeight + this.rowGap)\n );\n\n item.style.gridRowEnd = `span ${rowSpan}`;\n });\n }\n\n init() {\n if (!this.container) return;\n\n const { rowGap } = getComputedStyle(this.container);\n\n this.gridItems = Array.from(this.container.children);\n this.container.style.contain = 'layout';\n this.rowGap = parseInt(rowGap, 10);\n this.resizeObserver.observe(this.container);\n this.resizeAllItems();\n }\n\n destroy() {\n this.resizeObserver.unobserve(this.container);\n this.container.style.contain = '';\n this.container.style.alignItems = '';\n this.gridItems.forEach((item) => {\n item.style.gridRowEnd = '';\n });\n }\n}\n\nexport default MasonrySimple;\n"],"names":[],"version":3,"file":"index.js.map"}
@@ -1,38 +1,41 @@
1
1
  class $cf838c15c8b009ba$var$MasonrySimple {
2
- constructor(){
3
- this.container = null;
4
- this.gridItems = [];
5
- this.rowHeight = 1;
6
- this.rowGap = 0;
7
- this.resizeObserver = new ResizeObserver(this.resizeAllItems.bind(this));
2
+ container = null;
3
+ gridItems = [];
4
+ rowHeight = 1;
5
+ rowGap = 0;
6
+ resizeTimeout = null;
7
+ resizeObserver = new ResizeObserver(this.handleResize.bind(this));
8
+ constructor(options = {}){
9
+ this.container = options.container instanceof HTMLElement ? options.container : document.querySelector(options.container || ".masonry");
10
+ }
11
+ handleResize() {
12
+ if (this.resizeTimeout) window.cancelAnimationFrame(this.resizeTimeout);
13
+ this.resizeTimeout = window.requestAnimationFrame(()=>{
14
+ this.resizeAllItems();
15
+ });
8
16
  }
9
17
  resizeAllItems() {
10
18
  this.container.style.alignItems = "start";
11
19
  this.gridItems.forEach((item)=>{
12
20
  const rowSpan = Math.ceil((item.clientHeight + this.rowGap) / (this.rowHeight + this.rowGap));
13
- const newItem = item;
14
- newItem.style.gridRowEnd = `span ${rowSpan}`;
21
+ item.style.gridRowEnd = `span ${rowSpan}`;
15
22
  });
16
23
  }
17
- static init(options = {}) {
18
- const { container: container = ".masonry" } = options;
19
- const masonry = new $cf838c15c8b009ba$var$MasonrySimple();
20
- masonry.container = container instanceof HTMLElement ? container : document.querySelector(container);
21
- if (!masonry.container) return;
22
- masonry.gridItems = Array.from(masonry.container.children);
23
- masonry.container.style.contain = "layout";
24
- masonry.rowGap = parseInt(getComputedStyle(masonry.container).rowGap, 10);
25
- masonry.resizeObserver.observe(masonry.container);
26
- masonry.resizeAllItems();
27
- return masonry;
24
+ init() {
25
+ if (!this.container) return;
26
+ const { rowGap: rowGap } = getComputedStyle(this.container);
27
+ this.gridItems = Array.from(this.container.children);
28
+ this.container.style.contain = "layout";
29
+ this.rowGap = parseInt(rowGap, 10);
30
+ this.resizeObserver.observe(this.container);
31
+ this.resizeAllItems();
28
32
  }
29
33
  destroy() {
30
34
  this.resizeObserver.unobserve(this.container);
31
35
  this.container.style.contain = "";
32
36
  this.container.style.alignItems = "";
33
37
  this.gridItems.forEach((item)=>{
34
- const newItem = item;
35
- newItem.style.gridRowEnd = "";
38
+ item.style.gridRowEnd = "";
36
39
  });
37
40
  }
38
41
  }
@@ -1 +1 @@
1
- {"mappings":"AAAA,MAAM;IACJ,aAAc;QACZ,IAAI,CAAC,YAAY;QACjB,IAAI,CAAC,YAAY,EAAE;QACnB,IAAI,CAAC,YAAY;QACjB,IAAI,CAAC,SAAS;QACd,IAAI,CAAC,iBAAiB,IAAI,eAAe,IAAI,CAAC,eAAe,KAAK,IAAI;IACxE;IAEA,iBAAiB;QACf,IAAI,CAAC,UAAU,MAAM,aAAa;QAClC,IAAI,CAAC,UAAU,QAAQ,CAAC;YACtB,MAAM,UAAU,KAAK,KACnB,AAAC,CAAA,KAAK,eAAe,IAAI,CAAC,MAAK,IAAM,CAAA,IAAI,CAAC,YAAY,IAAI,CAAC,MAAK;YAGlE,MAAM,UAAU;YAEhB,QAAQ,MAAM,aAAa,CAAC,KAAK,EAAE,QAAQ,CAAC;QAC9C;IACF;IAEA,OAAO,KAAK,UAAU,CAAC,CAAC,EAAE;QACxB,MAAM,aAAE,YAAY,YAAY,GAAG;QACnC,MAAM,UAAU,IAAI;QAEpB,QAAQ,YAAY,qBAAqB,cACrC,YACA,SAAS,cAAc;QAE3B,IAAI,CAAC,QAAQ,WAAW;QAExB,QAAQ,YAAY,MAAM,KAAK,QAAQ,UAAU;QACjD,QAAQ,UAAU,MAAM,UAAU;QAElC,QAAQ,SAAS,SAAS,iBAAiB,QAAQ,WAAW,QAAQ;QAEtE,QAAQ,eAAe,QAAQ,QAAQ;QACvC,QAAQ;QAER,OAAO;IACT;IAEA,UAAU;QACR,IAAI,CAAC,eAAe,UAAU,IAAI,CAAC;QAEnC,IAAI,CAAC,UAAU,MAAM,UAAU;QAC/B,IAAI,CAAC,UAAU,MAAM,aAAa;QAClC,IAAI,CAAC,UAAU,QAAQ,CAAC;YACtB,MAAM,UAAU;YAEhB,QAAQ,MAAM,aAAa;QAC7B;IACF;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class MasonrySimple {\n constructor() {\n this.container = null;\n this.gridItems = [];\n this.rowHeight = 1;\n this.rowGap = 0;\n this.resizeObserver = new ResizeObserver(this.resizeAllItems.bind(this));\n }\n\n resizeAllItems() {\n this.container.style.alignItems = 'start';\n this.gridItems.forEach((item) => {\n const rowSpan = Math.ceil(\n (item.clientHeight + this.rowGap) / (this.rowHeight + this.rowGap),\n );\n\n const newItem = item;\n\n newItem.style.gridRowEnd = `span ${rowSpan}`;\n });\n }\n\n static init(options = {}) {\n const { container = '.masonry' } = options;\n const masonry = new MasonrySimple();\n\n masonry.container = container instanceof HTMLElement\n ? container\n : document.querySelector(container);\n\n if (!masonry.container) return;\n\n masonry.gridItems = Array.from(masonry.container.children);\n masonry.container.style.contain = 'layout';\n\n masonry.rowGap = parseInt(getComputedStyle(masonry.container).rowGap, 10);\n\n masonry.resizeObserver.observe(masonry.container);\n masonry.resizeAllItems();\n\n return masonry;\n }\n\n destroy() {\n this.resizeObserver.unobserve(this.container);\n\n this.container.style.contain = '';\n this.container.style.alignItems = '';\n this.gridItems.forEach((item) => {\n const newItem = item;\n\n newItem.style.gridRowEnd = '';\n });\n }\n}\n\nexport default MasonrySimple;\n"],"names":[],"version":3,"file":"index.module.js.map"}
1
+ {"mappings":"AAAA,MAAM;IACJ,YAAY,KAAK;IACjB,YAAY,EAAE,CAAC;IACf,YAAY,EAAE;IACd,SAAS,EAAE;IACX,gBAAgB,KAAK;IACrB,iBAAiB,IAAI,eAAe,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,GAAG;IAElE,YAAY,UAAU,CAAC,CAAC,CAAE;QACxB,IAAI,CAAC,SAAS,GACZ,QAAQ,SAAS,YAAY,cACzB,QAAQ,SAAS,GACjB,SAAS,aAAa,CAAC,QAAQ,SAAS,IAAI;IACpD;IAEA,eAAe;QACb,IAAI,IAAI,CAAC,aAAa,EACpB,OAAO,oBAAoB,CAAC,IAAI,CAAC,aAAa;QAGhD,IAAI,CAAC,aAAa,GAAG,OAAO,qBAAqB,CAAC;YAChD,IAAI,CAAC,cAAc;QACrB;IACF;IAEA,iBAAiB;QACf,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,GAAG;QAClC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;YACtB,MAAM,UAAU,KAAK,IAAI,CACvB,AAAC,CAAA,KAAK,YAAY,GAAG,IAAI,CAAC,MAAM,AAAD,IAAM,CAAA,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,MAAM,AAAD;YAGlE,KAAK,KAAK,CAAC,UAAU,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC;QAC3C;IACF;IAEA,OAAO;QACL,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;QAErB,MAAM,UAAE,MAAM,EAAE,GAAG,iBAAiB,IAAI,CAAC,SAAS;QAElD,IAAI,CAAC,SAAS,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ;QACnD,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG;QAC/B,IAAI,CAAC,MAAM,GAAG,SAAS,QAAQ;QAC/B,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS;QAC1C,IAAI,CAAC,cAAc;IACrB;IAEA,UAAU;QACR,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS;QAC5C,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG;QAC/B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,GAAG;QAClC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;YACtB,KAAK,KAAK,CAAC,UAAU,GAAG;QAC1B;IACF;AACF;IAEA,2CAAe","sources":["src/index.js"],"sourcesContent":["class MasonrySimple {\n container = null;\n gridItems = [];\n rowHeight = 1;\n rowGap = 0;\n resizeTimeout = null;\n resizeObserver = new ResizeObserver(this.handleResize.bind(this));\n\n constructor(options = {}) {\n this.container =\n options.container instanceof HTMLElement\n ? options.container\n : document.querySelector(options.container || '.masonry');\n }\n\n handleResize() {\n if (this.resizeTimeout) {\n window.cancelAnimationFrame(this.resizeTimeout);\n }\n\n this.resizeTimeout = window.requestAnimationFrame(() => {\n this.resizeAllItems();\n });\n }\n\n resizeAllItems() {\n this.container.style.alignItems = 'start';\n this.gridItems.forEach((item) => {\n const rowSpan = Math.ceil(\n (item.clientHeight + this.rowGap) / (this.rowHeight + this.rowGap)\n );\n\n item.style.gridRowEnd = `span ${rowSpan}`;\n });\n }\n\n init() {\n if (!this.container) return;\n\n const { rowGap } = getComputedStyle(this.container);\n\n this.gridItems = Array.from(this.container.children);\n this.container.style.contain = 'layout';\n this.rowGap = parseInt(rowGap, 10);\n this.resizeObserver.observe(this.container);\n this.resizeAllItems();\n }\n\n destroy() {\n this.resizeObserver.unobserve(this.container);\n this.container.style.contain = '';\n this.container.style.alignItems = '';\n this.gridItems.forEach((item) => {\n item.style.gridRowEnd = '';\n });\n }\n}\n\nexport default MasonrySimple;\n"],"names":[],"version":3,"file":"index.module.js.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "masonry-simple",
3
- "version": "2.2.0",
3
+ "version": "3.0.0",
4
4
  "description": "Responsive masonry grid",
5
5
  "author": "ux-ui.pro",
6
6
  "license": "MIT",
@@ -11,20 +11,26 @@
11
11
  "bugs": {
12
12
  "url": "https://github.com/ux-ui-pro/masonry-simple/issues"
13
13
  },
14
- "source": "src/index.js",
15
14
  "main": "dist/index.js",
16
15
  "module": "dist/index.module.js",
16
+ "targets": {
17
+ "main": {
18
+ "source": "src/index.js"
19
+ },
20
+ "module": {
21
+ "source": "src/index.js"
22
+ }
23
+ },
17
24
  "scripts": {
18
- "serve": "parcel watch",
19
- "build": "parcel build",
20
25
  "clean": "rm -rf dist .parcel-cache",
21
- "lint": "eslint --ext .js,.vue --report-unused-disable-directives ."
26
+ "build": "yarn clean && parcel build",
27
+ "lint:js": "eslint --ext .js",
28
+ "lintfix": "yarn lint:js --fix"
22
29
  },
23
30
  "devDependencies": {
24
- "eslint": "^7.32.0 || ^8.2.0",
31
+ "eslint": "^7.32.0 || ^8.56.0",
25
32
  "eslint-config-airbnb-base": "^15.0.0",
26
- "eslint-plugin-import": "^2.25.2",
27
- "parcel": "~2.9.3"
33
+ "parcel": "^2.11.0"
28
34
  },
29
35
  "keywords": [
30
36
  "masonry",
package/src/index.js CHANGED
@@ -1,42 +1,49 @@
1
1
  class MasonrySimple {
2
- constructor() {
3
- this.container = null;
4
- this.gridItems = [];
5
- this.rowHeight = 1;
6
- this.rowGap = 0;
7
- this.resizeObserver = new ResizeObserver(this.resizeAllItems.bind(this));
2
+ container = null;
3
+ gridItems = [];
4
+ rowHeight = 1;
5
+ rowGap = 0;
6
+ resizeTimeout = null;
7
+ resizeObserver = new ResizeObserver(this.handleResize.bind(this));
8
+
9
+ constructor(options = {}) {
10
+ this.container =
11
+ options.container instanceof HTMLElement
12
+ ? options.container
13
+ : document.querySelector(options.container || '.masonry');
14
+ }
15
+
16
+ handleResize() {
17
+ if (this.resizeTimeout) {
18
+ window.cancelAnimationFrame(this.resizeTimeout);
19
+ }
20
+
21
+ this.resizeTimeout = window.requestAnimationFrame(() => {
22
+ this.resizeAllItems();
23
+ });
8
24
  }
9
25
 
10
26
  resizeAllItems() {
11
27
  this.container.style.alignItems = 'start';
12
28
  this.gridItems.forEach((item) => {
13
29
  const rowSpan = Math.ceil(
14
- (item.clientHeight + this.rowGap) / (this.rowHeight + this.rowGap),
30
+ (item.clientHeight + this.rowGap) / (this.rowHeight + this.rowGap)
15
31
  );
16
32
 
17
- const newItem = item;
18
-
19
- newItem.style.gridRowEnd = `span ${rowSpan}`;
33
+ item.style.gridRowEnd = `span ${rowSpan}`;
20
34
  });
21
35
  }
22
36
 
23
- static init(options = {}) {
24
- const { container = '.masonry' } = options;
25
- const masonry = new MasonrySimple();
26
-
27
- masonry.container = container instanceof HTMLElement
28
- ? container
29
- : document.querySelector(container);
37
+ init() {
38
+ if (!this.container) return;
30
39
 
31
- if (!masonry.container) return;
40
+ const { rowGap } = getComputedStyle(this.container);
32
41
 
33
- masonry.gridItems = Array.from(masonry.container.children);
34
- masonry.container.style.contain = 'layout';
35
- masonry.rowGap = parseInt(getComputedStyle(masonry.container).rowGap, 10);
36
- masonry.resizeObserver.observe(masonry.container);
37
- masonry.resizeAllItems();
38
-
39
- return masonry;
42
+ this.gridItems = Array.from(this.container.children);
43
+ this.container.style.contain = 'layout';
44
+ this.rowGap = parseInt(rowGap, 10);
45
+ this.resizeObserver.observe(this.container);
46
+ this.resizeAllItems();
40
47
  }
41
48
 
42
49
  destroy() {
@@ -44,9 +51,7 @@ class MasonrySimple {
44
51
  this.container.style.contain = '';
45
52
  this.container.style.alignItems = '';
46
53
  this.gridItems.forEach((item) => {
47
- const newItem = item;
48
-
49
- newItem.style.gridRowEnd = '';
54
+ item.style.gridRowEnd = '';
50
55
  });
51
56
  }
52
57
  }