olova-router 1.0.0 → 1.0.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/olova-router.js +51 -0
- package/dist/router.d.ts +1 -1
- package/package.json +15 -18
- package/README.md +0 -18
- package/dist/router.js +0 -40
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { State, Effect, Component, h, Memo } from "olova";
|
|
2
|
+
|
|
3
|
+
const [currentRoute, setCurrentRoute] = State(window.location.pathname);
|
|
4
|
+
|
|
5
|
+
function Router(props) {
|
|
6
|
+
Effect(() => {
|
|
7
|
+
const handlePopState = () => {
|
|
8
|
+
setCurrentRoute(window.location.pathname);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
window.addEventListener("popstate", handlePopState);
|
|
12
|
+
return () => window.removeEventListener("popstate", handlePopState);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
return h("div", { class: "router" }, props?.children);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function Route({ path, component: RouteComponent }) {
|
|
19
|
+
const matches = Memo(() => {
|
|
20
|
+
return currentRoute() === path;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const routeContent = document.createElement("div");
|
|
24
|
+
|
|
25
|
+
Effect(() => {
|
|
26
|
+
if (matches()) {
|
|
27
|
+
routeContent.innerHTML = "";
|
|
28
|
+
routeContent.appendChild(h(RouteComponent, null));
|
|
29
|
+
} else {
|
|
30
|
+
routeContent.innerHTML = "";
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return routeContent;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function Link({ to, children }) {
|
|
38
|
+
const handleClick = (e) => {
|
|
39
|
+
e.preventDefault();
|
|
40
|
+
navigate(to);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return h("a", { href: to, onClick: handleClick }, children);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function navigate(to) {
|
|
47
|
+
window.history.pushState({}, "", to);
|
|
48
|
+
setCurrentRoute(to);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { Router, Route, Link, navigate };
|
package/dist/router.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
declare module "router";
|
|
1
|
+
declare module "olova-router";
|
package/package.json
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "olova-router",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
"olova": "latest"
|
|
17
|
-
}
|
|
18
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "olova-router",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"main": "dist/olova-router.js",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"javascript",
|
|
7
|
+
"framework",
|
|
8
|
+
"router",
|
|
9
|
+
"olova-router",
|
|
10
|
+
"olovajs"
|
|
11
|
+
],
|
|
12
|
+
"author": "Nazmul Hossain",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"description": "olova-router for olovaJs"
|
|
15
|
+
}
|
package/README.md
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# olova
|
|
2
|
-
|
|
3
|
-
```html
|
|
4
|
-
<div id="app">
|
|
5
|
-
<template>
|
|
6
|
-
<div i-data="{count : 0}">
|
|
7
|
-
<div>{ count }</div>
|
|
8
|
-
<button @click="count++">Increment</button>
|
|
9
|
-
</div>
|
|
10
|
-
</template>
|
|
11
|
-
</div>
|
|
12
|
-
|
|
13
|
-
<script type="module">
|
|
14
|
-
import { createApp } from "//unpkg.com/olova";
|
|
15
|
-
const app = createApp();
|
|
16
|
-
app.mount("#app");
|
|
17
|
-
</script>
|
|
18
|
-
```
|
package/dist/router.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { createApp } from "olova";
|
|
2
|
-
class Router {
|
|
3
|
-
constructor() {
|
|
4
|
-
(this.routes = new Map()), (this.currentApp = null), this.init();
|
|
5
|
-
}
|
|
6
|
-
init() {
|
|
7
|
-
window.addEventListener("popstate", () => this.handleRoute()),
|
|
8
|
-
document.addEventListener("click", (t) => {
|
|
9
|
-
if (t.target.matches("a")) {
|
|
10
|
-
t.preventDefault();
|
|
11
|
-
const e = t.target.getAttribute("href");
|
|
12
|
-
this.navigate(e);
|
|
13
|
-
}
|
|
14
|
-
}),
|
|
15
|
-
window.addEventListener("load", () => {
|
|
16
|
-
this.handleRoute();
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
add(t, e) {
|
|
20
|
-
return this.routes.set(t, e), this;
|
|
21
|
-
}
|
|
22
|
-
navigate(t) {
|
|
23
|
-
window.history.pushState(null, "", t), this.handleRoute();
|
|
24
|
-
}
|
|
25
|
-
handleRoute() {
|
|
26
|
-
const t = window.location.pathname,
|
|
27
|
-
e = this.routes.get(t),
|
|
28
|
-
n = document.querySelector("[i-view]");
|
|
29
|
-
if (n)
|
|
30
|
-
if (e) {
|
|
31
|
-
this.currentApp && (n.innerHTML = "");
|
|
32
|
-
const t = document.createElement("div");
|
|
33
|
-
(t.id = "route-container"),
|
|
34
|
-
n.appendChild(t),
|
|
35
|
-
(this.currentApp = createApp({ ...e })),
|
|
36
|
-
this.currentApp.mount("#route-container");
|
|
37
|
-
} else n.innerHTML = "<h1>404 Not Found</h1>";
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
export const router = new Router();
|