preact-hashish-router 0.0.8 → 0.0.10
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/README.md +93 -1
- package/dist/Router.js +9 -5
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1 +1,93 @@
|
|
|
1
|
-
# Preact Hashish Router
|
|
1
|
+
# Preact Hashish Router
|
|
2
|
+
|
|
3
|
+
## Features
|
|
4
|
+
|
|
5
|
+
- `hash` and `browser` routing types.
|
|
6
|
+
- Support for lazy-loaded routes (`lazy` loading).
|
|
7
|
+
- Error handling integration with `ErrorRoute`.
|
|
8
|
+
- Fully typed.
|
|
9
|
+
- Ultra lightweight.
|
|
10
|
+
- No external dependencies.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install preact-hashish-router@latest
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Context Setup
|
|
21
|
+
|
|
22
|
+
First, ensure your application is wrapped within the router context. This will allow you to access routes and related functions.
|
|
23
|
+
|
|
24
|
+
```tsx
|
|
25
|
+
import { ErrorRoute, Route, Router, RouterErrorBoundary } from "preact-hashish-router";
|
|
26
|
+
import _404 from "./routes/404";
|
|
27
|
+
import AboutPage from "./routes/About";
|
|
28
|
+
import HomePage from "./routes/Home";
|
|
29
|
+
|
|
30
|
+
export default function App() {
|
|
31
|
+
return (
|
|
32
|
+
<Router type="hash">
|
|
33
|
+
<RouterErrorBoundary>
|
|
34
|
+
<Route path="/">
|
|
35
|
+
<HomePage />
|
|
36
|
+
</Route>
|
|
37
|
+
|
|
38
|
+
<Route path="/about">
|
|
39
|
+
<AboutPage />
|
|
40
|
+
</Route>
|
|
41
|
+
|
|
42
|
+
<ErrorRoute>
|
|
43
|
+
<_404 />
|
|
44
|
+
</ErrorRoute>
|
|
45
|
+
</RouterErrorBoundary>
|
|
46
|
+
</Router>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Using the `useRouter` Hook
|
|
52
|
+
|
|
53
|
+
The `useRouter` hook gives you access to the router context to programmatically navigate or retrieve information about the current route.
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
import { useRouter } from "preact-hashish-router";
|
|
57
|
+
|
|
58
|
+
function HomePage() {
|
|
59
|
+
const router = useRouter();
|
|
60
|
+
|
|
61
|
+
function goToAbout() {
|
|
62
|
+
router.go("/about");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<div>
|
|
67
|
+
<h1>Home Page</h1>
|
|
68
|
+
<button onClick={goToAbout}>Go to About</button>
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### `<A />` Component for Navigation
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
import { A } from "preact-hashish-router";
|
|
78
|
+
|
|
79
|
+
export default function Header() {
|
|
80
|
+
return (
|
|
81
|
+
<header>
|
|
82
|
+
<nav>
|
|
83
|
+
<A href="/">Home</A>
|
|
84
|
+
<A href="/about">About</A>
|
|
85
|
+
</nav>
|
|
86
|
+
</header>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Development
|
|
92
|
+
|
|
93
|
+
If you have any improvements or find any issues, feel free to contribute or open an issue in the associated repository.
|
package/dist/Router.js
CHANGED
|
@@ -3,7 +3,7 @@ import { useEffect, useMemo, useState } from "preact/hooks";
|
|
|
3
3
|
import { router_context } from "./context";
|
|
4
4
|
const get_hash_route = () => location.hash.slice(1) || "/";
|
|
5
5
|
export const Router = (props) => {
|
|
6
|
-
const [path, setPath] = useState(
|
|
6
|
+
const [path, setPath] = useState();
|
|
7
7
|
const [query, setQuery] = useState("");
|
|
8
8
|
const [itMatch, setItMatch] = useState(false);
|
|
9
9
|
const router_type = useMemo(() => {
|
|
@@ -44,6 +44,10 @@ export const Router = (props) => {
|
|
|
44
44
|
const [path, query] = get_hash_route().split("?");
|
|
45
45
|
setQuery(query || "");
|
|
46
46
|
setPath(path);
|
|
47
|
+
if (location.pathname !== "/") {
|
|
48
|
+
location.hash = location.pathname;
|
|
49
|
+
location.pathname = "";
|
|
50
|
+
}
|
|
47
51
|
hashEffectHandler.effect();
|
|
48
52
|
return () => hashEffectHandler.cleanUp();
|
|
49
53
|
}, []);
|
|
@@ -55,15 +59,15 @@ export const Router = (props) => {
|
|
|
55
59
|
browserEffectHandler.effect();
|
|
56
60
|
return () => browserEffectHandler.cleanUp();
|
|
57
61
|
}, []);
|
|
58
|
-
const handlerManualRouteChange = (
|
|
59
|
-
setPath(
|
|
62
|
+
const handlerManualRouteChange = (newPath) => {
|
|
63
|
+
setPath(newPath);
|
|
60
64
|
setItMatch(false);
|
|
61
65
|
if (router_type === "hash") {
|
|
62
|
-
location.hash =
|
|
66
|
+
location.hash = newPath;
|
|
63
67
|
return;
|
|
64
68
|
}
|
|
65
69
|
if (router_type === "browser") {
|
|
66
|
-
history.pushState(null, "", new URL(
|
|
70
|
+
history.pushState(null, "", new URL(newPath, location.origin));
|
|
67
71
|
return;
|
|
68
72
|
}
|
|
69
73
|
};
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "preact-hashish-router",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"description": "A simple router for preact",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "tsc -p ./tsconfig.json",
|
|
8
|
+
"build": "tsc -p ./tsconfig.lib.json",
|
|
9
9
|
"format": "prettier --write src --ignore-unknown",
|
|
10
|
+
"app:dev": "vite",
|
|
10
11
|
"prepublishOnly": "npm run build && npm run format",
|
|
11
12
|
"push": "npm version patch && git push",
|
|
12
13
|
"push-minor": "npm version minor && git push",
|
|
@@ -30,6 +31,7 @@
|
|
|
30
31
|
"author": "LiasCode",
|
|
31
32
|
"devDependencies": {
|
|
32
33
|
"@preact/preset-vite": "^2.10.1",
|
|
34
|
+
"@types/node": "^22.13.4",
|
|
33
35
|
"preact-hashish-router": "^0.0.6",
|
|
34
36
|
"prettier": "^3.5.1",
|
|
35
37
|
"prettier-plugin-organize-imports": "^4.1.0",
|