@smartimpact-it/scroll-utils 1.1.0

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.
Files changed (49) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +158 -0
  3. package/dist/cjs/Bootstrap4AccordionScrollIntoView.js +74 -0
  4. package/dist/cjs/BootstrapAccordionScrollIntoView.js +68 -0
  5. package/dist/cjs/FixHashScrollPosition.js +38 -0
  6. package/dist/cjs/FoundationAccordionScrollIntoView.js +52 -0
  7. package/dist/cjs/ScrollDirection.js +120 -0
  8. package/dist/cjs/ScrollOffset.js +346 -0
  9. package/dist/cjs/ScrollPages.js +97 -0
  10. package/dist/cjs/index.js +93 -0
  11. package/dist/cjs/scrollWithMarginTop.js +34 -0
  12. package/dist/cjs/utils/onReady.js +46 -0
  13. package/dist/cjs/utils/utils.js +77 -0
  14. package/dist/esm/Bootstrap4AccordionScrollIntoView.js +57 -0
  15. package/dist/esm/BootstrapAccordionScrollIntoView.js +51 -0
  16. package/dist/esm/FixHashScrollPosition.js +24 -0
  17. package/dist/esm/FoundationAccordionScrollIntoView.js +35 -0
  18. package/dist/esm/ScrollDirection.js +98 -0
  19. package/dist/esm/ScrollOffset.js +283 -0
  20. package/dist/esm/ScrollPages.js +80 -0
  21. package/dist/esm/index.js +9 -0
  22. package/dist/esm/scrollWithMarginTop.js +25 -0
  23. package/dist/esm/utils/onReady.js +33 -0
  24. package/dist/esm/utils/utils.js +62 -0
  25. package/dist/types/Bootstrap4AccordionScrollIntoView.d.ts +11 -0
  26. package/dist/types/BootstrapAccordionScrollIntoView.d.ts +11 -0
  27. package/dist/types/FixHashScrollPosition.d.ts +7 -0
  28. package/dist/types/FoundationAccordionScrollIntoView.d.ts +11 -0
  29. package/dist/types/ScrollDirection.d.ts +29 -0
  30. package/dist/types/ScrollOffset.d.ts +49 -0
  31. package/dist/types/ScrollPages.d.ts +21 -0
  32. package/dist/types/index.d.ts +9 -0
  33. package/dist/types/scrollWithMarginTop.d.ts +5 -0
  34. package/dist/types/utils/onReady.d.ts +20 -0
  35. package/dist/types/utils/utils.d.ts +4 -0
  36. package/dist/umd/index.js +2 -0
  37. package/dist/umd/index.js.LICENSE.txt +8 -0
  38. package/package.json +101 -0
  39. package/src/Bootstrap4AccordionScrollIntoView.ts +51 -0
  40. package/src/BootstrapAccordionScrollIntoView.ts +45 -0
  41. package/src/FixHashScrollPosition.ts +31 -0
  42. package/src/FoundationAccordionScrollIntoView.ts +31 -0
  43. package/src/ScrollDirection.ts +122 -0
  44. package/src/ScrollOffset.ts +316 -0
  45. package/src/ScrollPages.ts +81 -0
  46. package/src/index.js +9 -0
  47. package/src/scrollWithMarginTop.ts +33 -0
  48. package/src/utils/onReady.ts +38 -0
  49. package/src/utils/utils.ts +78 -0
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Run a callback when the document.readyState has a certain value
3
+ * @param {string|string[]} state - the state(s) when to run the callback
4
+ * @param {Function} callback - the callback to run
5
+ */
6
+ export const onReadyState = (state: string | string[], callback: Function): void => {
7
+ const validStates = Array.isArray(state) ? state : [state];
8
+ const checkAndRun = () => {
9
+ if (validStates.includes(document.readyState)) {
10
+ document.removeEventListener('readystatechange', checkAndRun);
11
+ callback();
12
+ return true;
13
+ }
14
+ return false;
15
+ };
16
+ if (!checkAndRun()) {
17
+ document.addEventListener('readystatechange', checkAndRun);
18
+ }
19
+ };
20
+
21
+ /**
22
+ * Run a callback when the document.readyState is 'complete'
23
+ * (DOM + assets are loaded)
24
+ * @param {Function} callback - the callback to run
25
+ */
26
+ export const onComplete = (callback: Function): void =>
27
+ onReadyState('complete', callback);
28
+
29
+ /**
30
+ * Run a callback when the document.readyState is 'interactive'
31
+ * (DOM is loaded - equivalent to DOMContentLoaded)
32
+ * @param {Function} callback - the callback to run
33
+ */
34
+ export const onInteractive = (callback: Function): void =>
35
+ onReadyState(['interactive', 'complete'], callback);
36
+ export const onReady = onInteractive;
37
+
38
+ export default onReady;
@@ -0,0 +1,78 @@
1
+ // deep compare objects
2
+ export const deepEquals = (x, y) => {
3
+ if (x === y) return true;
4
+ // if both x and y are null or undefined and exactly the same
5
+
6
+ if (!(x instanceof Object) || !(y instanceof Object)) return false;
7
+ // if they are not strictly equal, they both need to be Objects
8
+
9
+ if (x.constructor !== y.constructor) return false;
10
+ // they must have the exact same prototype chain, the closest we can do is
11
+ // test there constructor.
12
+
13
+ for (let p in x) {
14
+ if (!x.hasOwnProperty(p)) continue;
15
+ // other properties were tested using x.constructor === y.constructor
16
+
17
+ if (!y.hasOwnProperty(p)) return false;
18
+ // allows to compare x[ p ] and y[ p ] when set to undefined
19
+
20
+ if (x[p] === y[p]) continue;
21
+ // if they have the same strict value or identity then they are equal
22
+
23
+ if (typeof x[p] !== 'object') return false;
24
+ // Numbers, Strings, Functions, Booleans must be strictly equal
25
+
26
+ if (!deepEquals(x[p], y[p])) return false;
27
+ // Objects and Arrays must be tested recursively
28
+ }
29
+
30
+ for (let p in y) {
31
+ if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) return false;
32
+ // allows x[ p ] to be set to undefined
33
+ }
34
+ return true;
35
+ };
36
+
37
+ export const throttle = (func, limit = 100) => {
38
+ let inThrottle;
39
+ return function () {
40
+ const args = arguments,
41
+ context = this;
42
+ if (!inThrottle) {
43
+ func.apply(context, args);
44
+ inThrottle = true;
45
+ setTimeout(() => (inThrottle = false), limit);
46
+ }
47
+ };
48
+ };
49
+
50
+ // Returns a function, that, as long as it continues to be invoked, will not
51
+ // be triggered. The function will be called after it stops being called for
52
+ // N milliseconds. If `immediate` is passed, trigger the function on the
53
+ // leading edge, instead of the trailing.
54
+ export const debounce = (func, wait, immediate = false) => {
55
+ let timeout;
56
+ return function () {
57
+ const context = this,
58
+ args = arguments;
59
+ const later = function () {
60
+ timeout = null;
61
+ if (!immediate) func.apply(context, args);
62
+ };
63
+ const callNow = immediate && !timeout;
64
+ clearTimeout(timeout);
65
+ timeout = setTimeout(later, wait);
66
+ if (callNow) func.apply(context, args);
67
+ };
68
+ };
69
+
70
+ export const isFunction = value => {
71
+ // from https://stackoverflow.com/a/55785839
72
+ return (
73
+ Boolean(value) &&
74
+ (Object.prototype.toString.call(value) === '[object Function]' ||
75
+ 'function' === typeof value ||
76
+ value instanceof Function)
77
+ );
78
+ };