swup 3.1.1 → 4.0.0-rc.20

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.
Files changed (79) hide show
  1. package/README.md +94 -0
  2. package/dist/Swup.cjs +1 -1
  3. package/dist/Swup.cjs.map +1 -1
  4. package/dist/Swup.modern.js +1 -1
  5. package/dist/Swup.modern.js.map +1 -1
  6. package/dist/Swup.module.js +1 -1
  7. package/dist/Swup.module.js.map +1 -1
  8. package/dist/Swup.umd.js +1 -1
  9. package/dist/Swup.umd.js.map +1 -1
  10. package/dist/types/Swup.d.ts +53 -45
  11. package/dist/types/helpers/Location.d.ts +10 -7
  12. package/dist/types/helpers/delegateEvent.d.ts +2 -2
  13. package/dist/types/helpers/matchPath.d.ts +3 -0
  14. package/dist/types/helpers.d.ts +1 -4
  15. package/dist/types/index.d.ts +7 -4
  16. package/dist/types/modules/Cache.d.ts +14 -14
  17. package/dist/types/modules/Classes.d.ts +13 -0
  18. package/dist/types/modules/Context.d.ts +73 -0
  19. package/dist/types/modules/Hooks.d.ts +241 -0
  20. package/dist/types/modules/__test__/cache.test.d.ts +1 -0
  21. package/dist/types/modules/__test__/hooks.test.d.ts +1 -0
  22. package/dist/types/modules/__test__/replaceContent.test.d.ts +1 -0
  23. package/dist/types/modules/awaitAnimations.d.ts +21 -0
  24. package/dist/types/modules/enterPage.d.ts +5 -2
  25. package/dist/types/modules/fetchPage.d.ts +23 -3
  26. package/dist/types/modules/getAnchorElement.d.ts +2 -1
  27. package/dist/types/modules/leavePage.d.ts +5 -2
  28. package/dist/types/modules/plugins.d.ts +7 -0
  29. package/dist/types/modules/renderPage.d.ts +6 -6
  30. package/dist/types/modules/replaceContent.d.ts +8 -11
  31. package/dist/types/modules/visit.d.ts +33 -0
  32. package/dist/types/utils/index.d.ts +3 -1
  33. package/package.json +13 -9
  34. package/src/Swup.ts +172 -182
  35. package/src/__test__/index.test.ts +8 -3
  36. package/src/helpers/Location.ts +12 -9
  37. package/src/helpers/__test__/matchPath.test.ts +54 -0
  38. package/src/helpers/delegateEvent.ts +3 -2
  39. package/src/helpers/matchPath.ts +22 -0
  40. package/src/helpers.ts +2 -5
  41. package/src/index.ts +36 -4
  42. package/src/modules/Cache.ts +43 -33
  43. package/src/modules/Classes.ts +48 -0
  44. package/src/modules/Context.ts +121 -0
  45. package/src/modules/Hooks.ts +413 -0
  46. package/src/modules/__test__/cache.test.ts +142 -0
  47. package/src/modules/__test__/hooks.test.ts +263 -0
  48. package/src/modules/__test__/replaceContent.test.ts +92 -0
  49. package/src/modules/awaitAnimations.ts +169 -0
  50. package/src/modules/enterPage.ts +23 -17
  51. package/src/modules/fetchPage.ts +74 -29
  52. package/src/modules/getAnchorElement.ts +2 -1
  53. package/src/modules/leavePage.ts +26 -20
  54. package/src/modules/plugins.ts +7 -2
  55. package/src/modules/renderPage.ts +52 -33
  56. package/src/modules/replaceContent.ts +33 -16
  57. package/src/modules/visit.ts +143 -0
  58. package/src/utils/index.ts +25 -5
  59. package/dist/types/helpers/cleanupAnimationClasses.d.ts +0 -2
  60. package/dist/types/helpers/fetch.d.ts +0 -5
  61. package/dist/types/helpers/getDataFromHtml.d.ts +0 -7
  62. package/dist/types/helpers/markSwupElements.d.ts +0 -1
  63. package/dist/types/modules/events.d.ts +0 -33
  64. package/dist/types/modules/getAnimationPromises.d.ts +0 -7
  65. package/dist/types/modules/getPageData.d.ts +0 -6
  66. package/dist/types/modules/loadPage.d.ts +0 -15
  67. package/dist/types/modules/transitions.d.ts +0 -6
  68. package/readme.md +0 -60
  69. package/src/helpers/cleanupAnimationClasses.ts +0 -8
  70. package/src/helpers/fetch.ts +0 -33
  71. package/src/helpers/getDataFromHtml.ts +0 -39
  72. package/src/helpers/markSwupElements.ts +0 -16
  73. package/src/modules/__test__/events.test.ts +0 -72
  74. package/src/modules/events.ts +0 -92
  75. package/src/modules/getAnimationPromises.ts +0 -183
  76. package/src/modules/getPageData.ts +0 -24
  77. package/src/modules/loadPage.ts +0 -81
  78. package/src/modules/transitions.ts +0 -10
  79. /package/dist/types/{modules/__test__/events.test.d.ts → helpers/__test__/matchPath.test.d.ts} +0 -0
package/src/helpers.ts CHANGED
@@ -1,13 +1,10 @@
1
1
  // Re-export all helpers to allow custom package export path
2
- // e.g. import { getPageData } from 'swup/helpers'
2
+ // e.g. import { updateHistoryRecord } from 'swup'
3
3
 
4
4
  export { classify } from './helpers/classify.js';
5
5
  export { createHistoryRecord } from './helpers/createHistoryRecord.js';
6
6
  export { updateHistoryRecord } from './helpers/updateHistoryRecord.js';
7
7
  export { delegateEvent } from './helpers/delegateEvent.js';
8
- export { getDataFromHtml } from './helpers/getDataFromHtml.js';
9
- export { fetch } from './helpers/fetch.js';
10
8
  export { getCurrentUrl } from './helpers/getCurrentUrl.js';
11
9
  export { Location } from './helpers/Location.js';
12
- export { markSwupElements } from './helpers/markSwupElements.js';
13
- export { cleanupAnimationClasses } from './helpers/cleanupAnimationClasses.js';
10
+ export { matchPath } from './helpers/matchPath.js';
package/src/index.ts CHANGED
@@ -1,10 +1,42 @@
1
- import Swup, { Options } from './Swup.js';
2
- import { Plugin } from './modules/plugins.js';
3
- import { Handler } from './modules/events.js';
1
+ import Swup, { type Options } from './Swup.js';
2
+ import type { CacheData } from './modules/Cache.js';
3
+ import type {
4
+ Context,
5
+ FromContext,
6
+ ToContext,
7
+ AnimationContext,
8
+ ScrollContext,
9
+ HistoryContext
10
+ } from './modules/Context.js';
11
+ import type { Plugin } from './modules/plugins.js';
12
+ import type {
13
+ HookDefinitions,
14
+ HookName,
15
+ HookOptions,
16
+ HookUnregister,
17
+ Handler
18
+ } from './modules/Hooks.js';
19
+ import type { Path } from 'path-to-regexp';
4
20
 
5
21
  export default Swup;
6
22
 
7
23
  export * from './helpers.js';
8
24
  export * from './utils.js';
9
25
 
10
- export type { Options, Plugin, Handler };
26
+ export type {
27
+ Options,
28
+ Plugin,
29
+ CacheData,
30
+ Context,
31
+ FromContext,
32
+ ToContext,
33
+ AnimationContext,
34
+ ScrollContext,
35
+ HistoryContext,
36
+ HookDefinitions,
37
+ HookName,
38
+ HookOptions,
39
+ HookUnregister,
40
+ Handler,
41
+ Path
42
+ };
@@ -1,55 +1,65 @@
1
- import { getCurrentUrl, Location } from '../helpers.js';
2
1
  import Swup from '../Swup.js';
3
- import { PageData } from './getPageData.js';
2
+ import { Location } from '../helpers.js';
3
+ import { PageData } from './fetchPage.js';
4
+
5
+ export interface CacheData extends PageData {}
4
6
 
5
- export interface PageRecord extends PageData {
6
- url: string;
7
- responseURL: string;
8
- }
9
7
  export class Cache {
10
- pages: Record<string, PageRecord> = {};
11
- last: PageRecord | null = null;
12
- swup: Swup;
8
+ private swup: Swup;
9
+ private pages: Map<string, CacheData> = new Map();
13
10
 
14
11
  constructor(swup: Swup) {
15
12
  this.swup = swup;
16
13
  }
17
14
 
18
- getCacheUrl(url: string): string {
19
- return this.swup.resolveUrl(Location.fromUrl(url).url);
15
+ get size() {
16
+ return this.pages.size;
17
+ }
18
+
19
+ get all() {
20
+ return this.pages;
21
+ }
22
+
23
+ public has(url: string): boolean {
24
+ return this.pages.has(this.resolve(url));
25
+ }
26
+
27
+ public get(url: string): CacheData | undefined {
28
+ return this.pages.get(this.resolve(url));
20
29
  }
21
30
 
22
- cacheUrl(page: PageRecord) {
23
- page.url = this.getCacheUrl(page.url);
24
- if (page.url in this.pages === false) {
25
- this.pages[page.url] = page;
26
- }
27
- page.responseURL = this.getCacheUrl(page.responseURL);
28
- this.last = this.pages[page.url];
29
- this.swup.log(`Cache (${Object.keys(this.pages).length})`, this.pages);
31
+ public set(url: string, page: CacheData) {
32
+ url = this.resolve(url);
33
+ page = { ...page, url };
34
+ this.pages.set(url, page);
35
+ this.swup.hooks.triggerSync('cache:set', { page });
30
36
  }
31
37
 
32
- getPage(url: string): PageRecord {
33
- url = this.getCacheUrl(url);
34
- return this.pages[url];
38
+ public update(url: string, page: CacheData) {
39
+ url = this.resolve(url);
40
+ page = { ...this.get(url), ...page, url };
41
+ this.pages.set(url, page);
35
42
  }
36
43
 
37
- getCurrentPage(): PageRecord {
38
- return this.getPage(getCurrentUrl());
44
+ public delete(url: string): void {
45
+ this.pages.delete(this.resolve(url));
39
46
  }
40
47
 
41
- exists(url: string): boolean {
42
- url = this.getCacheUrl(url);
43
- return url in this.pages;
48
+ public clear(): void {
49
+ this.pages.clear();
50
+ this.swup.hooks.triggerSync('cache:clear');
44
51
  }
45
52
 
46
- empty(): void {
47
- this.pages = {};
48
- this.last = null;
49
- this.swup.log('Cache cleared');
53
+ public prune(predicate: (url: string, page: CacheData) => boolean): void {
54
+ this.pages.forEach((page, url) => {
55
+ if (predicate(url, page)) {
56
+ this.delete(url);
57
+ }
58
+ });
50
59
  }
51
60
 
52
- remove(url: string): void {
53
- delete this.pages[this.getCacheUrl(url)];
61
+ private resolve(urlToResolve: string): string {
62
+ const { url } = Location.fromUrl(urlToResolve);
63
+ return this.swup.resolveUrl(url);
54
64
  }
55
65
  }
@@ -0,0 +1,48 @@
1
+ import Swup from '../Swup.js';
2
+ import { queryAll } from '../utils.js';
3
+
4
+ export class Classes {
5
+ public swup: Swup;
6
+
7
+ swupClasses = ['to-', 'is-changing', 'is-rendering', 'is-popstate', 'is-animating'];
8
+
9
+ constructor(swup: Swup) {
10
+ this.swup = swup;
11
+ }
12
+
13
+ get selectors(): string[] {
14
+ const { scope } = this.swup.context.animation;
15
+ if (scope === 'containers') return this.swup.context.containers;
16
+ if (scope === 'html') return ['html'];
17
+ if (Array.isArray(scope)) return scope;
18
+ return [];
19
+ }
20
+
21
+ get selector(): string {
22
+ return this.selectors.join(',');
23
+ }
24
+
25
+ get targets(): HTMLElement[] {
26
+ if (!this.selector.trim()) return [];
27
+ return queryAll(this.selector) as HTMLElement[];
28
+ }
29
+
30
+ public add(...classes: string[]): void {
31
+ this.targets.forEach((target) => target.classList.add(...classes));
32
+ }
33
+
34
+ public remove(...classes: string[]): void {
35
+ this.targets.forEach((target) => target.classList.remove(...classes));
36
+ }
37
+
38
+ public clear(): void {
39
+ this.targets.forEach((target) => {
40
+ const remove = target.className.split(' ').filter((c) => this.isSwupClass(c));
41
+ target.classList.remove(...remove);
42
+ });
43
+ }
44
+
45
+ private isSwupClass(className: string): boolean {
46
+ return this.swupClasses.some((c) => className.startsWith(c));
47
+ }
48
+ }
@@ -0,0 +1,121 @@
1
+ import Swup, { Options } from '../Swup.js';
2
+ import { HistoryAction, HistoryDirection } from './visit.js';
3
+
4
+ export interface Context<TEvent = Event> {
5
+ /** The previous page, about to leave */
6
+ from: FromContext;
7
+ /** The next page, about to enter */
8
+ to: ToContext;
9
+ /** The content containers, about to be replaced */
10
+ containers: Options['containers'];
11
+ /** Information about animated page transitions */
12
+ animation: AnimationContext;
13
+ /** What triggered this visit */
14
+ trigger: TriggerContext<TEvent>;
15
+ /** Browser history behavior on this visit */
16
+ history: HistoryContext;
17
+ /** Scroll behavior on this visit */
18
+ scroll: ScrollContext;
19
+ }
20
+
21
+ export interface FromContext {
22
+ /** The URL of the previous page */
23
+ url: string;
24
+ }
25
+
26
+ export interface ToContext {
27
+ /** The URL of the next page */
28
+ url?: string;
29
+ /** The HTML content of the next page */
30
+ html?: string;
31
+ }
32
+
33
+ export interface AnimationContext {
34
+ /** Whether this visit is animated. Default: `true` */
35
+ animate: boolean;
36
+ /** Whether to wait for the next page to load before starting the animation. Default: `false` */
37
+ wait: boolean;
38
+ /** Name of a custom animation to run. */
39
+ name?: string;
40
+ /** Elements on which to add animation classes. Default: `html` element */
41
+ scope: 'html' | 'containers' | string[];
42
+ /** Selector for detecting animation timing. Default: `[class*="transition-"]` */
43
+ selector: Options['animationSelector'];
44
+ }
45
+
46
+ export interface ScrollContext {
47
+ /** Whether to reset the scroll position after the visit. Default: `true` */
48
+ reset: boolean;
49
+ /** Anchor element to scroll to on the next page. */
50
+ target?: string;
51
+ }
52
+
53
+ export interface TriggerContext<TEvent = Event> {
54
+ /** DOM element that triggered this visit. */
55
+ el?: Element;
56
+ /** DOM event that triggered this visit. */
57
+ event?: TEvent;
58
+ }
59
+
60
+ export interface HistoryContext {
61
+ /** History action to perform: `push` for creating a new history entry, `replace` for replacing the current entry. Default: `push` */
62
+ action: HistoryAction;
63
+ /** Whether this visit was triggered by a browser history navigation. */
64
+ popstate: boolean;
65
+ /** The direction of travel in case of a browser history navigation: backward or forward. */
66
+ direction: HistoryDirection | undefined;
67
+ }
68
+
69
+ export interface ContextInitOptions {
70
+ to: string | undefined;
71
+ from?: string;
72
+ hash?: string;
73
+ animate?: boolean;
74
+ animation?: string;
75
+ targets?: string[];
76
+ el?: Element;
77
+ event?: Event;
78
+ action?: HistoryAction;
79
+ resetScroll?: boolean;
80
+ }
81
+
82
+ export function createContext(
83
+ this: Swup,
84
+ {
85
+ to,
86
+ from = this.currentPageUrl,
87
+ hash: target,
88
+ animate = true,
89
+ animation: name,
90
+ el,
91
+ event,
92
+ action = 'push',
93
+ resetScroll: reset = true
94
+ }: ContextInitOptions
95
+ ): Context {
96
+ return {
97
+ from: { url: from },
98
+ to: { url: to },
99
+ containers: this.options.containers,
100
+ animation: {
101
+ animate,
102
+ wait: false,
103
+ name,
104
+ scope: this.options.animationScope,
105
+ selector: this.options.animationSelector
106
+ },
107
+ trigger: {
108
+ el,
109
+ event
110
+ },
111
+ history: {
112
+ action,
113
+ popstate: false,
114
+ direction: undefined
115
+ },
116
+ scroll: {
117
+ reset,
118
+ target
119
+ }
120
+ };
121
+ }