Skip to content

Commit

Permalink
fix(core): 👌 resolve once transitions
Browse files Browse the repository at this point in the history
use from/to rules for once transitions resolution + replace current with next (data)

Closes #439
  • Loading branch information
thierrymichel committed Nov 5, 2019
1 parent 470be8d commit 20cafe1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
11 changes: 6 additions & 5 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,15 @@ export class Core {

// 8. Barba ready
// Set next + trigger for once and `beforeEnter`/`afterEnter` view on page load.
const readyData = this.data;
const onceData = this.data;

readyData.trigger = 'barba';
readyData.next = readyData.current;
this.hooks.do('ready', readyData);
onceData.trigger = 'barba';
onceData.next = onceData.current;
onceData.current = { ...this.schemaPage };
this.hooks.do('ready', onceData);

// 9. Finally, do once…
this.once(readyData);
this.once(onceData);
// Clean data for first barba transition…
this._resetData();
}
Expand Down
24 changes: 11 additions & 13 deletions packages/core/src/modules/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,17 @@ export class Store {
this._rules.reverse().forEach(rule => {
if (valid) {
valid = this._check(t, rule, data, match);
// From/to check, only for page transitions
if (!filters.once) {
if (t.from && t.to) {
valid =
this._check(t, rule, data, match, 'from') &&
this._check(t, rule, data, match, 'to');
}
if (t.from && !t.to) {
valid = this._check(t, rule, data, match, 'from');
}
if (!t.from && t.to) {
valid = this._check(t, rule, data, match, 'to');
}
// From/to check
if (t.from && t.to) {
valid =
this._check(t, rule, data, match, 'from') &&
this._check(t, rule, data, match, 'to');
}
if (t.from && !t.to) {
valid = this._check(t, rule, data, match, 'from');
}
if (!t.from && t.to) {
valid = this._check(t, rule, data, match, 'to');
}
}
});
Expand Down
8 changes: 4 additions & 4 deletions packages/css/__tests__/css.hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import css from '../src';
// Dom
const wrapper = document.createElement('div');
const current = document.createElement('div');
const next = current.cloneNode();
const next = document.createElement('div');

wrapper.dataset.barba = 'wrapper';
current.dataset.barba = 'container';
Expand Down Expand Up @@ -37,8 +37,8 @@ it('do once hooks', async () => {
await barba.hooks.do('beforeOnce', data, t);
await barba.hooks.do('afterOnce', data, t);

expect(css.start).toHaveBeenCalledWith(current, 'once');
expect(css.end).toHaveBeenCalledWith(current, 'once');
expect(css.start).toHaveBeenCalledWith(next, 'once');
expect(css.end).toHaveBeenCalledWith(next, 'once');
});

it('do leave hooks', async () => {
Expand Down Expand Up @@ -73,7 +73,7 @@ it('override transitions', async () => {
await barba.transitions.leave(data, t);
await barba.transitions.enter(data, t);

expect(css.next).toHaveBeenNthCalledWith(1, current, 'once');
expect(css.next).toHaveBeenNthCalledWith(1, next, 'once');
expect(css.next).toHaveBeenNthCalledWith(2, current, 'leave');
expect(css.next).toHaveBeenNthCalledWith(3, next, 'enter');
});
5 changes: 4 additions & 1 deletion packages/css/__tests__/css.prefix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ const named = {
...unnamed,
name,
};
const data = { current: { container } };
const data = {
current: { container },
next: { container },
};

barba.use(css);
barba.init({
Expand Down
6 changes: 3 additions & 3 deletions packages/css/src/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class Css implements IBarbaPlugin<{}> {
* `beforeOnce` hook.
*/
private _beforeOnce(data: ITransitionData): Promise<void> {
return this.start(data.current.container, 'once');
return this.start(data.next.container, 'once');
}

/**
Expand All @@ -174,14 +174,14 @@ export class Css implements IBarbaPlugin<{}> {
private async _once(data: ITransitionData, t: ITransitionPage): Promise<any> {
await this.barba.hooks.do('once', data, t);

return this.next(data.current.container, 'once');
return this.next(data.next.container, 'once');
}

/**
* `afterOnce` hook.
*/
private _afterOnce(data: ITransitionData): Promise<void> {
return this.end(data.current.container, 'once');
return this.end(data.next.container, 'once');
}

/**
Expand Down

0 comments on commit 20cafe1

Please sign in to comment.