masonry-simple 0.0.2 → 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">500B 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,14 +31,19 @@ import MasonrySimple from 'masonry-simple'
31
31
 
32
32
  ### Usage
33
33
  ```javascript
34
- const masonry = new MasonrySimple('.grid')
34
+ const masonry = new MasonrySimple()
35
+
36
+ masonry.options({
37
+ container: '.masonry',
38
+ timeout: 200
39
+ })
35
40
  ```
36
41
  ```HTML
37
- <div class="grid">
38
- <div class="grid__item">
42
+ <div class="masonry">
43
+ <div class="masonry__item">
39
44
  ...
40
45
  </div>
41
- <div class="grid__item">
46
+ <div class="masonry__item">
42
47
  ...
43
48
  </div>
44
49
  ...
@@ -46,5 +51,12 @@ const masonry = new MasonrySimple('.grid')
46
51
  ```
47
52
  <br>
48
53
 
54
+ ### Settings
55
+ | Arg | Default | Description |
56
+ | --- | --- | --- |
57
+ | `container` | `'.masonry'` | The HTML class of the element inside which the grid elements will be located. |
58
+ | `debounce` | `200` | Sets the delay when resizing the browser window. `0` &mdash; disables the delay and allows the grid to rebuild faster. |
59
+ <br>
60
+
49
61
  ### License
50
62
  <sup>masonry-simple is released under MIT license</sup>
package/dist/index.js CHANGED
@@ -9,35 +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(container){
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;
13
21
  this.grid = container instanceof HTMLElement ? container : document.querySelector(container);
14
- this.gridItems = [
15
- ...this.grid.children
16
- ];
17
- this.resizeObserver = new ResizeObserver(()=>{
18
- this.resizeAllItems();
19
- });
20
- this.resizeObserver.observe(this.grid);
21
- window.onload = this.resizeAllItems.bind(this);
22
- window.addEventListener("beforeunload", ()=>{
23
- this.resizeObserver.unobserve(this.grid);
24
- });
22
+ this.gridItems = Array.from(this.grid.children);
25
23
  this.grid.style.contain = "layout";
26
- this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), 10);
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();
27
28
  }
28
- resizeItem(item) {
29
- const rowHeight = 1;
30
- const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
29
+ resizeItem(item, rowHeight, rowGap) {
31
30
  const rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap));
32
31
  item.style.gridRowEnd = "span " + rowSpan;
33
32
  }
34
33
  resizeAllItems() {
34
+ const rowHeight = 1;
35
+ const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
35
36
  this.grid.style.alignItems = "start";
36
- this.gridItems.forEach((item)=>this.resizeItem(item));
37
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
37
38
  this.grid.style.alignItems = "stretch";
38
- window.requestAnimationFrame(()=>{
39
- this.gridItems.forEach((item)=>this.resizeItem(item));
40
- });
39
+ setTimeout(()=>{
40
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
41
+ }, 0);
41
42
  }
42
43
  debounce(func, wait) {
43
44
  let timeout;
@@ -50,6 +51,9 @@ class $4fa36e821943b400$export$2e2bcd8739ae039 {
50
51
  timeout = setTimeout(later, wait);
51
52
  };
52
53
  }
54
+ debouncedResize = ()=>{
55
+ this.resizeAllItems();
56
+ };
53
57
  }
54
58
 
55
59
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;AAAe;IACd,YAAY,SAAS,CAAE;QACtB,IAAI,CAAC,IAAI,GAAG,qBAAqB,cAAc,YAAY,SAAS,aAAa,CAAC,UAAU;QAC5F,IAAI,CAAC,SAAS,GAAG;eAAI,IAAI,CAAC,IAAI,CAAC,QAAQ;SAAC;QACxC,IAAI,CAAC,cAAc,GAAG,IAAI,eAAe,IAAM;YAC9C,IAAI,CAAC,cAAc;QACpB;QACA,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;QAErC,OAAO,MAAM,GAAG,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI;QAC7C,OAAO,gBAAgB,CAAC,gBAAgB,IAAM;YAC7C,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI;QACxC;QAEA,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG;QAE1B,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,GAAG;IACrE;IAEA,WAAW,IAAI,EAAE;QAChB,MAAM,YAAY;QAClB,MAAM,SAAS,SAAS,OAAO,gBAAgB,CAAC,IAAI,CAAC,IAAI,EAAE,gBAAgB,CAAC;QAC5E,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,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG;QAC7B,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA,OAAQ,IAAI,CAAC,UAAU,CAAC;QAC/C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG;QAC7B,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;QACJ,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(container) {\r\n\t\tthis.grid = container instanceof HTMLElement ? container : document.querySelector(container)\r\n\t\tthis.gridItems = [...this.grid.children]\r\n\t\tthis.resizeObserver = new ResizeObserver(() => {\r\n\t\t\tthis.resizeAllItems()\r\n\t\t})\r\n\t\tthis.resizeObserver.observe(this.grid)\r\n\r\n\t\twindow.onload = this.resizeAllItems.bind(this)\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), 10)\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\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\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\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,33 +1,34 @@
1
1
  class $cf838c15c8b009ba$export$2e2bcd8739ae039 {
2
- constructor(container){
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;
3
11
  this.grid = container instanceof HTMLElement ? container : document.querySelector(container);
4
- this.gridItems = [
5
- ...this.grid.children
6
- ];
7
- this.resizeObserver = new ResizeObserver(()=>{
8
- this.resizeAllItems();
9
- });
10
- this.resizeObserver.observe(this.grid);
11
- window.onload = this.resizeAllItems.bind(this);
12
- window.addEventListener("beforeunload", ()=>{
13
- this.resizeObserver.unobserve(this.grid);
14
- });
12
+ this.gridItems = Array.from(this.grid.children);
15
13
  this.grid.style.contain = "layout";
16
- this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), 10);
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();
17
18
  }
18
- resizeItem(item) {
19
- const rowHeight = 1;
20
- const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
19
+ resizeItem(item, rowHeight, rowGap) {
21
20
  const rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap));
22
21
  item.style.gridRowEnd = "span " + rowSpan;
23
22
  }
24
23
  resizeAllItems() {
24
+ const rowHeight = 1;
25
+ const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
25
26
  this.grid.style.alignItems = "start";
26
- this.gridItems.forEach((item)=>this.resizeItem(item));
27
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
27
28
  this.grid.style.alignItems = "stretch";
28
- window.requestAnimationFrame(()=>{
29
- this.gridItems.forEach((item)=>this.resizeItem(item));
30
- });
29
+ setTimeout(()=>{
30
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
31
+ }, 0);
31
32
  }
32
33
  debounce(func, wait) {
33
34
  let timeout;
@@ -40,6 +41,9 @@ class $cf838c15c8b009ba$export$2e2bcd8739ae039 {
40
41
  timeout = setTimeout(later, wait);
41
42
  };
42
43
  }
44
+ debouncedResize = ()=>{
45
+ this.resizeAllItems();
46
+ };
43
47
  }
44
48
 
45
49
 
@@ -1 +1 @@
1
- {"mappings":"AAAe;IACd,YAAY,SAAS,CAAE;QACtB,IAAI,CAAC,IAAI,GAAG,qBAAqB,cAAc,YAAY,SAAS,aAAa,CAAC,UAAU;QAC5F,IAAI,CAAC,SAAS,GAAG;eAAI,IAAI,CAAC,IAAI,CAAC,QAAQ;SAAC;QACxC,IAAI,CAAC,cAAc,GAAG,IAAI,eAAe,IAAM;YAC9C,IAAI,CAAC,cAAc;QACpB;QACA,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;QAErC,OAAO,MAAM,GAAG,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI;QAC7C,OAAO,gBAAgB,CAAC,gBAAgB,IAAM;YAC7C,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI;QACxC;QAEA,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG;QAE1B,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,GAAG;IACrE;IAEA,WAAW,IAAI,EAAE;QAChB,MAAM,YAAY;QAClB,MAAM,SAAS,SAAS,OAAO,gBAAgB,CAAC,IAAI,CAAC,IAAI,EAAE,gBAAgB,CAAC;QAC5E,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,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG;QAC7B,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA,OAAQ,IAAI,CAAC,UAAU,CAAC;QAC/C,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG;QAC7B,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;QACJ,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(container) {\r\n\t\tthis.grid = container instanceof HTMLElement ? container : document.querySelector(container)\r\n\t\tthis.gridItems = [...this.grid.children]\r\n\t\tthis.resizeObserver = new ResizeObserver(() => {\r\n\t\t\tthis.resizeAllItems()\r\n\t\t})\r\n\t\tthis.resizeObserver.observe(this.grid)\r\n\r\n\t\twindow.onload = this.resizeAllItems.bind(this)\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), 10)\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\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\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\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.2",
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,36 +1,41 @@
1
1
  export default class MasonrySimple {
2
- constructor(container) {
3
- this.grid = container instanceof HTMLElement ? container : document.querySelector(container)
4
- this.gridItems = [...this.grid.children]
5
- this.resizeObserver = new ResizeObserver(() => {
6
- this.resizeAllItems()
7
- })
8
- this.resizeObserver.observe(this.grid)
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
9
 
10
- window.onload = this.resizeAllItems.bind(this)
11
- window.addEventListener('beforeunload', () => {
12
- this.resizeObserver.unobserve(this.grid)
13
- })
10
+ options(options = {}) {
11
+ const {
12
+ container = '.masonry',
13
+ timeout = this.timeout
14
+ } = options
14
15
 
16
+ this.grid = container instanceof HTMLElement ? container : document.querySelector(container)
17
+ this.gridItems = Array.from(this.grid.children)
15
18
  this.grid.style.contain = 'layout'
16
-
17
- this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), 10)
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()
18
23
  }
19
24
 
20
- resizeItem(item) {
21
- const rowHeight = 1
22
- const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue('grid-row-gap'))
25
+ resizeItem(item, rowHeight, rowGap) {
23
26
  const rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap))
24
27
  item.style.gridRowEnd = 'span ' + rowSpan
25
28
  }
26
29
 
27
30
  resizeAllItems() {
31
+ const rowHeight = 1
32
+ const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue('grid-row-gap'))
28
33
  this.grid.style.alignItems = 'start'
29
- this.gridItems.forEach(item => this.resizeItem(item))
34
+ this.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))
30
35
  this.grid.style.alignItems = 'stretch'
31
- window.requestAnimationFrame(() => {
32
- this.gridItems.forEach(item => this.resizeItem(item))
33
- })
36
+ setTimeout(() => {
37
+ this.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))
38
+ }, 0)
34
39
  }
35
40
 
36
41
  debounce(func, wait) {
@@ -44,4 +49,8 @@ export default class MasonrySimple {
44
49
  timeout = setTimeout(later, wait)
45
50
  }
46
51
  }
52
+
53
+ debouncedResize = () => {
54
+ this.resizeAllItems()
55
+ }
47
56
  }