swup 3.0.1 → 3.0.2
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/dist/Swup.cjs +1 -1
- package/dist/Swup.cjs.map +1 -1
- package/dist/Swup.modern.js +1 -1
- package/dist/Swup.modern.js.map +1 -1
- package/dist/Swup.module.js +1 -1
- package/dist/Swup.module.js.map +1 -1
- package/dist/Swup.umd.js +1 -1
- package/dist/Swup.umd.js.map +1 -1
- package/dist/types/Swup.d.ts +9 -14
- package/dist/types/modules/enterPage.d.ts +2 -4
- package/dist/types/modules/events.d.ts +1 -1
- package/dist/types/modules/leavePage.d.ts +2 -5
- package/dist/types/modules/loadPage.d.ts +7 -1
- package/dist/types/modules/renderPage.d.ts +5 -4
- package/dist/types/modules/transitions.d.ts +6 -0
- package/package.json +1 -1
- package/src/Swup.ts +14 -5
- package/src/__test__/index.test.ts +21 -9
- package/src/modules/enterPage.ts +4 -6
- package/src/modules/events.ts +1 -5
- package/src/modules/leavePage.ts +5 -9
- package/src/modules/loadPage.ts +27 -12
- package/src/modules/renderPage.ts +10 -7
- package/src/modules/transitions.ts +10 -0
- package/dist/types/modules/updateTransition.d.ts +0 -2
- package/src/modules/updateTransition.ts +0 -10
package/src/modules/loadPage.ts
CHANGED
|
@@ -7,15 +7,30 @@ export type TransitionOptions = {
|
|
|
7
7
|
customTransition?: string;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const { url, customTransition } = data;
|
|
16
|
-
const skipTransition = !!(popstate && !this.options.animateHistoryBrowsing);
|
|
10
|
+
export type PageLoadOptions = {
|
|
11
|
+
url: string;
|
|
12
|
+
event?: Event;
|
|
13
|
+
customTransition?: string;
|
|
14
|
+
};
|
|
17
15
|
|
|
18
|
-
|
|
16
|
+
export function loadPage(this: Swup, data: TransitionOptions) {
|
|
17
|
+
const { url } = data;
|
|
18
|
+
|
|
19
|
+
// Check if the visit should be ignored
|
|
20
|
+
if (this.shouldIgnoreVisit(url)) {
|
|
21
|
+
window.location.href = url;
|
|
22
|
+
} else {
|
|
23
|
+
this.performPageLoad(data);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function performPageLoad(this: Swup, data: PageLoadOptions) {
|
|
28
|
+
const { url, event, customTransition } = data ?? {};
|
|
29
|
+
|
|
30
|
+
const isHistoryVisit = event instanceof PopStateEvent;
|
|
31
|
+
const skipTransition = this.shouldSkipTransition({ url, event });
|
|
32
|
+
|
|
33
|
+
this.triggerEvent('transitionStart', event);
|
|
19
34
|
|
|
20
35
|
// set transition object
|
|
21
36
|
this.updateTransition(getCurrentUrl(), url, customTransition);
|
|
@@ -24,10 +39,10 @@ export const loadPage = function (
|
|
|
24
39
|
}
|
|
25
40
|
|
|
26
41
|
// start/skip animation
|
|
27
|
-
const animationPromises = this.leavePage(
|
|
42
|
+
const animationPromises = this.leavePage({ event, skipTransition });
|
|
28
43
|
|
|
29
44
|
// create history record if this is not a popstate call (with or without anchor)
|
|
30
|
-
if (!
|
|
45
|
+
if (!isHistoryVisit) {
|
|
31
46
|
createHistoryRecord(url + (this.scrollToElement || ''));
|
|
32
47
|
}
|
|
33
48
|
|
|
@@ -39,7 +54,7 @@ export const loadPage = function (
|
|
|
39
54
|
// when everything is ready, render the page
|
|
40
55
|
Promise.all<PageRecord | void>([fetchPromise, ...animationPromises])
|
|
41
56
|
.then(([pageData]) => {
|
|
42
|
-
this.renderPage(pageData as PageRecord, {
|
|
57
|
+
this.renderPage(pageData as PageRecord, { event, skipTransition });
|
|
43
58
|
})
|
|
44
59
|
.catch((errorUrl) => {
|
|
45
60
|
// Return early if errorUrl is not defined (probably aborted preload request)
|
|
@@ -54,4 +69,4 @@ export const loadPage = function (
|
|
|
54
69
|
// Go back to the actual page we're still at
|
|
55
70
|
history.go(-1);
|
|
56
71
|
});
|
|
57
|
-
}
|
|
72
|
+
}
|
|
@@ -2,12 +2,15 @@ import { Location, updateHistoryRecord, getCurrentUrl } from '../helpers';
|
|
|
2
2
|
import Swup from '../Swup';
|
|
3
3
|
import { PageRecord } from './Cache';
|
|
4
4
|
|
|
5
|
+
export type PageRenderOptions = {
|
|
6
|
+
event?: Event;
|
|
7
|
+
skipTransition?: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
5
10
|
export const renderPage = function (
|
|
6
11
|
this: Swup,
|
|
7
12
|
page: PageRecord,
|
|
8
|
-
{
|
|
9
|
-
popstate: null
|
|
10
|
-
}
|
|
13
|
+
{ event, skipTransition }: PageRenderOptions = {}
|
|
11
14
|
) {
|
|
12
15
|
document.documentElement.classList.remove('is-leaving');
|
|
13
16
|
|
|
@@ -30,11 +33,11 @@ export const renderPage = function (
|
|
|
30
33
|
document.documentElement.classList.add('is-rendering');
|
|
31
34
|
}
|
|
32
35
|
|
|
33
|
-
this.triggerEvent('willReplaceContent',
|
|
36
|
+
this.triggerEvent('willReplaceContent', event);
|
|
34
37
|
|
|
35
38
|
this.replaceContent(page).then(() => {
|
|
36
|
-
this.triggerEvent('contentReplaced',
|
|
37
|
-
this.triggerEvent('pageView',
|
|
39
|
+
this.triggerEvent('contentReplaced', event);
|
|
40
|
+
this.triggerEvent('pageView', event);
|
|
38
41
|
|
|
39
42
|
// empty cache if it's disabled (in case preload plugin filled it)
|
|
40
43
|
if (!this.options.cache) {
|
|
@@ -42,7 +45,7 @@ export const renderPage = function (
|
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
// Perform in transition
|
|
45
|
-
this.enterPage({
|
|
48
|
+
this.enterPage({ event, skipTransition });
|
|
46
49
|
|
|
47
50
|
// reset scroll-to element
|
|
48
51
|
this.scrollToElement = null;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import Swup from '../Swup';
|
|
2
|
+
|
|
3
|
+
export function updateTransition(this: Swup, from: string, to: string, custom?: string): void {
|
|
4
|
+
this.transition = { from, to, custom };
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function shouldSkipTransition(this: Swup, { event }: { url?: string; event?: Event }) {
|
|
8
|
+
const isHistoryVisit = event instanceof PopStateEvent;
|
|
9
|
+
return !!(isHistoryVisit && !this.options.animateHistoryBrowsing);
|
|
10
|
+
}
|