@straylightagency/utils 1.1.7 → 1.1.8

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.1.7",
3
+ "version": "1.1.8",
4
4
  "description": "Some JavaScript utilities functions",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -1,15 +1,18 @@
1
1
  /**
2
- * Listen when the window is resized. Returned an array with two methods:
3
- * - init() : callback is called immediately with width and height values
4
- * - cancel() : cancel the event listener
2
+ * Listen when the window is resized. Return a callback to cancel the event listener.
5
3
  *
6
4
  * @param {Function} callbackFn
7
5
  * @param {boolean} immediately
8
- * @returns {[]}
6
+ * @param {Document|HTMLElement} container
7
+ * @returns {function(): void}
9
8
  */
10
- export default function whenResize(callbackFn, immediately = true) {
9
+ export default function whenResize(callbackFn, immediately = true, container = undefined) {
11
10
  let ww, wh;
12
11
 
12
+ if ( ! container ) {
13
+ container = document;
14
+ }
15
+
13
16
  const wrappedFn = e => {
14
17
  ww = window.innerWidth;
15
18
  wh = window.innerHeight;
@@ -17,16 +20,11 @@ export default function whenResize(callbackFn, immediately = true) {
17
20
  callbackFn( { ww, wh }, e );
18
21
  };
19
22
 
20
- window.addEventListener( 'resize', wrappedFn );
21
-
22
- const init = () => wrappedFn();
23
- const cancel = () => window.removeEventListener( 'resize', wrappedFn );
23
+ container.addEventListener( 'resize', wrappedFn );
24
24
 
25
25
  if ( immediately ) {
26
- init();
26
+ wrappedFn();
27
27
  }
28
28
 
29
- return [
30
- init, cancel
31
- ];
29
+ return () => container.removeEventListener( 'resize', wrappedFn );
32
30
  }
@@ -1,15 +1,18 @@
1
1
  /**
2
- * Listen when the window document is scrolled. Returned an array with two methods:
3
- * - init() : callback is called immediately
4
- * - cancel() : cancel the event listener
2
+ * Listen when the window document is scrolled. Return a callback to cancel the event listener.
5
3
  *
6
4
  * @param {Function} callbackFn
7
5
  * @param {boolean} immediately
8
- * @returns {[]}
6
+ * @param {Document|HTMLElement} container
7
+ * @returns {function(): void}
9
8
  */
10
- export default function whenScroll(callbackFn, immediately = true) {
9
+ export default function whenScroll(callbackFn, immediately = true, container = undefined) {
11
10
  let x, y;
12
11
 
12
+ if ( ! container ) {
13
+ container = document;
14
+ }
15
+
13
16
  const wrappedFn = e => {
14
17
  x = window.scrollX;
15
18
  y = window.scrollY;
@@ -17,16 +20,11 @@ export default function whenScroll(callbackFn, immediately = true) {
17
20
  callbackFn( { x, y }, e );
18
21
  };
19
22
 
20
- document.addEventListener( 'scroll', wrappedFn );
21
-
22
- const init = () => wrappedFn();
23
- const cancel = () => document.removeEventListener( 'scroll', wrappedFn );
23
+ container.addEventListener( 'scroll', wrappedFn );
24
24
 
25
25
  if ( immediately ) {
26
- init();
26
+ wrappedFn();
27
27
  }
28
28
 
29
- return [
30
- init, cancel
31
- ];
29
+ return () => container.removeEventListener( 'scroll', wrappedFn );
32
30
  }