@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 CHANGED
@@ -6,4 +6,5 @@ export { usePathname } from "./use-pathname";
6
6
  export { useSearchParams } from "./use-search-params";
7
7
  export { useHash } from "./use-hash";
8
8
  export { useParams } from "./use-params";
9
+ export { Switch } from "./switch";
9
10
  //# sourceMappingURL=index.d.ts.map
@@ -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
@@ -6,3 +6,4 @@ export { usePathname } from "./use-pathname";
6
6
  export { useSearchParams } from "./use-search-params";
7
7
  export { useHash } from "./use-hash";
8
8
  export { useParams } from "./use-params";
9
+ export { Switch } from "./switch";
@@ -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;;;kDAMV,GAAG,WAAW,MAAM,OAAO,MAAM;qDAG9B,GAAG,WAAW,MAAM,OAAO,MAAM;;EAKnE"}
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"}
@@ -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
  }
@@ -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;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"}
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
@@ -34,7 +34,7 @@ export function Switch({ children }) {
34
34
  ];
35
35
  }
36
36
  return null;
37
- }, [routeMeta]);
37
+ }, [routeMeta, pathname]);
38
38
  if (!match) {
39
39
  return null;
40
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@farbenmeer/router",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "author": {
5
5
  "name": "Michel Smola",
6
6
  "email": "michel.smola@farbenmeer.de"