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