@farbenmeer/router 0.4.0 → 0.6.0
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/context.d.ts +3 -2
- package/dist/context.d.ts.map +1 -1
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +2 -17
- package/dist/mock-history.d.ts +8 -0
- package/dist/mock-history.d.ts.map +1 -0
- package/dist/mock-history.js +15 -0
- package/dist/path.d.ts +13 -0
- package/dist/path.d.ts.map +1 -0
- package/dist/path.js +60 -0
- package/dist/route.d.ts +2 -3
- package/dist/route.d.ts.map +1 -1
- package/dist/route.js +1 -36
- package/dist/router.d.ts +1 -1
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +19 -30
- package/dist/switch.d.ts +8 -0
- package/dist/switch.d.ts.map +1 -0
- package/dist/switch.js +43 -0
- package/dist/use-params.d.ts +1 -1
- package/dist/use-params.d.ts.map +1 -1
- package/dist/use-router.d.ts +3 -4
- package/dist/use-router.d.ts.map +1 -1
- package/dist/use-router.js +23 -3
- package/package.json +1 -1
package/dist/context.d.ts
CHANGED
|
@@ -6,9 +6,10 @@ export declare const RouterContext: import("react").Context<{
|
|
|
6
6
|
push: (href: string) => void;
|
|
7
7
|
replace: (href: string) => void;
|
|
8
8
|
}>;
|
|
9
|
-
export
|
|
9
|
+
export interface RouteContextValue {
|
|
10
10
|
path: string;
|
|
11
11
|
params: Record<string, string | string[]>;
|
|
12
12
|
matchedPathname: string;
|
|
13
|
-
}
|
|
13
|
+
}
|
|
14
|
+
export declare const RouteContext: import("react").Context<RouteContextValue>;
|
|
14
15
|
//# sourceMappingURL=context.d.ts.map
|
package/dist/context.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAElE,eAAO,MAAM,eAAe,iCAA6B,CAAC;AAE1D,eAAO,MAAM,mBAAmB,gDAE/B,CAAC;AAEF,eAAO,MAAM,WAAW,iCAA4B,CAAC;AAErD,eAAO,MAAM,aAAa;UAClB,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI;aACnB,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI;EAI/B,CAAC;AAEH,
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAElE,eAAO,MAAM,eAAe,iCAA6B,CAAC;AAE1D,eAAO,MAAM,mBAAmB,gDAE/B,CAAC;AAEF,eAAO,MAAM,WAAW,iCAA4B,CAAC;AAErD,eAAO,MAAM,aAAa;UAClB,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI;aACnB,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI;EAI/B,CAAC;AAEH,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;IAC1C,eAAe,EAAE,MAAM,CAAC;CACzB;AAED,eAAO,MAAM,YAAY,4CAIvB,CAAC"}
|
package/dist/link.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"link.d.ts","sourceRoot":"","sources":["../src/link.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,SAAS,EAAkB,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"link.d.ts","sourceRoot":"","sources":["../src/link.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,SAAS,EAAkB,MAAM,OAAO,CAAC;AASrE,UAAU,KAAM,SAAQ,SAAS,CAAC,iBAAiB,CAAC;IAClD,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,wBAAgB,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,QAAQ,EAAE,EAAE,KAAK,2CA6B5E"}
|
package/dist/link.js
CHANGED
|
@@ -1,28 +1,13 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { use, useMemo } from "react";
|
|
3
3
|
import { PathnameContext, RouteContext, RouterContext, SearchParamsContext, } from "./context";
|
|
4
|
+
import { resolve } from "./path";
|
|
4
5
|
export function Link({ href, replace, children, onClick, ...rawProps }) {
|
|
5
6
|
const { matchedPathname: parentPathname } = use(RouteContext);
|
|
6
7
|
const router = use(RouterContext);
|
|
7
8
|
const pathname = use(PathnameContext);
|
|
8
9
|
const searchParams = use(SearchParamsContext);
|
|
9
|
-
const target = useMemo(() => {
|
|
10
|
-
if (href.startsWith("/")) {
|
|
11
|
-
return href;
|
|
12
|
-
}
|
|
13
|
-
if (href.startsWith("?")) {
|
|
14
|
-
return pathname + href;
|
|
15
|
-
}
|
|
16
|
-
if (href.startsWith("#")) {
|
|
17
|
-
return (pathname +
|
|
18
|
-
(searchParams.size > 0 ? "?" + searchParams.toString() : "") +
|
|
19
|
-
href);
|
|
20
|
-
}
|
|
21
|
-
if (!href) {
|
|
22
|
-
return parentPathname;
|
|
23
|
-
}
|
|
24
|
-
return parentPathname + "/" + href;
|
|
25
|
-
}, [href, parentPathname, pathname]);
|
|
10
|
+
const target = useMemo(() => resolve(href, { pathname, parentPathname, searchParams }), [href, parentPathname, pathname]);
|
|
26
11
|
return (_jsx("a", { href: target, onClick: (event) => {
|
|
27
12
|
onClick?.(event);
|
|
28
13
|
if (event.defaultPrevented)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare function mockHistory(pathname?: string): {
|
|
2
|
+
location: URL;
|
|
3
|
+
history: {
|
|
4
|
+
pushState: import("vitest").Mock<(_state: any, _unused: string, url: string) => void>;
|
|
5
|
+
replaceState: import("vitest").Mock<(_state: any, _unused: string, url: string) => void>;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=mock-history.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock-history.d.ts","sourceRoot":"","sources":["../src/mock-history.ts"],"names":[],"mappings":"AAEA,wBAAgB,WAAW,CAAC,QAAQ,SAAM;;;kDAMV,GAAG,WAAW,MAAM,OAAO,MAAM;qDAG9B,GAAG,WAAW,MAAM,OAAO,MAAM;;EAKnE"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { vi } from "vitest";
|
|
2
|
+
export function mockHistory(pathname = "/") {
|
|
3
|
+
const location = new URL(pathname, "http://localhost:3000");
|
|
4
|
+
return {
|
|
5
|
+
location,
|
|
6
|
+
history: {
|
|
7
|
+
pushState: vi.fn((_state, _unused, url) => {
|
|
8
|
+
location.href = new URL(url, location.href).href;
|
|
9
|
+
}),
|
|
10
|
+
replaceState: vi.fn((_state, _unused, url) => {
|
|
11
|
+
location.href = new URL(url, location.href).href;
|
|
12
|
+
}),
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
package/dist/path.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ImmutableSearchParams } from "./immutable-search-params";
|
|
2
|
+
interface Options {
|
|
3
|
+
pathname: string;
|
|
4
|
+
parentPathname: string;
|
|
5
|
+
searchParams: ImmutableSearchParams;
|
|
6
|
+
}
|
|
7
|
+
export declare function resolve(path: string, { pathname, parentPathname, searchParams }: Options): string;
|
|
8
|
+
export declare function removeTrailingSlash(path: string): string;
|
|
9
|
+
export declare function compilePathRegex(path: string): RegExp;
|
|
10
|
+
export declare function compileExactPathRegex(path: string): RegExp;
|
|
11
|
+
export declare function buildFullPath(parentPath: string, path?: string): string;
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=path.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path.d.ts","sourceRoot":"","sources":["../src/path.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAElE,UAAU,OAAO;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,MAAM,CAAC;IACvB,YAAY,EAAE,qBAAqB,CAAC;CACrC;AAED,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,EAAE,QAAQ,EAAE,cAAc,EAAE,YAAY,EAAE,EAAE,OAAO,UAepD;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGxD;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAiBrD;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAU1D;AAED,wBAAgB,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,UAa9D"}
|
package/dist/path.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ImmutableSearchParams } from "./immutable-search-params";
|
|
2
|
+
export function resolve(path, { pathname, parentPathname, searchParams }) {
|
|
3
|
+
if (path.startsWith("/")) {
|
|
4
|
+
return path;
|
|
5
|
+
}
|
|
6
|
+
if (path.startsWith("?")) {
|
|
7
|
+
return pathname + path;
|
|
8
|
+
}
|
|
9
|
+
if (path.startsWith("#")) {
|
|
10
|
+
return pathname + searchParams.search + path;
|
|
11
|
+
}
|
|
12
|
+
if (!path) {
|
|
13
|
+
return parentPathname;
|
|
14
|
+
}
|
|
15
|
+
return parentPathname === "/" ? `/${path}` : `${parentPathname}/${path}`;
|
|
16
|
+
}
|
|
17
|
+
export function removeTrailingSlash(path) {
|
|
18
|
+
if (path === "/")
|
|
19
|
+
return path;
|
|
20
|
+
return path.endsWith("/") ? path.slice(0, -1) : path;
|
|
21
|
+
}
|
|
22
|
+
export function compilePathRegex(path) {
|
|
23
|
+
if (path === "/") {
|
|
24
|
+
return /^\//;
|
|
25
|
+
}
|
|
26
|
+
// Handle wildcards: *name captures as named group, * catches all without capturing
|
|
27
|
+
const pattern = path
|
|
28
|
+
.replaceAll(/\*(\w+)/g, "(?<$1>.+)") // *name -> named capture group
|
|
29
|
+
.replaceAll(/\*/g, ".+") // * -> match everything including /
|
|
30
|
+
.replaceAll(/:(\w+)/g, "(?<$1>[\\w-]+)"); // :param -> named capture group
|
|
31
|
+
// If pattern contains a wildcard, it already matches everything - use exact match
|
|
32
|
+
if (path.includes("*")) {
|
|
33
|
+
return new RegExp(`^(${pattern})$`);
|
|
34
|
+
}
|
|
35
|
+
// For non-wildcard paths, allow optional trailing paths
|
|
36
|
+
return new RegExp(`^(${pattern})(/.*)?$`);
|
|
37
|
+
}
|
|
38
|
+
export function compileExactPathRegex(path) {
|
|
39
|
+
if (path === "/") {
|
|
40
|
+
return /^\/$/;
|
|
41
|
+
}
|
|
42
|
+
// Handle wildcards: *name captures as named group, * catches all without capturing
|
|
43
|
+
const pattern = path
|
|
44
|
+
.replaceAll(/\*(\w+)/g, "(?<$1>.+)") // *name -> named capture group
|
|
45
|
+
.replaceAll(/\*/g, ".+") // * -> match everything including /
|
|
46
|
+
.replaceAll(/:(\w+)/g, "(?<$1>[\\w-]+)"); // :param -> named capture group
|
|
47
|
+
return new RegExp(`^(${pattern})$`);
|
|
48
|
+
}
|
|
49
|
+
export function buildFullPath(parentPath, path) {
|
|
50
|
+
if (path?.startsWith("/")) {
|
|
51
|
+
return path;
|
|
52
|
+
}
|
|
53
|
+
if (path) {
|
|
54
|
+
if (parentPath === "/") {
|
|
55
|
+
return "/" + path;
|
|
56
|
+
}
|
|
57
|
+
return parentPath + "/" + path;
|
|
58
|
+
}
|
|
59
|
+
return parentPath;
|
|
60
|
+
}
|
package/dist/route.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
interface
|
|
1
|
+
export interface RouteProps {
|
|
2
2
|
path?: string;
|
|
3
3
|
exact?: boolean;
|
|
4
4
|
children: React.ReactNode;
|
|
5
5
|
}
|
|
6
|
-
export declare function Route({ path, exact, children }:
|
|
7
|
-
export {};
|
|
6
|
+
export declare function Route({ path, exact, children }: RouteProps): import("react/jsx-runtime").JSX.Element | null;
|
|
8
7
|
//# sourceMappingURL=route.d.ts.map
|
package/dist/route.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../src/route.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../src/route.tsx"],"names":[],"mappings":"AAIA,MAAM,WAAW,UAAU;IACzB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAED,wBAAgB,KAAK,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,UAAU,kDA0B1D"}
|
package/dist/route.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { use, useMemo } from "react";
|
|
3
3
|
import { PathnameContext, RouteContext } from "./context";
|
|
4
|
+
import { buildFullPath, compileExactPathRegex, compilePathRegex } from "./path";
|
|
4
5
|
export function Route({ path, exact, children }) {
|
|
5
6
|
const parentRoute = use(RouteContext);
|
|
6
7
|
const pathname = use(PathnameContext);
|
|
@@ -16,39 +17,3 @@ export function Route({ path, exact, children }) {
|
|
|
16
17
|
return null;
|
|
17
18
|
return _jsx(RouteContext, { value: routeContextValue, children: children });
|
|
18
19
|
}
|
|
19
|
-
function compilePathRegex(path) {
|
|
20
|
-
if (path === "/") {
|
|
21
|
-
return /^\//;
|
|
22
|
-
}
|
|
23
|
-
// Handle wildcards: *name captures as named group, * catches all without capturing
|
|
24
|
-
const pattern = path
|
|
25
|
-
.replaceAll(/\*(\w+)/g, "(?<$1>.+)") // *name -> named capture group
|
|
26
|
-
.replaceAll(/\*/g, ".+") // * -> match everything including /
|
|
27
|
-
.replaceAll(/:(\w+)/g, "(?<$1>[\\w-]+)"); // :param -> named capture group
|
|
28
|
-
// If pattern contains a wildcard, it already matches everything - use exact match
|
|
29
|
-
if (path.includes("*")) {
|
|
30
|
-
return new RegExp(`^(${pattern})$`);
|
|
31
|
-
}
|
|
32
|
-
// For non-wildcard paths, allow optional trailing paths
|
|
33
|
-
return new RegExp(`^(${pattern})(/.*)?$`);
|
|
34
|
-
}
|
|
35
|
-
function compileExactPathRegex(path) {
|
|
36
|
-
if (path === "/") {
|
|
37
|
-
return /^\/$/;
|
|
38
|
-
}
|
|
39
|
-
// Handle wildcards: *name captures as named group, * catches all without capturing
|
|
40
|
-
const pattern = path
|
|
41
|
-
.replaceAll(/\*(\w+)/g, "(?<$1>.+)") // *name -> named capture group
|
|
42
|
-
.replaceAll(/\*/g, ".+") // * -> match everything including /
|
|
43
|
-
.replaceAll(/:(\w+)/g, "(?<$1>[\\w-]+)"); // :param -> named capture group
|
|
44
|
-
return new RegExp(`^(${pattern})$`);
|
|
45
|
-
}
|
|
46
|
-
function buildFullPath(parentPath, path) {
|
|
47
|
-
if (path?.startsWith("/")) {
|
|
48
|
-
return path;
|
|
49
|
-
}
|
|
50
|
-
if (path) {
|
|
51
|
-
return parentPath + "/" + path;
|
|
52
|
-
}
|
|
53
|
-
return parentPath;
|
|
54
|
-
}
|
package/dist/router.d.ts
CHANGED
|
@@ -11,6 +11,6 @@ interface Props {
|
|
|
11
11
|
replaceState: (state: any, title: string, url: string) => void;
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
|
-
export declare function Router(
|
|
14
|
+
export declare function Router({ history, location, children, }: Props): import("react/jsx-runtime").JSX.Element;
|
|
15
15
|
export {};
|
|
16
16
|
//# sourceMappingURL=router.d.ts.map
|
package/dist/router.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAsC,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAsC,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAU3E,UAAU,KAAK;IACb,QAAQ,EAAE,SAAS,CAAC;IACpB,QAAQ,CAAC,EAAE;QACT,QAAQ,EAAE,MAAM,CAAC;QACjB,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;IACF,OAAO,CAAC,EAAE;QACR,SAAS,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;QAC5D,YAAY,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;KAChE,CAAC;CACH;AAED,wBAAgB,MAAM,CAAC,EACrB,OAAwB,EACxB,QAA0B,EAC1B,QAAQ,GACT,EAAE,KAAK,2CAwCP"}
|
package/dist/router.js
CHANGED
|
@@ -2,39 +2,28 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { startTransition, useMemo, useState } from "react";
|
|
3
3
|
import { HashContext, PathnameContext, RouterContext, SearchParamsContext, } from "./context";
|
|
4
4
|
import { ImmutableSearchParams } from "./immutable-search-params";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const [
|
|
8
|
-
const [
|
|
9
|
-
const
|
|
5
|
+
import { removeTrailingSlash } from "./path";
|
|
6
|
+
export function Router({ history = window.history, location = window.location, children, }) {
|
|
7
|
+
const [pathname, setPathname] = useState(removeTrailingSlash(location.pathname));
|
|
8
|
+
const [searchParams, setSearchParams] = useState(new ImmutableSearchParams(location.search));
|
|
9
|
+
const [hash, setHash] = useState(location.hash);
|
|
10
10
|
const routerContextValue = useMemo(() => ({
|
|
11
11
|
push: (url) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
});
|
|
19
|
-
}
|
|
12
|
+
history.pushState(null, "", url);
|
|
13
|
+
startTransition(() => {
|
|
14
|
+
setPathname(removeTrailingSlash(location.pathname));
|
|
15
|
+
setSearchParams(new ImmutableSearchParams(location.search));
|
|
16
|
+
setHash(location.hash);
|
|
17
|
+
});
|
|
20
18
|
},
|
|
21
19
|
replace: (url) => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
});
|
|
29
|
-
}
|
|
20
|
+
history.replaceState(null, "", url);
|
|
21
|
+
startTransition(() => {
|
|
22
|
+
setPathname(removeTrailingSlash(location.pathname));
|
|
23
|
+
setSearchParams(new ImmutableSearchParams(location.search));
|
|
24
|
+
setHash(location.hash);
|
|
25
|
+
});
|
|
30
26
|
},
|
|
31
|
-
}), []);
|
|
32
|
-
return (_jsx(RouterContext, { value: routerContextValue, children: _jsx(PathnameContext, { value:
|
|
33
|
-
? removeTrailingSlash(props.location.pathname)
|
|
34
|
-
: pathname, children: _jsx(SearchParamsContext, { value: searchParams, children: _jsx(HashContext, { value: hash, children: props.children }) }) }) }));
|
|
35
|
-
}
|
|
36
|
-
function removeTrailingSlash(path) {
|
|
37
|
-
if (path === "/")
|
|
38
|
-
return path;
|
|
39
|
-
return path.endsWith("/") ? path.slice(0, -1) : path;
|
|
27
|
+
}), [location, history]);
|
|
28
|
+
return (_jsx(RouterContext, { value: routerContextValue, children: _jsx(PathnameContext, { value: pathname, children: _jsx(SearchParamsContext, { value: searchParams, children: _jsx(HashContext, { value: hash, children: children }) }) }) }));
|
|
40
29
|
}
|
package/dist/switch.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type ReactElement } from "react";
|
|
2
|
+
import type { Route, RouteProps } from "./route";
|
|
3
|
+
interface Props {
|
|
4
|
+
children: ReactElement<RouteProps, typeof Route> | ReactElement<RouteProps, typeof Route>[];
|
|
5
|
+
}
|
|
6
|
+
export declare function Switch({ children }: Props): import("react/jsx-runtime").JSX.Element | null;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=switch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"switch.d.ts","sourceRoot":"","sources":["../src/switch.tsx"],"names":[],"mappings":"AAAA,OAAO,EAA0B,KAAK,YAAY,EAAE,MAAM,OAAO,CAAC;AAClE,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAQjD,UAAU,KAAK;IACb,QAAQ,EACJ,YAAY,CAAC,UAAU,EAAE,OAAO,KAAK,CAAC,GACtC,YAAY,CAAC,UAAU,EAAE,OAAO,KAAK,CAAC,EAAE,CAAC;CAC9C;AAED,wBAAgB,MAAM,CAAC,EAAE,QAAQ,EAAE,EAAE,KAAK,kDAmDzC"}
|
package/dist/switch.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Children, use, useMemo } from "react";
|
|
3
|
+
import { PathnameContext, RouteContext, } from "./context";
|
|
4
|
+
import { buildFullPath, compileExactPathRegex, compilePathRegex } from "./path";
|
|
5
|
+
export function Switch({ children }) {
|
|
6
|
+
const parentRoute = use(RouteContext);
|
|
7
|
+
const pathname = use(PathnameContext);
|
|
8
|
+
const props = Children.map(children, (route) => route.props);
|
|
9
|
+
const routeMeta = useMemo(() => props.map((route) => {
|
|
10
|
+
const path = route.path ?? "";
|
|
11
|
+
const fullPath = buildFullPath(parentRoute.path, path);
|
|
12
|
+
return {
|
|
13
|
+
path,
|
|
14
|
+
fullPath,
|
|
15
|
+
pathRegex: route.exact
|
|
16
|
+
? compileExactPathRegex(fullPath)
|
|
17
|
+
: compilePathRegex(fullPath),
|
|
18
|
+
};
|
|
19
|
+
}), [
|
|
20
|
+
parentRoute.path,
|
|
21
|
+
props.map((route) => (route.exact ? "e" : "l" + route.path)).join(" "),
|
|
22
|
+
]);
|
|
23
|
+
const match = useMemo(() => {
|
|
24
|
+
for (const meta of routeMeta) {
|
|
25
|
+
const match = pathname.match(meta.pathRegex);
|
|
26
|
+
if (match)
|
|
27
|
+
return [
|
|
28
|
+
meta.path,
|
|
29
|
+
{
|
|
30
|
+
path: meta.fullPath,
|
|
31
|
+
params: match?.groups ?? {},
|
|
32
|
+
matchedPathname: match?.[1] ?? "",
|
|
33
|
+
},
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}, [routeMeta]);
|
|
38
|
+
if (!match) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const [path, context] = match;
|
|
42
|
+
return (_jsx(RouteContext, { value: context, children: props.find((route) => route.path === path)?.children }));
|
|
43
|
+
}
|
package/dist/use-params.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function useParams<T extends Record<string, string[]> = Record<string, string[]>>(): T;
|
|
1
|
+
export declare function useParams<T extends Record<string, string | string[]> = Record<string, string | string[]>>(): T;
|
|
2
2
|
//# sourceMappingURL=use-params.d.ts.map
|
package/dist/use-params.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-params.d.ts","sourceRoot":"","sources":["../src/use-params.ts"],"names":[],"mappings":"AAGA,wBAAgB,SAAS,CACvB,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"use-params.d.ts","sourceRoot":"","sources":["../src/use-params.ts"],"names":[],"mappings":"AAGA,wBAAgB,SAAS,CACvB,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,GAAG,MAAM,CAClD,MAAM,EACN,MAAM,GAAG,MAAM,EAAE,CAClB,KAIgB,CAAC,CACnB"}
|
package/dist/use-router.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
1
|
+
import { type ContextType } from "react";
|
|
2
|
+
import { RouterContext } from "./context";
|
|
3
|
+
export declare function useRouter(): ContextType<typeof RouterContext>;
|
|
5
4
|
//# sourceMappingURL=use-router.d.ts.map
|
package/dist/use-router.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-router.d.ts","sourceRoot":"","sources":["../src/use-router.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-router.d.ts","sourceRoot":"","sources":["../src/use-router.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAgB,MAAM,OAAO,CAAC;AACvD,OAAO,EAGL,aAAa,EAEd,MAAM,WAAW,CAAC;AAGnB,wBAAgB,SAAS,IAAI,WAAW,CAAC,OAAO,aAAa,CAAC,CA6B7D"}
|
package/dist/use-router.js
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
|
-
import { use } from "react";
|
|
2
|
-
import { RouterContext } from "./context";
|
|
1
|
+
import { use, useMemo } from "react";
|
|
2
|
+
import { PathnameContext, RouteContext, RouterContext, SearchParamsContext, } from "./context";
|
|
3
|
+
import { resolve } from "./path";
|
|
3
4
|
export function useRouter() {
|
|
4
|
-
|
|
5
|
+
const { push, replace } = use(RouterContext);
|
|
6
|
+
const { matchedPathname: parentPathname } = use(RouteContext);
|
|
7
|
+
const pathname = use(PathnameContext);
|
|
8
|
+
const searchParams = use(SearchParamsContext);
|
|
9
|
+
return useMemo(() => ({
|
|
10
|
+
push(href) {
|
|
11
|
+
push(resolve(href, {
|
|
12
|
+
pathname,
|
|
13
|
+
searchParams,
|
|
14
|
+
parentPathname,
|
|
15
|
+
}));
|
|
16
|
+
},
|
|
17
|
+
replace(href) {
|
|
18
|
+
replace(resolve(href, {
|
|
19
|
+
pathname,
|
|
20
|
+
searchParams,
|
|
21
|
+
parentPathname,
|
|
22
|
+
}));
|
|
23
|
+
},
|
|
24
|
+
}), [pathname, searchParams, parentPathname]);
|
|
5
25
|
}
|