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.
- package/dist/match-height.js +14 -13
- package/dist/match-height.min.js +1 -1
- package/dist/match-height.module.js +14 -13
- package/dist/throttle.d.ts +1 -2
- package/package.json +6 -6
- package/rollup.config.mjs +3 -1
- package/src/match-height.ts +9 -10
- package/src/throttle.ts +8 -11
package/dist/match-height.js
CHANGED
|
@@ -11,19 +11,20 @@
|
|
|
11
11
|
})(this, (function () { 'use strict';
|
|
12
12
|
|
|
13
13
|
function throttle(fn, threshold) {
|
|
14
|
-
let last
|
|
15
|
-
|
|
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(
|
|
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
|
|
81
|
-
const isBorderBox =
|
|
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(
|
|
87
|
-
parseFloat(
|
|
88
|
-
parseFloat(
|
|
89
|
-
parseFloat(
|
|
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
|
});
|
package/dist/match-height.min.js
CHANGED
|
@@ -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,
|
|
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
|
|
9
|
-
|
|
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(
|
|
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
|
|
75
|
-
const isBorderBox =
|
|
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(
|
|
81
|
-
parseFloat(
|
|
82
|
-
parseFloat(
|
|
83
|
-
parseFloat(
|
|
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
|
});
|
package/dist/throttle.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
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.
|
|
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": "^
|
|
13
|
-
"rollup": "^4.
|
|
14
|
-
"terser": "^5.
|
|
15
|
-
"tslib": "^2.
|
|
16
|
-
"typescript": "^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
|
|
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
|
package/src/match-height.ts
CHANGED
|
@@ -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
|
|
95
|
-
const isBorderBox =
|
|
94
|
+
const styles = window.getComputedStyle( item.el );
|
|
95
|
+
const isBorderBox = styles.boxSizing === 'border-box';
|
|
96
96
|
|
|
97
97
|
if ( isBorderBox ) {
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
item.el.style.minHeight = `${ maxHeightInRow + error }px`;
|
|
100
100
|
|
|
101
101
|
} else {
|
|
102
|
-
|
|
103
102
|
const paddingAndBorder =
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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(
|
|
1
|
+
export default function throttle<T extends (...args: any[]) => void>( fn: T, threshold: number ) {
|
|
2
2
|
|
|
3
|
-
let last: 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(
|
|
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;
|