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