summaryrefslogtreecommitdiff
path: root/node_modules/swup/src
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2024-10-13 10:37:19 +0800
committerlittleblack111 <littleblack11111@gmail.com>2024-10-13 10:37:19 +0800
commite29b244aeab08ea36fb385c7beb2ad0560217039 (patch)
treec66b744dc29573f1acc871862937a23f1b80e6bf /node_modules/swup/src
parent62c2881629e6413a8fec6d3dd092e4568e2a81dc (diff)
bump: @babel/cli 7.25.6 → 7.25.7, @babel/core 7.25.2 → 7.25.8, @babel/preset-env 7.25.4 → 7.25.8, postcss 8.4.41 → 8.4.47, swup 4.7.0 → 4.8.1
Diffstat (limited to 'node_modules/swup/src')
-rw-r--r--node_modules/swup/src/Swup.ts13
-rw-r--r--node_modules/swup/src/modules/Hooks.ts26
-rw-r--r--node_modules/swup/src/modules/Visit.ts4
-rwxr-xr-xnode_modules/swup/src/modules/navigate.ts5
4 files changed, 43 insertions, 5 deletions
diff --git a/node_modules/swup/src/Swup.ts b/node_modules/swup/src/Swup.ts
index 8d5d769e..7fb76f06 100644
--- a/node_modules/swup/src/Swup.ts
+++ b/node_modules/swup/src/Swup.ts
@@ -8,7 +8,7 @@ import { type DelegateEventUnsubscribe } from './helpers/delegateEvent.js';
import { Cache } from './modules/Cache.js';
import { Classes } from './modules/Classes.js';
import { type Visit, createVisit } from './modules/Visit.js';
-import { Hooks } from './modules/Hooks.js';
+import { Hooks, type HookName, type HookInitOptions } from './modules/Hooks.js';
import { getAnchorElement } from './modules/getAnchorElement.js';
import { awaitAnimations } from './modules/awaitAnimations.js';
import { navigate, performNavigation, type NavigationToSelfAction } from './modules/navigate.js';
@@ -43,6 +43,8 @@ export type Options = {
linkToSelf: NavigationToSelfAction;
/** Enable native animations using the View Transitions API. */
native: boolean;
+ /** Hook handlers to register. */
+ hooks: Partial<HookInitOptions>;
/** Plugins to register on startup. */
plugins: Plugin[];
/** Custom headers sent along with fetch requests. */
@@ -61,6 +63,7 @@ const defaults: Options = {
animationScope: 'html',
cache: true,
containers: ['#swup'],
+ hooks: {},
ignoreVisit: (url, { el } = {}) => !!el?.closest('[data-no-swup]'),
linkSelector: 'a[href]',
linkToSelf: 'scroll',
@@ -188,6 +191,14 @@ export default class Swup {
// Mount plugins
this.options.plugins.forEach((plugin) => this.use(plugin));
+ // Install user hooks
+ for (const [key, handler] of Object.entries(this.options.hooks)) {
+ // Build hook options from modifier suffix: 'content:replace.before' => { before: true }
+ const [hook, modifiers] = this.hooks.parseName(key as HookName);
+ // @ts-expect-error: object.entries() does not preserve key/value types
+ this.hooks.on(hook, handler, modifiers);
+ }
+
// Create initial history record
if ((window.history.state as HistoryState)?.source !== 'swup') {
updateHistoryRecord(null, { index: this.currentHistoryIndex });
diff --git a/node_modules/swup/src/modules/Hooks.ts b/node_modules/swup/src/modules/Hooks.ts
index 36af2bf7..e3fb459c 100644
--- a/node_modules/swup/src/modules/Hooks.ts
+++ b/node_modules/swup/src/modules/Hooks.ts
@@ -49,6 +49,10 @@ export type HookArguments<T extends HookName> = HookDefinitions[T];
export type HookName = keyof HookDefinitions;
+export type HookNameWithModifier = `${HookName}.${HookModifier}`;
+
+type HookModifier = 'once' | 'before' | 'replace';
+
/** A generic hook handler. */
export type HookHandler<T extends HookName> = (
/** Context about the current visit. */
@@ -71,6 +75,12 @@ export type Handlers = {
[K in HookName]: HookHandler<K>[];
};
+export type HookInitOptions = {
+ [K in HookName as K | `${K}.${HookModifier}`]: HookHandler<K>;
+} & {
+ [K in HookName as K | `${K}.${HookModifier}.${HookModifier}`]: HookHandler<K>;
+};
+
/** Unregister a previously registered hook handler. */
export type HookUnregister = () => void;
@@ -516,7 +526,7 @@ export class Hooks {
handler = [{ id: 0, hook, handler: defaultHandler }];
if (replaced) {
const index = replace.length - 1;
- const replacingHandler = replace[index].handler;
+ const { handler: replacingHandler, once } = replace[index];
const createDefaultHandler = (index: number): HookDefaultHandler<T> | undefined => {
const next = replace[index - 1];
if (next) {
@@ -527,9 +537,7 @@ export class Hooks {
}
};
const nestedDefaultHandler = createDefaultHandler(index);
- handler = [
- { id: 0, hook, handler: replacingHandler, defaultHandler: nestedDefaultHandler }
- ];
+ handler = [{ id: 0, hook, once, handler: replacingHandler, defaultHandler: nestedDefaultHandler }]; // prettier-ignore
}
}
@@ -570,4 +578,14 @@ export class Hooks {
new CustomEvent<HookEventDetail>(`swup:${hook}`, { detail, bubbles: true })
);
}
+
+ /**
+ * Parse a hook name into the name and any modifiers.
+ * @param hook Name of the hook.
+ */
+ parseName(hook: HookName | HookNameWithModifier): [HookName, Partial<HookOptions>] {
+ const [name, ...modifiers] = hook.split('.');
+ const options = modifiers.reduce((acc, mod) => ({ ...acc, [mod]: true }), {});
+ return [name as HookName, options];
+ }
}
diff --git a/node_modules/swup/src/modules/Visit.ts b/node_modules/swup/src/modules/Visit.ts
index b0d5d271..17ed6c7d 100644
--- a/node_modules/swup/src/modules/Visit.ts
+++ b/node_modules/swup/src/modules/Visit.ts
@@ -114,6 +114,8 @@ export class Visit {
history: VisitHistory;
/** Scroll behavior on this visit */
scroll: VisitScroll;
+ /** User-defined metadata */
+ meta: Record<string, unknown>;
constructor(swup: Swup, options: VisitInitOptions) {
const { to, from, hash, el, event } = options;
@@ -145,7 +147,9 @@ export class Visit {
reset: true,
target: undefined
};
+ this.meta = {};
}
+
/** @internal */
advance(state: VisitState) {
if (this.state < state) {
diff --git a/node_modules/swup/src/modules/navigate.ts b/node_modules/swup/src/modules/navigate.ts
index 7793f891..ac88935b 100755
--- a/node_modules/swup/src/modules/navigate.ts
+++ b/node_modules/swup/src/modules/navigate.ts
@@ -19,6 +19,8 @@ type NavigationOptions = {
history?: HistoryAction;
/** Whether this visit should read from or write to the cache. */
cache?: CacheControl;
+ /** Custom metadata associated with this visit. */
+ meta?: Record<string, unknown>;
};
/**
@@ -106,6 +108,9 @@ export async function performNavigation(
visit.animation.name = animation;
}
+ // Get custom metadata from option
+ visit.meta = options.meta || {};
+
// Sanitize cache option
if (typeof options.cache === 'object') {
visit.cache.read = options.cache.read ?? visit.cache.read;