@straylightagency/utils 1.0.6 → 1.0.7
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/package.json +1 -1
- package/src/dom/getElementSizing.js +39 -0
- package/src/dom/index.js +1 -0
package/package.json
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param element {HTMLElement}
|
|
3
|
+
* @returns {DOMRect}
|
|
4
|
+
*/
|
|
5
|
+
export default function getElementSizing(element) {
|
|
6
|
+
const el_style = window.getComputedStyle( element );
|
|
7
|
+
const el_display = el_style.display;
|
|
8
|
+
const el_position = el_style.position;
|
|
9
|
+
const el_visibility = el_style.visibility;
|
|
10
|
+
const el_max_height = el_style.maxHeight.replace('px', '').replace('%', '');
|
|
11
|
+
let width, height;
|
|
12
|
+
|
|
13
|
+
/** if it's not hidden we just return normal height */
|
|
14
|
+
if ( el_display !== 'none' && el_max_height !== '0' ) {
|
|
15
|
+
width = element.offsetWidth + parseInt( el_style.marginLeft, 10 ) + parseInt( el_style.marginRight, 10 );
|
|
16
|
+
height = element.offsetHeight + parseInt( el_style.marginTop, 10 ) + parseInt( el_style.marginBottom, 10 );
|
|
17
|
+
|
|
18
|
+
const rect = element.getBoundingClientRect();
|
|
19
|
+
|
|
20
|
+
return Object.assign( rect, { width, height } );
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** the element is hidden, so we are making the element block, so we can measure its height but still be hidden */
|
|
24
|
+
element.style.position = 'absolute';
|
|
25
|
+
element.style.visibility = 'hidden';
|
|
26
|
+
element.style.display = 'block';
|
|
27
|
+
|
|
28
|
+
width = element.offsetWidth + parseInt( el_style.marginLeft, 10 ) + parseInt( el_style.marginRight, 10 );
|
|
29
|
+
height = element.offsetHeight + parseInt( el_style.marginTop, 10 ) + parseInt( el_style.marginBottom, 10 );
|
|
30
|
+
|
|
31
|
+
const rect = element.getBoundingClientRect();
|
|
32
|
+
|
|
33
|
+
/** reverting to the original values */
|
|
34
|
+
element.style.display = el_display;
|
|
35
|
+
element.style.position = el_position === 'static' ? null : el_position;
|
|
36
|
+
element.style.visibility = el_visibility === 'visible' ? null : el_visibility;
|
|
37
|
+
|
|
38
|
+
return Object.assign( rect, { width, height } );
|
|
39
|
+
}
|
package/src/dom/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export { default as addEventListeners } from "./addEventListeners.js";
|
|
|
2
2
|
export { default as closestElement } from "./closestElement.js";
|
|
3
3
|
export { default as delegate } from "./delegate.js";
|
|
4
4
|
export { default as getFieldsValues } from "./getFieldsValues.js";
|
|
5
|
+
export { default as getElementSizing } from "./getElementSizing.js";
|
|
5
6
|
export { default as onEachElements } from "./onEachElements.js";
|
|
6
7
|
export { default as unlessElementExists } from "./unlessElementExists.js";
|
|
7
8
|
export { default as whenReady } from "./whenReady.js";
|