@straylightagency/utils 1.0.7 → 1.0.9
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 +58 -21
- package/src/dom/onEachElements.js +3 -3
- package/src/index.js +1 -0
package/package.json
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @param element {HTMLElement}
|
|
3
|
-
* @returns {
|
|
3
|
+
* @returns {{width: *, height: *, offsetWidth: *, offsetHeight: *, top: number, left: number, bottom: number, right: number,
|
|
4
|
+
* x: number, y: number, marginLeft: number, marginTop: number, marginBottom: number, marginRight: number,
|
|
5
|
+
* paddingLeft: number, paddingTop: number, paddingBottom: number, paddingRight: number}}
|
|
4
6
|
*/
|
|
5
7
|
export default function getElementSizing(element) {
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
let width, height;
|
|
8
|
+
const style = window.getComputedStyle( element );
|
|
9
|
+
const display = style.display;
|
|
10
|
+
const position = style.position;
|
|
11
|
+
const visibility = style.visibility;
|
|
12
|
+
const max_height = style.maxHeight.replace('px', '').replace('%', '');
|
|
12
13
|
|
|
13
14
|
/** if it's not hidden we just return normal height */
|
|
14
|
-
if (
|
|
15
|
-
|
|
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 } );
|
|
15
|
+
if ( display !== 'none' && max_height !== '0' ) {
|
|
16
|
+
return getSizing( element, style );
|
|
21
17
|
}
|
|
22
18
|
|
|
23
19
|
/** the element is hidden, so we are making the element block, so we can measure its height but still be hidden */
|
|
@@ -25,15 +21,56 @@ export default function getElementSizing(element) {
|
|
|
25
21
|
element.style.visibility = 'hidden';
|
|
26
22
|
element.style.display = 'block';
|
|
27
23
|
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
const sizing = getSizing( element, style );
|
|
25
|
+
|
|
26
|
+
/** reverting to the original values */
|
|
27
|
+
element.style.display = display;
|
|
28
|
+
element.style.position = position === 'static' ? null : position;
|
|
29
|
+
element.style.visibility = visibility === 'visible' ? null : visibility;
|
|
30
|
+
|
|
31
|
+
return sizing;
|
|
32
|
+
}
|
|
30
33
|
|
|
34
|
+
/**
|
|
35
|
+
* @param element {HTMLElement}
|
|
36
|
+
* @param style {CSSStyleDeclaration}
|
|
37
|
+
* @returns {{width: *, height: *, offsetWidth: *, offsetHeight: *, top: number, left: number, bottom: number, right: number,
|
|
38
|
+
* x: number, y: number, marginLeft: number, marginTop: number, marginBottom: number, marginRight: number,
|
|
39
|
+
* paddingLeft: number, paddingTop: number, paddingBottom: number, paddingRight: number}}
|
|
40
|
+
*/
|
|
41
|
+
function getSizing(element, style) {
|
|
31
42
|
const rect = element.getBoundingClientRect();
|
|
32
43
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
const marginX = parseInt( style.marginLeft, 10 ) + parseInt( style.marginRight, 10 );
|
|
45
|
+
const marginY = parseInt( style.marginTop, 10 ) + parseInt( style.marginBottom, 10 );
|
|
46
|
+
const paddingX = parseInt( style.paddingLeft, 10 ) + parseInt( style.paddingRight, 10 );
|
|
47
|
+
const paddingY = parseInt( style.paddingTop, 10 ) + parseInt( style.paddingBottom, 10 );
|
|
48
|
+
|
|
49
|
+
const width = element.offsetWidth + marginX;
|
|
50
|
+
const height = element.offsetHeight + marginY;
|
|
37
51
|
|
|
38
|
-
return
|
|
52
|
+
return {
|
|
53
|
+
width,
|
|
54
|
+
height,
|
|
55
|
+
offsetWidth: element.offsetWidth,
|
|
56
|
+
offsetHeight: element.offsetHeight,
|
|
57
|
+
top: rect.top,
|
|
58
|
+
left: rect.left,
|
|
59
|
+
bottom: rect.bottom,
|
|
60
|
+
right: rect.right,
|
|
61
|
+
x: rect.x,
|
|
62
|
+
y: rect.y,
|
|
63
|
+
marginLeft: parseInt( style.marginLeft ),
|
|
64
|
+
marginTop: parseInt( style.marginTop ),
|
|
65
|
+
marginBottom: parseInt( style.marginBottom ),
|
|
66
|
+
marginRight: parseInt( style.marginRight ),
|
|
67
|
+
marginX,
|
|
68
|
+
marginY,
|
|
69
|
+
paddingLeft: parseInt( style.paddingLeft ),
|
|
70
|
+
paddingTop: parseInt( style.paddingTop ),
|
|
71
|
+
paddingBottom: parseInt( style.paddingBottom ),
|
|
72
|
+
paddingRight: parseInt( style.paddingRight ),
|
|
73
|
+
paddingX,
|
|
74
|
+
paddingY,
|
|
75
|
+
};
|
|
39
76
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @param selectorOrParent {String|HTMLElement}
|
|
3
|
-
* @param callbackOrSelector {Function|
|
|
4
|
-
* @param callbackOrNull {Function|
|
|
3
|
+
* @param callbackOrSelector {Function|String}
|
|
4
|
+
* @param callbackOrNull {Function|null}
|
|
5
5
|
* @returns {*}
|
|
6
6
|
*/
|
|
7
|
-
export default function onEachElements(selectorOrParent, callbackOrSelector, callbackOrNull) {
|
|
7
|
+
export default function onEachElements(selectorOrParent, callbackOrSelector, callbackOrNull = null) {
|
|
8
8
|
if ( typeof selectorOrParent === 'string' && typeof callbackOrSelector === 'function' && typeof callbackOrNull !== 'function' ) {
|
|
9
9
|
const elements = document.querySelectorAll( selectorOrParent );
|
|
10
10
|
|
package/src/index.js
CHANGED
|
@@ -11,6 +11,7 @@ export { default as addEventListeners } from "./dom/addEventListeners.js";
|
|
|
11
11
|
export { default as closestElement } from "./dom/closestElement.js";
|
|
12
12
|
export { default as delegate } from "./dom/delegate.js";
|
|
13
13
|
export { default as getFieldsValues } from "./dom/getFieldsValues.js";
|
|
14
|
+
export { default as getElementSizing } from "./dom/getElementSizing.js";
|
|
14
15
|
export { default as onEachElements } from "./dom/onEachElements.js";
|
|
15
16
|
export { default as unlessElementExists } from "./dom/unlessElementExists.js";
|
|
16
17
|
export { default as whenReady } from "./dom/whenReady.js";
|