@modastar/z-router 0.0.6 → 0.0.8
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 +1 -540
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +73 -58
- package/dist/index.d.ts +73 -58
- package/dist/index.js +1 -495
- package/dist/index.js.map +1 -1
- package/example/index.html +14 -0
- package/example/package.json +25 -0
- package/example/pnpm-lock.yaml +1417 -0
- package/example/pnpm-workspace.yaml +2 -0
- package/example/vite.config.ts +14 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,541 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
DefaultTransitionDuration: () => DefaultTransitionDuration,
|
|
24
|
-
LocationContext: () => LocationContext,
|
|
25
|
-
LocationProvider: () => LocationProvider,
|
|
26
|
-
PageRenderer: () => PageRenderer,
|
|
27
|
-
RedirectError: () => RedirectError,
|
|
28
|
-
RouteComponent: () => RouteComponent,
|
|
29
|
-
RouteContext: () => RouteContext,
|
|
30
|
-
RouterContext: () => RouterContext,
|
|
31
|
-
RouterProvider: () => RouterProvider,
|
|
32
|
-
Stack: () => Stack,
|
|
33
|
-
createRouter: () => createRouter,
|
|
34
|
-
matchRoute: () => matchRoute,
|
|
35
|
-
matchUrl: () => matchUrl,
|
|
36
|
-
parseLocationFromHref: () => parseLocationFromHref,
|
|
37
|
-
redirect: () => redirect,
|
|
38
|
-
useLocation: () => useLocation,
|
|
39
|
-
useMatches: () => useMatches,
|
|
40
|
-
useRoute: () => useRoute,
|
|
41
|
-
useRouter: () => useRouter
|
|
42
|
-
});
|
|
43
|
-
module.exports = __toCommonJS(index_exports);
|
|
44
|
-
|
|
45
|
-
// src/context/locationContext.ts
|
|
46
|
-
var import_react = require("react");
|
|
47
|
-
var LocationContext = (0, import_react.createContext)(null);
|
|
48
|
-
|
|
49
|
-
// src/components/locationProvider.tsx
|
|
50
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
51
|
-
var LocationProvider = ({
|
|
52
|
-
location,
|
|
53
|
-
children
|
|
54
|
-
}) => {
|
|
55
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LocationContext.Provider, { value: location, children });
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
// src/components/routeComponent.tsx
|
|
59
|
-
var import_react4 = require("react");
|
|
60
|
-
|
|
61
|
-
// src/hooks/useRouter.ts
|
|
62
|
-
var import_react3 = require("react");
|
|
63
|
-
|
|
64
|
-
// src/context/routerContext.ts
|
|
65
|
-
var import_react2 = require("react");
|
|
66
|
-
var RouterContext = (0, import_react2.createContext)(null);
|
|
67
|
-
|
|
68
|
-
// src/hooks/useRouter.ts
|
|
69
|
-
var useRouter = () => {
|
|
70
|
-
const router = (0, import_react3.useContext)(RouterContext);
|
|
71
|
-
if (router === null) {
|
|
72
|
-
throw new Error("useRouter must be used within a Stack");
|
|
73
|
-
}
|
|
74
|
-
return router;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
// src/components/routeComponent.tsx
|
|
78
|
-
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
79
|
-
var RouteComponent = ({
|
|
80
|
-
route,
|
|
81
|
-
children
|
|
82
|
-
}) => {
|
|
83
|
-
const router = useRouter();
|
|
84
|
-
const [pending, setPending] = (0, import_react4.useState)(!!route.beforeLoad);
|
|
85
|
-
(0, import_react4.useEffect)(() => {
|
|
86
|
-
if (route.beforeLoad) {
|
|
87
|
-
route.beforeLoad({ location: router.location }).catch((error) => {
|
|
88
|
-
if (error instanceof Error && typeof error.cause === "object" && error.cause !== null && "to" in error.cause) {
|
|
89
|
-
router.navigate({
|
|
90
|
-
to: error.cause.to,
|
|
91
|
-
replace: error.cause.replace
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}).finally(() => setPending(false));
|
|
95
|
-
}
|
|
96
|
-
}, []);
|
|
97
|
-
if (pending) {
|
|
98
|
-
const PendingComponent = route.pendingComponent;
|
|
99
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(PendingComponent, {});
|
|
100
|
-
}
|
|
101
|
-
const Component = route.component;
|
|
102
|
-
return Component ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Component, { children }) : children;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
// src/components/routerProvider.tsx
|
|
106
|
-
var import_react5 = require("react");
|
|
107
|
-
|
|
108
|
-
// src/utils.ts
|
|
109
|
-
var DefaultTransitionDuration = 300;
|
|
110
|
-
var redirect = (options) => {
|
|
111
|
-
return new Error("", { cause: options });
|
|
112
|
-
};
|
|
113
|
-
var matchUrl = (pattern, url) => {
|
|
114
|
-
try {
|
|
115
|
-
let pathname, searchParams;
|
|
116
|
-
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
117
|
-
const urlObj = new URL(url);
|
|
118
|
-
pathname = urlObj.pathname;
|
|
119
|
-
searchParams = urlObj.searchParams;
|
|
120
|
-
} else {
|
|
121
|
-
const [path, queryString] = url.split("?");
|
|
122
|
-
if (!path) {
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
pathname = path;
|
|
126
|
-
searchParams = new URLSearchParams(queryString || "");
|
|
127
|
-
}
|
|
128
|
-
const cleanPath = pathname.replaceAll(/^\/|\/$/g, "");
|
|
129
|
-
const cleanPattern = pattern.replaceAll(/^\/|\/$/g, "");
|
|
130
|
-
const pathSegments = cleanPath.split("/");
|
|
131
|
-
const patternSegments = cleanPattern.split("/");
|
|
132
|
-
if (pathSegments.length !== patternSegments.length) {
|
|
133
|
-
return null;
|
|
134
|
-
}
|
|
135
|
-
const params = {};
|
|
136
|
-
for (let i = 0; i < patternSegments.length; i++) {
|
|
137
|
-
const patternSegment = patternSegments[i];
|
|
138
|
-
const pathSegment = pathSegments[i];
|
|
139
|
-
if (patternSegment.startsWith(":")) {
|
|
140
|
-
const paramName = patternSegment.slice(1);
|
|
141
|
-
params[paramName] = decodeURIComponent(pathSegment);
|
|
142
|
-
} else if (patternSegment !== pathSegment) {
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
const query = Object.fromEntries(searchParams.entries());
|
|
147
|
-
return { params, query };
|
|
148
|
-
} catch {
|
|
149
|
-
return null;
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
var matchRoute = (rootRoute, url) => {
|
|
153
|
-
const _matchRoute = (matches, route) => {
|
|
154
|
-
if (route.children) {
|
|
155
|
-
for (const childRoute of route.children) {
|
|
156
|
-
const matchesResult = _matchRoute([...matches, childRoute], childRoute);
|
|
157
|
-
if (matchesResult) {
|
|
158
|
-
return matchesResult;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return null;
|
|
162
|
-
}
|
|
163
|
-
let pattern = "";
|
|
164
|
-
for (const match of matches) {
|
|
165
|
-
if (match.pathname === void 0) continue;
|
|
166
|
-
pattern += `/${match.pathname}`;
|
|
167
|
-
}
|
|
168
|
-
const result = matchUrl(pattern, url);
|
|
169
|
-
if (result) {
|
|
170
|
-
return { matches, ...result };
|
|
171
|
-
}
|
|
172
|
-
return null;
|
|
173
|
-
};
|
|
174
|
-
return _matchRoute([], rootRoute);
|
|
175
|
-
};
|
|
176
|
-
var parseLocationFromHref = (rootRoute, to) => {
|
|
177
|
-
const result = matchRoute(rootRoute, to);
|
|
178
|
-
if (!result) return null;
|
|
179
|
-
return {
|
|
180
|
-
pathname: to,
|
|
181
|
-
params: result.params,
|
|
182
|
-
query: result.query
|
|
183
|
-
};
|
|
184
|
-
};
|
|
185
|
-
var createRouter = (options) => {
|
|
186
|
-
return options;
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
// src/components/routerProvider.tsx
|
|
190
|
-
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
191
|
-
var RouterProvider = ({
|
|
192
|
-
router,
|
|
193
|
-
children
|
|
194
|
-
}) => {
|
|
195
|
-
const [history, setHistory] = (0, import_react5.useState)([]);
|
|
196
|
-
const [currentLocationIndex, setCurrentLocationIndex] = (0, import_react5.useState)(-1);
|
|
197
|
-
const [isTransitioning, setIsTransitioning] = (0, import_react5.useState)(false);
|
|
198
|
-
const [transitionDuration, setTransitionDuration] = (0, import_react5.useState)(
|
|
199
|
-
DefaultTransitionDuration
|
|
200
|
-
);
|
|
201
|
-
const [transitioningToIndex, setTransitioningToIndex] = (0, import_react5.useState)(null);
|
|
202
|
-
const navigate = (0, import_react5.useCallback)(
|
|
203
|
-
({
|
|
204
|
-
to,
|
|
205
|
-
replace,
|
|
206
|
-
transition,
|
|
207
|
-
duration,
|
|
208
|
-
updateHistory,
|
|
209
|
-
onFinish,
|
|
210
|
-
...locationOptions
|
|
211
|
-
}) => {
|
|
212
|
-
if (isTransitioning) return;
|
|
213
|
-
const newLocationIndex = replace ? currentLocationIndex : currentLocationIndex + 1;
|
|
214
|
-
const newLocation = {
|
|
215
|
-
index: newLocationIndex,
|
|
216
|
-
params: {},
|
|
217
|
-
query: {},
|
|
218
|
-
state: /* @__PURE__ */ new Map(),
|
|
219
|
-
pathname: to,
|
|
220
|
-
...locationOptions
|
|
221
|
-
};
|
|
222
|
-
if (newLocationIndex === history.length) {
|
|
223
|
-
setHistory([...history, newLocation]);
|
|
224
|
-
} else {
|
|
225
|
-
setHistory((prevHistory) => {
|
|
226
|
-
const newHistory = [...prevHistory];
|
|
227
|
-
newHistory[newLocationIndex] = newLocation;
|
|
228
|
-
return newHistory.slice(0, currentLocationIndex + 2);
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
if (!replace && currentLocationIndex >= 0 && (transition ?? router.defaultViewTransition?.(
|
|
232
|
-
history.at(currentLocationIndex),
|
|
233
|
-
history.at(newLocationIndex)
|
|
234
|
-
))) {
|
|
235
|
-
const currentDuration = duration ?? DefaultTransitionDuration;
|
|
236
|
-
setIsTransitioning(true);
|
|
237
|
-
setTransitionDuration(currentDuration);
|
|
238
|
-
setTransitioningToIndex(newLocationIndex);
|
|
239
|
-
setTimeout(() => {
|
|
240
|
-
setIsTransitioning(false);
|
|
241
|
-
setTransitioningToIndex(null);
|
|
242
|
-
setCurrentLocationIndex(newLocationIndex);
|
|
243
|
-
onFinish?.();
|
|
244
|
-
if (updateHistory) {
|
|
245
|
-
window.history.pushState({}, "", to);
|
|
246
|
-
}
|
|
247
|
-
}, currentDuration);
|
|
248
|
-
} else if (!replace) {
|
|
249
|
-
setCurrentLocationIndex(newLocationIndex);
|
|
250
|
-
if (updateHistory) {
|
|
251
|
-
window.history.pushState({}, "", to);
|
|
252
|
-
}
|
|
253
|
-
} else if (updateHistory) {
|
|
254
|
-
window.history.replaceState({}, "", to);
|
|
255
|
-
}
|
|
256
|
-
},
|
|
257
|
-
[currentLocationIndex, history, isTransitioning, router]
|
|
258
|
-
);
|
|
259
|
-
(0, import_react5.useEffect)(() => {
|
|
260
|
-
console.log("Navigate: History updated:", history);
|
|
261
|
-
}, [history]);
|
|
262
|
-
const back = (0, import_react5.useCallback)(
|
|
263
|
-
(options) => {
|
|
264
|
-
if (isTransitioning) return;
|
|
265
|
-
const newLocationIndex = currentLocationIndex - (options?.depth ?? 1);
|
|
266
|
-
if (currentLocationIndex > 0 && (options?.transition ?? router.defaultViewTransition?.(
|
|
267
|
-
history.at(currentLocationIndex),
|
|
268
|
-
history.at(newLocationIndex)
|
|
269
|
-
))) {
|
|
270
|
-
const currentDuration = options?.duration ?? DefaultTransitionDuration;
|
|
271
|
-
setIsTransitioning(true);
|
|
272
|
-
setTransitionDuration(currentDuration);
|
|
273
|
-
setTransitioningToIndex(newLocationIndex);
|
|
274
|
-
setTimeout(() => {
|
|
275
|
-
setIsTransitioning(false);
|
|
276
|
-
setTransitioningToIndex(null);
|
|
277
|
-
setCurrentLocationIndex(newLocationIndex);
|
|
278
|
-
options?.onFinish?.();
|
|
279
|
-
}, currentDuration);
|
|
280
|
-
} else {
|
|
281
|
-
setCurrentLocationIndex(newLocationIndex);
|
|
282
|
-
}
|
|
283
|
-
},
|
|
284
|
-
[currentLocationIndex, history, isTransitioning, router]
|
|
285
|
-
);
|
|
286
|
-
const forward = (0, import_react5.useCallback)(
|
|
287
|
-
(options) => {
|
|
288
|
-
if (isTransitioning) return;
|
|
289
|
-
const newLocationIndex = currentLocationIndex + 1;
|
|
290
|
-
if (newLocationIndex < history.length && (options?.transition ?? router.defaultViewTransition?.(
|
|
291
|
-
history.at(currentLocationIndex),
|
|
292
|
-
history.at(newLocationIndex)
|
|
293
|
-
))) {
|
|
294
|
-
const duration = options?.duration ?? DefaultTransitionDuration;
|
|
295
|
-
setIsTransitioning(true);
|
|
296
|
-
setTransitionDuration(duration);
|
|
297
|
-
setTransitioningToIndex(newLocationIndex);
|
|
298
|
-
setTimeout(() => {
|
|
299
|
-
setIsTransitioning(false);
|
|
300
|
-
setTransitioningToIndex(null);
|
|
301
|
-
setCurrentLocationIndex(newLocationIndex);
|
|
302
|
-
options?.onFinish?.();
|
|
303
|
-
}, duration);
|
|
304
|
-
} else {
|
|
305
|
-
setCurrentLocationIndex(newLocationIndex);
|
|
306
|
-
}
|
|
307
|
-
},
|
|
308
|
-
[currentLocationIndex, history, isTransitioning, router]
|
|
309
|
-
);
|
|
310
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
311
|
-
RouterContext.Provider,
|
|
312
|
-
{
|
|
313
|
-
value: {
|
|
314
|
-
options: router,
|
|
315
|
-
history,
|
|
316
|
-
currentLocationIndex,
|
|
317
|
-
location: history.at(currentLocationIndex) || null,
|
|
318
|
-
canGoBack: currentLocationIndex > 0,
|
|
319
|
-
canGoForward: currentLocationIndex < history.length - 1,
|
|
320
|
-
isTransitioning,
|
|
321
|
-
transitionDuration,
|
|
322
|
-
transitioningToIndex,
|
|
323
|
-
navigate,
|
|
324
|
-
back,
|
|
325
|
-
forward
|
|
326
|
-
},
|
|
327
|
-
children
|
|
328
|
-
}
|
|
329
|
-
);
|
|
330
|
-
};
|
|
331
|
-
|
|
332
|
-
// src/components/stack.tsx
|
|
333
|
-
var import_react10 = require("react");
|
|
334
|
-
|
|
335
|
-
// src/hooks/useLocation.ts
|
|
336
|
-
var import_react6 = require("react");
|
|
337
|
-
var useLocation = () => {
|
|
338
|
-
const context = (0, import_react6.useContext)(LocationContext);
|
|
339
|
-
if (context === null) {
|
|
340
|
-
throw new Error("useLocation must be used within a LocationProvider");
|
|
341
|
-
}
|
|
342
|
-
return context;
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
// src/hooks/useRoute.ts
|
|
346
|
-
var import_react8 = require("react");
|
|
347
|
-
|
|
348
|
-
// src/context/routeContext.ts
|
|
349
|
-
var import_react7 = require("react");
|
|
350
|
-
var RouteContext = (0, import_react7.createContext)(null);
|
|
351
|
-
|
|
352
|
-
// src/hooks/useRoute.ts
|
|
353
|
-
var useRoute = () => {
|
|
354
|
-
const route = (0, import_react8.useContext)(RouteContext);
|
|
355
|
-
if (route === null) {
|
|
356
|
-
throw new Error("useRoute must be used within a RouteProvider");
|
|
357
|
-
}
|
|
358
|
-
return route;
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
// src/hooks/useMatches.ts
|
|
362
|
-
var useMatches = () => {
|
|
363
|
-
const route = useRoute();
|
|
364
|
-
const location = useLocation();
|
|
365
|
-
if (!location) return [];
|
|
366
|
-
return matchRoute(route, location.pathname)?.matches || [];
|
|
367
|
-
};
|
|
368
|
-
|
|
369
|
-
// src/components/routeProvider.tsx
|
|
370
|
-
var import_react9 = require("react");
|
|
371
|
-
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
372
|
-
var RouteProvider = ({
|
|
373
|
-
rootRoute,
|
|
374
|
-
children
|
|
375
|
-
}) => {
|
|
376
|
-
const router = useRouter();
|
|
377
|
-
(0, import_react9.useEffect)(() => {
|
|
378
|
-
const currentLocation = parseLocationFromHref(
|
|
379
|
-
rootRoute,
|
|
380
|
-
window.location.href
|
|
381
|
-
);
|
|
382
|
-
if (!currentLocation) return;
|
|
383
|
-
router.navigate({
|
|
384
|
-
to: currentLocation.pathname,
|
|
385
|
-
params: currentLocation.params,
|
|
386
|
-
query: currentLocation.query
|
|
387
|
-
});
|
|
388
|
-
return () => {
|
|
389
|
-
router.back();
|
|
390
|
-
};
|
|
391
|
-
}, []);
|
|
392
|
-
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(RouteContext.Provider, { value: rootRoute, children });
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
// src/components/stack.tsx
|
|
396
|
-
var import_jsx_runtime5 = require("react/jsx-runtime");
|
|
397
|
-
var PageRenderer = () => {
|
|
398
|
-
const route = useRoute();
|
|
399
|
-
const matches = useMatches();
|
|
400
|
-
if (!matches || matches.length === 0) {
|
|
401
|
-
const NotFoundComponent = route.notFoundComponent;
|
|
402
|
-
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(NotFoundComponent, {});
|
|
403
|
-
}
|
|
404
|
-
let content = null;
|
|
405
|
-
for (let i = matches.length - 1; i >= 0; i--) {
|
|
406
|
-
const route2 = matches[i];
|
|
407
|
-
content = /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(RouteComponent, { route: route2, children: content });
|
|
408
|
-
}
|
|
409
|
-
return content;
|
|
410
|
-
};
|
|
411
|
-
var StackComponent = () => {
|
|
412
|
-
const {
|
|
413
|
-
history,
|
|
414
|
-
currentLocationIndex,
|
|
415
|
-
canGoBack,
|
|
416
|
-
canGoForward,
|
|
417
|
-
isTransitioning,
|
|
418
|
-
transitioningToIndex,
|
|
419
|
-
transitionDuration,
|
|
420
|
-
back,
|
|
421
|
-
forward
|
|
422
|
-
} = useRouter();
|
|
423
|
-
const [isDragging, setIsDragging] = (0, import_react10.useState)(false);
|
|
424
|
-
const [startX, setStartX] = (0, import_react10.useState)(0);
|
|
425
|
-
const [dragOffset, setDragOffset] = (0, import_react10.useState)(0);
|
|
426
|
-
const [isCanceling, setIsCanceling] = (0, import_react10.useState)(false);
|
|
427
|
-
const [isTransitionStarted, setIsTransitionStarted] = (0, import_react10.useState)(false);
|
|
428
|
-
(0, import_react10.useEffect)(() => {
|
|
429
|
-
if (!isTransitioning || transitioningToIndex === null) return;
|
|
430
|
-
setIsTransitionStarted(true);
|
|
431
|
-
setTimeout(() => {
|
|
432
|
-
setIsTransitionStarted(false);
|
|
433
|
-
}, transitionDuration);
|
|
434
|
-
}, [isTransitioning, transitioningToIndex]);
|
|
435
|
-
const reset = () => {
|
|
436
|
-
setIsDragging(false);
|
|
437
|
-
setDragOffset(0);
|
|
438
|
-
setIsCanceling(false);
|
|
439
|
-
};
|
|
440
|
-
const handleTouchStart = (e) => {
|
|
441
|
-
if (isTransitioning || !canGoForward && !canGoBack) return;
|
|
442
|
-
setIsDragging(true);
|
|
443
|
-
setStartX(e.touches[0].clientX);
|
|
444
|
-
};
|
|
445
|
-
const handleTouchMove = (e) => {
|
|
446
|
-
if (!isDragging) return;
|
|
447
|
-
const offset = e.touches[0].clientX - startX;
|
|
448
|
-
if (offset > 0 && currentLocationIndex === 0 || offset < 0 && currentLocationIndex + 1 === history.length) {
|
|
449
|
-
setDragOffset(0);
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
setDragOffset(Math.min(window.innerWidth, offset));
|
|
453
|
-
};
|
|
454
|
-
const handleTouchEnd = () => {
|
|
455
|
-
if (!isDragging) return;
|
|
456
|
-
if (dragOffset > window.innerWidth * 0.3 && canGoBack) {
|
|
457
|
-
back({
|
|
458
|
-
onFinish: reset
|
|
459
|
-
});
|
|
460
|
-
} else if (dragOffset < -window.innerWidth * 0.3 && canGoForward) {
|
|
461
|
-
forward({
|
|
462
|
-
onFinish: reset
|
|
463
|
-
});
|
|
464
|
-
} else {
|
|
465
|
-
setIsCanceling(true);
|
|
466
|
-
setTimeout(reset, transitionDuration);
|
|
467
|
-
}
|
|
468
|
-
};
|
|
469
|
-
return /* @__PURE__ */ (0, import_jsx_runtime5.jsxs)("div", { className: "relative inset-0 h-full w-full overflow-hidden", children: [
|
|
470
|
-
currentLocationIndex >= 1 && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)("div", { className: "absolute inset-0 -z-10", children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LocationProvider, { location: history.at(currentLocationIndex - 1), children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(PageRenderer, {}, currentLocationIndex - 1) }) }),
|
|
471
|
-
/* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
472
|
-
"div",
|
|
473
|
-
{
|
|
474
|
-
className: "bg-background absolute inset-0 overflow-hidden",
|
|
475
|
-
style: {
|
|
476
|
-
transform: isTransitioning && transitioningToIndex !== null && transitioningToIndex < currentLocationIndex ? `translateX(100%)` : isDragging && dragOffset > 0 && !isCanceling ? `translateX(${dragOffset}px)` : "translateX(0px)",
|
|
477
|
-
transition: isCanceling || isTransitioning && transitioningToIndex !== null && transitioningToIndex < currentLocationIndex ? `transform ${transitionDuration}ms ease-out` : "",
|
|
478
|
-
boxShadow: isDragging && dragOffset > 0 ? "-4px 0 8px rgba(0,0,0,0.1)" : "none"
|
|
479
|
-
},
|
|
480
|
-
onTouchStart: handleTouchStart,
|
|
481
|
-
onTouchMove: handleTouchMove,
|
|
482
|
-
onTouchEnd: handleTouchEnd,
|
|
483
|
-
children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(LocationProvider, { location: history.at(currentLocationIndex), children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(PageRenderer, {}, currentLocationIndex) })
|
|
484
|
-
},
|
|
485
|
-
currentLocationIndex
|
|
486
|
-
),
|
|
487
|
-
(isDragging && dragOffset < 0 || isTransitioning && transitioningToIndex !== null && currentLocationIndex < transitioningToIndex) && /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
488
|
-
"div",
|
|
489
|
-
{
|
|
490
|
-
className: "bg-background absolute inset-0 z-10 overflow-hidden transition-transform ease-in",
|
|
491
|
-
style: {
|
|
492
|
-
transform: isTransitionStarted ? `translateX(0px)` : isDragging && !isCanceling ? `translateX(${window.innerWidth + dragOffset}px)` : "translateX(100%)",
|
|
493
|
-
transitionDuration: isTransitioning || isCanceling ? `${transitionDuration}ms` : "0ms"
|
|
494
|
-
},
|
|
495
|
-
children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(
|
|
496
|
-
LocationProvider,
|
|
497
|
-
{
|
|
498
|
-
location: isDragging ? history.at(currentLocationIndex + 1) : history.at(transitioningToIndex),
|
|
499
|
-
children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(PageRenderer, {}, transitioningToIndex)
|
|
500
|
-
}
|
|
501
|
-
)
|
|
502
|
-
},
|
|
503
|
-
transitioningToIndex
|
|
504
|
-
)
|
|
505
|
-
] });
|
|
506
|
-
};
|
|
507
|
-
var Stack = ({ rootRoute }) => {
|
|
508
|
-
return /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(RouteProvider, { rootRoute, children: /* @__PURE__ */ (0, import_jsx_runtime5.jsx)(StackComponent, {}) });
|
|
509
|
-
};
|
|
510
|
-
|
|
511
|
-
// src/types.d.ts
|
|
512
|
-
var RedirectError = class extends Error {
|
|
513
|
-
options;
|
|
514
|
-
constructor(options) {
|
|
515
|
-
super();
|
|
516
|
-
this.options = options;
|
|
517
|
-
}
|
|
518
|
-
};
|
|
519
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
520
|
-
0 && (module.exports = {
|
|
521
|
-
DefaultTransitionDuration,
|
|
522
|
-
LocationContext,
|
|
523
|
-
LocationProvider,
|
|
524
|
-
PageRenderer,
|
|
525
|
-
RedirectError,
|
|
526
|
-
RouteComponent,
|
|
527
|
-
RouteContext,
|
|
528
|
-
RouterContext,
|
|
529
|
-
RouterProvider,
|
|
530
|
-
Stack,
|
|
531
|
-
createRouter,
|
|
532
|
-
matchRoute,
|
|
533
|
-
matchUrl,
|
|
534
|
-
parseLocationFromHref,
|
|
535
|
-
redirect,
|
|
536
|
-
useLocation,
|
|
537
|
-
useMatches,
|
|
538
|
-
useRoute,
|
|
539
|
-
useRouter
|
|
540
|
-
});
|
|
1
|
+
"use strict";var tt=Object.defineProperty;var Ft=Object.getOwnPropertyDescriptor;var Nt=Object.getOwnPropertyNames;var $t=Object.prototype.hasOwnProperty;var Bt=(t,n)=>{for(var o in n)tt(t,o,{get:n[o],enumerable:!0})},Gt=(t,n,o,s)=>{if(n&&typeof n=="object"||typeof n=="function")for(let e of Nt(n))!$t.call(t,e)&&e!==o&&tt(t,e,{get:()=>n[e],enumerable:!(s=Ft(n,e))||s.enumerable});return t};var It=t=>Gt(tt({},"__esModule",{value:!0}),t);var zt={};Bt(zt,{DefaultTransitionDuration:()=>Wt,Link:()=>Xt,LocationContext:()=>$,LocationProvider:()=>E,Outlet:()=>ot,RootRouteContext:()=>X,RouterContext:()=>N,RouterProvider:()=>qt,Stack:()=>Ht,buildPathnameFromMatches:()=>kt,createRouterOptions:()=>Ut,matchPattern:()=>Tt,matchRoute:()=>it,parseLocation:()=>st,parseRoute:()=>at,redirect:()=>jt,useLocation:()=>B,useRootRoute:()=>W,useRoute:()=>et,useRouter:()=>w});module.exports=It(zt);var mt=require("react");var pt=require("react"),N=(0,pt.createContext)(null);var w=()=>{let t=(0,mt.useContext)(N);if(t===null)throw new Error("useRouter must be used within a Stack");return t};var dt=require("react/jsx-runtime"),Xt=({to:t,replace:n,transition:o,duration:s,onFinish:e,...r})=>{let i=w();return(0,dt.jsx)("a",{...r,href:t,onClick:u=>{u.preventDefault(),i.navigate({to:t,replace:n,transition:o,duration:s,onFinish:e})}})};var lt=require("react"),$=(0,lt.createContext)(null);var q=require("react"),ft=require("react/jsx-runtime"),E=({location:t,...n})=>{let o=w(),s=(0,q.useCallback)(i=>t.state[i],[t]),e=(0,q.useCallback)((i,u)=>{o.setLocationState(t.index,{...t.state,[i]:u})},[o,t]),r=(0,q.useCallback)(i=>{delete t.state[i],o.setLocationState(t.index,t.state)},[o,t]);return(0,ft.jsx)($.Provider,{value:{...t,canGoBack:t.index>0,canGoForward:t.index<o.history.length-1,getState:s,setState:e,deleteState:r},...n})};var Rt=require("react"),A=(0,Rt.createContext)(0);var ht=require("react"),gt=()=>(0,ht.useContext)(A);var xt=require("react"),H=(0,xt.createContext)(null);var yt=require("react"),z=()=>{let t=(0,yt.useContext)(H);if(t===null)throw new Error("useRouteMatch must be used within a RouteMatchProvider");return t};var Pt=require("react/jsx-runtime"),vt=({depth:t,...n})=>(0,Pt.jsx)(A.Provider,{value:t,...n});var Ct=require("react");var B=()=>{let t=(0,Ct.useContext)($);if(t===null)throw new Error("useLocation must be used within a LocationProvider");return t};var Lt=require("react"),G=(0,Lt.createContext)(null);var wt=require("react"),et=()=>{let t=(0,wt.useContext)(G);if(t===null)throw new Error("useRoute must be used within a RouteProvider");return t};var _=require("react");var I=require("react/jsx-runtime"),K=({depth:t=0})=>{let n=w(),o=B(),s=z(),e=et(),r=`_Z.${e.id}.pending`,[i,u]=(0,_.useState)(!!e?.beforeLoad&&e?.getState(r)!==!1);if((0,_.useEffect)(()=>{!e||t>=s.matches.length||i&&e?.beforeLoad&&(e.setState(r,!0),e.beforeLoad({location:o}).catch(({cause:c})=>{"to"in c&&n.navigate(c)}).finally(()=>{e.setState(r,!1),u(!1)}))},[]),!e)return null;if(t>=s.matches.length){let c=e.notFoundComponent;return(0,I.jsx)(c,{})}if(i){let c=e.pendingComponent;return(0,I.jsx)(c,{})}let l=e.component;return l?(0,I.jsx)(l,{}):(0,I.jsx)(ot,{})};var St=require("react");var bt=require("react"),X=(0,bt.createContext)(null);var W=()=>{let t=(0,St.useContext)(X);if(t===null)throw new Error("useRootRoute must be used within a RootRouteProvider");return t};var nt=require("react"),rt=require("react/jsx-runtime"),Z=({route:t,...n})=>{if(!t)return(0,rt.jsx)(G.Provider,{value:null,...n});let o=W(),s=(0,nt.useCallback)(r=>o.getRouteState(t.id,r),[o.getRouteState]),e=(0,nt.useCallback)((r,i)=>{o.setRouteState(t.id,r,i)},[o.setRouteState]);return(0,rt.jsx)(G.Provider,{value:{...t,getState:s,setState:e},...n})};var J=require("react/jsx-runtime"),ot=()=>{let t=z(),n=gt()+1;return(0,J.jsx)(vt,{depth:n,children:(0,J.jsx)(Z,{route:t.matches.at(n),children:(0,J.jsx)(K,{depth:n})})})};var g=require("react");var Ot={defaultTransitionDuration:300};var Wt=300,jt=t=>new Error("",{cause:t}),Tt=(t,n)=>{try{let o,s;if(n.startsWith("http://")||n.startsWith("https://")){let m=new URL(n);o=m.pathname,s=m.searchParams}else{let[m,f]=n.split("?");if(!m)return null;o=m,s=new URLSearchParams(f||"")}let e=o.replaceAll(/^\/|\/$/g,""),r=t.replaceAll(/^\/|\/$/g,""),i=e.split("/"),u=r.split("/");if(i.length!==u.length)return null;let l={};for(let m=0;m<u.length;m++){let f=u[m],b=i[m];if(f.startsWith(":")){let T=f.slice(1);l[T]=decodeURIComponent(b)}else if(f!==b)return null}let c=Object.fromEntries(s.entries());return{params:l,query:c}}catch{return null}},it=(t,n)=>{let o=(s,{children:e})=>{if(e&&e.length>0){for(let i of e){let u=o([...s,i],i);if(u)return u}return null}let r=Tt(kt(s),n);return r?{matches:s,...r}:null};return o([t],t)||{matches:[],params:{},query:{}}},kt=t=>{let n=[];for(let o of t)o.pathname!==void 0&&n.push(o.pathname.replaceAll(/^\/|\/$/g,""));return"/"+n.join("/")},st=t=>({index:0,state:{index:0},pathname:t.pathname,search:Object.fromEntries(new URLSearchParams(t.search))}),Ut=t=>({...Ot,...t}),at=t=>{let n=(o,s)=>{let e=o.name??(o.pathname?`${s}/${o.pathname.replaceAll(/^\/|\/$/g,"")}`:s);return{...o,id:e,children:o.children?.map(i=>n(i,e))}};return n(t,"")};var ut=require("react/jsx-runtime"),qt=({options:t,...n})=>{let[o,s]=(0,g.useState)([st(window.location)]),[e,r]=(0,g.useState)(0),i=o.at(e),[u,l]=(0,g.useState)(!1),[c,m]=(0,g.useState)(0),[f,b]=(0,g.useState)();(0,g.useEffect)(()=>{window.history.replaceState(o[0].state,"",o[0].pathname);let p=({state:d})=>{r(d?.index??0)};return addEventListener("popstate",p),()=>{removeEventListener("popstate",p)}},[]);let T=(p,d=t.defaultTransitionDuration,y)=>{l(!0),m(d),b(p),setTimeout(()=>{l(!1),b(void 0),r(p.index),y?.()},d)},Y=(0,g.useCallback)((p,d)=>{s(y=>y.map(h=>h.index===p?{...h,state:d}:h)),p===e&&window.history.replaceState(d,"",i.pathname)},[e]),P=(0,g.useCallback)(({to:p,replace:d,transition:y,duration:h,onFinish:C,...v})=>{if(u)return;let R=d?e:e+1,L;if(p.startsWith("/"))L=p;else{let S=i.pathname.split("/").filter(M=>M.length>0),Dt=p.split("/").filter(M=>M.length>0);for(let M of Dt)M!=="."&&(M===".."?S.pop():S.push(M));L="/"+S.join("/")}let k={index:R},U={index:R,search:{},state:k,pathname:L,...v},ct=()=>{d?(s(S=>[...S.slice(0,R),U,...S.slice(R+1)]),window.history.replaceState(k,"",L)):(s(S=>[...S.slice(0,R),U]),r(R),window.history.pushState(k,"",L)),C?.()};y??t.defaultUseTransition?.(i,U)?T(U,h,ct):ct()},[e,o,u,t]),D=(0,g.useCallback)(({transition:p,duration:d,onFinish:y,depth:h}={})=>{if(e===0||u)return;let C=h??1,v=o.at(e-C),R=()=>{window.history.go(-C),y?.()};v&&(p??t.defaultUseTransition?.(i,v))?T(v,d,R):R()},[e,o,u,t]),F=(0,g.useCallback)(({transition:p,duration:d,depth:y,onFinish:h}={})=>{if(e+1>=o.length||u)return;let C=y??1,v=o.at(e+C),R=()=>{window.history.go(C),h?.()};v&&(p??t.defaultUseTransition?.(i,v))?T(v,d,R):R()},[e,o,u,t]);return(0,ut.jsx)(N.Provider,{value:{options:t,history:o,location:i,canGoBack:e>0,canGoForward:e<o.length-1,isTransitioning:u,transitionDuration:c,transitioningToLocation:f,navigate:P,back:D,forward:F,setLocationState:Y},children:(0,ut.jsx)(E,{location:i,...n})})};var O=require("react");var Q=require("react/jsx-runtime"),V=()=>{let t=W(),n=B(),o=it(t,n.pathname);return(0,Q.jsx)(H.Provider,{value:o,children:(0,Q.jsx)(Z,{route:t,children:(0,Q.jsx)(K,{})})})};var j=require("react"),Et=require("react/jsx-runtime"),Mt=({rootRoute:t,...n})=>{let o=at(t),[s,e]=(0,j.useState)({}),r=(0,j.useCallback)((u,l)=>s[u]?.[l],[s]),i=(0,j.useCallback)((u,l,c)=>{e(m=>({...m,[u]:{...m[u],[l]:c}}))},[]);return(0,Et.jsx)(X.Provider,{value:{...o,getRouteState:r,setRouteState:i},...n})};var x=require("react/jsx-runtime"),At=()=>{let{history:t,location:n,canGoBack:o,canGoForward:s,isTransitioning:e,transitioningToLocation:r,transitionDuration:i,back:u,forward:l}=w(),c=n?.index,m=r?.index,[f,b]=(0,O.useState)(!1),[T,Y]=(0,O.useState)(0),[P,D]=(0,O.useState)(0),[F,p]=(0,O.useState)(!1),[d,y]=(0,O.useState)(!1);if((0,O.useEffect)(()=>{!e||!r||(y(!0),setTimeout(()=>{y(!1)},i))},[e,r]),c===void 0)return;let h=()=>{b(!1),D(0),p(!1)},C=L=>{e||!s&&!o||(b(!0),Y(L.touches[0].clientX))},v=L=>{if(!f)return;let k=L.touches[0].clientX-T;if(k>0&&c===0||k<0&&c+1===t.length){D(0);return}D(Math.min(innerWidth,k))},R=()=>{f&&(P>innerWidth*.3&&o?u({onFinish:h}):P<-innerWidth*.3&&s?l({onFinish:h}):(p(!0),setTimeout(h,i)))};return(0,x.jsxs)("div",{style:{position:"relative",inset:0,height:"100%",width:"100%",overflow:"hidden"},children:[c>=1&&(f&&P>0||e&&r&&r.index<c)&&(0,x.jsx)("div",{style:{position:"absolute",inset:0,zIndex:-10},children:(0,x.jsx)(E,{location:t.at(c-1),children:(0,x.jsx)(V,{},c-1)})}),(0,x.jsx)("div",{style:{background:"white",position:"absolute",inset:0,overflow:"hidden",transform:e&&r&&r.index<c?"translateX(100%)":f&&P>0&&!F?`translateX(${P}px)`:"translateX(0px)",transition:F||e&&r&&r.index<c?`transform ${i}ms ease-out`:"",boxShadow:f&&P>0?"-4px 0 8px rgba(0,0,0,0.1)":"none"},onTouchStart:C,onTouchMove:v,onTouchEnd:R,children:(0,x.jsx)(V,{})},c),(f&&P<0||e&&r&&c<=r.index)&&(0,x.jsx)("div",{style:{background:"white",position:"absolute",inset:0,zIndex:10,overflow:"hidden",transition:"transform ease-in",transform:d?"translateX(0px)":e?"translateX(100%)":`translateX(${innerWidth+P}px)`,transitionDuration:e||F?`${i}ms`:"0ms"},children:(0,x.jsx)(E,{location:e?r:t.at(c+1),children:(0,x.jsx)(V,{},m)})})]})},Ht=({rootRoute:t})=>(0,x.jsx)(Mt,{rootRoute:t,children:(0,x.jsx)(At,{})});0&&(module.exports={DefaultTransitionDuration,Link,LocationContext,LocationProvider,Outlet,RootRouteContext,RouterContext,RouterProvider,Stack,buildPathnameFromMatches,createRouterOptions,matchPattern,matchRoute,parseLocation,parseRoute,redirect,useLocation,useRootRoute,useRoute,useRouter});
|
|
541
2
|
//# sourceMappingURL=index.cjs.map
|