aprog 1.0.1 → 1.1.0
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 +51 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/src/NotFound.js +32 -0
- package/src/router.js +28 -0
package/README.md
CHANGED
|
@@ -18,6 +18,57 @@ npm i aprog -D
|
|
|
18
18
|
|
|
19
19
|
### Basic Usage
|
|
20
20
|
|
|
21
|
+
Підключення router:
|
|
22
|
+
|
|
23
|
+
```shell
|
|
24
|
+
import Categories from "./pages/Categories";
|
|
25
|
+
import Access from "./pages/Access";
|
|
26
|
+
import Unauthorized from "./pages/Unauthorized";
|
|
27
|
+
|
|
28
|
+
const routes = [
|
|
29
|
+
{
|
|
30
|
+
path: "/categories",
|
|
31
|
+
component: Categories,
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
path: "/access/denied",
|
|
35
|
+
component: Access,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
path: "/unauthorized",
|
|
39
|
+
component: Unauthorized,
|
|
40
|
+
}
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
document.addEventListener("DOMContentLoaded", async function() {
|
|
44
|
+
// router
|
|
45
|
+
const body = document.body;
|
|
46
|
+
await router({ routes, body, "Page Not Found" });
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Створення Pages:
|
|
52
|
+
|
|
53
|
+
```shell
|
|
54
|
+
|
|
55
|
+
const NamePage = () => {
|
|
56
|
+
return div({
|
|
57
|
+
class: "w-full h-screen flex flex-col items-center justify-center text-3xl",
|
|
58
|
+
value: [
|
|
59
|
+
div({
|
|
60
|
+
class: "text-red font-bold text-5xl",
|
|
61
|
+
value: "Блок №1"
|
|
62
|
+
}),
|
|
63
|
+
div({value: "Блок №2"})
|
|
64
|
+
]
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export default NamePage;
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
|
|
21
72
|
Приклад використання методів формування HTML елементів:
|
|
22
73
|
|
|
23
74
|
```shell
|
package/index.js
CHANGED
package/package.json
CHANGED
package/src/NotFound.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {div} from "./aprog.js";
|
|
2
|
+
|
|
3
|
+
const NotFound = (message = "Page Not Found") => {
|
|
4
|
+
return div({
|
|
5
|
+
style: style,
|
|
6
|
+
value: [
|
|
7
|
+
div({
|
|
8
|
+
style: style2,
|
|
9
|
+
value: "404"
|
|
10
|
+
}),
|
|
11
|
+
div({value: message})
|
|
12
|
+
]
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const style = {
|
|
17
|
+
width: "100%",
|
|
18
|
+
height: "100vh",
|
|
19
|
+
display: "flex",
|
|
20
|
+
flexDirection: "column",
|
|
21
|
+
alignItems: "center",
|
|
22
|
+
justifyContent: "center",
|
|
23
|
+
fontSize: "1.875rem"
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const style2 = {
|
|
27
|
+
fontSize: "3rem",
|
|
28
|
+
fontWeight: "bold",
|
|
29
|
+
color: "#ff2800"
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default NotFound;
|
package/src/router.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import NotFound from "NotFound";
|
|
2
|
+
|
|
3
|
+
const router = async ({ routes, main, message = "Page Not Found" }) => {
|
|
4
|
+
const renderRoute = async () => {
|
|
5
|
+
const routeComponent = await useRoutes(routes, message);
|
|
6
|
+
main.innerHTML = "";
|
|
7
|
+
if (routeComponent) {
|
|
8
|
+
main.appendChild(routeComponent);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
window.addEventListener('popstate', renderRoute);
|
|
12
|
+
await renderRoute();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
const navigateTo = (path) => {
|
|
17
|
+
window.history.pushState("", "", path);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
const useRoutes = async (routes, message) => {
|
|
22
|
+
const path = window.location.pathname;
|
|
23
|
+
const matchingRoute = routes.find(route => route.path === path);
|
|
24
|
+
return matchingRoute ? await matchingRoute.component() : NotFound(message);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
export { router, navigateTo };
|