|
11 | 11 | * @example
|
12 | 12 | * import {sameheight} from '@carbon/ibmdotcom-utilities';
|
13 | 13 | *
|
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')); |
15 | 18 | *
|
16 | 19 | * @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 |
17 | 22 | */
|
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; |
38 | 43 | }
|
39 | 44 | }
|
40 | 45 |
|
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 | + } |
46 | 60 | }
|
47 | 61 |
|
48 | 62 | export default sameheight;
|
0 commit comments