@richie-router/react 0.0.1 → 0.1.1

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.
@@ -0,0 +1,110 @@
1
+ // packages/react/src/history.ts
2
+ function readBrowserLocation() {
3
+ return {
4
+ href: `${window.location.pathname}${window.location.search}${window.location.hash}`,
5
+ pathname: window.location.pathname,
6
+ search: window.location.search,
7
+ hash: window.location.hash,
8
+ state: window.history.state
9
+ };
10
+ }
11
+ function createBrowserHistory() {
12
+ return {
13
+ get location() {
14
+ return readBrowserLocation();
15
+ },
16
+ listen(listener) {
17
+ window.addEventListener("popstate", listener);
18
+ return () => window.removeEventListener("popstate", listener);
19
+ },
20
+ push(href, state) {
21
+ window.history.pushState(state, "", href);
22
+ },
23
+ replace(href, state) {
24
+ window.history.replaceState(state, "", href);
25
+ }
26
+ };
27
+ }
28
+ function readHashLocation() {
29
+ const rawHash = window.location.hash.replace(/^#/, "") || "/";
30
+ const url = new URL(rawHash.startsWith("/") ? `http://hash.local${rawHash}` : `http://hash.local/${rawHash}`);
31
+ return {
32
+ href: `${url.pathname}${url.search}${url.hash}`,
33
+ pathname: url.pathname,
34
+ search: url.search,
35
+ hash: url.hash,
36
+ state: window.history.state
37
+ };
38
+ }
39
+ function createHashHistory() {
40
+ return {
41
+ get location() {
42
+ return readHashLocation();
43
+ },
44
+ listen(listener) {
45
+ window.addEventListener("popstate", listener);
46
+ window.addEventListener("hashchange", listener);
47
+ return () => {
48
+ window.removeEventListener("popstate", listener);
49
+ window.removeEventListener("hashchange", listener);
50
+ };
51
+ },
52
+ push(href, state) {
53
+ window.history.pushState(state, "", `#${href.startsWith("/") ? href : `/${href}`}`);
54
+ },
55
+ replace(href, state) {
56
+ window.history.replaceState(state, "", `#${href.startsWith("/") ? href : `/${href}`}`);
57
+ }
58
+ };
59
+ }
60
+
61
+ class MemoryHistory {
62
+ entries;
63
+ index = 0;
64
+ listeners = new Set;
65
+ constructor(options) {
66
+ const initialEntries = options?.initialEntries?.length ? options.initialEntries : ["/"];
67
+ this.entries = initialEntries.map((entry) => ({ href: entry, state: null }));
68
+ this.index = this.entries.length - 1;
69
+ }
70
+ get location() {
71
+ const current = this.entries[this.index] ?? { href: "/", state: null };
72
+ const url = new URL(current.href, "http://memory.local");
73
+ return {
74
+ href: `${url.pathname}${url.search}${url.hash}`,
75
+ pathname: url.pathname,
76
+ search: url.search,
77
+ hash: url.hash,
78
+ state: current.state
79
+ };
80
+ }
81
+ listen(listener) {
82
+ this.listeners.add(listener);
83
+ return () => {
84
+ this.listeners.delete(listener);
85
+ };
86
+ }
87
+ push(href, state) {
88
+ this.entries.splice(this.index + 1);
89
+ this.entries.push({ href, state });
90
+ this.index = this.entries.length - 1;
91
+ this.emit();
92
+ }
93
+ replace(href, state) {
94
+ this.entries[this.index] = { href, state };
95
+ this.emit();
96
+ }
97
+ emit() {
98
+ for (const listener of this.listeners) {
99
+ listener();
100
+ }
101
+ }
102
+ }
103
+ function createMemoryHistory(options) {
104
+ return new MemoryHistory(options);
105
+ }
106
+ export {
107
+ createMemoryHistory,
108
+ createHashHistory,
109
+ createBrowserHistory
110
+ };
@@ -0,0 +1,3 @@
1
+ // packages/react/src/index.ts
2
+ export * from "./history.mjs";
3
+ export * from "./router.mjs";
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }