@straylightagency/utils 1.0.8 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@straylightagency/utils",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Some JavaScript utilities functions",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -1,23 +1,19 @@
1
1
  /**
2
2
  * @param element {HTMLElement}
3
- * @returns {DOMRect}
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 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;
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 ( 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 } );
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
- 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 );
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
- /** 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;
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 Object.assign( rect, { width, height } );
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|HTMLElement}
4
- * @param callbackOrNull {Function|undefined}
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