swup 3.0.3 → 3.0.4

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.
@@ -1,42 +1,61 @@
1
1
  import Swup from '../Swup';
2
+ import delegate from 'delegate-it';
2
3
 
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[]>;
4
+ type HandlersEventMap = {
5
+ animationInDone: undefined;
6
+ animationInStart: undefined;
7
+ animationOutDone: undefined;
8
+ animationOutStart: undefined;
9
+ animationSkipped: undefined;
10
+ clickLink: delegate.Event<MouseEvent>;
11
+ contentReplaced: PopStateEvent | undefined;
12
+ disabled: undefined;
13
+ enabled: undefined;
14
+ openPageInNewTab: delegate.Event<MouseEvent>;
15
+ pageLoaded: undefined;
16
+ pageRetrievedFromCache: undefined;
17
+ pageView: PopStateEvent | undefined;
18
+ popState: PopStateEvent;
19
+ samePage: delegate.Event<MouseEvent>;
20
+ samePageWithHash: delegate.Event<MouseEvent>;
21
+ serverError: undefined;
22
+ transitionStart: PopStateEvent | undefined;
23
+ transitionEnd: PopStateEvent | undefined;
24
+ willReplaceContent: PopStateEvent | undefined;
25
+ };
26
+ type AvailableEventNames = keyof HandlersEventMap;
26
27
 
27
- export function on(this: Swup, event: EventType, handler: Handler) {
28
- if (this._handlers[event]) {
29
- this._handlers[event].push(handler);
28
+ export type Handler<T extends keyof HandlersEventMap> = (event: HandlersEventMap[T]) => void;
29
+ export type Handlers = {
30
+ [Key in keyof HandlersEventMap]: Handler<Key>[];
31
+ };
32
+
33
+ export function on<TEventType extends AvailableEventNames>(
34
+ this: Swup,
35
+ event: TEventType,
36
+ handler: Handler<TEventType>
37
+ ): void {
38
+ const eventHandlers = this._handlers[event] as Handler<TEventType>[];
39
+
40
+ if (eventHandlers) {
41
+ eventHandlers.push(handler);
30
42
  } else {
31
43
  console.warn(`Unsupported event ${event}.`);
32
44
  }
33
45
  }
34
46
 
35
- export function off(this: Swup, event?: EventType, handler?: Handler) {
47
+ export function off<TEventType extends AvailableEventNames>(
48
+ this: Swup,
49
+ event?: TEventType,
50
+ handler?: Handler<TEventType>
51
+ ) {
36
52
  if (event && handler) {
53
+ const eventHandlers = this._handlers[event] as Handler<TEventType>[];
37
54
  // Remove specific handler
38
- if (this._handlers[event].includes(handler)) {
39
- this._handlers[event] = this._handlers[event].filter((h) => h !== handler);
55
+ if (eventHandlers.includes(handler)) {
56
+ (this._handlers[event] as Handler<TEventType>[]) = eventHandlers.filter(
57
+ (h) => h !== handler
58
+ );
40
59
  } else {
41
60
  console.warn(`Handler for event '${event}' not found.`);
42
61
  }
@@ -46,16 +65,22 @@ export function off(this: Swup, event?: EventType, handler?: Handler) {
46
65
  } else {
47
66
  // Remove all handlers for all events
48
67
  Object.keys(this._handlers).forEach((event) => {
49
- this._handlers[event as EventType] = [];
68
+ this._handlers[event as keyof HandlersEventMap] = [];
50
69
  });
51
70
  }
52
71
  }
53
72
 
54
- export function triggerEvent(this: Swup, eventName: EventType, originalEvent?: Event): void {
73
+ export function triggerEvent<TEventType extends AvailableEventNames>(
74
+ this: Swup,
75
+ eventName: TEventType,
76
+ originalEvent?: HandlersEventMap[TEventType]
77
+ ): void {
78
+ const eventHandlers = this._handlers[eventName] as Handler<TEventType>[];
79
+
55
80
  // call saved handlers with "on" method and pass originalEvent object if available
56
- this._handlers[eventName].forEach((handler) => {
81
+ eventHandlers.forEach((handler) => {
57
82
  try {
58
- handler(originalEvent);
83
+ handler(originalEvent as HandlersEventMap[TEventType]);
59
84
  } catch (error) {
60
85
  console.error(error);
61
86
  }
@@ -9,7 +9,7 @@ export type TransitionOptions = {
9
9
 
10
10
  export type PageLoadOptions = {
11
11
  url: string;
12
- event?: Event;
12
+ event?: PopStateEvent;
13
13
  customTransition?: string;
14
14
  };
15
15
 
@@ -3,7 +3,7 @@ import Swup from '../Swup';
3
3
  import { PageRecord } from './Cache';
4
4
 
5
5
  export type PageRenderOptions = {
6
- event?: Event;
6
+ event?: PopStateEvent;
7
7
  skipTransition?: boolean;
8
8
  };
9
9