masonry-simple 0.0.3 → 1.1.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">600B 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,47 +9,31 @@ $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.resizeObserver = null;
16
+ }
17
+ options(options = {}) {
18
+ const { container: container = ".masonry" } = options;
14
19
  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
- });
20
+ this.gridItems = Array.from(this.grid.children);
26
21
  this.grid.style.contain = "layout";
27
- this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), debounce);
22
+ this.resizeObserver = new ResizeObserver(this.resizeAllItems.bind(this));
23
+ this.resizeObserver.observe(this.grid);
24
+ this.resizeAllItems();
28
25
  }
29
- resizeItem(item) {
30
- const rowHeight = 1;
31
- const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
26
+ resizeItem(item, rowHeight, rowGap) {
32
27
  const rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap));
33
28
  item.style.gridRowEnd = "span " + rowSpan;
34
29
  }
35
30
  resizeAllItems() {
31
+ const rowHeight = 1;
32
+ const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
36
33
  this.grid.style.alignItems = "start";
37
- this.gridItems.forEach((item)=>this.resizeItem(item));
34
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
38
35
  this.grid.style.alignItems = "stretch";
39
- window.requestAnimationFrame(()=>{
40
- this.gridItems.forEach((item)=>this.resizeItem(item));
41
- });
42
- }
43
- debounce(func, wait) {
44
- let timeout;
45
- return function executedFunction(...args) {
46
- const later = ()=>{
47
- clearTimeout(timeout);
48
- func(...args);
49
- };
50
- clearTimeout(timeout);
51
- timeout = setTimeout(later, wait);
52
- };
36
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
53
37
  }
54
38
  }
55
39
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;AAAe;IACd,YAAY,UAAU,CAAC,CAAC,CAAE;QACzB,MAAM,aACL,YAAY,uBACZ,WAAW,MACX,GAAG;QAEJ,IAAI,CAAC,IAAI,GAAG,qBAAqB,cAAc,YAAY,SAAS,aAAa,CAAC,UAAU;QAC5F,IAAI,CAAC,SAAS,GAAG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AAQ,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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G;QAC7B,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA,OAAQ,IAAI,CAAC,UAAU,CAAC,MAAM,WAAW;IACjE;AACD","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.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} = 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.resizeAllItems.bind(this))\r\n\t\tthis.resizeObserver.observe(this.grid)\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\tthis.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))\r\n\t}\r\n}"],"names":[],"version":3,"file":"index.js.map"}
@@ -1,45 +1,29 @@
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.resizeObserver = null;
6
+ }
7
+ options(options = {}) {
8
+ const { container: container = ".masonry" } = options;
4
9
  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
- });
10
+ this.gridItems = Array.from(this.grid.children);
16
11
  this.grid.style.contain = "layout";
17
- this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), debounce);
12
+ this.resizeObserver = new ResizeObserver(this.resizeAllItems.bind(this));
13
+ this.resizeObserver.observe(this.grid);
14
+ this.resizeAllItems();
18
15
  }
19
- resizeItem(item) {
20
- const rowHeight = 1;
21
- const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
16
+ resizeItem(item, rowHeight, rowGap) {
22
17
  const rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap));
23
18
  item.style.gridRowEnd = "span " + rowSpan;
24
19
  }
25
20
  resizeAllItems() {
21
+ const rowHeight = 1;
22
+ const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue("grid-row-gap"));
26
23
  this.grid.style.alignItems = "start";
27
- this.gridItems.forEach((item)=>this.resizeItem(item));
24
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
28
25
  this.grid.style.alignItems = "stretch";
29
- window.requestAnimationFrame(()=>{
30
- this.gridItems.forEach((item)=>this.resizeItem(item));
31
- });
32
- }
33
- debounce(func, wait) {
34
- let timeout;
35
- return function executedFunction(...args) {
36
- const later = ()=>{
37
- clearTimeout(timeout);
38
- func(...args);
39
- };
40
- clearTimeout(timeout);
41
- timeout = setTimeout(later, wait);
42
- };
26
+ this.gridItems.forEach((item)=>this.resizeItem(item, rowHeight, rowGap));
43
27
  }
44
28
  }
45
29
 
@@ -1 +1 @@
1
- {"mappings":"AAAe;IACd,YAAY,UAAU,CAAC,CAAC,CAAE;QACz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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,cAAc,GAAG,IAAI;IAC3B;IAEA,QAAQ,UAAU,CAAC,CAAC,EAAE;QACrB,MAAM,aACL,YAAY,aACZ,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,cAAc,CAAC,IAAI,CAAC,IAAI;QACtE,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI;QACrC,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,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAA,OAAQ,IAAI,CAAC,UAAU,CAAC,MAAM,WAAW;IACjE;AACD","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.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} = 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.resizeAllItems.bind(this))\r\n\t\tthis.resizeObserver.observe(this.grid)\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\tthis.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))\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.1.0",
4
4
  "description": "Responsive masonry grid",
5
5
  "author": "ux-ui.pro",
6
6
  "license": "MIT",
package/src/index.js CHANGED
@@ -1,58 +1,34 @@
1
1
  export default class MasonrySimple {
2
- constructor(options = {}) {
2
+ constructor() {
3
+ this.grid = null
4
+ this.gridItems = []
5
+ this.resizeObserver = null
6
+ }
7
+
8
+ options(options = {}) {
3
9
  const {
4
10
  container = '.masonry',
5
- debounce = 200
6
11
  } = options
7
12
 
8
13
  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
-
14
+ this.gridItems = Array.from(this.grid.children)
23
15
  this.grid.style.contain = 'layout'
24
-
25
- this.resizeAllItems = this.debounce(this.resizeAllItems.bind(this), debounce)
16
+ this.resizeObserver = new ResizeObserver(this.resizeAllItems.bind(this))
17
+ this.resizeObserver.observe(this.grid)
18
+ this.resizeAllItems()
26
19
  }
27
20
 
28
- resizeItem(item) {
29
- const rowHeight = 1
30
- const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue('grid-row-gap'))
21
+ resizeItem(item, rowHeight, rowGap) {
31
22
  const rowSpan = Math.ceil((item.clientHeight + rowGap) / (rowHeight + rowGap))
32
-
33
23
  item.style.gridRowEnd = 'span ' + rowSpan
34
24
  }
35
25
 
36
26
  resizeAllItems() {
27
+ const rowHeight = 1
28
+ const rowGap = parseInt(window.getComputedStyle(this.grid).getPropertyValue('grid-row-gap'))
37
29
  this.grid.style.alignItems = 'start'
38
- this.gridItems.forEach(item => this.resizeItem(item))
30
+ this.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))
39
31
  this.grid.style.alignItems = 'stretch'
40
-
41
- window.requestAnimationFrame(() => {
42
- this.gridItems.forEach(item => this.resizeItem(item))
43
- })
44
- }
45
-
46
- debounce(func, wait) {
47
- let timeout
48
-
49
- return function executedFunction(...args) {
50
- const later = () => {
51
- clearTimeout(timeout)
52
- func(...args)
53
- }
54
- clearTimeout(timeout)
55
- timeout = setTimeout(later, wait)
56
- }
32
+ this.gridItems.forEach(item => this.resizeItem(item, rowHeight, rowGap))
57
33
  }
58
34
  }