matchheight 1.1.2 → 1.2.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.
@@ -11,19 +11,20 @@
11
11
  })(this, (function () { 'use strict';
12
12
 
13
13
  function throttle(fn, threshold) {
14
- let last, deferTimer;
15
- return function () {
14
+ let last;
15
+ let deferTimer;
16
+ return function (...args) {
16
17
  const now = Date.now();
17
- if (last && now < last + threshold) {
18
+ if (last !== undefined && now < last + threshold) {
18
19
  clearTimeout(deferTimer);
19
- deferTimer = setTimeout(function () {
20
+ deferTimer = setTimeout(() => {
20
21
  last = now;
21
- fn();
22
+ fn(...args);
22
23
  }, threshold);
23
24
  }
24
25
  else {
25
26
  last = now;
26
- fn();
27
+ fn(...args);
27
28
  }
28
29
  };
29
30
  }
@@ -38,7 +39,7 @@
38
39
  if (document.readyState === 'loading') {
39
40
  document.addEventListener('DOMContentLoaded', update, { once: true });
40
41
  }
41
- if (document.readyState === 'interactive') {
42
+ else if (document.readyState === 'interactive') {
42
43
  document.addEventListener('load', update, { once: true });
43
44
  }
44
45
  else {
@@ -77,16 +78,16 @@
77
78
  const maxHeightInRow = Math.max(...processingTargets.map((item) => item.height));
78
79
  processingTargets.forEach((item) => {
79
80
  const error = processingTop - item.top + errorThreshold;
80
- const getPropertyValue = (value) => window.getComputedStyle(item.el).getPropertyValue(value);
81
- const isBorderBox = getPropertyValue('box-sizing') === 'border-box';
81
+ const styles = window.getComputedStyle(item.el);
82
+ const isBorderBox = styles.boxSizing === 'border-box';
82
83
  if (isBorderBox) {
83
84
  item.el.style.minHeight = `${maxHeightInRow + error}px`;
84
85
  }
85
86
  else {
86
- const paddingAndBorder = parseFloat(getPropertyValue('padding-top')) +
87
- parseFloat(getPropertyValue('padding-bottom')) +
88
- parseFloat(getPropertyValue('border-top-width')) +
89
- parseFloat(getPropertyValue('border-bottom-width'));
87
+ const paddingAndBorder = (parseFloat(styles.paddingTop) || 0) +
88
+ (parseFloat(styles.paddingBottom) || 0) +
89
+ (parseFloat(styles.borderTopWidth) || 0) +
90
+ (parseFloat(styles.borderBottomWidth) || 0);
90
91
  item.el.style.minHeight = `${maxHeightInRow - paddingAndBorder + error}px`;
91
92
  }
92
93
  });
@@ -4,4 +4,4 @@
4
4
  * https://github.com/yomotsu/MatchHeight
5
5
  * Released under the MIT License.
6
6
  */
7
- (function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.MatchHeight=factory())})(this,(function(){"use strict";function throttle(fn,threshold){let last,deferTimer;return function(){const now=Date.now();if(last&&now<last+threshold){clearTimeout(deferTimer);deferTimer=setTimeout((function(){last=now;fn()}),threshold)}else{last=now;fn()}}}const errorThreshold=1;class MatchHeight{constructor(selector="[data-mh]"){this._remains=[];this._selector=selector;const update=this.update.bind(this);const throttledUpdate=throttle(update,200);if(document.readyState==="loading"){document.addEventListener("DOMContentLoaded",update,{once:true})}if(document.readyState==="interactive"){document.addEventListener("load",update,{once:true})}else{update()}window.addEventListener("resize",throttledUpdate);this.disconnect=()=>{window.removeEventListener("resize",throttledUpdate)}}update(){const elements=document.querySelectorAll(this._selector);if(elements.length===0)return;this._remains=Array.prototype.map.call(elements,(el=>({el:el,top:0,height:0})));this._remains.forEach((item=>{item.el.style.minHeight=""}));this._process()}_process(){this._remains.forEach((item=>{const bb=item.el.getBoundingClientRect();item.top=bb.top;item.height=bb.height}));this._remains.sort(((a,b)=>a.top-b.top));const processingTop=this._remains[0].top;const processingTargets=this._remains.filter((item=>Math.abs(item.top-processingTop)<=errorThreshold));const maxHeightInRow=Math.max(...processingTargets.map((item=>item.height)));processingTargets.forEach((item=>{const error=processingTop-item.top+errorThreshold;const getPropertyValue=value=>window.getComputedStyle(item.el).getPropertyValue(value);const isBorderBox=getPropertyValue("box-sizing")==="border-box";if(isBorderBox){item.el.style.minHeight=`${maxHeightInRow+error}px`}else{const paddingAndBorder=parseFloat(getPropertyValue("padding-top"))+parseFloat(getPropertyValue("padding-bottom"))+parseFloat(getPropertyValue("border-top-width"))+parseFloat(getPropertyValue("border-bottom-width"));item.el.style.minHeight=`${maxHeightInRow-paddingAndBorder+error}px`}}));this._remains.splice(0,processingTargets.length);if(0<this._remains.length)this._process()}}return MatchHeight}));
7
+ (function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.MatchHeight=factory())})(this,function(){"use strict";function throttle(fn,threshold){let last;let deferTimer;return function(...args){const now=Date.now();if(last!==undefined&&now<last+threshold){clearTimeout(deferTimer);deferTimer=setTimeout(()=>{last=now;fn(...args)},threshold)}else{last=now;fn(...args)}}}const errorThreshold=1;class MatchHeight{constructor(selector="[data-mh]"){this._remains=[];this._selector=selector;const update=this.update.bind(this);const throttledUpdate=throttle(update,200);if(document.readyState==="loading"){document.addEventListener("DOMContentLoaded",update,{once:true})}else if(document.readyState==="interactive"){document.addEventListener("load",update,{once:true})}else{update()}window.addEventListener("resize",throttledUpdate);this.disconnect=()=>{window.removeEventListener("resize",throttledUpdate)}}update(){const elements=document.querySelectorAll(this._selector);if(elements.length===0)return;this._remains=Array.prototype.map.call(elements,el=>({el:el,top:0,height:0}));this._remains.forEach(item=>{item.el.style.minHeight=""});this._process()}_process(){this._remains.forEach(item=>{const bb=item.el.getBoundingClientRect();item.top=bb.top;item.height=bb.height});this._remains.sort((a,b)=>a.top-b.top);const processingTop=this._remains[0].top;const processingTargets=this._remains.filter(item=>Math.abs(item.top-processingTop)<=errorThreshold);const maxHeightInRow=Math.max(...processingTargets.map(item=>item.height));processingTargets.forEach(item=>{const error=processingTop-item.top+errorThreshold;const styles=window.getComputedStyle(item.el);const isBorderBox=styles.boxSizing==="border-box";if(isBorderBox){item.el.style.minHeight=`${maxHeightInRow+error}px`}else{const paddingAndBorder=(parseFloat(styles.paddingTop)||0)+(parseFloat(styles.paddingBottom)||0)+(parseFloat(styles.borderTopWidth)||0)+(parseFloat(styles.borderBottomWidth)||0);item.el.style.minHeight=`${maxHeightInRow-paddingAndBorder+error}px`}});this._remains.splice(0,processingTargets.length);if(0<this._remains.length)this._process()}}return MatchHeight});
@@ -5,19 +5,20 @@
5
5
  * Released under the MIT License.
6
6
  */
7
7
  function throttle(fn, threshold) {
8
- let last, deferTimer;
9
- return function () {
8
+ let last;
9
+ let deferTimer;
10
+ return function (...args) {
10
11
  const now = Date.now();
11
- if (last && now < last + threshold) {
12
+ if (last !== undefined && now < last + threshold) {
12
13
  clearTimeout(deferTimer);
13
- deferTimer = setTimeout(function () {
14
+ deferTimer = setTimeout(() => {
14
15
  last = now;
15
- fn();
16
+ fn(...args);
16
17
  }, threshold);
17
18
  }
18
19
  else {
19
20
  last = now;
20
- fn();
21
+ fn(...args);
21
22
  }
22
23
  };
23
24
  }
@@ -32,7 +33,7 @@ class MatchHeight {
32
33
  if (document.readyState === 'loading') {
33
34
  document.addEventListener('DOMContentLoaded', update, { once: true });
34
35
  }
35
- if (document.readyState === 'interactive') {
36
+ else if (document.readyState === 'interactive') {
36
37
  document.addEventListener('load', update, { once: true });
37
38
  }
38
39
  else {
@@ -71,16 +72,16 @@ class MatchHeight {
71
72
  const maxHeightInRow = Math.max(...processingTargets.map((item) => item.height));
72
73
  processingTargets.forEach((item) => {
73
74
  const error = processingTop - item.top + errorThreshold;
74
- const getPropertyValue = (value) => window.getComputedStyle(item.el).getPropertyValue(value);
75
- const isBorderBox = getPropertyValue('box-sizing') === 'border-box';
75
+ const styles = window.getComputedStyle(item.el);
76
+ const isBorderBox = styles.boxSizing === 'border-box';
76
77
  if (isBorderBox) {
77
78
  item.el.style.minHeight = `${maxHeightInRow + error}px`;
78
79
  }
79
80
  else {
80
- const paddingAndBorder = parseFloat(getPropertyValue('padding-top')) +
81
- parseFloat(getPropertyValue('padding-bottom')) +
82
- parseFloat(getPropertyValue('border-top-width')) +
83
- parseFloat(getPropertyValue('border-bottom-width'));
81
+ const paddingAndBorder = (parseFloat(styles.paddingTop) || 0) +
82
+ (parseFloat(styles.paddingBottom) || 0) +
83
+ (parseFloat(styles.borderTopWidth) || 0) +
84
+ (parseFloat(styles.borderBottomWidth) || 0);
84
85
  item.el.style.minHeight = `${maxHeightInRow - paddingAndBorder + error}px`;
85
86
  }
86
87
  });
@@ -1,2 +1 @@
1
- declare function throttle(fn: () => void, threshold: number): () => void;
2
- export default throttle;
1
+ export default function throttle<T extends (...args: any[]) => void>(fn: T, threshold: number): (...args: Parameters<T>) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "matchheight",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "author": "Yomotsu",
5
5
  "license": "MIT",
6
6
  "repository": "yomotsu/matchheight",
@@ -9,11 +9,11 @@
9
9
  "module": "dist/match-height.module.js",
10
10
  "types": "dist/match-height.d.ts",
11
11
  "devDependencies": {
12
- "@rollup/plugin-typescript": "^11.1.6",
13
- "rollup": "^4.14.3",
14
- "terser": "^5.30.3",
15
- "tslib": "^2.6.2",
16
- "typescript": "^5.4.5"
12
+ "@rollup/plugin-typescript": "^12.3.0",
13
+ "rollup": "^4.54.0",
14
+ "terser": "^5.44.1",
15
+ "tslib": "^2.8.1",
16
+ "typescript": "^5.9.3"
17
17
  },
18
18
  "scripts": {
19
19
  "dev": "rollup --config --watch",
package/rollup.config.mjs CHANGED
@@ -1,7 +1,9 @@
1
- import pkg from './package.json' assert { type: "json" };
1
+ import { readFileSync } from 'fs';
2
2
  import typescript from 'typescript';
3
3
  import rollupTypescript from '@rollup/plugin-typescript';
4
4
 
5
+ const pkg = JSON.parse( readFileSync( './package.json', 'utf-8' ) );
6
+
5
7
  const license = `/*!
6
8
  * @author yomotsu
7
9
  * MatchHeight
@@ -25,7 +25,7 @@ class MatchHeight {
25
25
 
26
26
  document.addEventListener( 'DOMContentLoaded', update, { once: true } );
27
27
 
28
- } if ( document.readyState === 'interactive' ) {
28
+ } else if ( document.readyState === 'interactive' ) {
29
29
 
30
30
  document.addEventListener( 'load', update, { once: true } );
31
31
 
@@ -91,23 +91,22 @@ class MatchHeight {
91
91
  processingTargets.forEach( ( item ) => {
92
92
 
93
93
  const error = processingTop - item.top + errorThreshold;
94
- const getPropertyValue = ( value: string ) => window.getComputedStyle( item.el ).getPropertyValue( value );
95
- const isBorderBox = getPropertyValue( 'box-sizing' ) === 'border-box';
94
+ const styles = window.getComputedStyle( item.el );
95
+ const isBorderBox = styles.boxSizing === 'border-box';
96
96
 
97
97
  if ( isBorderBox ) {
98
98
 
99
- item.el.style.minHeight = `${ maxHeightInRow + error }px`;
99
+ item.el.style.minHeight = `${ maxHeightInRow + error }px`;
100
100
 
101
101
  } else {
102
-
103
102
  const paddingAndBorder =
104
- parseFloat( getPropertyValue( 'padding-top' ) ) +
105
- parseFloat( getPropertyValue( 'padding-bottom' ) ) +
106
- parseFloat( getPropertyValue( 'border-top-width' ) ) +
107
- parseFloat( getPropertyValue( 'border-bottom-width' ) );
103
+ ( parseFloat( styles.paddingTop ) || 0 ) +
104
+ ( parseFloat( styles.paddingBottom ) || 0 ) +
105
+ ( parseFloat( styles.borderTopWidth ) || 0 ) +
106
+ ( parseFloat( styles.borderBottomWidth ) || 0 );
108
107
  item.el.style.minHeight = `${ maxHeightInRow - paddingAndBorder + error }px`;
109
-
110
108
  }
109
+
111
110
  } );
112
111
 
113
112
  this._remains.splice( 0, processingTargets.length );
package/src/throttle.ts CHANGED
@@ -1,30 +1,27 @@
1
- function throttle( fn: () => void, threshold: number ) {
1
+ export default function throttle<T extends (...args: any[]) => void>( fn: T, threshold: number ) {
2
2
 
3
- let last: number, deferTimer: number;
3
+ let last: number | undefined;
4
+ let deferTimer: ReturnType<typeof setTimeout> | undefined;
4
5
 
5
- return function () {
6
+ return function ( ...args: Parameters<T> ) {
6
7
 
7
8
  const now = Date.now();
8
9
 
9
- if ( last && now < last + threshold ) {
10
+ if ( last !== undefined && now < last + threshold ) {
10
11
 
11
12
  clearTimeout( deferTimer );
12
- deferTimer = setTimeout( function () {
13
-
13
+ deferTimer = setTimeout( () => {
14
14
  last = now;
15
- fn();
16
-
15
+ fn( ...args );
17
16
  }, threshold );
18
17
 
19
18
  } else {
20
19
 
21
20
  last = now;
22
- fn();
21
+ fn( ...args );
23
22
 
24
23
  }
25
24
 
26
25
  };
27
26
 
28
27
  }
29
-
30
- export default throttle;