Skip to content

Commit

Permalink
Add support for grouped transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Oct 15, 2024
1 parent 73592a0 commit 02d7787
Showing 1 changed file with 52 additions and 22 deletions.
74 changes: 52 additions & 22 deletions packages/ui/decorators/withTransition.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getInstances } from '@studiometa/js-toolkit';
import { transition } from '@studiometa/js-toolkit/utils';
import type {
Base,
Expand All @@ -17,6 +18,7 @@ export interface TransitionProps extends BaseProps {
leaveActive: string;
leaveTo: string;
leaveKeep: boolean;
group: string;
};
}

Expand Down Expand Up @@ -59,6 +61,7 @@ export function withTransition<S extends Base>(
leaveActive: String,
leaveTo: String,
leaveKeep: Boolean,
group: String,
},
};

Expand All @@ -72,37 +75,64 @@ export function withTransition<S extends Base>(
/**
* Trigger the enter transition.
*/
async enter(target?: HTMLElement | HTMLElement[]): Promise<void> {
async enter(target?: HTMLElement | HTMLElement[], { dispatch = true } = {}): Promise<void> {
const { enterFrom, enterActive, enterTo, enterKeep, leaveTo } = this.$options;

await transition(
target ?? this.target,
{
// eslint-disable-next-line prefer-template
from: (leaveTo + ' ' + enterFrom).trim(),
active: enterActive as string,
to: enterTo as string,
},
enterKeep ? 'keep' : undefined,
);
await Promise.all([
transition(
target ?? this.target,
{
// eslint-disable-next-line prefer-template
from: (leaveTo + ' ' + enterFrom).trim(),
active: enterActive as string,
to: enterTo as string,
},
enterKeep ? 'keep' : undefined,
),
dispatch && this.dispatch('enter'),
]);
}

/**
* Trigger the leave transition.
*/
async leave(target?: HTMLElement | HTMLElement[]): Promise<void> {
async leave(target?: HTMLElement | HTMLElement[], { dispatch = true } = {}): Promise<void> {
const { leaveFrom, leaveActive, leaveTo, leaveKeep, enterTo } = this.$options;

await transition(
target ?? this.target,
{
// eslint-disable-next-line prefer-template
from: (enterTo + ' ' + leaveFrom).trim(),
active: leaveActive as string,
to: leaveTo as string,
},
leaveKeep ? 'keep' : undefined,
);
await Promise.all([
transition(
target ?? this.target,
{
// eslint-disable-next-line prefer-template
from: (enterTo + ' ' + leaveFrom).trim(),
active: leaveActive as string,
to: leaveTo as string,
},
leaveKeep ? 'keep' : undefined,
),
dispatch && this.dispatch('leave'),
]);
}

/**
* Dispatch the callback to related instances.
*/
async dispatch(method: 'enter' | 'leave') {
const { group } = this.$options;

if (!group) {
return;
}

const promises = [];

Check warning on line 127 in packages/ui/decorators/withTransition.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/decorators/withTransition.ts#L127

Added line #L127 was not covered by tests

for (const instance of getInstances(Transition)) {
if (instance !== this && instance.$options.group === group) {
promises.push(instance[method](undefined, { dispatch: false }));
}
}

Check warning on line 133 in packages/ui/decorators/withTransition.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/decorators/withTransition.ts#L129-L133

Added lines #L129 - L133 were not covered by tests

await Promise.all(promises);

Check warning on line 135 in packages/ui/decorators/withTransition.ts

View check run for this annotation

Codecov / codecov/patch

packages/ui/decorators/withTransition.ts#L135

Added line #L135 was not covered by tests
}
}

Expand Down

0 comments on commit 02d7787

Please sign in to comment.