swup 3.0.0 → 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 +14 -19
- package/dist/types/modules/enterPage.d.ts +2 -4
- package/dist/types/modules/{on.d.ts → events.d.ts} +3 -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 +20 -13
- package/src/__test__/index.test.ts +29 -1
- package/src/modules/enterPage.ts +4 -6
- package/src/modules/events.ts +67 -0
- 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/off.d.ts +0 -3
- package/dist/types/modules/triggerEvent.d.ts +0 -3
- package/dist/types/modules/updateTransition.d.ts +0 -2
- package/src/modules/off.ts +0 -21
- package/src/modules/on.ts +0 -33
- package/src/modules/triggerEvent.ts +0 -21
- package/src/modules/updateTransition.ts +0 -10
package/src/modules/enterPage.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { nextTick } from '../utils';
|
|
2
2
|
import Swup from '../Swup';
|
|
3
|
+
import { PageRenderOptions } from './renderPage';
|
|
3
4
|
|
|
4
|
-
export const enterPage = function (
|
|
5
|
-
this: Swup,
|
|
6
|
-
{ popstate, skipTransition }: { popstate?: PopStateEvent; skipTransition?: boolean }
|
|
7
|
-
) {
|
|
5
|
+
export const enterPage = function (this: Swup, { event, skipTransition }: PageRenderOptions = {}) {
|
|
8
6
|
if (skipTransition) {
|
|
9
|
-
this.triggerEvent('transitionEnd',
|
|
7
|
+
this.triggerEvent('transitionEnd', event);
|
|
10
8
|
this.cleanupAnimationClasses();
|
|
11
9
|
return [Promise.resolve()];
|
|
12
10
|
}
|
|
@@ -19,7 +17,7 @@ export const enterPage = function (
|
|
|
19
17
|
const animationPromises = this.getAnimationPromises('in');
|
|
20
18
|
Promise.all(animationPromises).then(() => {
|
|
21
19
|
this.triggerEvent('animationInDone');
|
|
22
|
-
this.triggerEvent('transitionEnd',
|
|
20
|
+
this.triggerEvent('transitionEnd', event);
|
|
23
21
|
this.cleanupAnimationClasses();
|
|
24
22
|
});
|
|
25
23
|
return animationPromises;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import Swup from '../Swup';
|
|
2
|
+
|
|
3
|
+
export type EventType =
|
|
4
|
+
| 'animationInDone'
|
|
5
|
+
| 'animationInStart'
|
|
6
|
+
| 'animationOutDone'
|
|
7
|
+
| 'animationOutStart'
|
|
8
|
+
| 'animationSkipped'
|
|
9
|
+
| 'clickLink'
|
|
10
|
+
| 'contentReplaced'
|
|
11
|
+
| 'disabled'
|
|
12
|
+
| 'enabled'
|
|
13
|
+
| 'openPageInNewTab'
|
|
14
|
+
| 'pageLoaded'
|
|
15
|
+
| 'pageRetrievedFromCache'
|
|
16
|
+
| 'pageView'
|
|
17
|
+
| 'popState'
|
|
18
|
+
| 'samePage'
|
|
19
|
+
| 'samePageWithHash'
|
|
20
|
+
| 'serverError'
|
|
21
|
+
| 'transitionStart'
|
|
22
|
+
| 'transitionEnd'
|
|
23
|
+
| 'willReplaceContent';
|
|
24
|
+
export type Handler = (event?: Event) => void;
|
|
25
|
+
export type Handlers = Record<EventType, Handler[]>;
|
|
26
|
+
|
|
27
|
+
export function on(this: Swup, event: EventType, handler: Handler) {
|
|
28
|
+
if (this._handlers[event]) {
|
|
29
|
+
this._handlers[event].push(handler);
|
|
30
|
+
} else {
|
|
31
|
+
console.warn(`Unsupported event ${event}.`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function off(this: Swup, event?: EventType, handler?: Handler) {
|
|
36
|
+
if (event && handler) {
|
|
37
|
+
// Remove specific handler
|
|
38
|
+
if (this._handlers[event].includes(handler)) {
|
|
39
|
+
this._handlers[event] = this._handlers[event].filter((h) => h !== handler);
|
|
40
|
+
} else {
|
|
41
|
+
console.warn(`Handler for event '${event}' not found.`);
|
|
42
|
+
}
|
|
43
|
+
} else if (event) {
|
|
44
|
+
// Remove all handlers for specific event
|
|
45
|
+
this._handlers[event] = [];
|
|
46
|
+
} else {
|
|
47
|
+
// Remove all handlers for all events
|
|
48
|
+
Object.keys(this._handlers).forEach((event) => {
|
|
49
|
+
this._handlers[event as EventType] = [];
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function triggerEvent(this: Swup, eventName: EventType, originalEvent?: Event): void {
|
|
55
|
+
// call saved handlers with "on" method and pass originalEvent object if available
|
|
56
|
+
this._handlers[eventName].forEach((handler) => {
|
|
57
|
+
try {
|
|
58
|
+
handler(originalEvent);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error(error);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// trigger event on document with prefix "swup:"
|
|
65
|
+
const event = new CustomEvent(`swup:${eventName}`, { detail: eventName });
|
|
66
|
+
document.dispatchEvent(event);
|
|
67
|
+
}
|
package/src/modules/leavePage.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import Swup from '../Swup';
|
|
2
|
-
import {
|
|
2
|
+
import { PageRenderOptions } from './renderPage';
|
|
3
|
+
|
|
4
|
+
export const leavePage = function (this: Swup, { event, skipTransition }: PageRenderOptions = {}) {
|
|
5
|
+
const isHistoryVisit = event instanceof PopStateEvent;
|
|
3
6
|
|
|
4
|
-
export const leavePage = function (
|
|
5
|
-
this: Swup,
|
|
6
|
-
data: TransitionOptions,
|
|
7
|
-
{ popstate, skipTransition }: { popstate: PopStateEvent | null; skipTransition?: boolean } = {
|
|
8
|
-
popstate: null
|
|
9
|
-
}
|
|
10
|
-
) {
|
|
11
7
|
if (skipTransition) {
|
|
12
8
|
this.triggerEvent('animationSkipped');
|
|
13
9
|
return [Promise.resolve()];
|
|
@@ -17,7 +13,7 @@ export const leavePage = function (
|
|
|
17
13
|
|
|
18
14
|
// handle classes
|
|
19
15
|
document.documentElement.classList.add('is-changing', 'is-leaving', 'is-animating');
|
|
20
|
-
if (
|
|
16
|
+
if (isHistoryVisit) {
|
|
21
17
|
document.documentElement.classList.add('is-popstate');
|
|
22
18
|
}
|
|
23
19
|
|
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
|
+
}
|
package/src/modules/off.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import Swup from '../Swup';
|
|
2
|
-
import { EventType, Handler } from './on';
|
|
3
|
-
|
|
4
|
-
export const off = function off(this: Swup, event?: EventType, handler?: Handler) {
|
|
5
|
-
if (event && handler) {
|
|
6
|
-
// Remove specific handler
|
|
7
|
-
if (this._handlers[event].includes(handler)) {
|
|
8
|
-
this._handlers[event] = this._handlers[event].filter((h) => h !== handler);
|
|
9
|
-
} else {
|
|
10
|
-
console.warn(`Handler for event '${event}' not found.`);
|
|
11
|
-
}
|
|
12
|
-
} else if (event) {
|
|
13
|
-
// Remove all handlers for specific event
|
|
14
|
-
this._handlers[event] = [];
|
|
15
|
-
} else {
|
|
16
|
-
// Remove all handlers for all events
|
|
17
|
-
Object.keys(this._handlers).forEach((event) => {
|
|
18
|
-
this._handlers[event as EventType] = [];
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
};
|
package/src/modules/on.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import Swup from '../Swup';
|
|
2
|
-
|
|
3
|
-
export type EventType =
|
|
4
|
-
| 'animationInDone'
|
|
5
|
-
| 'animationInStart'
|
|
6
|
-
| 'animationOutDone'
|
|
7
|
-
| 'animationOutStart'
|
|
8
|
-
| 'animationSkipped'
|
|
9
|
-
| 'clickLink'
|
|
10
|
-
| 'contentReplaced'
|
|
11
|
-
| 'disabled'
|
|
12
|
-
| 'enabled'
|
|
13
|
-
| 'openPageInNewTab'
|
|
14
|
-
| 'pageLoaded'
|
|
15
|
-
| 'pageRetrievedFromCache'
|
|
16
|
-
| 'pageView'
|
|
17
|
-
| 'popState'
|
|
18
|
-
| 'samePage'
|
|
19
|
-
| 'samePageWithHash'
|
|
20
|
-
| 'serverError'
|
|
21
|
-
| 'transitionStart'
|
|
22
|
-
| 'transitionEnd'
|
|
23
|
-
| 'willReplaceContent';
|
|
24
|
-
export type Handler = (event?: Event) => void;
|
|
25
|
-
export type Handlers = Record<EventType, Handler[]>;
|
|
26
|
-
|
|
27
|
-
export const on = function on(this: Swup, event: EventType, handler: Handler) {
|
|
28
|
-
if (this._handlers[event]) {
|
|
29
|
-
this._handlers[event].push(handler);
|
|
30
|
-
} else {
|
|
31
|
-
console.warn(`Unsupported event ${event}.`);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { EventType } from './on';
|
|
2
|
-
import Swup from '../Swup';
|
|
3
|
-
|
|
4
|
-
export const triggerEvent = function (
|
|
5
|
-
this: Swup,
|
|
6
|
-
eventName: EventType,
|
|
7
|
-
originalEvent?: PopStateEvent | MouseEvent
|
|
8
|
-
): void {
|
|
9
|
-
// call saved handlers with "on" method and pass originalEvent object if available
|
|
10
|
-
this._handlers[eventName].forEach((handler) => {
|
|
11
|
-
try {
|
|
12
|
-
handler(originalEvent);
|
|
13
|
-
} catch (error) {
|
|
14
|
-
console.error(error);
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
// trigger event on document with prefix "swup:"
|
|
19
|
-
const event = new CustomEvent(`swup:${eventName}`, { detail: eventName });
|
|
20
|
-
document.dispatchEvent(event);
|
|
21
|
-
};
|