Skip to content

Commit 85d91f3

Browse files
committed
Feat: work on transition and namespaced events
1 parent 3158169 commit 85d91f3

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

packages/query/src/behavior.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,19 @@ export class Behavior {
437437
}
438438
}
439439

440-
dispatchEvent(eventName, detail = {}, eventSettings = {}, element = this.element) {
440+
dispatchEvent(
441+
eventName,
442+
detail = {},
443+
eventSettings = {},
444+
{ element = this.element, namespace = this.namespace } = {},
445+
) {
446+
// Auto-namespace if not already namespaced
447+
const namespacedEvent = eventName.includes(':')
448+
? eventName
449+
: `${namespace}:${eventName}`;
450+
441451
element.dispatchEvent(
442-
new CustomEvent(eventName, {
452+
new CustomEvent(namespacedEvent, {
443453
detail,
444454
bubbles: true,
445455
cancelable: true,
@@ -449,9 +459,14 @@ export class Behavior {
449459
}
450460

451461
// dispatch an event across entire group
452-
dispatchGroupEvent(eventName, detail = {}, eventSettings = {}) {
453-
return this.$elements.get().map(element => {
454-
this.dispatchEvent(eventName, detail, eventSettings, element);
462+
dispatchGroupEvent(
463+
eventName,
464+
detail = {},
465+
eventSettings = {},
466+
{ $elements = this.$elements, namespace = this.namespace } = {},
467+
) {
468+
return $elements.get().map(element => {
469+
this.dispatchEvent(eventName, detail, eventSettings, { element, namespace });
455470
});
456471
}
457472

src/behaviors/transition/transition.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,6 @@ const createBehavior = (
8383
...runtimeSettings,
8484
};
8585

86-
// handle case of already animating
87-
// handle animation queuing
88-
if (self.isAnimating()) {
89-
if (settings.queue) {
90-
await self.animationsFinished();
91-
}
92-
else {
93-
self.stop();
94-
}
95-
}
96-
9786
// determine canonical animations from css, this is cached between runs
9887
const cssAnimations = self.findCSSAnimation(animationSettings.animation);
9988

@@ -123,7 +112,6 @@ const createBehavior = (
123112
dispatchGroupEvent('transitionGroupStarted', {
124113
groupOrder,
125114
});
126-
console.log('group animation started');
127115
}
128116
if (self.isLastInGroup(direction)) {
129117
dispatchGroupEvent('transitionGroupEnded', {
@@ -250,6 +238,17 @@ const createBehavior = (
250238
return;
251239
}
252240

241+
// handle case of already animating
242+
// handle animation queuing
243+
if (self.isAnimating()) {
244+
if (settings.queue) {
245+
await $(el).afterEvent('transition:end');
246+
}
247+
else {
248+
self.stop();
249+
}
250+
}
251+
253252
// find animations for this particular direction (array of animations)
254253
const animationsToPlay = cssAnimations.animations[direction] ?? cssAnimations.animations.standard;
255254

@@ -283,9 +282,9 @@ const createBehavior = (
283282
return el.animate(animData.keyframes, options);
284283
});
285284

286-
dispatchEvent('transitionScheduled', { cssAnimations: cssAnimations, animations: activeAnimations });
285+
dispatchEvent('scheduled', { cssAnimations: cssAnimations, animations: activeAnimations });
287286
await self.animationsStarted(activeAnimations);
288-
dispatchEvent('transitionStarted', { cssAnimations: cssAnimations, animations: activeAnimations });
287+
dispatchEvent('started', { cssAnimations: cssAnimations, animations: activeAnimations });
289288

290289
// make element visible
291290
self.setInitialDisplayState(direction);
@@ -300,16 +299,16 @@ const createBehavior = (
300299

301300
// Handle show/hide callbacks based on direction
302301
if (direction === 'in') {
303-
dispatchEvent('transitionVisible', { cssAnimations: cssAnimations, animations: activeAnimations });
302+
dispatchEvent('visible', { cssAnimations: cssAnimations, animations: activeAnimations });
304303
settings.onShow.call(el);
305304
}
306305
else if (direction === 'out') {
307-
dispatchEvent('transitionHidden', { cssAnimations: cssAnimations, animations: activeAnimations });
306+
dispatchEvent('hidden', { cssAnimations: cssAnimations, animations: activeAnimations });
308307
settings.onHide.call(el);
309308
}
310309

311310
// can use callback, event or await this function
312-
dispatchEvent('transitionEnded', { cssAnimations: cssAnimations, animations: activeAnimations });
311+
dispatchEvent('end', { cssAnimations: cssAnimations, animations: activeAnimations });
313312
callback.call(el);
314313
},
315314

0 commit comments

Comments
 (0)