@studiometa/js-toolkit 2.0.0-alpha.9 → 2.0.0-beta.11

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 (75) hide show
  1. package/Base/index.d.ts +53 -2
  2. package/Base/index.js +153 -46
  3. package/Base/managers/ChildrenManager.js +4 -0
  4. package/Base/managers/EventsManager.d.ts +3 -1
  5. package/Base/managers/EventsManager.js +12 -2
  6. package/Base/managers/OptionsManager.d.ts +2 -0
  7. package/Base/managers/OptionsManager.js +3 -0
  8. package/Base/managers/RefsManager.d.ts +2 -0
  9. package/Base/managers/RefsManager.js +9 -0
  10. package/Base/managers/ServicesManager.d.ts +36 -4
  11. package/Base/managers/ServicesManager.js +27 -6
  12. package/Base/utils.d.ts +0 -8
  13. package/Base/utils.js +0 -25
  14. package/README.md +13 -60
  15. package/decorators/index.d.ts +8 -7
  16. package/decorators/index.js +8 -7
  17. package/decorators/withBreakpointObserver.js +2 -2
  18. package/decorators/withDrag.d.ts +3 -3
  19. package/decorators/withDrag.js +14 -14
  20. package/decorators/withIntersectionObserver.js +5 -7
  21. package/decorators/withMountWhenInView.d.ts +4 -4
  22. package/decorators/withMountWhenInView.js +13 -6
  23. package/decorators/withScrolledInView.d.ts +17 -0
  24. package/decorators/withScrolledInView.js +94 -0
  25. package/decorators/withVue2.d.ts +7 -0
  26. package/decorators/withVue2.js +45 -0
  27. package/helpers/index.d.ts +4 -5
  28. package/helpers/index.js +4 -5
  29. package/index.d.ts +4 -19
  30. package/index.js +4 -5
  31. package/index.modern.js +1 -1
  32. package/index.umd.js +1 -1
  33. package/package.json +1 -1
  34. package/services/Service.d.ts +22 -17
  35. package/services/Service.js +12 -7
  36. package/services/drag.d.ts +5 -19
  37. package/services/drag.js +82 -105
  38. package/services/index.d.ts +13 -10
  39. package/services/index.js +7 -8
  40. package/services/key.d.ts +4 -13
  41. package/services/key.js +55 -56
  42. package/services/load.d.ts +3 -13
  43. package/services/load.js +32 -26
  44. package/services/pointer.d.ts +4 -13
  45. package/services/pointer.js +61 -64
  46. package/services/raf.d.ts +3 -13
  47. package/services/raf.js +22 -25
  48. package/services/resize.d.ts +3 -13
  49. package/services/resize.js +48 -30
  50. package/services/scroll.d.ts +3 -13
  51. package/services/scroll.js +65 -61
  52. package/utils/collide/index.d.ts +6 -7
  53. package/utils/collide/index.js +6 -7
  54. package/utils/css/index.d.ts +4 -5
  55. package/utils/css/index.js +6 -5
  56. package/utils/css/transition.js +2 -2
  57. package/utils/history.d.ts +6 -6
  58. package/utils/history.js +8 -4
  59. package/utils/index.d.ts +11 -13
  60. package/utils/index.js +11 -13
  61. package/utils/math/damp.d.ts +6 -5
  62. package/utils/math/damp.js +2 -1
  63. package/utils/math/ease.d.ts +78 -0
  64. package/utils/math/ease.js +41 -0
  65. package/utils/math/index.d.ts +8 -7
  66. package/utils/math/index.js +8 -7
  67. package/utils/math/inertiaFinalValue.d.ts +2 -2
  68. package/utils/math/inertiaFinalValue.js +4 -4
  69. package/utils/object/getAllProperties.js +1 -1
  70. package/utils/object/index.d.ts +3 -4
  71. package/utils/object/index.js +3 -4
  72. package/utils/scrollTo.d.ts +11 -0
  73. package/utils/scrollTo.js +75 -0
  74. package/utils/string/index.d.ts +10 -11
  75. package/utils/string/index.js +10 -11
@@ -1,8 +1,8 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
 
3
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
4
 
5
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
6
 
7
7
  import usePointer from '../../services/pointer.js';
8
8
  import useRaf from '../../services/raf.js';
@@ -10,7 +10,7 @@ import useResize from '../../services/resize.js';
10
10
  import useScroll from '../../services/scroll.js';
11
11
  import useKey from '../../services/key.js';
12
12
  import useLoad from '../../services/load.js';
13
- import { hasMethod, callMethod } from '../utils.js';
13
+ import { hasMethod } from '../utils.js';
14
14
  var SERVICES_MAP = {
15
15
  scrolled: useScroll,
16
16
  resized: useResize,
@@ -33,13 +33,14 @@ export default class ServicesManager {
33
33
  Object.defineProperties(this, {
34
34
  __base: {
35
35
  enumerable: false,
36
+ writable: false,
36
37
  value: instance
37
38
  }
38
39
  });
39
40
  }
40
41
 
41
42
  has(service) {
42
- if (!hasMethod(this.__base, service) && !this.__services[service]) {
43
+ if (!((hasMethod(this.__base, service) || this.__base.__hasEvent(service)) && this.__services[service])) {
43
44
  return false;
44
45
  }
45
46
 
@@ -55,7 +56,7 @@ export default class ServicesManager {
55
56
  return this.disable.bind(this, service);
56
57
  }
57
58
 
58
- if (!hasMethod(this.__base, service) || !this.__services[service]) {
59
+ if (!(hasMethod(this.__base, service) || this.__base.__hasEvent(service)) || !this.__services[service]) {
59
60
  return function noop() {};
60
61
  }
61
62
 
@@ -70,7 +71,7 @@ export default class ServicesManager {
70
71
  args[_key] = arguments[_key];
71
72
  }
72
73
 
73
- callMethod(self.__base, service, ...args);
74
+ self.__base.__callMethod(service, ...args);
74
75
  }
75
76
 
76
77
  add(this.__base.$id, serviceHandler);
@@ -99,8 +100,26 @@ export default class ServicesManager {
99
100
  remove(this.__base.$id);
100
101
  }
101
102
 
103
+ toggle(service, force) {
104
+ if (typeof force !== 'undefined') {
105
+ if (force && !this.has(service)) {
106
+ this.enable(service);
107
+ }
108
+
109
+ if (!force && this.has(service)) {
110
+ this.disable(service);
111
+ }
112
+ } else if (this.has(service)) {
113
+ this.disable(service);
114
+ } else {
115
+ this.enable(service);
116
+ }
117
+ }
118
+
102
119
  register(name, useFunction) {
103
120
  this.__customServices[name] = useFunction;
121
+
122
+ this.__base.__addEmits(name);
104
123
  }
105
124
 
106
125
  unregister(name) {
@@ -108,6 +127,8 @@ export default class ServicesManager {
108
127
  throw new Error("[ServicesManager] The `".concat(name, "` core service can not be unregistered."));
109
128
  }
110
129
 
130
+ this.__base.__removeEmits(name);
131
+
111
132
  delete this.__customServices[name];
112
133
  }
113
134
 
package/Base/utils.d.ts CHANGED
@@ -9,14 +9,6 @@
9
9
  * @return {Boolean}
10
10
  */
11
11
  export function hasMethod(obj: any, name: string): boolean;
12
- /**
13
- * Call the given method while applying the given arguments.
14
- *
15
- * @param {Base} instance The Base instance on which to trigger the method.
16
- * @param {String} method The method to call.
17
- * @param {...any} args The arguments to pass to the method.
18
- */
19
- export function callMethod(instance: Base, method: string, ...args: any[]): import("./index.js").default;
20
12
  /**
21
13
  * Get a list of elements based on the name of a component.
22
14
  *
package/Base/utils.js CHANGED
@@ -1,31 +1,6 @@
1
1
  export function hasMethod(obj, name) {
2
2
  return typeof obj[name] === 'function';
3
3
  }
4
- export function callMethod(instance, method) {
5
- for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
6
- args[_key - 2] = arguments[_key];
7
- }
8
-
9
- instance.__debug('callMethod', method, ...args);
10
-
11
- if (method === 'destroyed' && !instance.$isMounted || method === 'mounted' && instance.$isMounted) {
12
- instance.__debug('not', method, 'because the method has already been triggered once.');
13
-
14
- return instance;
15
- }
16
-
17
- instance.$emit(method, ...args);
18
-
19
- if (!hasMethod(instance, method)) {
20
- return instance;
21
- }
22
-
23
- instance[method].call(instance, ...args);
24
-
25
- instance.__debug(method, instance, ...args);
26
-
27
- return instance;
28
- }
29
4
  export function getComponentElements(nameOrSelector) {
30
5
  var element = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : document;
31
6
  var selector = "[data-component=\"".concat(nameOrSelector, "\"]");
package/README.md CHANGED
@@ -15,70 +15,23 @@ Install the latest version via NPM:
15
15
  npm install @studiometa/js-toolkit
16
16
  ```
17
17
 
18
- ## Usage
18
+ ## Documentation
19
19
 
20
- Import the `Base` class and extend it to create your components:
20
+ This project contains a JavaScript micro-framework and its utility functions whose main objectives are:
21
21
 
22
- ```js
23
- import { Base } from '@studiometa/js-toolkit';
24
- import Modal from '@studiometa/ui/Modal';
22
+ - Enforcing best-practice and consistency between projects
23
+ - Using elements from the DOM easily
24
+ - Enabling custom behaviours on component initialization or other user events
25
+ - Disabling custom behaviours on component destruction or other user events
26
+ - Initializing components in the right place at the right time
27
+ - Defining dependencies between components
25
28
 
26
- class Component {
27
- static config = {
28
- name: 'Component',
29
- }
30
- }
29
+ Visit [js-toolkit.studiometa.dev](https://js-toolkit.studiometa.dev) to learn more.
31
30
 
32
- class App extends Base {
33
- static config = {
34
- name: 'App',
35
- log: true,
36
- debug: true,
37
- components: {
38
- Modal,
39
- Component,
40
- },
41
- refs: ['btn', 'items[]'],
42
- options: {
43
- foo: String,
44
- bar: { type: String, default: 'bar' },
45
- }
46
- };
31
+ ## Contributing
47
32
 
48
- mounted() {
49
- this.$log('mounted');
50
-
51
- // Options
52
- this.$options.name; // 'App'
53
- this.$options.log; // true
54
- this.$options.debug; // true
55
- this.$options.foo // ''
56
- this.$options.bar // 'bar'
57
-
58
- // Children
59
- this.$children.Modal; // Array<Modal>
60
-
61
- // DOM references
62
- this.$refs.btn; // <button data-ref="btn"></button>
63
- this.$refs.items; // <li data-ref="items[]"></li>
64
- }
65
-
66
- destroyed() {
67
- this.$log('destroyed');
68
- }
69
-
70
- scrolled(props) {
71
- this.$log('scrolled', props);
72
- }
73
-
74
- resized(props) {
75
- this.$log('resized', props);
76
- }
77
- }
78
-
79
- export default new App(document.querySelector('#app'));
80
- ```
81
-
82
- Read the [documentation](https://js-toolkit.meta.fr/) to learn more.
33
+ This projects follows the [Git Flow](https://github.com/petervanderdoes/gitflow-avh) methodology to manage its branches and features. The packages and their dependencies are managed with NPM workspaces. The files are linted with ESLint, type checked with TypeScript and formatted with Prettier.
83
34
 
35
+ ## License
84
36
 
37
+ See [LICENSE](./LICENSE).
@@ -1,7 +1,8 @@
1
- import withBreakpointManager from "./withBreakpointManager.js";
2
- import withBreakpointObserver from "./withBreakpointObserver.js";
3
- import withDrag from "./withDrag.js";
4
- import withExtraConfig from "./withExtraConfig.js";
5
- import withIntersectionObserver from "./withIntersectionObserver.js";
6
- import withMountWhenInView from "./withMountWhenInView.js";
7
- export { withBreakpointManager, withBreakpointObserver, withDrag, withExtraConfig, withIntersectionObserver, withMountWhenInView };
1
+ export { default as withBreakpointManager } from "./withBreakpointManager.js";
2
+ export { default as withBreakpointObserver } from "./withBreakpointObserver.js";
3
+ export { default as withDrag } from "./withDrag.js";
4
+ export { default as withExtraConfig } from "./withExtraConfig.js";
5
+ export { default as withIntersectionObserver } from "./withIntersectionObserver.js";
6
+ export { default as withMountWhenInView } from "./withMountWhenInView.js";
7
+ export { default as withScrolledInView } from "./withScrolledInView.js";
8
+ export { default as withVue2 } from "./withVue2.js";
@@ -1,7 +1,8 @@
1
- import withBreakpointManager from './withBreakpointManager.js';
2
- import withBreakpointObserver from './withBreakpointObserver.js';
3
- import withDrag from './withDrag.js';
4
- import withExtraConfig from './withExtraConfig.js';
5
- import withIntersectionObserver from './withIntersectionObserver.js';
6
- import withMountWhenInView from './withMountWhenInView.js';
7
- export { withBreakpointManager, withBreakpointObserver, withDrag, withExtraConfig, withIntersectionObserver, withMountWhenInView };
1
+ export { default as withBreakpointManager } from './withBreakpointManager.js';
2
+ export { default as withBreakpointObserver } from './withBreakpointObserver.js';
3
+ export { default as withDrag } from './withDrag.js';
4
+ export { default as withExtraConfig } from './withExtraConfig.js';
5
+ export { default as withIntersectionObserver } from './withIntersectionObserver.js';
6
+ export { default as withMountWhenInView } from './withMountWhenInView.js';
7
+ export { default as withScrolledInView } from './withScrolledInView.js';
8
+ export { default as withVue2 } from './withVue2.js';
@@ -1,8 +1,8 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
 
3
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
4
 
5
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
6
 
7
7
  import useResize from '../services/resize.js';
8
8
 
@@ -4,7 +4,7 @@
4
4
  * @typedef {import('../services/drag').DragServiceOptions} DragServiceOptions
5
5
  */
6
6
  /**
7
- * @typedef {DragServiceOptions & { target: (this:Base) => HTMLElement }} DragDecoratorOptions
7
+ * @typedef {DragServiceOptions & { target?: (this:Base, Base) => HTMLElement }} DragDecoratorOptions
8
8
  */
9
9
  /**
10
10
  * Add dragging capabilities to a component.
@@ -14,10 +14,10 @@
14
14
  * @param {DragDecoratorOptions} options
15
15
  * @return {T}
16
16
  */
17
- export default function withDrag<T extends typeof import("../Base").default>(BaseClass: T, options?: DragDecoratorOptions): T;
17
+ export default function withDrag<T extends typeof import("../Base").default>(BaseClass: T, { target, ...options }?: DragDecoratorOptions): T;
18
18
  export type Base = import('../Base').default;
19
19
  export type BaseConstructor = import('../Base').BaseConstructor;
20
20
  export type DragServiceOptions = import('../services/drag').DragServiceOptions;
21
21
  export type DragDecoratorOptions = import("../services/drag.js").DragServiceOptions & {
22
- target: (this: Base) => HTMLElement;
22
+ target?: (this: import("../Base").default, Base: any) => HTMLElement;
23
23
  };
@@ -1,24 +1,21 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
1
2
  import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
2
3
  var _excluded = ["target"];
3
4
  import useDrag from '../services/drag.js';
4
5
  export default function withDrag(BaseClass) {
5
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
6
- target() {
7
- return this.$el;
8
- }
6
+ var _class, _temp;
7
+
8
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
9
+ {
10
+ target = instance => instance.$el
11
+ } = _ref,
12
+ options = _objectWithoutProperties(_ref, _excluded);
9
13
 
10
- };
11
- return class extends BaseClass {
14
+ return _temp = _class = class extends BaseClass {
12
15
  constructor(el) {
13
16
  super(el);
14
17
  this.$on('mounted', () => {
15
- var {
16
- target
17
- } = options,
18
- otherOptions = _objectWithoutProperties(options, _excluded);
19
-
20
- var boundUseDrag = useDrag.bind(undefined, target.call(this), otherOptions);
21
- this.$services.register('dragged', boundUseDrag);
18
+ this.$services.register('dragged', useDrag.bind(undefined, target.call(this, this), options));
22
19
  this.$services.enable('dragged');
23
20
  });
24
21
  this.$on('destroyed', () => {
@@ -27,5 +24,8 @@ export default function withDrag(BaseClass) {
27
24
  });
28
25
  }
29
26
 
30
- };
27
+ }, _defineProperty(_class, "config", {
28
+ name: "".concat(BaseClass.config.name, "WithDrag"),
29
+ emits: ['dragged']
30
+ }), _temp;
31
31
  }
@@ -1,8 +1,8 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
 
3
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
4
 
5
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
6
 
7
7
  function createArrayOfNumber(length) {
8
8
  return [...new Array(length + 1)].map((val, index) => index / length);
@@ -23,10 +23,7 @@ export default function withIntersectionObserver(BaseClass) {
23
23
  }
24
24
 
25
25
  this.$observer = new IntersectionObserver(entries => {
26
- this.__debug('intersected', entries);
27
-
28
- this.$emit('intersected', entries);
29
- this.intersected(entries);
26
+ this.__callMethod('intersected', entries);
30
27
  }, _objectSpread(_objectSpread({}, defaultOptions), this.$options.intersectionObserver || {}));
31
28
  this.$on('mounted', () => {
32
29
  this.$observer.observe(this.$el);
@@ -41,6 +38,7 @@ export default function withIntersectionObserver(BaseClass) {
41
38
  name: "".concat((_BaseClass$config$nam = BaseClass === null || BaseClass === void 0 ? void 0 : (_BaseClass$config = BaseClass.config) === null || _BaseClass$config === void 0 ? void 0 : _BaseClass$config.name) !== null && _BaseClass$config$nam !== void 0 ? _BaseClass$config$nam : '', "WithIntersectionObserver"),
42
39
  options: _objectSpread(_objectSpread({}, (BaseClass === null || BaseClass === void 0 ? void 0 : (_BaseClass$config2 = BaseClass.config) === null || _BaseClass$config2 === void 0 ? void 0 : _BaseClass$config2.options) || {}), {}, {
43
40
  intersectionObserver: Object
44
- })
41
+ }),
42
+ emits: ['intersected']
45
43
  })), _temp;
46
44
  }
@@ -5,7 +5,7 @@
5
5
  */
6
6
  /**
7
7
  * @typedef {Object} WithMountWhenInViewOptions
8
- * @property {Object} intersectionObserver
8
+ * @property {IntersectionObserverInit} intersectionObserver
9
9
  */
10
10
  /**
11
11
  * @typedef {Object} WithMountWhenInViewInterface
@@ -17,15 +17,15 @@
17
17
  *
18
18
  * @template {BaseConstructor} T
19
19
  * @param {T} BaseClass The Base class to extend.
20
- * @param {Object} [defaultOptions] The options for the IntersectionObserver instance.
20
+ * @param {IntersectionObserverInit} [defaultOptions] The options for the IntersectionObserver instance.
21
21
  * @return {T}
22
22
  */
23
- export default function withMountWhenInView<T extends typeof import("../Base").default>(BaseClass: T, defaultOptions?: any): T;
23
+ export default function withMountWhenInView<T extends typeof import("../Base").default>(BaseClass: T, defaultOptions?: IntersectionObserverInit): T;
24
24
  export type Base = import('../Base').default;
25
25
  export type BaseOptions = import('../Base').BaseOptions;
26
26
  export type BaseConstructor = import('../Base').BaseConstructor;
27
27
  export type WithMountWhenInViewOptions = {
28
- intersectionObserver: any;
28
+ intersectionObserver: IntersectionObserverInit;
29
29
  };
30
30
  export type WithMountWhenInViewInterface = {
31
31
  terminated: () => void;
@@ -2,9 +2,13 @@ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  import _classPrivateFieldGet from "@babel/runtime/helpers/classPrivateFieldGet";
3
3
  import _classPrivateFieldSet from "@babel/runtime/helpers/classPrivateFieldSet";
4
4
 
5
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
5
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
6
6
 
7
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
7
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
8
+
9
+ function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
10
+
11
+ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
8
12
 
9
13
  export default function withMountWhenInView(BaseClass) {
10
14
  var _class, _isVisible, _observer, _temp, _BaseClass$config$nam, _BaseClass$config, _BaseClass$config2;
@@ -16,12 +20,12 @@ export default function withMountWhenInView(BaseClass) {
16
20
  constructor(element) {
17
21
  super(element);
18
22
 
19
- _isVisible.set(this, {
23
+ _classPrivateFieldInitSpec(this, _isVisible, {
20
24
  writable: true,
21
25
  value: false
22
26
  });
23
27
 
24
- _observer.set(this, {
28
+ _classPrivateFieldInitSpec(this, _observer, {
25
29
  writable: true,
26
30
  value: void 0
27
31
  });
@@ -32,8 +36,11 @@ export default function withMountWhenInView(BaseClass) {
32
36
  if (_classPrivateFieldGet(this, _isVisible) !== isVisible) {
33
37
  _classPrivateFieldSet(this, _isVisible, isVisible);
34
38
 
35
- var method = isVisible ? '$mount' : '$destroy';
36
- this[method]();
39
+ if (isVisible) {
40
+ this.$mount();
41
+ } else {
42
+ setTimeout(() => this.$destroy());
43
+ }
37
44
  }
38
45
  }, _objectSpread(_objectSpread({}, defaultOptions), this.$options.intersectionObserver)));
39
46
 
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @typedef {import('../Base').default} Base
3
+ * @typedef {import('../Base').BaseConstructor} BaseConstructor
4
+ * @typedef {import('../Base').BaseConfig} BaseConfig
5
+ */
6
+ /**
7
+ * Add scrolled in view capabilities to a component.
8
+ *
9
+ * @template {BaseConstructor} T
10
+ * @param {T} BaseClass
11
+ * @param {IntersectionObserverInit} options
12
+ * @return {T}
13
+ */
14
+ export default function withScrolledInView<T extends typeof import("../Base").default>(BaseClass: T, options: IntersectionObserverInit): T;
15
+ export type Base = import('../Base').default;
16
+ export type BaseConstructor = import('../Base').BaseConstructor;
17
+ export type BaseConfig = import('../Base').BaseConfig;
@@ -0,0 +1,94 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import withMountWhenInView from './withMountWhenInView.js';
3
+ import { damp, clamp, clamp01 } from '../utils/index.js';
4
+ export default function withScrolledInView(BaseClass, options) {
5
+ var _class, _temp;
6
+
7
+ return _temp = _class = class extends withMountWhenInView(BaseClass, options) {
8
+ constructor() {
9
+ super(...arguments);
10
+
11
+ _defineProperty(this, "__props", {
12
+ start: {
13
+ x: 0,
14
+ y: 0
15
+ },
16
+ end: {
17
+ x: 0,
18
+ y: 0
19
+ },
20
+ current: {
21
+ x: 0,
22
+ y: 0
23
+ },
24
+ progress: {
25
+ x: 0,
26
+ y: 0
27
+ },
28
+ dampedProgress: {
29
+ x: 0,
30
+ y: 0
31
+ }
32
+ });
33
+
34
+ _defineProperty(this, "dampFactor", 0.1);
35
+
36
+ _defineProperty(this, "dampPrecision", 0.001);
37
+ }
38
+
39
+ mounted() {
40
+ this.__setProps();
41
+ }
42
+
43
+ resized() {
44
+ this.__setProps();
45
+ }
46
+
47
+ scrolled(props) {
48
+ if (!this.$services.has('ticked') && props.changed.y || props.changed.x) {
49
+ this.$services.enable('ticked');
50
+ }
51
+ }
52
+
53
+ ticked() {
54
+ this.__props.current.x = clamp(window.pageXOffset, this.__props.start.x, this.__props.end.x);
55
+ this.__props.progress.x = clamp01((this.__props.current.x - this.__props.start.x) / (this.__props.end.x - this.__props.start.x));
56
+ this.__props.dampedProgress.x = damp(this.__props.progress.x, this.__props.dampedProgress.x, this.dampFactor, this.dampPrecision);
57
+ this.__props.current.y = clamp(window.pageYOffset, this.__props.start.y, this.__props.end.y);
58
+ this.__props.progress.y = clamp01((this.__props.current.y - this.__props.start.y) / (this.__props.end.y - this.__props.start.y));
59
+ this.__props.dampedProgress.y = damp(this.__props.progress.y, this.__props.dampedProgress.y, this.dampFactor, this.dampPrecision);
60
+
61
+ if (this.__props.dampedProgress.x === this.__props.progress.x && this.__props.dampedProgress.y === this.__props.progress.y) {
62
+ this.$services.disable('ticked');
63
+ }
64
+
65
+ this.__callMethod('scrolledInView', this.__props);
66
+ }
67
+
68
+ __setProps() {
69
+ var sizes = this.$el.getBoundingClientRect();
70
+ var yEnd = sizes.top + window.pageYOffset + sizes.height;
71
+ var yStart = yEnd - window.innerHeight - sizes.height;
72
+ var yCurrent = clamp(window.pageYOffset, yStart, yEnd);
73
+ var yProgress = clamp01((yCurrent - yStart) / (yEnd - yStart));
74
+ var xEnd = sizes.left + window.pageXOffset + sizes.width;
75
+ var xStart = xEnd - window.innerWidth - sizes.width;
76
+ var xCurrent = clamp(window.pageXOffset, xStart, xEnd);
77
+ var xProgress = clamp01((xCurrent - xStart) / (xEnd - xStart));
78
+ this.__props.start.x = xStart;
79
+ this.__props.start.y = yStart;
80
+ this.__props.end.x = xEnd;
81
+ this.__props.end.y = yEnd;
82
+ this.__props.current.x = xCurrent;
83
+ this.__props.current.y = yCurrent;
84
+ this.__props.progress.x = xProgress;
85
+ this.__props.progress.y = yProgress;
86
+ this.__props.dampedProgress.x = damp(xProgress, this.__props.dampedProgress.x);
87
+ this.__props.dampedProgress.y = damp(yProgress, this.__props.dampedProgress.y);
88
+ }
89
+
90
+ }, _defineProperty(_class, "config", {
91
+ name: "".concat(BaseClass.config.name, "WithMountWhenInView"),
92
+ emits: ['scrolledInView']
93
+ }), _temp;
94
+ }
@@ -0,0 +1,7 @@
1
+ declare function _default<T extends typeof import("../Base").default>(BaseClass: T, Vue: import('vue').VueConstructor): T;
2
+ export default _default;
3
+ export type Base = import('../Base').default;
4
+ export type BaseConstructor = import('../Base').BaseConstructor;
5
+ export type WithVue2Refs = {
6
+ vue: HTMLElement;
7
+ };
@@ -0,0 +1,45 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
+
7
+ export default ((BaseClass, Vue) => {
8
+ var _class, _temp, _BaseClass$config$nam, _BaseClass$config, _BaseClass$config$ref, _BaseClass$config2;
9
+
10
+ return _temp = _class = class WithVue2 extends BaseClass {
11
+ constructor(element) {
12
+ super(element);
13
+
14
+ _defineProperty(this, "$vue", void 0);
15
+
16
+ var {
17
+ vueConfig
18
+ } = this.constructor;
19
+
20
+ if (!vueConfig) {
21
+ throw new Error('[withVue] You must define a `vueConfig` object.');
22
+ }
23
+
24
+ if (typeof vueConfig.render !== 'function') {
25
+ throw new Error('[withVue] You must define a `render` function in vueConfig.');
26
+ }
27
+
28
+ this.$vue = new Vue(vueConfig);
29
+ this.$on('mounted', () => {
30
+ if (!(this.$refs.vue instanceof HTMLElement)) {
31
+ throw new Error('[withVue] The `vue` refs must be a single HTMLElement.');
32
+ }
33
+
34
+ this.$vue.$mount(this.$refs.vue);
35
+ });
36
+ this.$on('destroyed', () => {
37
+ this.$vue.$destroy();
38
+ });
39
+ }
40
+
41
+ }, _defineProperty(_class, "config", _objectSpread(_objectSpread({}, BaseClass.config || {}), {}, {
42
+ name: "".concat((_BaseClass$config$nam = BaseClass === null || BaseClass === void 0 ? void 0 : (_BaseClass$config = BaseClass.config) === null || _BaseClass$config === void 0 ? void 0 : _BaseClass$config.name) !== null && _BaseClass$config$nam !== void 0 ? _BaseClass$config$nam : '', "WithVue"),
43
+ refs: [...((_BaseClass$config$ref = BaseClass === null || BaseClass === void 0 ? void 0 : (_BaseClass$config2 = BaseClass.config) === null || _BaseClass$config2 === void 0 ? void 0 : _BaseClass$config2.refs) !== null && _BaseClass$config$ref !== void 0 ? _BaseClass$config$ref : []), 'vue']
44
+ })), _defineProperty(_class, "vueConfig", void 0), _temp;
45
+ });
@@ -1,5 +1,4 @@
1
- import createApp from "./createApp.js";
2
- import importOnInteraction from "./importOnInteraction.js";
3
- import importWhenIdle from "./importWhenIdle.js";
4
- import importWhenVisible from "./importWhenVisible.js";
5
- export { createApp, importOnInteraction, importWhenIdle, importWhenVisible };
1
+ export { default as createApp } from "./createApp.js";
2
+ export { default as importOnInteraction } from "./importOnInteraction.js";
3
+ export { default as importWhenIdle } from "./importWhenIdle.js";
4
+ export { default as importWhenVisible } from "./importWhenVisible.js";
package/helpers/index.js CHANGED
@@ -1,5 +1,4 @@
1
- import createApp from './createApp.js';
2
- import importOnInteraction from './importOnInteraction.js';
3
- import importWhenIdle from './importWhenIdle.js';
4
- import importWhenVisible from './importWhenVisible.js';
5
- export { createApp, importOnInteraction, importWhenIdle, importWhenVisible };
1
+ export { default as createApp } from './createApp.js';
2
+ export { default as importOnInteraction } from './importOnInteraction.js';
3
+ export { default as importWhenIdle } from './importWhenIdle.js';
4
+ export { default as importWhenVisible } from './importWhenVisible.js';