defuss 3.4.3 → 3.4.5
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/index.cjs +18 -18
- package/dist/index.d.ts +17 -17
- package/dist/index.mjs +18 -18
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1163,10 +1163,10 @@ const setupRouter = (config = {
|
|
|
1163
1163
|
pendingResolvers: [],
|
|
1164
1164
|
currentPath: "",
|
|
1165
1165
|
popAttached: false,
|
|
1166
|
-
lifecycleHooks: {
|
|
1166
|
+
lifecycleHooks: { beforeLeave: [], leave: [] }
|
|
1167
1167
|
};
|
|
1168
1168
|
if (!state.lifecycleHooks) {
|
|
1169
|
-
state.lifecycleHooks = {
|
|
1169
|
+
state.lifecycleHooks = { beforeLeave: [], leave: [] };
|
|
1170
1170
|
}
|
|
1171
1171
|
const routeRegistrations = state.routeRegistrations;
|
|
1172
1172
|
if (typeof window !== "undefined" && !windowImpl) {
|
|
@@ -1294,31 +1294,31 @@ const setupRouter = (config = {
|
|
|
1294
1294
|
createRouteContext(request) {
|
|
1295
1295
|
return {
|
|
1296
1296
|
request,
|
|
1297
|
-
|
|
1298
|
-
state.lifecycleHooks.
|
|
1297
|
+
onBeforeLeave(fn) {
|
|
1298
|
+
state.lifecycleHooks.beforeLeave.push(fn);
|
|
1299
1299
|
},
|
|
1300
|
-
|
|
1301
|
-
state.lifecycleHooks.
|
|
1300
|
+
onLeave(fn) {
|
|
1301
|
+
state.lifecycleHooks.leave.push(fn);
|
|
1302
1302
|
}
|
|
1303
1303
|
};
|
|
1304
1304
|
},
|
|
1305
|
-
async
|
|
1306
|
-
for (const fn of state.lifecycleHooks.
|
|
1305
|
+
async runBeforeLeaveHooks() {
|
|
1306
|
+
for (const fn of state.lifecycleHooks.beforeLeave) {
|
|
1307
1307
|
const result = await fn();
|
|
1308
1308
|
if (result === false) return false;
|
|
1309
1309
|
}
|
|
1310
1310
|
return true;
|
|
1311
1311
|
},
|
|
1312
|
-
|
|
1313
|
-
for (const fn of state.lifecycleHooks.
|
|
1312
|
+
runLeaveHooks() {
|
|
1313
|
+
for (const fn of state.lifecycleHooks.leave) {
|
|
1314
1314
|
fn();
|
|
1315
1315
|
}
|
|
1316
1316
|
},
|
|
1317
1317
|
clearRouteLifecycle() {
|
|
1318
|
-
const
|
|
1319
|
-
state.lifecycleHooks.
|
|
1320
|
-
state.lifecycleHooks.
|
|
1321
|
-
return {
|
|
1318
|
+
const oldLeaveHooks = [...state.lifecycleHooks.leave];
|
|
1319
|
+
state.lifecycleHooks.beforeLeave = [];
|
|
1320
|
+
state.lifecycleHooks.leave = [];
|
|
1321
|
+
return { leaveHooks: oldLeaveHooks };
|
|
1322
1322
|
}
|
|
1323
1323
|
};
|
|
1324
1324
|
const handlePopState = (event) => {
|
|
@@ -1349,7 +1349,7 @@ if (!globalThis[ROUTER_STATE_KEY]) {
|
|
|
1349
1349
|
pendingResolvers: [],
|
|
1350
1350
|
currentPath: "",
|
|
1351
1351
|
popAttached: false,
|
|
1352
|
-
lifecycleHooks: {
|
|
1352
|
+
lifecycleHooks: { beforeLeave: [], leave: [] }
|
|
1353
1353
|
};
|
|
1354
1354
|
}
|
|
1355
1355
|
const getRouterState = () => globalThis[ROUTER_STATE_KEY];
|
|
@@ -1422,18 +1422,18 @@ const RouterSlot = ({
|
|
|
1422
1422
|
const currentPath = router.getRequest().path;
|
|
1423
1423
|
const isSamePath = currentPath === lastPath;
|
|
1424
1424
|
if (!isSamePath) {
|
|
1425
|
-
const allowed = await router.
|
|
1425
|
+
const allowed = await router.runBeforeLeaveHooks();
|
|
1426
1426
|
if (!allowed) {
|
|
1427
1427
|
window.history.pushState({}, "", lastPath);
|
|
1428
1428
|
router.resolve(lastPath);
|
|
1429
1429
|
return;
|
|
1430
1430
|
}
|
|
1431
|
-
const {
|
|
1431
|
+
const { leaveHooks } = router.clearRouteLifecycle();
|
|
1432
1432
|
await dom.$(ref).update(
|
|
1433
1433
|
typeof RouterOutlet === "function" ? RouterOutlet() : [],
|
|
1434
1434
|
transitionConfig
|
|
1435
1435
|
);
|
|
1436
|
-
for (const fn of
|
|
1436
|
+
for (const fn of leaveHooks) {
|
|
1437
1437
|
fn();
|
|
1438
1438
|
}
|
|
1439
1439
|
} else {
|
package/dist/index.d.ts
CHANGED
|
@@ -137,8 +137,8 @@ declare const T: ({ key, values, tag, ref, ...attrs }: TransProps<string>) => VN
|
|
|
137
137
|
type OnHandleRouteChangeFn = (newRoute: string, oldRoute: string) => void;
|
|
138
138
|
type OnRouteChangeFn = (cb: OnHandleRouteChangeFn) => void;
|
|
139
139
|
type RouterStrategy = "page-refresh" | "slot-refresh";
|
|
140
|
-
type
|
|
141
|
-
type
|
|
140
|
+
type BeforeLeaveHookFn = () => boolean | void | Promise<boolean | void>;
|
|
141
|
+
type LeaveHookFn = () => void;
|
|
142
142
|
/**
|
|
143
143
|
* Context object passed to components rendered via Route's `component` prop.
|
|
144
144
|
* Provides access to the current route request and lifecycle hooks.
|
|
@@ -146,11 +146,11 @@ type UnmountHookFn = () => void;
|
|
|
146
146
|
* @example
|
|
147
147
|
* ```tsx
|
|
148
148
|
* const MyScreen = ({ route }: { route: RouteContext }) => {
|
|
149
|
-
* route.
|
|
149
|
+
* route.onBeforeLeave(() => {
|
|
150
150
|
* // Return false to block navigation (e.g. unsaved changes)
|
|
151
151
|
* return confirm("Leave page?");
|
|
152
152
|
* });
|
|
153
|
-
* route.
|
|
153
|
+
* route.onLeave(() => {
|
|
154
154
|
* console.log("Route was left");
|
|
155
155
|
* });
|
|
156
156
|
* return <div>Current path: {route.request.path}</div>;
|
|
@@ -165,26 +165,26 @@ interface RouteContext {
|
|
|
165
165
|
* Returning `false` (or a Promise resolving to `false`) blocks navigation,
|
|
166
166
|
* allowing implementation of confirmation dialogs.
|
|
167
167
|
*/
|
|
168
|
-
|
|
168
|
+
onBeforeLeave(fn: BeforeLeaveHookFn): void;
|
|
169
169
|
/**
|
|
170
170
|
* Register a hook that fires after the route has been unmounted
|
|
171
171
|
* (navigation completed, new route is rendered).
|
|
172
172
|
*/
|
|
173
|
-
|
|
173
|
+
onLeave(fn: LeaveHookFn): void;
|
|
174
174
|
}
|
|
175
175
|
/**
|
|
176
|
-
* Props
|
|
177
|
-
* Extend your component props with this to
|
|
176
|
+
* Props interface for components rendered via Route's `component` prop.
|
|
177
|
+
* Extend your component's props with this to receive the `route` context.
|
|
178
178
|
*
|
|
179
179
|
* @example
|
|
180
180
|
* ```tsx
|
|
181
|
-
* import {
|
|
181
|
+
* import { type Props, type RouteProps } from "defuss";
|
|
182
182
|
*
|
|
183
|
-
*
|
|
183
|
+
* interface MyScreenProps extends Props, RouteProps {}
|
|
184
184
|
*
|
|
185
|
-
*
|
|
186
|
-
* const {
|
|
187
|
-
* return <h1>
|
|
185
|
+
* function MyScreen({ route }: MyScreenProps) {
|
|
186
|
+
* const { userId } = route.request.params;
|
|
187
|
+
* return <h1>User #{userId}</h1>;
|
|
188
188
|
* }
|
|
189
189
|
* ```
|
|
190
190
|
*/
|
|
@@ -323,8 +323,8 @@ interface RouterState {
|
|
|
323
323
|
currentPath: string;
|
|
324
324
|
popAttached: boolean;
|
|
325
325
|
lifecycleHooks: {
|
|
326
|
-
|
|
327
|
-
|
|
326
|
+
beforeLeave: Array<BeforeLeaveHookFn>;
|
|
327
|
+
leave: Array<LeaveHookFn>;
|
|
328
328
|
};
|
|
329
329
|
}
|
|
330
330
|
declare global {
|
|
@@ -361,7 +361,7 @@ interface Router {
|
|
|
361
361
|
ready(): Promise<void>;
|
|
362
362
|
/**
|
|
363
363
|
* Create a RouteContext object for a matched route request.
|
|
364
|
-
* The context provides lifecycle hooks (
|
|
364
|
+
* The context provides lifecycle hooks (onBeforeLeave, onLeave)
|
|
365
365
|
* and is passed to components rendered via Route's `component` prop.
|
|
366
366
|
*/
|
|
367
367
|
createRouteContext(request: RouteRequest): RouteContext;
|
|
@@ -486,4 +486,4 @@ declare const Suspense: ({ fallback, ref, children, class: _class, className, id
|
|
|
486
486
|
};
|
|
487
487
|
|
|
488
488
|
export { Async, AsyncDefussChild, DOMElement, FC, Globals, NodeType, PersistenceProviderImpl, PersistenceProviderOptions, PersistenceProviderType, Props, Redirect, Ref, RenderInput, Route, Router, RouterSlot, RouterSlotId, Suspense, T, Trans, TransitionConfig, VNode, VNodeAttributes, VNodeChild, addElementEvent, areDomNodesEqual, changeLanguage, checkElementVisibility, clearElementEvents, createI18n, createTrans, domNodeToVNode, getEventMap, getLanguage, getMimeType, getRouterState, htmlStringToVNodes, i18n, inDevMode, isHTML, isMarkup, isSVG, loadLanguage, matchRouteRegistrations, parseDOM, processAllFormElements, queueCallback, removeElementEvent, renderMarkup, replaceDomWithVdom, setupRouter, t, tokenizePath, updateDomWithVdom, waitForDOM, webstorage };
|
|
489
|
-
export type { AsyncProps, AsyncState, AsyncStateRef,
|
|
489
|
+
export type { AsyncProps, AsyncState, AsyncStateRef, BeforeLeaveHookFn, I18nStore, LeaveHookFn, MatchRouteRegistrationsOpts, OnHandleRouteChangeFn, OnLanguageChangeListener, OnRouteChangeFn, RedirectProps, Replacements, Resolve, RouteComponentProps, RouteContext, RouteHandler, RouteParams, RouteProps, RouteRegistration, RouteRequest, RouterConfig, RouterSlotProps, RouterStrategy, TokenizedPath, TransProps, TransRef, TranslationKeys, TranslationObject, Translations, ValidChild };
|
package/dist/index.mjs
CHANGED
|
@@ -1162,10 +1162,10 @@ const setupRouter = (config = {
|
|
|
1162
1162
|
pendingResolvers: [],
|
|
1163
1163
|
currentPath: "",
|
|
1164
1164
|
popAttached: false,
|
|
1165
|
-
lifecycleHooks: {
|
|
1165
|
+
lifecycleHooks: { beforeLeave: [], leave: [] }
|
|
1166
1166
|
};
|
|
1167
1167
|
if (!state.lifecycleHooks) {
|
|
1168
|
-
state.lifecycleHooks = {
|
|
1168
|
+
state.lifecycleHooks = { beforeLeave: [], leave: [] };
|
|
1169
1169
|
}
|
|
1170
1170
|
const routeRegistrations = state.routeRegistrations;
|
|
1171
1171
|
if (typeof window !== "undefined" && !windowImpl) {
|
|
@@ -1293,31 +1293,31 @@ const setupRouter = (config = {
|
|
|
1293
1293
|
createRouteContext(request) {
|
|
1294
1294
|
return {
|
|
1295
1295
|
request,
|
|
1296
|
-
|
|
1297
|
-
state.lifecycleHooks.
|
|
1296
|
+
onBeforeLeave(fn) {
|
|
1297
|
+
state.lifecycleHooks.beforeLeave.push(fn);
|
|
1298
1298
|
},
|
|
1299
|
-
|
|
1300
|
-
state.lifecycleHooks.
|
|
1299
|
+
onLeave(fn) {
|
|
1300
|
+
state.lifecycleHooks.leave.push(fn);
|
|
1301
1301
|
}
|
|
1302
1302
|
};
|
|
1303
1303
|
},
|
|
1304
|
-
async
|
|
1305
|
-
for (const fn of state.lifecycleHooks.
|
|
1304
|
+
async runBeforeLeaveHooks() {
|
|
1305
|
+
for (const fn of state.lifecycleHooks.beforeLeave) {
|
|
1306
1306
|
const result = await fn();
|
|
1307
1307
|
if (result === false) return false;
|
|
1308
1308
|
}
|
|
1309
1309
|
return true;
|
|
1310
1310
|
},
|
|
1311
|
-
|
|
1312
|
-
for (const fn of state.lifecycleHooks.
|
|
1311
|
+
runLeaveHooks() {
|
|
1312
|
+
for (const fn of state.lifecycleHooks.leave) {
|
|
1313
1313
|
fn();
|
|
1314
1314
|
}
|
|
1315
1315
|
},
|
|
1316
1316
|
clearRouteLifecycle() {
|
|
1317
|
-
const
|
|
1318
|
-
state.lifecycleHooks.
|
|
1319
|
-
state.lifecycleHooks.
|
|
1320
|
-
return {
|
|
1317
|
+
const oldLeaveHooks = [...state.lifecycleHooks.leave];
|
|
1318
|
+
state.lifecycleHooks.beforeLeave = [];
|
|
1319
|
+
state.lifecycleHooks.leave = [];
|
|
1320
|
+
return { leaveHooks: oldLeaveHooks };
|
|
1321
1321
|
}
|
|
1322
1322
|
};
|
|
1323
1323
|
const handlePopState = (event) => {
|
|
@@ -1348,7 +1348,7 @@ if (!globalThis[ROUTER_STATE_KEY]) {
|
|
|
1348
1348
|
pendingResolvers: [],
|
|
1349
1349
|
currentPath: "",
|
|
1350
1350
|
popAttached: false,
|
|
1351
|
-
lifecycleHooks: {
|
|
1351
|
+
lifecycleHooks: { beforeLeave: [], leave: [] }
|
|
1352
1352
|
};
|
|
1353
1353
|
}
|
|
1354
1354
|
const getRouterState = () => globalThis[ROUTER_STATE_KEY];
|
|
@@ -1421,18 +1421,18 @@ const RouterSlot = ({
|
|
|
1421
1421
|
const currentPath = router.getRequest().path;
|
|
1422
1422
|
const isSamePath = currentPath === lastPath;
|
|
1423
1423
|
if (!isSamePath) {
|
|
1424
|
-
const allowed = await router.
|
|
1424
|
+
const allowed = await router.runBeforeLeaveHooks();
|
|
1425
1425
|
if (!allowed) {
|
|
1426
1426
|
window.history.pushState({}, "", lastPath);
|
|
1427
1427
|
router.resolve(lastPath);
|
|
1428
1428
|
return;
|
|
1429
1429
|
}
|
|
1430
|
-
const {
|
|
1430
|
+
const { leaveHooks } = router.clearRouteLifecycle();
|
|
1431
1431
|
await $(ref).update(
|
|
1432
1432
|
typeof RouterOutlet === "function" ? RouterOutlet() : [],
|
|
1433
1433
|
transitionConfig
|
|
1434
1434
|
);
|
|
1435
|
-
for (const fn of
|
|
1435
|
+
for (const fn of leaveHooks) {
|
|
1436
1436
|
fn();
|
|
1437
1437
|
}
|
|
1438
1438
|
} else {
|