@straylightagency/utils 1.1.7 → 1.1.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/whenResize.js +11 -13
- package/src/dom/whenScroll.js +11 -13
- package/src/index.js +1 -0
- package/src/number/clamp.js +9 -0
- package/src/number/index.js +1 -0
package/package.json
CHANGED
package/src/dom/whenResize.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Listen when the window is resized.
|
|
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
|
-
* @
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
const init = () => wrappedFn();
|
|
23
|
-
const cancel = () => window.removeEventListener( 'resize', wrappedFn );
|
|
23
|
+
container.addEventListener( 'resize', wrappedFn );
|
|
24
24
|
|
|
25
25
|
if ( immediately ) {
|
|
26
|
-
|
|
26
|
+
wrappedFn();
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
return
|
|
30
|
-
init, cancel
|
|
31
|
-
];
|
|
29
|
+
return () => container.removeEventListener( 'resize', wrappedFn );
|
|
32
30
|
}
|
package/src/dom/whenScroll.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Listen when the window document is scrolled.
|
|
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
|
-
* @
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
const init = () => wrappedFn();
|
|
23
|
-
const cancel = () => document.removeEventListener( 'scroll', wrappedFn );
|
|
23
|
+
container.addEventListener( 'scroll', wrappedFn );
|
|
24
24
|
|
|
25
25
|
if ( immediately ) {
|
|
26
|
-
|
|
26
|
+
wrappedFn();
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
return
|
|
30
|
-
init, cancel
|
|
31
|
-
];
|
|
29
|
+
return () => container.removeEventListener( 'scroll', wrappedFn );
|
|
32
30
|
}
|
package/src/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export { default as slugify } from "./string/slugify.js";
|
|
|
4
4
|
export { default as interpolate } from "./string/interpolate.js";
|
|
5
5
|
export { default as format } from "./number/format.js";
|
|
6
6
|
export { default as random } from "./number/random.js";
|
|
7
|
+
export { default as clamp } from "./number/clamp.js";
|
|
7
8
|
export { default as filter } from "./object/filter.js";
|
|
8
9
|
export { default as map } from "./object/map.js";
|
|
9
10
|
export { default as only } from "./object/only.js";
|
package/src/number/index.js
CHANGED