masonry-simple 0.0.3 → 1.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,7 +7,7 @@
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><a href="https://bundlephobia.com/package/masonry-simple">1kB gzipped</a></sup>
10
+ <sup><a href="https://bundlephobia.com/package/masonry-simple">0.7kB gzipped</a></sup>
11
11
  <h3><a href="https://codepen.io/ux-ui/pen/poxGEqX">Demo</a></h3>
12
12
 
13
13
  </div>
@@ -31,9 +31,11 @@ import MasonrySimple from 'masonry-simple'
31
31
 
32
32
  ### Usage
33
33
  ```javascript
34
- new MasonrySimple({
34
+ const masonry = new MasonrySimple()
35
+
36
+ masonry.options({
35
37
  container: '.masonry',
36
- debounce: 200
38
+ timeout: 200
37
39
  })
38
40
  ```
39
41
  ```HTML
package/dist/index.js CHANGED
@@ -9,36 +9,36 @@ $parcel$defineInteropFlag(module.exports);
9
9
 
10
10
  $parcel$export(module.exports, "default", () => $4fa36e821943b400$export$2e2bcd8739ae039);
11
11
  class $4fa36e821943b400$export$2e2bcd8739ae039 {
12
- constructor(options = {}){
13
- const { container: container = ".masonry" , debounce: debounce = 200 } = options;
12
+ constructor(){
13
+ this.grid = null;
14
+ this.gridItems = [];
15
+ this.timeout = 200;
16
+ this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), this.timeout);
17
+ this.resizeObserver = null;
18
+ }
19
+ options(options = {}) {
20
+ const { container: container = ".masonry" , timeout: timeout = this.timeout } = options;
14
21
  this.grid = container instanceof HTMLElement ? container : document.querySelector(container);
15
- this.gridItems = [
16
- ...this.grid.children
17
- ];
18
- this.resizeObserver = new ResizeObserver(()=>{
19
- this.resizeAllItems();
20
- });
21
- this.resizeObserver.observe(this.grid);
22
- window.onload = this.resizeAllItems.bind(this);
23
- window.addEventListener("beforeunload", ()=>{
24
- this.resizeObserver.unobserve(this.grid);
25
- });
22
+ this.gridItems = Array.from(this.grid.children);
26
23
  this.grid.style.contain = "layout";
27
- this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), debounce);
24
+ this.resizeObserver = new ResizeObserver(this.debouncedResize);
25
+ this.resizeObserver.observe(this.grid);
26
+ this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), timeout);
27
+ this.resizeAllItems();
28
28
  }
29
- resizeItem(item) {
30
- const rowHeight = 1;
31
- const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
29
+ resizeItem(item, rowHeight, rowGap) {
32
30
  const rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap));
33
31
  item.style.gridRowEnd = "span " + rowSpan;
34
32
  }
35
33
  resizeAllItems() {
34
+ const rowHeight = 1;
35
+ const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
36
36
  this.grid.style.alignItems = "start";
37
- this.gridItems.forEach((item)=>this.resizeItem(item));
37
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
38
38
  this.grid.style.alignItems = "stretch";
39
- window.requestAnimationFrame(()=>{
40
- this.gridItems.forEach((item)=>this.resizeItem(item));
41
- });
39
+ setTimeout(()=>{
40
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
41
+ }, 0);
42
42
  }
43
43
  debounce(func, wait) {
44
44
  let timeout;
@@ -51,6 +51,9 @@ class $4fa36e821943b400$export$2e2bcd8739ae039 {
51
51
  timeout = setTimeout(later, wait);
52
52
  };
53
53
  }
54
+ debouncedResize = ()=>{
55
+ this.resizeAllItems();
56
+ };
54
57
  }
55
58
 
56
59
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;AAA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AAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA,OAAQ,IAAI,CAAC,UAAU,CAAC;QAChD;IACD;IAEA,SAAS,IAAI,EAAE,IAAI,EAAE;QACpB,IAAI;QAEJ,OAAO,SAAS,iBAAiB,GAAG,IAAI,EAAE;YACzC,MAAM,QAAQ,IAAM;gBACnB,aAAa;gBACb,QAAQ;YACT;YACA,aAAa;YACb,UAAU,WAAW,OAAO;QAC7B;IACD;AACD","sources":["src/index.js"],"sourcesContent":["export default class MasonrySimple {\r\n\tconstructor(options = {}) {\r\n\t\tconst {\r\n\t\t\tcontainer = '.masonry',\r\n\t\t\tdebounce = 200\r\n\t\t} = options\r\n\r\n\t\tthis.grid = container instanceof HTMLElement ? container : document.querySelector(container)\r\n\t\tthis.gridItems = [...this.grid.children]\r\n\r\n\t\tthis.resizeObserver = new ResizeObserver(() => {\r\n\t\t\tthis.resizeAllItems()\r\n\t\t})\r\n\r\n\t\tthis.resizeObserver.observe(this.grid)\r\n\r\n\t\twindow.onload = this.resizeAllItems.bind(this)\r\n\r\n\t\twindow.addEventListener('beforeunload', () => {\r\n\t\t\tthis.resizeObserver.unobserve(this.grid)\r\n\t\t})\r\n\r\n\t\tthis.grid.style.contain = 'layout'\r\n\r\n\t\tthis.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), debounce)\r\n\t}\r\n\r\n\tresizeItem(item) {\r\n\t\tconst rowHeight = 1\r\n\t\tconst rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue('grid-row-gap'))\r\n\t\tconst rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap))\r\n\r\n\t\titem.style.gridRowEnd = 'span ' + rowSpan\r\n\t}\r\n\r\n\tresizeAllItems() {\r\n\t\tthis.grid.style.alignItems = 'start'\r\n\t\tthis.gridItems.forEach(item => this.resizeItem(item))\r\n\t\tthis.grid.style.alignItems = 'stretch'\r\n\r\n\t\twindow.requestAnimationFrame(() => {\r\n\t\t\tthis.gridItems.forEach(item => this.resizeItem(item))\r\n\t\t})\r\n\t}\r\n\r\n\tdebounce(func, wait) {\r\n\t\tlet timeout\r\n\r\n\t\treturn function executedFunction(...args) {\r\n\t\t\tconst later = () => {\r\n\t\t\t\tclearTimeout(timeout)\r\n\t\t\t\tfunc(...args)\r\n\t\t\t}\r\n\t\t\tclearTimeout(timeout)\r\n\t\t\ttimeout = setTimeout(later, wait)\r\n\t\t}\r\n\t}\r\n}"],"names":[],"version":3,"file":"index.js.map"}
1
+ {"mappings":";;;;;;;;;;AAAe;IACd,aAAc;QACb,IAAI,CAAC,IAAI,GAAG,IAAI;QAChB,IAAI,CAAC,SAAS,GAAG,EAAE;QACnB,IAAI,CAAC,OAAO,GAAG;QACf,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,OAAO;QAChF,IAAI,CAAC,cAAc,GAAG,IAAI;IAC3B;IAEA,QAAQ,UAAU,CAAC,CAAC,EAAE;QACrB,MAAM,aACL,YAAY,sBACZ,UAAU,IAAI,CAAC,OAAO,GACtB,GAAG;QAEJ,IAAI,CAAC,IAAI,GAAG,qBAAqB,cAAc,YAAY,SAAS,aAAa,CAAC,UAAU;QAC5F,IAAI,CAAC,SAAS,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ;QAC9C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG;QAC1B,IAAI,CAAC,cAAc,GAAG,IAAI,eAAe,IAAI,CAAC,eAAe;QAC7D,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;QACrC,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,GAAG;QACpE,IAAI,CAAC,cAAc;IACpB;IAEA,WAAW,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE;QACnC,MAAM,UAAU,KAAK,IAAI,CAAC,AAAC,CAAA,KAAK,YAAY,GAAG,MAAK,IAAM,CAAA,YAAY,MAAK;QAC3E,KAAK,KAAK,CAAC,UAAU,GAAG,UAAU;IACnC;IAEA,iBAAiB;QAChB,MAAM,YAAY;QAClB,MAAM,SAAS,SAAS,OAAO,gBAAgB,CAAC,IAAI,CAAC,IAAI,EAAE,gBAAgB,CAAC;QAC5E,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG;QAC7B,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA,OAAQ,IAAI,CAAC,UAAU,CAAC,MAAM,WAAW;QAChE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG;QAC7B,WAAW,IAAM;YAChB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA,OAAQ,IAAI,CAAC,UAAU,CAAC,MAAM,WAAW;QACjE,GAAG;IACJ;IAEA,SAAS,IAAI,EAAE,IAAI,EAAE;QACpB,IAAI;QACJ,OAAO,SAAS,iBAAiB,GAAG,IAAI,EAAE;YACzC,MAAM,QAAQ,IAAM;gBACnB,aAAa;gBACb,QAAQ;YACT;YACA,aAAa;YACb,UAAU,WAAW,OAAO;QAC7B;IACD;IAEA,kBAAkB,IAAM;QACvB,IAAI,CAAC,cAAc;IACpB,EAAC;AACF","sources":["src/index.js"],"sourcesContent":["export default class MasonrySimple {\r\n\tconstructor() {\r\n\t\tthis.grid = null\r\n\t\tthis.gridItems = []\r\n\t\tthis.timeout = 200\r\n\t\tthis.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), this.timeout)\r\n\t\tthis.resizeObserver = null\r\n\t}\r\n\r\n\toptions(options = {}) {\r\n\t\tconst {\r\n\t\t\tcontainer = '.masonry',\r\n\t\t\ttimeout = this.timeout\r\n\t\t} = options\r\n\r\n\t\tthis.grid = container instanceof HTMLElement ? container : document.querySelector(container)\r\n\t\tthis.gridItems = Array.from(this.grid.children)\r\n\t\tthis.grid.style.contain = 'layout'\r\n\t\tthis.resizeObserver = new ResizeObserver(this.debouncedResize)\r\n\t\tthis.resizeObserver.observe(this.grid)\r\n\t\tthis.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), timeout)\r\n\t\tthis.resizeAllItems()\r\n\t}\r\n\r\n\tresizeItem(item, rowHeight, rowGap) {\r\n\t\tconst rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap))\r\n\t\titem.style.gridRowEnd = 'span ' + rowSpan\r\n\t}\r\n\r\n\tresizeAllItems() {\r\n\t\tconst rowHeight = 1\r\n\t\tconst rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue('grid-row-gap'))\r\n\t\tthis.grid.style.alignItems = 'start'\r\n\t\tthis.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))\r\n\t\tthis.grid.style.alignItems = 'stretch'\r\n\t\tsetTimeout(() => {\r\n\t\t\tthis.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))\r\n\t\t}, 0)\r\n\t}\r\n\r\n\tdebounce(func, wait) {\r\n\t\tlet timeout\r\n\t\treturn function executedFunction(...args) {\r\n\t\t\tconst later = () => {\r\n\t\t\t\tclearTimeout(timeout)\r\n\t\t\t\tfunc(...args)\r\n\t\t\t}\r\n\t\t\tclearTimeout(timeout)\r\n\t\t\ttimeout = setTimeout(later, wait)\r\n\t\t}\r\n\t}\r\n\r\n\tdebouncedResize = () => {\r\n\t\tthis.resizeAllItems()\r\n\t}\r\n}"],"names":[],"version":3,"file":"index.js.map"}
@@ -1,34 +1,34 @@
1
1
  class $cf838c15c8b009ba$export$2e2bcd8739ae039 {
2
- constructor(options = {}){
3
- const { container: container = ".masonry" , debounce: debounce = 200 } = options;
2
+ constructor(){
3
+ this.grid = null;
4
+ this.gridItems = [];
5
+ this.timeout = 200;
6
+ this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), this.timeout);
7
+ this.resizeObserver = null;
8
+ }
9
+ options(options = {}) {
10
+ const { container: container = ".masonry" , timeout: timeout = this.timeout } = options;
4
11
  this.grid = container instanceof HTMLElement ? container : document.querySelector(container);
5
- this.gridItems = [
6
- ...this.grid.children
7
- ];
8
- this.resizeObserver = new ResizeObserver(()=>{
9
- this.resizeAllItems();
10
- });
11
- this.resizeObserver.observe(this.grid);
12
- window.onload = this.resizeAllItems.bind(this);
13
- window.addEventListener("beforeunload", ()=>{
14
- this.resizeObserver.unobserve(this.grid);
15
- });
12
+ this.gridItems = Array.from(this.grid.children);
16
13
  this.grid.style.contain = "layout";
17
- this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), debounce);
14
+ this.resizeObserver = new ResizeObserver(this.debouncedResize);
15
+ this.resizeObserver.observe(this.grid);
16
+ this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), timeout);
17
+ this.resizeAllItems();
18
18
  }
19
- resizeItem(item) {
20
- const rowHeight = 1;
21
- const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
19
+ resizeItem(item, rowHeight, rowGap) {
22
20
  const rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap));
23
21
  item.style.gridRowEnd = "span " + rowSpan;
24
22
  }
25
23
  resizeAllItems() {
24
+ const rowHeight = 1;
25
+ const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
26
26
  this.grid.style.alignItems = "start";
27
- this.gridItems.forEach((item)=>this.resizeItem(item));
27
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
28
28
  this.grid.style.alignItems = "stretch";
29
- window.requestAnimationFrame(()=>{
30
- this.gridItems.forEach((item)=>this.resizeItem(item));
31
- });
29
+ setTimeout(()=>{
30
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
31
+ }, 0);
32
32
  }
33
33
  debounce(func, wait) {
34
34
  let timeout;
@@ -41,6 +41,9 @@ class $cf838c15c8b009ba$export$2e2bcd8739ae039 {
41
41
  timeout = setTimeout(later, wait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AI,CAAC,UAAU,CAAC;QAC/C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG;QAE7B,OAAO,qBAAqB,CAAC,IAAM;YAClC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA,OAAQ,IAAI,CAAC,UAAU,CAAC;QAChD;IACD;IAEA,SAAS,IAAI,EAAE,IAAI,EAAE;QACpB,IAAI;QAEJ,OAAO,SAAS,iBAAiB,GAAG,IAAI,EAAE;YACzC,MAAM,QAAQ,IAAM;gBACnB,aAAa;gBACb,QAAQ;YACT;YACA,aAAa;YACb,UAAU,WAAW,OAAO;QAC7B;IACD;AACD","sources":["src/index.js"],"sourcesContent":["export default class MasonrySimple {\r\n\tconstructor(options = {}) {\r\n\t\tconst {\r\n\t\t\tcontainer = '.masonry',\r\n\t\t\tdebounce = 200\r\n\t\t} = options\r\n\r\n\t\tthis.grid = container instanceof HTMLElement ? container : document.querySelector(container)\r\n\t\tthis.gridItems = [...this.grid.children]\r\n\r\n\t\tthis.resizeObserver = new ResizeObserver(() => {\r\n\t\t\tthis.resizeAllItems()\r\n\t\t})\r\n\r\n\t\tthis.resizeObserver.observe(this.grid)\r\n\r\n\t\twindow.onload = this.resizeAllItems.bind(this)\r\n\r\n\t\twindow.addEventListener('beforeunload', () => {\r\n\t\t\tthis.resizeObserver.unobserve(this.grid)\r\n\t\t})\r\n\r\n\t\tthis.grid.style.contain = 'layout'\r\n\r\n\t\tthis.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), debounce)\r\n\t}\r\n\r\n\tresizeItem(item) {\r\n\t\tconst rowHeight = 1\r\n\t\tconst rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue('grid-row-gap'))\r\n\t\tconst rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap))\r\n\r\n\t\titem.style.gridRowEnd = 'span ' + rowSpan\r\n\t}\r\n\r\n\tresizeAllItems() {\r\n\t\tthis.grid.style.alignItems = 'start'\r\n\t\tthis.gridItems.forEach(item => this.resizeItem(item))\r\n\t\tthis.grid.style.alignItems = 'stretch'\r\n\r\n\t\twindow.requestAnimationFrame(() => {\r\n\t\t\tthis.gridItems.forEach(item => this.resizeItem(item))\r\n\t\t})\r\n\t}\r\n\r\n\tdebounce(func, wait) {\r\n\t\tlet timeout\r\n\r\n\t\treturn function executedFunction(...args) {\r\n\t\t\tconst later = () => {\r\n\t\t\t\tclearTimeout(timeout)\r\n\t\t\t\tfunc(...args)\r\n\t\t\t}\r\n\t\t\tclearTimeout(timeout)\r\n\t\t\ttimeout = setTimeout(later, wait)\r\n\t\t}\r\n\t}\r\n}"],"names":[],"version":3,"file":"index.module.js.map"}
1
+ {"mappings":"AAAe;IACd,aAAc;QACb,IAAI,CAAC,IAAI,GAAG,IAAI;QAChB,IAAI,CAAC,SAAS,GAAG,EAAE;QACnB,IAAI,CAAC,OAAO,GAAG;QACf,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,OAAO;QAChF,IAAI,CAAC,cAAc,GAAG,IAAI;IAC3B;IAEA,QAAQ,UAAU,CAAC,CAAC,EAAE;QACrB,MAAM,aACL,YAAY,sBACZ,UAAU,IAAI,CAAC,OAAO,GACtB,GAAG;QAEJ,IAAI,CAAC,IAAI,GAAG,qBAAqB,cAAc,YAAY,SAAS,aAAa,CAAC,UAAU;QAC5F,IAAI,CAAC,SAAS,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ;QAC9C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG;QAC1B,IAAI,CAAC,cAAc,GAAG,IAAI,eAAe,IAAI,CAAC,eAAe;QAC7D,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;QACrC,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,GAAG;QACpE,IAAI,CAAC,cAAc;IACpB;IAEA,WAAW,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE;QACnC,MAAM,UAAU,KAAK,IAAI,CAAC,AAAC,CAAA,KAAK,YAAY,GAAG,MAAK,IAAM,CAAA,YAAY,MAAK;QAC3E,KAAK,KAAK,CAAC,UAAU,GAAG,UAAU;IACnC;IAEA,iBAAiB;QAChB,MAAM,YAAY;QAClB,MAAM,SAAS,SAAS,OAAO,gBAAgB,CAAC,IAAI,CAAC,IAAI,EAAE,gBAAgB,CAAC;QAC5E,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG;QAC7B,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA,OAAQ,IAAI,CAAC,UAAU,CAAC,MAAM,WAAW;QAChE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG;QAC7B,WAAW,IAAM;YAChB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA,OAAQ,IAAI,CAAC,UAAU,CAAC,MAAM,WAAW;QACjE,GAAG;IACJ;IAEA,SAAS,IAAI,EAAE,IAAI,EAAE;QACpB,IAAI;QACJ,OAAO,SAAS,iBAAiB,GAAG,IAAI,EAAE;YACzC,MAAM,QAAQ,IAAM;gBACnB,aAAa;gBACb,QAAQ;YACT;YACA,aAAa;YACb,UAAU,WAAW,OAAO;QAC7B;IACD;IAEA,kBAAkB,IAAM;QACvB,IAAI,CAAC,cAAc;IACpB,EAAC;AACF","sources":["src/index.js"],"sourcesContent":["export default class MasonrySimple {\r\n\tconstructor() {\r\n\t\tthis.grid = null\r\n\t\tthis.gridItems = []\r\n\t\tthis.timeout = 200\r\n\t\tthis.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), this.timeout)\r\n\t\tthis.resizeObserver = null\r\n\t}\r\n\r\n\toptions(options = {}) {\r\n\t\tconst {\r\n\t\t\tcontainer = '.masonry',\r\n\t\t\ttimeout = this.timeout\r\n\t\t} = options\r\n\r\n\t\tthis.grid = container instanceof HTMLElement ? container : document.querySelector(container)\r\n\t\tthis.gridItems = Array.from(this.grid.children)\r\n\t\tthis.grid.style.contain = 'layout'\r\n\t\tthis.resizeObserver = new ResizeObserver(this.debouncedResize)\r\n\t\tthis.resizeObserver.observe(this.grid)\r\n\t\tthis.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), timeout)\r\n\t\tthis.resizeAllItems()\r\n\t}\r\n\r\n\tresizeItem(item, rowHeight, rowGap) {\r\n\t\tconst rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap))\r\n\t\titem.style.gridRowEnd = 'span ' + rowSpan\r\n\t}\r\n\r\n\tresizeAllItems() {\r\n\t\tconst rowHeight = 1\r\n\t\tconst rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue('grid-row-gap'))\r\n\t\tthis.grid.style.alignItems = 'start'\r\n\t\tthis.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))\r\n\t\tthis.grid.style.alignItems = 'stretch'\r\n\t\tsetTimeout(() => {\r\n\t\t\tthis.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))\r\n\t\t}, 0)\r\n\t}\r\n\r\n\tdebounce(func, wait) {\r\n\t\tlet timeout\r\n\t\treturn function executedFunction(...args) {\r\n\t\t\tconst later = () => {\r\n\t\t\t\tclearTimeout(timeout)\r\n\t\t\t\tfunc(...args)\r\n\t\t\t}\r\n\t\t\tclearTimeout(timeout)\r\n\t\t\ttimeout = setTimeout(later, wait)\r\n\t\t}\r\n\t}\r\n\r\n\tdebouncedResize = () => {\r\n\t\tthis.resizeAllItems()\r\n\t}\r\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": "0.0.3",
3
+ "version": "1.0.0",
4
4
  "description": "Responsive masonry grid",
5
5
  "author": "ux-ui.pro",
6
6
  "license": "MIT",
package/src/index.js CHANGED
@@ -1,51 +1,45 @@
1
1
  export default class MasonrySimple {
2
- constructor(options = {}) {
2
+ constructor() {
3
+ this.grid = null
4
+ this.gridItems = []
5
+ this.timeout = 200
6
+ this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), this.timeout)
7
+ this.resizeObserver = null
8
+ }
9
+
10
+ options(options = {}) {
3
11
  const {
4
12
  container = '.masonry',
5
- debounce = 200
13
+ timeout = this.timeout
6
14
  } = options
7
15
 
8
16
  this.grid = container instanceof HTMLElement ? container : document.querySelector(container)
9
- this.gridItems = [...this.grid.children]
10
-
11
- this.resizeObserver = new ResizeObserver(() => {
12
- this.resizeAllItems()
13
- })
14
-
15
- this.resizeObserver.observe(this.grid)
16
-
17
- window.onload = this.resizeAllItems.bind(this)
18
-
19
- window.addEventListener('beforeunload', () => {
20
- this.resizeObserver.unobserve(this.grid)
21
- })
22
-
17
+ this.gridItems = Array.from(this.grid.children)
23
18
  this.grid.style.contain = 'layout'
24
-
25
- this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), debounce)
19
+ this.resizeObserver = new ResizeObserver(this.debouncedResize)
20
+ this.resizeObserver.observe(this.grid)
21
+ this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), timeout)
22
+ this.resizeAllItems()
26
23
  }
27
24
 
28
- resizeItem(item) {
29
- const rowHeight = 1
30
- const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue('grid-row-gap'))
25
+ resizeItem(item, rowHeight, rowGap) {
31
26
  const rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap))
32
-
33
27
  item.style.gridRowEnd = 'span ' + rowSpan
34
28
  }
35
29
 
36
30
  resizeAllItems() {
31
+ const rowHeight = 1
32
+ const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue('grid-row-gap'))
37
33
  this.grid.style.alignItems = 'start'
38
- this.gridItems.forEach(item => this.resizeItem(item))
34
+ this.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))
39
35
  this.grid.style.alignItems = 'stretch'
40
-
41
- window.requestAnimationFrame(() => {
42
- this.gridItems.forEach(item => this.resizeItem(item))
43
- })
36
+ setTimeout(() => {
37
+ this.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))
38
+ }, 0)
44
39
  }
45
40
 
46
41
  debounce(func, wait) {
47
42
  let timeout
48
-
49
43
  return function executedFunction(...args) {
50
44
  const later = () => {
51
45
  clearTimeout(timeout)
@@ -55,4 +49,8 @@ export default class MasonrySimple {
55
49
  timeout = setTimeout(later, wait)
56
50
  }
57
51
  }
52
+
53
+ debouncedResize = () => {
54
+ this.resizeAllItems()
55
+ }
58
56
  }