@modastar/z-router 0.0.5 → 0.0.7
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 +7 -3
- package/vite.config.ts +10 -0
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 Y=Object.defineProperty;var Dt=Object.getOwnPropertyDescriptor;var Et=Object.getOwnPropertyNames;var Ft=Object.prototype.hasOwnProperty;var It=(t,n)=>{for(var o in n)Y(t,o,{get:n[o],enumerable:!0})},Nt=(t,n,o,s)=>{if(n&&typeof n=="object"||typeof n=="function")for(let e of Et(n))!Ft.call(t,e)&&e!==o&&Y(t,e,{get:()=>n[e],enumerable:!(s=Dt(n,e))||s.enumerable});return t};var $t=t=>Nt(Y({},"__esModule",{value:!0}),t);var qt={};It(qt,{DefaultTransitionDuration:()=>D,Link:()=>Bt,LocationContext:()=>N,LocationProvider:()=>M,Outlet:()=>et,RootRouteContext:()=>W,RouterContext:()=>I,RouterProvider:()=>Xt,Stack:()=>Ut,buildPathnameFromMatches:()=>Ot,createRouterOptions:()=>Wt,matchPattern:()=>St,matchRoute:()=>rt,parseLocation:()=>it,parseRoute:()=>st,redirect:()=>Gt,useLocation:()=>$,useRootRoute:()=>X,useRoute:()=>tt,useRouter:()=>b});module.exports=$t(qt);var ct=require("react");var ut=require("react"),I=(0,ut.createContext)(null);var b=()=>{let t=(0,ct.useContext)(I);if(t===null)throw new Error("useRouter must be used within a Stack");return t};var pt=require("react/jsx-runtime"),Bt=({to:t,replace:n,transition:o,duration:s,onFinish:e,...r})=>{let i=b();return(0,pt.jsx)("a",{...r,href:t,onClick:u=>{u.preventDefault(),i.navigate({to:t,replace:n,transition:o,duration:s,onFinish:e})}})};var mt=require("react"),N=(0,mt.createContext)(null);var U=require("react"),dt=require("react/jsx-runtime"),M=({location:t,...n})=>{let o=b(),s=(0,U.useCallback)(i=>t.state[i],[t]),e=(0,U.useCallback)((i,u)=>{o.setLocationState(t.index,{...t.state,[i]:u})},[o,t]),r=(0,U.useCallback)(i=>{delete t.state[i],o.setLocationState(t.index,t.state)},[o,t]);return(0,dt.jsx)(N.Provider,{value:{...t,canGoBack:t.index>0,canGoForward:t.index<o.history.length-1,getState:s,setState:e,deleteState:r},...n})};var ft=require("react"),q=(0,ft.createContext)(0);var lt=require("react"),Rt=()=>(0,lt.useContext)(q);var ht=require("react"),A=(0,ht.createContext)(null);var gt=require("react"),z=()=>{let t=(0,gt.useContext)(A);if(t===null)throw new Error("useRouteMatch must be used within a RouteMatchProvider");return t};var yt=require("react/jsx-runtime"),xt=({depth:t,...n})=>(0,yt.jsx)(q.Provider,{value:t,...n});var vt=require("react");var $=()=>{let t=(0,vt.useContext)(N);if(t===null)throw new Error("useLocation must be used within a LocationProvider");return t};var wt=require("react"),B=(0,wt.createContext)(null);var Pt=require("react"),tt=()=>{let t=(0,Pt.useContext)(B);if(t===null)throw new Error("useRoute must be used within a RouteProvider");return t};var _=require("react");var G=require("react/jsx-runtime"),K=({depth:t=0})=>{let n=b(),o=$(),s=z(),e=tt(),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:p})=>{"to"in p&&n.navigate(p)}).finally(()=>{e.setState(r,!1),u(!1)}))},[]),!e)return null;if(t>=s.matches.length){let p=e.notFoundComponent;return(0,G.jsx)(p,{})}if(i){let p=e.pendingComponent;return(0,G.jsx)(p,{})}let d=e.component;return d?(0,G.jsx)(d,{}):(0,G.jsx)(et,{})};var bt=require("react");var Ct=require("react"),W=(0,Ct.createContext)(null);var X=()=>{let t=(0,bt.useContext)(W);if(t===null)throw new Error("useRootRoute must be used within a RootRouteProvider");return t};var ot=require("react"),nt=require("react/jsx-runtime"),Z=({route:t,...n})=>{if(!t)return(0,nt.jsx)(B.Provider,{value:null,...n});let o=X(),s=(0,ot.useCallback)(r=>o.getRouteState(t.id,r),[o.getRouteState]),e=(0,ot.useCallback)((r,i)=>{o.setRouteState(t.id,r,i)},[o.setRouteState]);return(0,nt.jsx)(B.Provider,{value:{...t,getState:s,setState:e},...n})};var H=require("react/jsx-runtime"),et=()=>{let t=z(),n=Rt()+1;return(0,H.jsx)(xt,{depth:n,children:(0,H.jsx)(Z,{route:t.matches.at(n),children:(0,H.jsx)(K,{depth:n})})})};var g=require("react");var Lt={defaultTransitionDuration:300};var D=300,Gt=t=>new Error("",{cause:t}),St=(t,n)=>{try{let o,s;if(n.startsWith("http://")||n.startsWith("https://")){let c=new URL(n);o=c.pathname,s=c.searchParams}else{let[c,w]=n.split("?");if(!c)return null;o=c,s=new URLSearchParams(w||"")}let e=o.replaceAll(/^\/|\/$/g,""),r=t.replaceAll(/^\/|\/$/g,""),i=e.split("/"),u=r.split("/");if(i.length!==u.length)return null;let d={};for(let c=0;c<u.length;c++){let w=u[c],v=i[c];if(w.startsWith(":")){let E=w.slice(1);d[E]=decodeURIComponent(v)}else if(w!==v)return null}let p=Object.fromEntries(s.entries());return{params:d,query:p}}catch{return null}},rt=(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=St(Ot(s),n);return r?{matches:s,...r}:null};return o([t],t)||{matches:[],params:{},query:{}}},Ot=t=>{let n=[];for(let o of t)o.pathname!==void 0&&n.push(o.pathname.replaceAll(/^\/|\/$/g,""));return"/"+n.join("/")},it=t=>({index:0,state:{},pathname:t.pathname,search:Object.fromEntries(new URLSearchParams(t.search))}),Wt=t=>({...Lt,...t}),st=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 at=require("react/jsx-runtime"),Xt=({options:t,...n})=>{let[o,s]=(0,g.useState)([it(window.location)]),[e,r]=(0,g.useState)(0),i=o.at(e),[u,d]=(0,g.useState)(!1),[p,c]=(0,g.useState)(D),[w,v]=(0,g.useState)();(0,g.useEffect)(()=>{window.history.replaceState({index:0},"");let m=({state:l})=>{r(l?.index??0)};return window.addEventListener("popstate",m),()=>{window.removeEventListener("popstate",m)}},[r]);let E=(0,g.useCallback)((m,l)=>{s(y=>y.map(f=>f.index===m?{...f,state:l}:f)),m===e&&window.history.replaceState({index:m,...l},"",i.pathname)},[e]),P=(0,g.useCallback)(({to:m,replace:l,transition:y,duration:f,updateHistory:h=!0,onFinish:k,...V})=>{if(u)return;let R=l?e:e+1,O;if(m.startsWith(".")){let L=i.pathname.split("/").filter(C=>C.length>0),Mt=m.split("/").filter(C=>C.length>0);for(let C of Mt)if(C.startsWith(".")){if(C===".")continue;if(C==="..")L.pop();else throw new Error(`Invalid relative path segment: ${C} in ${m}`)}else C.length>0&&L.push(C);O="/"+L.join("/")}else O=m;if(s(L=>[...R===o.length?o:L.slice(0,R),{index:R,search:{},state:{},pathname:O,...V}]),!l&&e>=0&&(y??t.defaultUseTransition?.(i,o.at(R)))){let L=f??D;d(!0),c(L),v(R),setTimeout(()=>{d(!1),v(void 0),r(R),k?.(),h&&window.history.pushState({index:R},"",m)},L)}else l?h&&window.history.replaceState({index:R},"",m):(r(R),h&&window.history.pushState({index:R},"",m))},[e,o,u,t]),F=(0,g.useCallback)(({transition:m,duration:l,onFinish:y,depth:f}={})=>{if(e===0||u)return;let h=e-(f??1);if(e>0&&(m??t.defaultUseTransition?.(i,o.at(h)))){let k=l??D;d(!0),c(k),v(h),setTimeout(()=>{d(!1),v(void 0),r(h),y?.(),window.history.back()},k)}else r(h),y?.(),window.history.back()},[e,o,u,t]),T=(0,g.useCallback)(({transition:m,duration:l,onFinish:y}={})=>{if(e+1>=o.length||u)return;let f=e+1;if(f<o.length&&(m??t.defaultUseTransition?.(i,o.at(f)))){let h=l??D;d(!0),c(h),v(f),setTimeout(()=>{d(!1),v(void 0),r(f),y?.(),window.history.forward()},h)}else r(f),y?.(),window.history.forward()},[e,o,u,t]);return(0,at.jsx)(I.Provider,{value:{options:t,history:o,location:i,canGoBack:e>0,canGoForward:e<o.length-1,isTransitioning:u,transitionDuration:p,transitioningToIndex:w,navigate:P,back:F,forward:T,setLocationState:E},children:(0,at.jsx)(M,{location:i,...n})})};var S=require("react");var J=require("react/jsx-runtime"),Q=()=>{let t=X(),n=$(),o=rt(t,n.pathname);return(0,J.jsx)(A.Provider,{value:o,children:(0,J.jsx)(Z,{route:t,children:(0,J.jsx)(K,{})})})};var j=require("react"),kt=require("react/jsx-runtime"),Tt=({rootRoute:t,...n})=>{let o=st(t),[s,e]=(0,j.useState)({}),r=(0,j.useCallback)((u,d)=>s[u]?.[d],[s]),i=(0,j.useCallback)((u,d,p)=>{e(c=>({...c,[u]:{...c[u],[d]:p}}))},[]);return(0,kt.jsx)(W.Provider,{value:{...o,getRouteState:r,setRouteState:i},...n})};var x=require("react/jsx-runtime"),jt=()=>{let{history:t,location:n,canGoBack:o,canGoForward:s,isTransitioning:e,transitioningToIndex:r,transitionDuration:i,back:u,forward:d}=b(),p=n?.index,[c,w]=(0,S.useState)(!1),[v,E]=(0,S.useState)(0),[P,F]=(0,S.useState)(0),[T,m]=(0,S.useState)(!1),[l,y]=(0,S.useState)(!1);if((0,S.useEffect)(()=>{!e||r===null||(y(!0),setTimeout(()=>{y(!1)},i))},[e,r]),p===void 0)return;let f=()=>{w(!1),F(0),m(!1)},h=R=>{e||!s&&!o||(w(!0),E(R.touches[0].clientX))},k=R=>{if(!c)return;let O=R.touches[0].clientX-v;if(O>0&&p===0||O<0&&p+1===t.length){F(0);return}F(Math.min(window.innerWidth,O))},V=()=>{c&&(P>window.innerWidth*.3&&o?u({onFinish:f}):P<-window.innerWidth*.3&&s?d({onFinish:f}):(m(!0),setTimeout(f,i)))};return(0,x.jsxs)("div",{style:{position:"relative",inset:0,height:"100%",width:"100%",overflow:"hidden"},children:[p>=1&&(c&&P>0||e&&r!==void 0&&r<p)&&(0,x.jsx)("div",{style:{position:"absolute",inset:0,zIndex:-10},children:(0,x.jsx)(M,{location:t.at(p-1),children:(0,x.jsx)(Q,{},p-1)})}),(0,x.jsx)("div",{style:{background:"white",position:"absolute",inset:0,overflow:"hidden",transform:e&&r!==void 0&&r<p?"translateX(100%)":c&&P>0&&!T?`translateX(${P}px)`:"translateX(0px)",transition:T||e&&r!==void 0&&r<p?`transform ${i}ms ease-out`:"",boxShadow:c&&P>0?"-4px 0 8px rgba(0,0,0,0.1)":"none"},onTouchStart:h,onTouchMove:k,onTouchEnd:V,children:(0,x.jsx)(Q,{})},p),(c&&P<0||e&&r!==void 0&&p<r)&&(0,x.jsx)("div",{style:{background:"white",position:"absolute",inset:0,zIndex:10,overflow:"hidden",transition:"transform ease-in",transform:l?"translateX(0px)":c&&!T?`translateX(${window.innerWidth+P}px)`:"translateX(100%)",transitionDuration:e||T?`${i}ms`:"0ms"},children:(0,x.jsx)(M,{location:c?t.at(p+1):t.at(r),children:(0,x.jsx)(Q,{},r)})},r)]})},Ut=({rootRoute:t})=>(0,x.jsx)(Tt,{rootRoute:t,children:(0,x.jsx)(jt,{})});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
|