summaryrefslogtreecommitdiff
path: root/node_modules/swup/src/modules
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/swup/src/modules')
-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
3 files changed, 31 insertions, 4 deletions
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;