@gohanfromgoku/ui-kit 0.0.4 → 0.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/dist/components/Button/index.js +1 -1
- package/dist/components/index.js +5 -0
- package/dist/helpers/index.js +3 -0
- package/dist/hooks/index.js +3 -0
- package/dist/navigation/Outlet.js +26 -0
- package/dist/navigation/Page404.js +2 -0
- package/dist/navigation/createRoutes.js +6 -0
- package/dist/navigation/extractParamsFromURL.js +24 -0
- package/dist/navigation/extractQueryParamsFromURL.js +24 -0
- package/dist/navigation/index.js +4 -0
- package/dist/navigation/isRouteMatching.js +15 -0
- package/dist/navigation/navigate.js +41 -0
- package/dist/navigation/navigation.store.js +4 -0
- package/dist/navigation/useLocationDetails.js +10 -0
- package/package.json +16 -42
- package/types/components/index.d.ts +5 -0
- package/types/helpers/index.d.ts +3 -0
- package/types/hooks/index.d.ts +3 -0
- package/types/navigation/Outlet.d.ts +7 -0
- package/types/navigation/Page404.d.ts +1 -0
- package/types/navigation/createRoutes.d.ts +8 -0
- package/types/navigation/extractParamsFromURL.d.ts +4 -0
- package/types/navigation/extractQueryParamsFromURL.d.ts +4 -0
- package/types/navigation/index.d.ts +4 -0
- package/types/navigation/isRouteMatching.d.ts +5 -0
- package/types/navigation/navigate.d.ts +6 -0
- package/types/navigation/navigation.store.d.ts +46 -0
- package/types/navigation/useLocationDetails.d.ts +5 -0
|
@@ -9,7 +9,7 @@ const defaultProps = {
|
|
|
9
9
|
};
|
|
10
10
|
const Button = forwardRef((props, ref) => {
|
|
11
11
|
const { children, className, onClick, disabled, type, ...rest } = { ...defaultProps, ...props };
|
|
12
|
-
return (_jsx("button", { ref: ref, className: className, onClick: (event) => {
|
|
12
|
+
return (_jsx("button", { ref: ref, className: className, disabled: disabled, type: type, onClick: (event) => {
|
|
13
13
|
if (type === "button" || !type) {
|
|
14
14
|
event.preventDefault();
|
|
15
15
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
2
|
+
import { activeRoute, location } from "./navigation.store";
|
|
3
|
+
import { Page404 } from "./Page404";
|
|
4
|
+
import { useEffect } from "preact/compat";
|
|
5
|
+
import isRouteMatching from "./isRouteMatching";
|
|
6
|
+
import { computed } from "@preact/signals";
|
|
7
|
+
import { extractParamsFromURL } from "./extractParamsFromURL";
|
|
8
|
+
import { extractQueryParamsFromURL } from "./extractQueryParamsFromURL";
|
|
9
|
+
const Outlet = ({ routes, page404: Page404Page = Page404 }) => {
|
|
10
|
+
const component = computed(() => activeRoute.value?.component ?? Page404Page);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
const updateLocation = () => {
|
|
13
|
+
const active = routes.find(r => isRouteMatching(r.pathname)) || { pathname: window.location.href, component: Page404Page };
|
|
14
|
+
const { pathname: currentpath, href } = window.location;
|
|
15
|
+
const params = extractParamsFromURL(currentpath, active.pathname);
|
|
16
|
+
const queryParams = extractQueryParamsFromURL(href);
|
|
17
|
+
activeRoute.setState({ value: active });
|
|
18
|
+
location.setState({ value: { params, queryParams } });
|
|
19
|
+
};
|
|
20
|
+
updateLocation();
|
|
21
|
+
window.addEventListener("popstate", updateLocation);
|
|
22
|
+
return () => window.removeEventListener("popstate", updateLocation);
|
|
23
|
+
}, [routes]);
|
|
24
|
+
return _jsx(component.value, {});
|
|
25
|
+
};
|
|
26
|
+
export default Outlet;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
export const extractParamsFromURL = (url, compareWith) => {
|
|
5
|
+
const params = {};
|
|
6
|
+
const href = url.startsWith(window.location.origin) ? url.split(window.location.origin)[1] : url;
|
|
7
|
+
const parts = href.split("?");
|
|
8
|
+
if (parts.length > 2) {
|
|
9
|
+
throw "Invalid URL";
|
|
10
|
+
}
|
|
11
|
+
const pathname = parts[0];
|
|
12
|
+
const pathnameSegments = pathname.split("/");
|
|
13
|
+
const routeSegments = compareWith.split("/");
|
|
14
|
+
for (let index = 0; index < routeSegments.length; index++) {
|
|
15
|
+
if (routeSegments[index].startsWith(":")) {
|
|
16
|
+
const paramParts = routeSegments[index].split(":");
|
|
17
|
+
if (paramParts.length > 2) {
|
|
18
|
+
throw "Invalid URL";
|
|
19
|
+
}
|
|
20
|
+
params[paramParts[1]] = pathnameSegments[index];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return params;
|
|
24
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
export const extractQueryParamsFromURL = (url) => {
|
|
5
|
+
const query = {};
|
|
6
|
+
const href = url.startsWith(window.location.origin) ? url.split(window.location.origin)[1] : url;
|
|
7
|
+
const parts = href.split("?");
|
|
8
|
+
if (parts.length > 2) {
|
|
9
|
+
throw "Invalid URL";
|
|
10
|
+
}
|
|
11
|
+
const search = parts[1];
|
|
12
|
+
if (search && search.length > 0) {
|
|
13
|
+
const searchSegments = search.split("&");
|
|
14
|
+
for (const ss of searchSegments) {
|
|
15
|
+
const ssparts = ss.split("=");
|
|
16
|
+
if (ssparts.length > 2) {
|
|
17
|
+
throw "Invalid URL";
|
|
18
|
+
}
|
|
19
|
+
const [key, value] = ssparts;
|
|
20
|
+
query[decodeURIComponent(key)] = decodeURIComponent(value ?? "");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return query;
|
|
24
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
4
|
+
const isRouteMatching = (routePath) => {
|
|
5
|
+
const current = window.location.pathname;
|
|
6
|
+
if (routePath === "/") {
|
|
7
|
+
return current === "/";
|
|
8
|
+
}
|
|
9
|
+
const routeParts = routePath.split("/").filter(Boolean);
|
|
10
|
+
const currentParts = current.split("/").filter(Boolean);
|
|
11
|
+
if (routeParts.length !== currentParts.length)
|
|
12
|
+
return false;
|
|
13
|
+
return routeParts.every((part, i) => part.startsWith(":") || part === currentParts[i]);
|
|
14
|
+
};
|
|
15
|
+
export default isRouteMatching;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { extractQueryParamsFromURL } from "./extractQueryParamsFromURL";
|
|
2
|
+
const createSearch = (queryParams) => {
|
|
3
|
+
const keys = Object.keys(queryParams);
|
|
4
|
+
if (keys.length > 0) {
|
|
5
|
+
let search = "?";
|
|
6
|
+
for (let index = 0; index < keys.length; index++) {
|
|
7
|
+
const key = keys[index];
|
|
8
|
+
search = `${search}${key}=${queryParams[key]}`;
|
|
9
|
+
if (index !== keys.length - 1) {
|
|
10
|
+
search = search + "&";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return search;
|
|
14
|
+
}
|
|
15
|
+
return "";
|
|
16
|
+
};
|
|
17
|
+
const generateURLFromParams = (pathname, queryParams) => {
|
|
18
|
+
const pathnameQueryParams = extractQueryParamsFromURL(pathname);
|
|
19
|
+
const allQueryParams = { ...pathnameQueryParams, ...queryParams };
|
|
20
|
+
const url = pathname.startsWith("/") ? `${window.location.origin}${pathname.split("?")[0]}` : `${window.location.origin}/${pathname.split("?")[0]}`;
|
|
21
|
+
const path = `${url}${createSearch(allQueryParams)}`;
|
|
22
|
+
return path;
|
|
23
|
+
};
|
|
24
|
+
const navigate = (path, { queryParams = {}, replace = false } = {}) => {
|
|
25
|
+
if (typeof path === "number") {
|
|
26
|
+
window.history.go(path);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const pathname = generateURLFromParams(path, queryParams);
|
|
30
|
+
if (window.location.href === pathname) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (replace) {
|
|
34
|
+
window.history.replaceState({}, "", pathname);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
window.history.pushState({}, "", pathname);
|
|
38
|
+
}
|
|
39
|
+
window.dispatchEvent(new Event("popstate"));
|
|
40
|
+
};
|
|
41
|
+
export default navigate;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import createStore from "../storage/index";
|
|
2
|
+
export const location = createStore("location", { value: { params: {}, queryParams: {} } });
|
|
3
|
+
export const activeRoute = createStore("activeRoute", { value: {} });
|
|
4
|
+
export const globalRoutes = createStore("globalRoutes", { value: [] });
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { computed } from "@preact/signals";
|
|
2
|
+
import { globalRoutes, location } from "./navigation.store";
|
|
3
|
+
const useLocationDetails = () => {
|
|
4
|
+
if (globalRoutes.value.length === 0) {
|
|
5
|
+
throw "useLocationDetails cannot be used without initializing routes, use createRoutes initialize routes";
|
|
6
|
+
}
|
|
7
|
+
const values = computed(() => location.value);
|
|
8
|
+
return values.value;
|
|
9
|
+
};
|
|
10
|
+
export default useLocationDetails;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gohanfromgoku/ui-kit",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "ui-kit is a TypeScript based UI component library designed for building modern web applications with ease and efficiency.",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"scripts": {
|
|
@@ -25,11 +25,13 @@
|
|
|
25
25
|
},
|
|
26
26
|
"author": "GohanfromGoku",
|
|
27
27
|
"license": "MIT",
|
|
28
|
-
"
|
|
28
|
+
"peerDependencies": {
|
|
29
29
|
"@preact/signals": "^2.5.1",
|
|
30
30
|
"preact": "^10.28.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
+
"@preact/signals": "^2.5.1",
|
|
34
|
+
"preact": "^10.28.0",
|
|
33
35
|
"tsc-alias": "^1.8.16",
|
|
34
36
|
"typescript": "^5.9.3"
|
|
35
37
|
},
|
|
@@ -48,49 +50,21 @@
|
|
|
48
50
|
"import": "./dist/api/index.js",
|
|
49
51
|
"types": "./types/api/index.d.ts"
|
|
50
52
|
},
|
|
51
|
-
"./
|
|
52
|
-
"import": "./dist/components/
|
|
53
|
-
"types": "./types/components/
|
|
54
|
-
},
|
|
55
|
-
"./Image": {
|
|
56
|
-
"import": "./dist/components/Image/index.js",
|
|
57
|
-
"types": "./types/components/Image/index.d.ts"
|
|
58
|
-
},
|
|
59
|
-
"./Input": {
|
|
60
|
-
"import": "./dist/components/Input/index.js",
|
|
61
|
-
"types": "./types/components/Input/index.d.ts"
|
|
62
|
-
},
|
|
63
|
-
"./Text": {
|
|
64
|
-
"import": "./dist/components/Text/index.js",
|
|
65
|
-
"types": "./types/components/Text/index.d.ts"
|
|
66
|
-
},
|
|
67
|
-
"./Tooltip": {
|
|
68
|
-
"import": "./dist/components/Tooltip/index.js",
|
|
69
|
-
"types": "./types/components/Tooltip/index.d.ts"
|
|
70
|
-
},
|
|
71
|
-
"./checkProps": {
|
|
72
|
-
"import": "./dist/helpers/checkProps.js",
|
|
73
|
-
"types": "./types/helpers/checkProps.d.ts"
|
|
74
|
-
},
|
|
75
|
-
"./objectPath": {
|
|
76
|
-
"import": "./dist/helpers/objectPath.js",
|
|
77
|
-
"types": "./types/helpers/objectPath.d.ts"
|
|
78
|
-
},
|
|
79
|
-
"./toast": {
|
|
80
|
-
"import": "./dist/helpers/toast.js",
|
|
81
|
-
"types": "./types/helpers/toast.d.ts"
|
|
53
|
+
"./components": {
|
|
54
|
+
"import": "./dist/components/index.js",
|
|
55
|
+
"types": "./types/components/index.d.ts"
|
|
82
56
|
},
|
|
83
|
-
"./
|
|
84
|
-
"import": "./dist/
|
|
85
|
-
"types": "./types/
|
|
57
|
+
"./helpers": {
|
|
58
|
+
"import": "./dist/helpers/index.js",
|
|
59
|
+
"types": "./types/helpers/index.d.ts"
|
|
86
60
|
},
|
|
87
|
-
"./
|
|
88
|
-
"import": "./dist/hooks/
|
|
89
|
-
"types": "./types/hooks/
|
|
61
|
+
"./hooks": {
|
|
62
|
+
"import": "./dist/hooks/index.js",
|
|
63
|
+
"types": "./types/hooks/index.d.ts"
|
|
90
64
|
},
|
|
91
|
-
"./
|
|
92
|
-
"import": "./dist/
|
|
93
|
-
"types": "./types/
|
|
65
|
+
"./navigation": {
|
|
66
|
+
"import": "./dist/navigation/index.js",
|
|
67
|
+
"types": "./types/navigation/index.d.ts"
|
|
94
68
|
},
|
|
95
69
|
"./storage": {
|
|
96
70
|
"import": "./dist/storage/index.js",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const Page404: () => import("preact").JSX.Element;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Route } from "./createRoutes";
|
|
2
|
+
export declare const location: {
|
|
3
|
+
value: {
|
|
4
|
+
params: {};
|
|
5
|
+
queryParams: {};
|
|
6
|
+
};
|
|
7
|
+
} & {
|
|
8
|
+
setState: (values: Partial<{
|
|
9
|
+
value: {
|
|
10
|
+
params: {};
|
|
11
|
+
queryParams: {};
|
|
12
|
+
};
|
|
13
|
+
}>) => void;
|
|
14
|
+
resetState: () => void;
|
|
15
|
+
} & {
|
|
16
|
+
__signal: import("@preact/signals").Signal<{
|
|
17
|
+
value: {
|
|
18
|
+
params: {};
|
|
19
|
+
queryParams: {};
|
|
20
|
+
};
|
|
21
|
+
}>;
|
|
22
|
+
};
|
|
23
|
+
export declare const activeRoute: {
|
|
24
|
+
value: Route;
|
|
25
|
+
} & {
|
|
26
|
+
setState: (values: Partial<{
|
|
27
|
+
value: Route;
|
|
28
|
+
}>) => void;
|
|
29
|
+
resetState: () => void;
|
|
30
|
+
} & {
|
|
31
|
+
__signal: import("@preact/signals").Signal<{
|
|
32
|
+
value: Route;
|
|
33
|
+
}>;
|
|
34
|
+
};
|
|
35
|
+
export declare const globalRoutes: {
|
|
36
|
+
value: Route[];
|
|
37
|
+
} & {
|
|
38
|
+
setState: (values: Partial<{
|
|
39
|
+
value: Route[];
|
|
40
|
+
}>) => void;
|
|
41
|
+
resetState: () => void;
|
|
42
|
+
} & {
|
|
43
|
+
__signal: import("@preact/signals").Signal<{
|
|
44
|
+
value: Route[];
|
|
45
|
+
}>;
|
|
46
|
+
};
|