Skip to content

Commit 93d7830

Browse files
committed
feat(utility): added minSize parameter, documentation updated
1 parent a22b53c commit 93d7830

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

packages/utilities/src/utilities/sameheight/sameheight.js

+40-26
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,52 @@
1111
* @example
1212
* import {sameheight} from '@carbon/ibmdotcom-utilities';
1313
*
14-
* sameheight(ElementArray);
14+
* sameheight(ElementArray, 'md');
15+
*
16+
* if you want the utility to refresh the sizes as you resize the screen, consideer using a listener:
17+
* window.addEventListener('resize', sameheight(ElementArray, 'md'));
1518
*
1619
* @param {Array} elemCollection Html objects array
20+
* @param {string} minSize Minimum size for the utility to be activated, empty for small,
21+
* md for medium, lg for large, xlg for xlarge, max for maximum
1722
*/
18-
function sameheight(elemCollection) {
19-
/**
20-
* Internal function made for avoiding adding more eventlisteners
21-
*
22-
* @param {Array} elemArr array of elements to be alligned
23-
*/
24-
function setHeight(elemArr) {
25-
if (window.innerWidth > 671) {
26-
let targetHeight = 0;
27-
elemArr.forEach(elem => {
28-
elem.offsetHeight > targetHeight
29-
? (targetHeight = elem.offsetHeight)
30-
: false;
31-
});
32-
33-
elemArr.forEach(elem => {
34-
elem.offsetHeight == targetHeight
35-
? (elem.style.height = 'auto')
36-
: (elem.style.height = targetHeight + 'px');
37-
});
23+
function sameheight(elemCollection, minSize) {
24+
const elemArr = Array.prototype.slice.call(elemCollection);
25+
let targetWidth;
26+
if (minSize) {
27+
switch (minSize) {
28+
case 'md':
29+
targetWidth = 671;
30+
break;
31+
case 'lg':
32+
targetWidth = 1055;
33+
break;
34+
case 'xlg':
35+
targetWidth = 1311;
36+
break;
37+
case 'max':
38+
targetWidth = 1583;
39+
break;
40+
default:
41+
targetWidth = 0;
42+
break;
3843
}
3944
}
4045

41-
const elemArr = Array.prototype.slice.call(elemCollection);
42-
setHeight(elemArr);
43-
window.addEventListener('resize', function() {
44-
setHeight(elemArr);
45-
});
46+
if (window.innerWidth > targetWidth) {
47+
let targetHeight = 0;
48+
elemArr.forEach(elem => {
49+
elem.offsetHeight > targetHeight
50+
? (targetHeight = elem.offsetHeight)
51+
: false;
52+
});
53+
54+
elemArr.forEach(elem => {
55+
elem.offsetHeight == targetHeight
56+
? (elem.style.height = 'auto')
57+
: (elem.style.height = targetHeight + 'px');
58+
});
59+
}
4660
}
4761

4862
export default sameheight;

0 commit comments

Comments
 (0)