@studiometa/js-toolkit 2.0.0-beta.15 → 2.0.0-beta.18

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/Base/index.cjs CHANGED
@@ -69,12 +69,10 @@ var Base = class extends EventTarget {
69
69
  this.$id = `${__config.name}-${id}`;
70
70
  id += 1;
71
71
  this.$el = element;
72
- if (!("__base__" in this.$el)) {
73
- Object.defineProperty(this.$el, "__base__", {
74
- get: () => this,
75
- configurable: true
76
- });
72
+ if (!this.$el.__base__) {
73
+ this.$el.__base__ = /* @__PURE__ */ new WeakMap();
77
74
  }
75
+ this.$el.__base__.set(this.__ctor, this);
78
76
  this.__options = new import_OptionsManager.default(element, __config.options || {}, __config);
79
77
  this.__services = new import_ServicesManager.default(this);
80
78
  this.__events = new import_EventsManager.default(element, this);
@@ -205,11 +203,7 @@ var Base = class extends EventTarget {
205
203
  }
206
204
  this.$destroy();
207
205
  this.__callMethod("terminated");
208
- Object.defineProperty(this.$el, "__base__", {
209
- value: "terminated",
210
- configurable: false,
211
- writable: false
212
- });
206
+ this.$el.__base__.set(this.__ctor, "terminated");
213
207
  }
214
208
  __testEventIsDefined(event) {
215
209
  if (!(Array.isArray(this.__config.emits) && this.__config.emits.includes(event))) {
@@ -217,7 +211,7 @@ var Base = class extends EventTarget {
217
211
  }
218
212
  }
219
213
  __addEmits(event) {
220
- const ctor = this.constructor;
214
+ const ctor = this.__ctor;
221
215
  if (Array.isArray(ctor.config.emits)) {
222
216
  ctor.config.emits.push(event);
223
217
  } else {
@@ -225,10 +219,13 @@ var Base = class extends EventTarget {
225
219
  }
226
220
  }
227
221
  __removeEmits(event) {
228
- const ctor = this.constructor;
222
+ const ctor = this.__ctor;
229
223
  const index = ctor.config.emits.findIndex((value) => value === event);
230
224
  ctor.config.emits.splice(index, 1);
231
225
  }
226
+ get __ctor() {
227
+ return this.constructor;
228
+ }
232
229
  $on(event, listener, options) {
233
230
  if (isDev) {
234
231
  this.__debug("$on", event, listener, options);
package/Base/index.d.ts CHANGED
@@ -57,9 +57,11 @@ export default class Base extends EventTarget {
57
57
  $id: string;
58
58
  /**
59
59
  * The root element.
60
- * @type {HTMLElement}
60
+ * @type {HTMLElement & { __base__?: WeakMap<BaseConstructor, Base | 'terminated'> }}
61
61
  */
62
- $el: HTMLElement;
62
+ $el: HTMLElement & {
63
+ __base__?: WeakMap<BaseConstructor, Base | 'terminated'>;
64
+ };
63
65
  /**
64
66
  * The state of the component.
65
67
  * @type {Boolean}
@@ -199,6 +201,13 @@ export default class Base extends EventTarget {
199
201
  * @returns {void}
200
202
  */
201
203
  private __removeEmits;
204
+ /**
205
+ * Get the instance constructor.
206
+ *
207
+ * @private
208
+ * @returns {BaseConstructor}
209
+ */
210
+ private get __ctor();
202
211
  /**
203
212
  * Bind a listener function to an event.
204
213
  *
package/Base/index.js CHANGED
@@ -37,12 +37,10 @@ class Base extends EventTarget {
37
37
  this.$id = `${__config.name}-${id}`;
38
38
  id += 1;
39
39
  this.$el = element;
40
- if (!("__base__" in this.$el)) {
41
- Object.defineProperty(this.$el, "__base__", {
42
- get: () => this,
43
- configurable: true
44
- });
40
+ if (!this.$el.__base__) {
41
+ this.$el.__base__ = /* @__PURE__ */ new WeakMap();
45
42
  }
43
+ this.$el.__base__.set(this.__ctor, this);
46
44
  this.__options = new OptionsManager(element, __config.options || {}, __config);
47
45
  this.__services = new ServicesManager(this);
48
46
  this.__events = new EventsManager(element, this);
@@ -173,11 +171,7 @@ class Base extends EventTarget {
173
171
  }
174
172
  this.$destroy();
175
173
  this.__callMethod("terminated");
176
- Object.defineProperty(this.$el, "__base__", {
177
- value: "terminated",
178
- configurable: false,
179
- writable: false
180
- });
174
+ this.$el.__base__.set(this.__ctor, "terminated");
181
175
  }
182
176
  __testEventIsDefined(event) {
183
177
  if (!(Array.isArray(this.__config.emits) && this.__config.emits.includes(event))) {
@@ -185,7 +179,7 @@ class Base extends EventTarget {
185
179
  }
186
180
  }
187
181
  __addEmits(event) {
188
- const ctor = this.constructor;
182
+ const ctor = this.__ctor;
189
183
  if (Array.isArray(ctor.config.emits)) {
190
184
  ctor.config.emits.push(event);
191
185
  } else {
@@ -193,10 +187,13 @@ class Base extends EventTarget {
193
187
  }
194
188
  }
195
189
  __removeEmits(event) {
196
- const ctor = this.constructor;
190
+ const ctor = this.__ctor;
197
191
  const index = ctor.config.emits.findIndex((value) => value === event);
198
192
  ctor.config.emits.splice(index, 1);
199
193
  }
194
+ get __ctor() {
195
+ return this.constructor;
196
+ }
200
197
  $on(event, listener, options) {
201
198
  if (isDev) {
202
199
  this.__debug("$on", event, listener, options);
@@ -38,6 +38,7 @@ var ChildrenManager = class {
38
38
  __publicField(this, "__element");
39
39
  __publicField(this, "__components");
40
40
  __publicField(this, "__eventsManager");
41
+ __publicField(this, "__asyncComponentPromises", /* @__PURE__ */ new WeakMap());
41
42
  Object.defineProperties(this, {
42
43
  __base: {
43
44
  enumerable: false,
@@ -81,17 +82,36 @@ var ChildrenManager = class {
81
82
  });
82
83
  }
83
84
  __getChild(el, ComponentClass, name) {
84
- if (el.__base__) {
85
- return el.__base__;
86
- }
87
- if ("$isBase" in ComponentClass) {
88
- const child = new ComponentClass(el);
85
+ const asyncComponentPromise = this.__asyncComponentPromises.get(ComponentClass);
86
+ if ("$isBase" in ComponentClass || asyncComponentPromise && asyncComponentPromise.status === "resolved") {
87
+ let ctor = ComponentClass;
88
+ if (asyncComponentPromise) {
89
+ ctor = asyncComponentPromise.ctor;
90
+ }
91
+ if (el.__base__ && el.__base__.has(ctor)) {
92
+ return el.__base__.get(ctor);
93
+ }
94
+ const child = new ctor(el);
89
95
  Object.defineProperty(child, "$parent", { get: () => this.__base });
90
96
  return child;
91
97
  }
92
- return ComponentClass(this.__base).then((module2) => {
98
+ const promise = asyncComponentPromise ? asyncComponentPromise.promise : ComponentClass(this.__base);
99
+ if (!asyncComponentPromise) {
100
+ this.__asyncComponentPromises.set(ComponentClass, {
101
+ promise,
102
+ status: "pending",
103
+ ctor: void 0
104
+ });
105
+ }
106
+ return promise.then((module2) => {
93
107
  var _a;
94
- return this.__getChild(el, (_a = module2.default) != null ? _a : module2, name);
108
+ const ctor = (_a = module2.default) != null ? _a : module2;
109
+ this.__asyncComponentPromises.set(ComponentClass, {
110
+ promise,
111
+ status: "resolved",
112
+ ctor
113
+ });
114
+ return this.__getChild(el, ctor, name);
95
115
  });
96
116
  }
97
117
  mountAll() {
@@ -40,6 +40,14 @@ export default class ChildrenManager {
40
40
  * @private
41
41
  */
42
42
  private __eventsManager;
43
+ /**
44
+ * Store async component promises to avoid calling them multiple times and
45
+ * waiting for them when they are already resolved.
46
+ *
47
+ * @private
48
+ * @type {WeakMap<BaseAsyncConstructor, { promise: ReturnType<BaseAsyncConstructor>, status: 'pending'|'resolved', ctor?: BaseConstructor }>}
49
+ */
50
+ private __asyncComponentPromises;
43
51
  /**
44
52
  * @return {string[]}
45
53
  */
@@ -61,7 +69,7 @@ export default class ChildrenManager {
61
69
  /**
62
70
  * Get a child component's instance.
63
71
  *
64
- * @param {HTMLElement & { __base__?: Base | 'terminated' }} el
72
+ * @param {HTMLElement & { __base__?: WeakMap<BaseConstructor, Base> }} el
65
73
  * The root element of the child component.
66
74
  * @param {BaseConstructor|BaseAsyncConstructor} ComponentClass
67
75
  * A Base class or a Promise for async components.
@@ -11,6 +11,7 @@ class ChildrenManager {
11
11
  __publicField(this, "__element");
12
12
  __publicField(this, "__components");
13
13
  __publicField(this, "__eventsManager");
14
+ __publicField(this, "__asyncComponentPromises", /* @__PURE__ */ new WeakMap());
14
15
  Object.defineProperties(this, {
15
16
  __base: {
16
17
  enumerable: false,
@@ -54,17 +55,36 @@ class ChildrenManager {
54
55
  });
55
56
  }
56
57
  __getChild(el, ComponentClass, name) {
57
- if (el.__base__) {
58
- return el.__base__;
59
- }
60
- if ("$isBase" in ComponentClass) {
61
- const child = new ComponentClass(el);
58
+ const asyncComponentPromise = this.__asyncComponentPromises.get(ComponentClass);
59
+ if ("$isBase" in ComponentClass || asyncComponentPromise && asyncComponentPromise.status === "resolved") {
60
+ let ctor = ComponentClass;
61
+ if (asyncComponentPromise) {
62
+ ctor = asyncComponentPromise.ctor;
63
+ }
64
+ if (el.__base__ && el.__base__.has(ctor)) {
65
+ return el.__base__.get(ctor);
66
+ }
67
+ const child = new ctor(el);
62
68
  Object.defineProperty(child, "$parent", { get: () => this.__base });
63
69
  return child;
64
70
  }
65
- return ComponentClass(this.__base).then((module) => {
71
+ const promise = asyncComponentPromise ? asyncComponentPromise.promise : ComponentClass(this.__base);
72
+ if (!asyncComponentPromise) {
73
+ this.__asyncComponentPromises.set(ComponentClass, {
74
+ promise,
75
+ status: "pending",
76
+ ctor: void 0
77
+ });
78
+ }
79
+ return promise.then((module) => {
66
80
  var _a;
67
- return this.__getChild(el, (_a = module.default) != null ? _a : module, name);
81
+ const ctor = (_a = module.default) != null ? _a : module;
82
+ this.__asyncComponentPromises.set(ComponentClass, {
83
+ promise,
84
+ status: "resolved",
85
+ ctor
86
+ });
87
+ return this.__getChild(el, ctor, name);
68
88
  });
69
89
  }
70
90
  mountAll() {
@@ -26,7 +26,7 @@ var createApp_exports = {};
26
26
  __export(createApp_exports, {
27
27
  default: () => createApp
28
28
  });
29
- function createApp(App, rootElement) {
29
+ function createApp(App, rootElement = document.body) {
30
30
  let isLoaded = document.readyState === "complete";
31
31
  let app;
32
32
  if (!isLoaded) {
@@ -28,10 +28,10 @@
28
28
  *
29
29
  * @template {BaseConstructor} T
30
30
  * @param {T} App
31
- * @param {HTMLElement} rootElement
31
+ * @param {HTMLElement=} [rootElement]
32
32
  *
33
33
  * @return {() => Promise<InstanceType<T>>}
34
34
  *
35
35
  */
36
- export default function createApp<T extends typeof import("../Base/index.js").default>(App: T, rootElement: HTMLElement): () => Promise<InstanceType<T>>;
36
+ export default function createApp<T extends typeof import("../Base/index.js").default>(App: T, rootElement?: HTMLElement | undefined): () => Promise<InstanceType<T>>;
37
37
  export type BaseConstructor = import('../Base/index.js').BaseConstructor;
@@ -1,4 +1,4 @@
1
- function createApp(App, rootElement) {
1
+ function createApp(App, rootElement = document.body) {
2
2
  let isLoaded = document.readyState === "complete";
3
3
  let app;
4
4
  if (!isLoaded) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@studiometa/js-toolkit",
3
- "version": "2.0.0-beta.15",
3
+ "version": "2.0.0-beta.18",
4
4
  "description": "A set of useful little bits of JavaScript to boost your project! 🚀",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/services/drag.cjs CHANGED
@@ -47,7 +47,7 @@ var MODES = {
47
47
  INERTIA: "inertia",
48
48
  STOP: "stop"
49
49
  };
50
- var instanceIds = 0;
50
+ var id = 0;
51
51
  var Drag = class extends import_Service.default {
52
52
  constructor(target, options = {}) {
53
53
  super();
@@ -77,10 +77,11 @@ var Drag = class extends import_Service.default {
77
77
  }
78
78
  });
79
79
  __publicField(this, "options", {
80
- dampFactor: 0.85
80
+ dampFactor: 0.85,
81
+ dragTreshold: 10
81
82
  });
82
- instanceIds += 1;
83
- this.id = `drag-${instanceIds}`;
83
+ id += 1;
84
+ this.id = `drag-${id}`;
84
85
  this.props.target = target;
85
86
  this.options = { ...this.options, ...options };
86
87
  this.pointerHandler = this.pointerHandler.bind(this);
@@ -91,18 +92,34 @@ var Drag = class extends import_Service.default {
91
92
  const options = { passive: true };
92
93
  this.props.target.addEventListener("mousedown", this, options);
93
94
  this.props.target.addEventListener("touchstart", this, options);
95
+ this.props.target.addEventListener("dragstart", this, { capture: true });
96
+ this.props.target.addEventListener("click", this, { capture: true });
94
97
  const pointer = (0, import_pointer.default)();
95
98
  pointer.add(this.id, this.pointerHandler);
96
99
  return this;
97
100
  }
98
101
  kill() {
99
- this.props.target.removeEventListener("mousemove", this);
100
- this.props.target.removeEventListener("touchmove", this);
102
+ this.props.target.removeEventListener("mousedown", this);
103
+ this.props.target.removeEventListener("touchstart", this);
104
+ this.props.target.removeEventListener("dragstart", this);
105
+ this.props.target.removeEventListener("click", this);
101
106
  const pointer = (0, import_pointer.default)();
102
107
  pointer.remove(this.id);
103
108
  return this;
104
109
  }
105
110
  handleEvent(event) {
111
+ if (event.type === "dragstart") {
112
+ event.preventDefault();
113
+ return;
114
+ }
115
+ if (event.type === "click") {
116
+ if (this.__shouldPreventClick) {
117
+ event.stopImmediatePropagation();
118
+ event.stopPropagation();
119
+ event.preventDefault();
120
+ }
121
+ return;
122
+ }
106
123
  const x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
107
124
  const y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
108
125
  this.start(x, y);
@@ -180,6 +197,9 @@ var Drag = class extends import_Service.default {
180
197
  updateProps() {
181
198
  return this.props;
182
199
  }
200
+ get __shouldPreventClick() {
201
+ return Math.abs(this.props.distance.x) > this.options.dragTreshold || Math.abs(this.props.distance.y) > this.options.dragTreshold;
202
+ }
183
203
  };
184
204
  var instances = /* @__PURE__ */ new WeakMap();
185
205
  function useDrag(target, options) {
@@ -74,4 +74,5 @@ export type DragServiceProps = {
74
74
  };
75
75
  export type DragServiceOptions = {
76
76
  dampFactor?: number | undefined;
77
+ dragTreshold?: number | undefined;
77
78
  };
package/services/drag.js CHANGED
@@ -15,7 +15,7 @@ const MODES = {
15
15
  INERTIA: "inertia",
16
16
  STOP: "stop"
17
17
  };
18
- let instanceIds = 0;
18
+ let id = 0;
19
19
  class Drag extends Service {
20
20
  constructor(target, options = {}) {
21
21
  super();
@@ -45,10 +45,11 @@ class Drag extends Service {
45
45
  }
46
46
  });
47
47
  __publicField(this, "options", {
48
- dampFactor: 0.85
48
+ dampFactor: 0.85,
49
+ dragTreshold: 10
49
50
  });
50
- instanceIds += 1;
51
- this.id = `drag-${instanceIds}`;
51
+ id += 1;
52
+ this.id = `drag-${id}`;
52
53
  this.props.target = target;
53
54
  this.options = { ...this.options, ...options };
54
55
  this.pointerHandler = this.pointerHandler.bind(this);
@@ -59,18 +60,34 @@ class Drag extends Service {
59
60
  const options = { passive: true };
60
61
  this.props.target.addEventListener("mousedown", this, options);
61
62
  this.props.target.addEventListener("touchstart", this, options);
63
+ this.props.target.addEventListener("dragstart", this, { capture: true });
64
+ this.props.target.addEventListener("click", this, { capture: true });
62
65
  const pointer = usePointer();
63
66
  pointer.add(this.id, this.pointerHandler);
64
67
  return this;
65
68
  }
66
69
  kill() {
67
- this.props.target.removeEventListener("mousemove", this);
68
- this.props.target.removeEventListener("touchmove", this);
70
+ this.props.target.removeEventListener("mousedown", this);
71
+ this.props.target.removeEventListener("touchstart", this);
72
+ this.props.target.removeEventListener("dragstart", this);
73
+ this.props.target.removeEventListener("click", this);
69
74
  const pointer = usePointer();
70
75
  pointer.remove(this.id);
71
76
  return this;
72
77
  }
73
78
  handleEvent(event) {
79
+ if (event.type === "dragstart") {
80
+ event.preventDefault();
81
+ return;
82
+ }
83
+ if (event.type === "click") {
84
+ if (this.__shouldPreventClick) {
85
+ event.stopImmediatePropagation();
86
+ event.stopPropagation();
87
+ event.preventDefault();
88
+ }
89
+ return;
90
+ }
74
91
  const x = event instanceof MouseEvent ? event.clientX : event.touches[0].clientX;
75
92
  const y = event instanceof MouseEvent ? event.clientY : event.touches[0].clientY;
76
93
  this.start(x, y);
@@ -148,6 +165,9 @@ class Drag extends Service {
148
165
  updateProps() {
149
166
  return this.props;
150
167
  }
168
+ get __shouldPreventClick() {
169
+ return Math.abs(this.props.distance.x) > this.options.dragTreshold || Math.abs(this.props.distance.y) > this.options.dragTreshold;
170
+ }
151
171
  }
152
172
  const instances = /* @__PURE__ */ new WeakMap();
153
173
  function useDrag(target, options) {
@@ -28,13 +28,16 @@ __export(classes_exports, {
28
28
  remove: () => remove,
29
29
  toggle: () => toggle
30
30
  });
31
- function setClasses(element, classNames, method = "add") {
31
+ function setClasses(element, classNames, method = "add", forceToggle = void 0) {
32
32
  if (!element || !classNames) {
33
33
  return;
34
34
  }
35
- classNames.split(" ").forEach((className) => {
36
- element.classList[method](className);
37
- });
35
+ const normalizedClassNames = Array.isArray(classNames) ? classNames : classNames.split(" ");
36
+ if (method !== "toggle") {
37
+ element.classList[method](...normalizedClassNames);
38
+ } else {
39
+ normalizedClassNames.forEach((className) => element.classList[method](className, forceToggle));
40
+ }
38
41
  }
39
42
  function add(element, classNames) {
40
43
  setClasses(element, classNames);
@@ -42,8 +45,8 @@ function add(element, classNames) {
42
45
  function remove(element, classNames) {
43
46
  setClasses(element, classNames, "remove");
44
47
  }
45
- function toggle(element, classNames) {
46
- setClasses(element, classNames, "toggle");
48
+ function toggle(element, classNames, force = void 0) {
49
+ setClasses(element, classNames, "toggle", force);
47
50
  }
48
51
  module.exports = __toCommonJS(classes_exports);
49
52
  if (module.exports.default) module.exports = module.exports.default;
@@ -1,24 +1,25 @@
1
1
  /**
2
2
  * Add class names to an element.
3
3
  *
4
- * @param {HTMLElement} element The element to update.
5
- * @param {String} classNames A string of class names.
4
+ * @param {HTMLElement} element The element to update.
5
+ * @param {string|string[]} classNames A string of class names.
6
6
  * @return {void}
7
7
  */
8
- export function add(element: HTMLElement, classNames: string): void;
8
+ export function add(element: HTMLElement, classNames: string | string[]): void;
9
9
  /**
10
10
  * Remove class names from an element.
11
11
  *
12
- * @param {HTMLElement} element The element to update.
13
- * @param {String} classNames A string of class names.
12
+ * @param {HTMLElement} element The element to update.
13
+ * @param {string|string[]} classNames A string of class names.
14
14
  * @return {void}
15
15
  */
16
- export function remove(element: HTMLElement, classNames: string): void;
16
+ export function remove(element: HTMLElement, classNames: string | string[]): void;
17
17
  /**
18
18
  * Toggle class names from an element.
19
19
  *
20
- * @param {HTMLElement} element The element to update.
21
- * @param {String} classNames A string of class names.
20
+ * @param {HTMLElement} element The element to update.
21
+ * @param {string|string[]} classNames A string of class names.
22
+ * @param {boolean} [force] Force toggle?
22
23
  * @return {void}
23
24
  */
24
- export function toggle(element: HTMLElement, classNames: string): void;
25
+ export function toggle(element: HTMLElement, classNames: string | string[], force?: boolean): void;
@@ -1,10 +1,13 @@
1
- function setClasses(element, classNames, method = "add") {
1
+ function setClasses(element, classNames, method = "add", forceToggle = void 0) {
2
2
  if (!element || !classNames) {
3
3
  return;
4
4
  }
5
- classNames.split(" ").forEach((className) => {
6
- element.classList[method](className);
7
- });
5
+ const normalizedClassNames = Array.isArray(classNames) ? classNames : classNames.split(" ");
6
+ if (method !== "toggle") {
7
+ element.classList[method](...normalizedClassNames);
8
+ } else {
9
+ normalizedClassNames.forEach((className) => element.classList[method](className, forceToggle));
10
+ }
8
11
  }
9
12
  function add(element, classNames) {
10
13
  setClasses(element, classNames);
@@ -12,8 +15,8 @@ function add(element, classNames) {
12
15
  function remove(element, classNames) {
13
16
  setClasses(element, classNames, "remove");
14
17
  }
15
- function toggle(element, classNames) {
16
- setClasses(element, classNames, "toggle");
18
+ function toggle(element, classNames, force = void 0) {
19
+ setClasses(element, classNames, "toggle", force);
17
20
  }
18
21
  export {
19
22
  add,
@@ -29,13 +29,16 @@ var __toCommonJS = /* @__PURE__ */ ((cache) => {
29
29
  // packages/js-toolkit/utils/css/index.js
30
30
  var css_exports = {};
31
31
  __export(css_exports, {
32
- classes: () => classes,
32
+ addClass: () => import_classes.add,
33
+ addStyle: () => import_styles.add,
33
34
  matrix: () => import_matrix.default,
34
- styles: () => styles,
35
+ removeClass: () => import_classes.remove,
36
+ removeStyle: () => import_styles.remove,
37
+ toggleClass: () => import_classes.toggle,
35
38
  transition: () => import_transition.default
36
39
  });
37
- var classes = __toESM(require("./classes.cjs"), 1);
38
- var styles = __toESM(require("./styles.cjs"), 1);
40
+ var import_classes = require("./classes.cjs");
41
+ var import_styles = require("./styles.cjs");
39
42
  var import_matrix = __toESM(require("./matrix.cjs"), 1);
40
43
  var import_transition = __toESM(require("./transition.cjs"), 1);
41
44
  module.exports = __toCommonJS(css_exports);
@@ -1,4 +1,4 @@
1
- export * as classes from "./classes.js";
2
- export * as styles from "./styles.js";
3
1
  export { default as matrix } from "./matrix.js";
4
2
  export { default as transition } from "./transition.js";
3
+ export { add as addClass, remove as removeClass, toggle as toggleClass } from "./classes.js";
4
+ export { add as addStyle, remove as removeStyle } from "./styles.js";
@@ -1,10 +1,13 @@
1
- import * as classes from "./classes.js";
2
- import * as styles from "./styles.js";
1
+ import { add, remove, toggle } from "./classes.js";
2
+ import { add as add2, remove as remove2 } from "./styles.js";
3
3
  import { default as default2 } from "./matrix.js";
4
4
  import { default as default3 } from "./transition.js";
5
5
  export {
6
- classes,
6
+ add as addClass,
7
+ add2 as addStyle,
7
8
  default2 as matrix,
8
- styles,
9
+ remove as removeClass,
10
+ remove2 as removeStyle,
11
+ toggle as toggleClass,
9
12
  default3 as transition
10
13
  };
@@ -30,7 +30,6 @@ var __toCommonJS = /* @__PURE__ */ ((cache) => {
30
30
  var styles_exports = {};
31
31
  __export(styles_exports, {
32
32
  add: () => add,
33
- default: () => setStyles,
34
33
  remove: () => remove
35
34
  });
36
35
  var import_isObject = __toESM(require("../object/isObject.cjs"), 1);
@@ -1,28 +1,16 @@
1
- /**
2
- * @typedef {Record<string, string | null> & Partial<CSSStyleDeclaration>} CssStyleObject
3
- */
4
- /**
5
- * Manage a list of style properties on an element.
6
- *
7
- * @param {HTMLElement} element The element to update.
8
- * @param {CssStyleObject} styles An object of styles properties and values.
9
- * @param {String} method The method to use: add or remove.
10
- */
11
- export default function setStyles(element: HTMLElement, styles: CssStyleObject, method?: string): void;
12
1
  /**
13
2
  * Add styles to an element.
14
3
  *
15
- * @param {HTMLElement} element The element to update.
16
- * @param {CssStyleObject} styles A string of class names.
4
+ * @param {HTMLElement} element The element to update.
5
+ * @param {Partial<CSSStyleDeclaration>} styles A string of class names.
17
6
  * @return {void}
18
7
  */
19
- export function add(element: HTMLElement, styles: CssStyleObject): void;
8
+ export function add(element: HTMLElement, styles: Partial<CSSStyleDeclaration>): void;
20
9
  /**
21
10
  * Remove class names from an element.
22
11
  *
23
- * @param {HTMLElement} element The element to update.
24
- * @param {CssStyleObject} styles A string of class names.
12
+ * @param {HTMLElement} element The element to update.
13
+ * @param {Partial<CSSStyleDeclaration>} styles A string of class names.
25
14
  * @return {void}
26
15
  */
27
- export function remove(element: HTMLElement, styles: CssStyleObject): void;
28
- export type CssStyleObject = Record<string, string | null> & Partial<CSSStyleDeclaration>;
16
+ export function remove(element: HTMLElement, styles: Partial<CSSStyleDeclaration>): void;
@@ -15,6 +15,5 @@ function remove(element, styles) {
15
15
  }
16
16
  export {
17
17
  add,
18
- setStyles as default,
19
18
  remove
20
19
  };
@@ -56,10 +56,10 @@ var Transition = class {
56
56
  }
57
57
  };
58
58
  function setClassesOrStyles(element, classesOrStyles, method = "add") {
59
- if (typeof classesOrStyles === "string") {
60
- classes[method](element, classesOrStyles, method);
59
+ if (typeof classesOrStyles === "string" || Array.isArray(classesOrStyles)) {
60
+ classes[method](element, classesOrStyles);
61
61
  } else {
62
- styles[method](element, classesOrStyles, method);
62
+ styles[method](element, classesOrStyles);
63
63
  }
64
64
  }
65
65
  function testTransition(element) {
@@ -1,20 +1,26 @@
1
1
  /**
2
2
  * Update either the classes or the styles of an element with the given method.
3
3
  *
4
- * @param {HTMLElement} element The element to update.
5
- * @param {String|Object} classesOrStyles The classes or styles to apply.
6
- * @param {String} method The method to use, one of `add` or `remove`.
4
+ * @param {HTMLElement} element The element to update.
5
+ * @param {ClassesOrStyles} classesOrStyles The classes or styles to apply.
6
+ * @param {'add'|'remove'} method The method to use, one of `add` or `remove`.
7
7
  */
8
- export function setClassesOrStyles(element: HTMLElement, classesOrStyles: string | any, method?: string): void;
8
+ export function setClassesOrStyles(element: HTMLElement, classesOrStyles: ClassesOrStyles, method?: 'add' | 'remove'): void;
9
9
  /**
10
10
  * Manage CSS transition with class.
11
11
  *
12
12
  * This is heavily inspired by the Vue `<transition>` component
13
13
  * and the `@barba/css` package, many thanks to them!
14
14
  *
15
- * @param {HTMLElement} element The target element.
16
- * @param {String|Object} name The name of the transition or an object with the hooks classesOrStyles.
17
- * @param {String} endMode Whether to remove or keep the `to` classes/styles
18
- * @return {Promise} A promise resolving at the end of the transition.
15
+ * @param {HTMLElement} element The target element.
16
+ * @param {string|TransitionStyles} name The name of the transition or an object with the hooks classesOrStyles.
17
+ * @param {String} endMode Whether to remove or keep the `to` classes/styles
18
+ * @return {Promise<void>} A promise resolving at the end of the transition.
19
19
  */
20
- export default function transition(element: HTMLElement, name: string | any, endMode?: string): Promise<any>;
20
+ export default function transition(element: HTMLElement, name: string | TransitionStyles, endMode?: string): Promise<void>;
21
+ export type ClassesOrStyles = string | string[] | Partial<CSSStyleDeclaration>;
22
+ export type TransitionStyles = {
23
+ from?: ClassesOrStyles;
24
+ active?: ClassesOrStyles;
25
+ to?: ClassesOrStyles;
26
+ };
@@ -23,10 +23,10 @@ class Transition {
23
23
  }
24
24
  }
25
25
  function setClassesOrStyles(element, classesOrStyles, method = "add") {
26
- if (typeof classesOrStyles === "string") {
27
- classes[method](element, classesOrStyles, method);
26
+ if (typeof classesOrStyles === "string" || Array.isArray(classesOrStyles)) {
27
+ classes[method](element, classesOrStyles);
28
28
  } else {
29
- styles[method](element, classesOrStyles, method);
29
+ styles[method](element, classesOrStyles);
30
30
  }
31
31
  }
32
32
  function testTransition(element) {
package/utils/index.cjs CHANGED
@@ -34,22 +34,24 @@ __export(utils_exports, {
34
34
  historyPush: () => import_history.push,
35
35
  historyReplace: () => import_history.replace,
36
36
  keyCodes: () => import_keyCodes.default,
37
- matrix: () => import_css.matrix,
38
37
  memoize: () => import_memoize.default,
39
38
  nextFrame: () => import_nextFrame.default,
39
+ nextMicrotask: () => import_nextMicrotask.default,
40
+ nextTick: () => import_nextTick.default,
40
41
  objectToURLSearchParams: () => import_history.objectToURLSearchParams,
41
42
  scrollTo: () => import_scrollTo.default,
42
- throttle: () => import_throttle.default,
43
- transition: () => import_css.transition
43
+ throttle: () => import_throttle.default
44
44
  });
45
45
  var import_debounce = __toESM(require("./debounce.cjs"), 1);
46
46
  var import_focusTrap = __toESM(require("./focusTrap.cjs"), 1);
47
47
  var import_keyCodes = __toESM(require("./keyCodes.cjs"), 1);
48
48
  var import_memoize = __toESM(require("./memoize.cjs"), 1);
49
49
  var import_nextFrame = __toESM(require("./nextFrame.cjs"), 1);
50
+ var import_nextTick = __toESM(require("./nextTick.cjs"), 1);
51
+ var import_nextMicrotask = __toESM(require("./nextMicrotask.cjs"), 1);
50
52
  var import_throttle = __toESM(require("./throttle.cjs"), 1);
51
53
  var import_scrollTo = __toESM(require("./scrollTo.cjs"), 1);
52
- var import_css = require("./css/index.cjs");
54
+ __reExport(utils_exports, require("./css/index.cjs"));
53
55
  var import_history = require("./history.cjs");
54
56
  __reExport(utils_exports, require("./collide/index.cjs"));
55
57
  __reExport(utils_exports, require("./math/index.cjs"));
package/utils/index.d.ts CHANGED
@@ -3,10 +3,12 @@ export { default as focusTrap } from "./focusTrap.js";
3
3
  export { default as keyCodes } from "./keyCodes.js";
4
4
  export { default as memoize } from "./memoize.js";
5
5
  export { default as nextFrame } from "./nextFrame.js";
6
+ export { default as nextTick } from "./nextTick.js";
7
+ export { default as nextMicrotask } from "./nextMicrotask.js";
6
8
  export { default as throttle } from "./throttle.js";
7
9
  export { default as scrollTo } from "./scrollTo.js";
10
+ export * from "./css/index.js";
8
11
  export * from "./collide/index.js";
9
12
  export * from "./math/index.js";
10
13
  export * from "./string/index.js";
11
- export { matrix, transition } from "./css/index.js";
12
14
  export { objectToURLSearchParams, push as historyPush, replace as historyReplace } from "./history.js";
package/utils/index.js CHANGED
@@ -3,9 +3,11 @@ import { default as default3 } from "./focusTrap.js";
3
3
  import { default as default4 } from "./keyCodes.js";
4
4
  import { default as default5 } from "./memoize.js";
5
5
  import { default as default6 } from "./nextFrame.js";
6
- import { default as default7 } from "./throttle.js";
7
- import { default as default8 } from "./scrollTo.js";
8
- import { matrix, transition } from "./css/index.js";
6
+ import { default as default7 } from "./nextTick.js";
7
+ import { default as default8 } from "./nextMicrotask.js";
8
+ import { default as default9 } from "./throttle.js";
9
+ import { default as default10 } from "./scrollTo.js";
10
+ export * from "./css/index.js";
9
11
  import {
10
12
  objectToURLSearchParams,
11
13
  push,
@@ -20,11 +22,11 @@ export {
20
22
  push as historyPush,
21
23
  replace as historyReplace,
22
24
  default4 as keyCodes,
23
- matrix,
24
25
  default5 as memoize,
25
26
  default6 as nextFrame,
27
+ default8 as nextMicrotask,
28
+ default7 as nextTick,
26
29
  objectToURLSearchParams,
27
- default8 as scrollTo,
28
- default7 as throttle,
29
- transition
30
+ default10 as scrollTo,
31
+ default9 as throttle
30
32
  };
@@ -34,11 +34,9 @@ function getRaf() {
34
34
  function getCancelRaf() {
35
35
  return typeof window !== "undefined" && window.cancelAnimationFrame ? window.cancelAnimationFrame.bind(window) : clearTimeout;
36
36
  }
37
- function nextFrame(fn = () => {
38
- }) {
39
- const raf = getRaf();
37
+ function nextFrame(fn) {
40
38
  return new Promise((resolve) => {
41
- raf(() => raf(() => resolve(fn())));
39
+ getRaf()(() => resolve(typeof fn === "function" && fn()));
42
40
  });
43
41
  }
44
42
  module.exports = __toCommonJS(nextFrame_exports);
@@ -13,8 +13,9 @@ export function getCancelRaf(): (id: number) => void;
13
13
  /**
14
14
  * Wait for the next frame to execute a function.
15
15
  *
16
- * @param {Function=} [fn=() => {}] The callback function to execute.
17
- * @return {Promise} A Promise resolving when the next frame is reached.
16
+ * @template {() => any} T
17
+ * @param {T=} [fn] The callback function to execute.
18
+ * @returns {Promise<T extends Function ? ReturnType<T> : undefined>} A Promise resolving when the next frame is reached.
18
19
  *
19
20
  * @example
20
21
  * ```js
@@ -24,4 +25,4 @@ export function getCancelRaf(): (id: number) => void;
24
25
  * console.log('hello world');
25
26
  * ```
26
27
  */
27
- export default function nextFrame(fn?: Function | undefined): Promise<any>;
28
+ export default function nextFrame<T extends () => any>(fn?: T): Promise<T extends Function ? ReturnType<T> : undefined>;
@@ -4,11 +4,9 @@ function getRaf() {
4
4
  function getCancelRaf() {
5
5
  return typeof window !== "undefined" && window.cancelAnimationFrame ? window.cancelAnimationFrame.bind(window) : clearTimeout;
6
6
  }
7
- function nextFrame(fn = () => {
8
- }) {
9
- const raf = getRaf();
7
+ function nextFrame(fn) {
10
8
  return new Promise((resolve) => {
11
- raf(() => raf(() => resolve(fn())));
9
+ getRaf()(() => resolve(typeof fn === "function" && fn()));
12
10
  });
13
11
  }
14
12
  export {
@@ -0,0 +1,33 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __reExport = (target, module2, copyDefault, desc) => {
11
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
12
+ for (let key of __getOwnPropNames(module2))
13
+ if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
14
+ __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
15
+ }
16
+ return target;
17
+ };
18
+ var __toCommonJS = /* @__PURE__ */ ((cache) => {
19
+ return (module2, temp) => {
20
+ return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
21
+ };
22
+ })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
23
+
24
+ // packages/js-toolkit/utils/nextMicrotask.js
25
+ var nextMicrotask_exports = {};
26
+ __export(nextMicrotask_exports, {
27
+ default: () => nextMicrotask
28
+ });
29
+ async function nextMicrotask(fn) {
30
+ return Promise.resolve().then(() => typeof fn === "function" && fn());
31
+ }
32
+ module.exports = __toCommonJS(nextMicrotask_exports);
33
+ if (module.exports.default) module.exports = module.exports.default;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Wait for the next microtask.
3
+ *
4
+ * @template {() => any} T
5
+ * @param {T=} [fn]
6
+ * @returns {Promise<T extends Function ? ReturnType<T> : undefined>}
7
+ */
8
+ export default function nextMicrotask<T extends () => any>(fn?: T): Promise<T extends Function ? ReturnType<T> : undefined>;
@@ -0,0 +1,6 @@
1
+ async function nextMicrotask(fn) {
2
+ return Promise.resolve().then(() => typeof fn === "function" && fn());
3
+ }
4
+ export {
5
+ nextMicrotask as default
6
+ };
@@ -0,0 +1,35 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __reExport = (target, module2, copyDefault, desc) => {
11
+ if (module2 && typeof module2 === "object" || typeof module2 === "function") {
12
+ for (let key of __getOwnPropNames(module2))
13
+ if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
14
+ __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
15
+ }
16
+ return target;
17
+ };
18
+ var __toCommonJS = /* @__PURE__ */ ((cache) => {
19
+ return (module2, temp) => {
20
+ return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
21
+ };
22
+ })(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);
23
+
24
+ // packages/js-toolkit/utils/nextTick.js
25
+ var nextTick_exports = {};
26
+ __export(nextTick_exports, {
27
+ default: () => nextTick
28
+ });
29
+ async function nextTick(fn) {
30
+ return new Promise((resolve) => setTimeout(() => {
31
+ resolve(typeof fn === "function" && fn());
32
+ }, 0));
33
+ }
34
+ module.exports = __toCommonJS(nextTick_exports);
35
+ if (module.exports.default) module.exports = module.exports.default;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Wait for the next tick.
3
+ *
4
+ * @template {() => any} T
5
+ * @param {T=} [fn]
6
+ * @returns {Promise<T extends Function ? ReturnType<T> : undefined>}
7
+ */
8
+ export default function nextTick<T extends () => any>(fn?: T): Promise<T extends Function ? ReturnType<T> : undefined>;
@@ -0,0 +1,8 @@
1
+ async function nextTick(fn) {
2
+ return new Promise((resolve) => setTimeout(() => {
3
+ resolve(typeof fn === "function" && fn());
4
+ }, 0));
5
+ }
6
+ export {
7
+ nextTick as default
8
+ };