@farbenmeer/router 0.6.1 → 0.6.2
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.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/mock-history.d.ts +1 -0
- package/dist/mock-history.d.ts.map +1 -1
- package/dist/mock-history.js +10 -0
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +12 -1
- package/dist/switch.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/mock-history.d.ts
CHANGED
|
@@ -4,5 +4,6 @@ export declare function mockHistory(pathname?: string): {
|
|
|
4
4
|
pushState: import("vitest").Mock<(_state: any, _unused: string, url: string) => void>;
|
|
5
5
|
replaceState: import("vitest").Mock<(_state: any, _unused: string, url: string) => void>;
|
|
6
6
|
};
|
|
7
|
+
back(): void;
|
|
7
8
|
};
|
|
8
9
|
//# sourceMappingURL=mock-history.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock-history.d.ts","sourceRoot":"","sources":["../src/mock-history.ts"],"names":[],"mappings":"AAEA,wBAAgB,WAAW,CAAC,QAAQ,SAAM;;;
|
|
1
|
+
{"version":3,"file":"mock-history.d.ts","sourceRoot":"","sources":["../src/mock-history.ts"],"names":[],"mappings":"AAEA,wBAAgB,WAAW,CAAC,QAAQ,SAAM;;;kDAOV,GAAG,WAAW,MAAM,OAAO,MAAM;qDAI9B,GAAG,WAAW,MAAM,OAAO,MAAM;;;EAanE"}
|
package/dist/mock-history.js
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
import { vi } from "vitest";
|
|
2
2
|
export function mockHistory(pathname = "/") {
|
|
3
3
|
const location = new URL(pathname, "http://localhost:3000");
|
|
4
|
+
const stack = [location.href];
|
|
4
5
|
return {
|
|
5
6
|
location,
|
|
6
7
|
history: {
|
|
7
8
|
pushState: vi.fn((_state, _unused, url) => {
|
|
8
9
|
location.href = new URL(url, location.href).href;
|
|
10
|
+
stack.push(location.href);
|
|
9
11
|
}),
|
|
10
12
|
replaceState: vi.fn((_state, _unused, url) => {
|
|
11
13
|
location.href = new URL(url, location.href).href;
|
|
14
|
+
stack[stack.length - 1] = location.href;
|
|
12
15
|
}),
|
|
13
16
|
},
|
|
17
|
+
back() {
|
|
18
|
+
if (stack.length > 1) {
|
|
19
|
+
stack.pop();
|
|
20
|
+
location.href = stack[stack.length - 1];
|
|
21
|
+
window.dispatchEvent(new PopStateEvent("popstate"));
|
|
22
|
+
}
|
|
23
|
+
},
|
|
14
24
|
};
|
|
15
25
|
}
|
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,
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAiD,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAUtF,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,2CAoDP"}
|
package/dist/router.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { startTransition, useMemo, useState } from "react";
|
|
2
|
+
import { startTransition, useEffect, useMemo, useState } from "react";
|
|
3
3
|
import { HashContext, PathnameContext, RouterContext, SearchParamsContext, } from "./context";
|
|
4
4
|
import { ImmutableSearchParams } from "./immutable-search-params";
|
|
5
5
|
import { removeTrailingSlash } from "./path";
|
|
@@ -7,6 +7,17 @@ export function Router({ history = window.history, location = window.location, c
|
|
|
7
7
|
const [pathname, setPathname] = useState(removeTrailingSlash(location.pathname));
|
|
8
8
|
const [searchParams, setSearchParams] = useState(new ImmutableSearchParams(location.search));
|
|
9
9
|
const [hash, setHash] = useState(location.hash);
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
const handlePopstate = () => {
|
|
12
|
+
startTransition(() => {
|
|
13
|
+
setPathname(removeTrailingSlash(location.pathname));
|
|
14
|
+
setSearchParams(new ImmutableSearchParams(location.search));
|
|
15
|
+
setHash(location.hash);
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
window.addEventListener("popstate", handlePopstate);
|
|
19
|
+
return () => window.removeEventListener("popstate", handlePopstate);
|
|
20
|
+
}, [location]);
|
|
10
21
|
const routerContextValue = useMemo(() => ({
|
|
11
22
|
push: (url) => {
|
|
12
23
|
history.pushState(null, "", url);
|
package/dist/switch.js
CHANGED