@gohanfromgoku/ui-kit 1.0.0 → 1.0.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.
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { useEffect
|
|
2
|
+
import { useEffect } from "react";
|
|
3
3
|
import createStore from "../storage/index";
|
|
4
4
|
import { generateURLToNavigate, isRouteMatching } from "./support/url.support";
|
|
5
5
|
import { extractParamsFromURL } from "./support/params.support";
|
|
6
6
|
import { extractQueryParamsFromURL } from "./support/query-params.support";
|
|
7
|
-
// Initialize stores
|
|
8
|
-
const { locationDetails } = createStore("locationDetails", {
|
|
9
|
-
value: { params: {}, queryParams: {}, ...window.location }
|
|
10
|
-
});
|
|
11
|
-
const { activeRoute } = createStore("activeRoute", {
|
|
12
|
-
value: {}
|
|
13
|
-
});
|
|
14
7
|
const createBrowserRouter = (routes, { page404 } = { page404: () => _jsx(_Fragment, { children: "Page Not Found" }) }) => {
|
|
15
|
-
|
|
8
|
+
const getLatestLocationDetails = () => {
|
|
9
|
+
const { hash, href, origin, pathname, port, protocol, search } = window.location;
|
|
10
|
+
return { hash, href, origin, pathname, port, protocol, search };
|
|
11
|
+
};
|
|
12
|
+
const { locationDetails, useLocationDetailsState } = createStore("locationDetails", { params: {}, queryParams: {}, ...getLatestLocationDetails() });
|
|
13
|
+
const { activeRoute, useActiveRouteState } = createStore("activeRoute", routes.find(r => isRouteMatching(r.pathname)) || { pathname: window.location.pathname, component: page404 });
|
|
16
14
|
const navigate = (to, { queryParams, replace } = {}) => {
|
|
17
15
|
if (typeof to === "number") {
|
|
18
16
|
window.history.go(to);
|
|
@@ -27,44 +25,28 @@ const createBrowserRouter = (routes, { page404 } = { page404: () => _jsx(_Fragme
|
|
|
27
25
|
else {
|
|
28
26
|
window.history.pushState({}, "", url);
|
|
29
27
|
}
|
|
30
|
-
// Trigger update
|
|
31
28
|
window.dispatchEvent(new Event("popstate"));
|
|
32
29
|
};
|
|
33
|
-
// Hook to get current location details
|
|
34
30
|
const useLocationDetails = () => {
|
|
35
31
|
if (routes.length === 0) {
|
|
36
32
|
throw new Error("useLocationDetails cannot be used without initializing routes. Use createBrowserRouter with routes.");
|
|
37
33
|
}
|
|
38
|
-
|
|
39
|
-
useEffect(() => {
|
|
40
|
-
const update = () => setState(locationDetails.value);
|
|
41
|
-
window.addEventListener("popstate", update);
|
|
42
|
-
return () => window.removeEventListener("popstate", update);
|
|
43
|
-
}, []);
|
|
44
|
-
return state;
|
|
34
|
+
return useLocationDetailsState();
|
|
45
35
|
};
|
|
46
|
-
// Outlet component
|
|
47
36
|
const Outlet = () => {
|
|
48
|
-
const
|
|
37
|
+
const { component } = useActiveRouteState();
|
|
49
38
|
useEffect(() => {
|
|
50
39
|
const updateLocation = () => {
|
|
51
|
-
|
|
52
|
-
const active = routes.find(r => isRouteMatching(r.pathname)) ||
|
|
53
|
-
{ pathname: window.location.pathname, component: page404 };
|
|
54
|
-
activeRoute.setState({ value: active });
|
|
55
|
-
// Update location details
|
|
40
|
+
const active = routes.find(r => isRouteMatching(r.pathname)) || { pathname: window.location.pathname, component: page404 };
|
|
56
41
|
const params = extractParamsFromURL(window.location.pathname, active.pathname);
|
|
57
42
|
const queryParams = extractQueryParamsFromURL(window.location.href);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
setRender(r => r + 1);
|
|
43
|
+
activeRoute.setState(active);
|
|
44
|
+
locationDetails.setState({ params, queryParams, ...getLatestLocationDetails() });
|
|
61
45
|
};
|
|
62
|
-
// Initial update
|
|
63
|
-
updateLocation();
|
|
64
46
|
window.addEventListener("popstate", updateLocation);
|
|
65
47
|
return () => window.removeEventListener("popstate", updateLocation);
|
|
66
48
|
}, []);
|
|
67
|
-
const Component =
|
|
49
|
+
const Component = component ?? page404;
|
|
68
50
|
return _jsx(Component, {});
|
|
69
51
|
};
|
|
70
52
|
return {
|
|
@@ -5,12 +5,16 @@ import { generateURLToNavigate, isRouteMatching } from "./support/url.support";
|
|
|
5
5
|
import { extractParamsFromURL } from "./support/params.support";
|
|
6
6
|
import { extractQueryParamsFromURL } from "./support/query-params.support";
|
|
7
7
|
const createMemoryRouter = (routes, { page404 } = { page404: () => _jsx(_Fragment, { children: "Page Not Found" }) }) => {
|
|
8
|
+
const getLatestLocationDetails = () => {
|
|
9
|
+
const { hash, href, origin, port, protocol, search } = window.location;
|
|
10
|
+
return { hash, href, origin, port, protocol, search };
|
|
11
|
+
};
|
|
8
12
|
if (routes.length === 0) {
|
|
9
13
|
throw "Atleast one route should present";
|
|
10
14
|
}
|
|
11
15
|
;
|
|
12
|
-
const { activeRoute } = createStore("activeRoute", routes.find(() => isRouteMatching("/")) || { pathname: "/", component: page404 });
|
|
13
|
-
const { locationDetails } = createStore("locationDetails", { params: {}, queryParams: {}, ...
|
|
16
|
+
const { activeRoute, useActiveRouteState } = createStore("activeRoute", routes.find(() => isRouteMatching("/")) || { pathname: "/", component: page404 });
|
|
17
|
+
const { locationDetails, useLocationDetailsState } = createStore("locationDetails", { params: {}, queryParams: {}, ...getLatestLocationDetails(), pathname: activeRoute.pathname });
|
|
14
18
|
const { entries } = createStore("entries", { value: ["/"] });
|
|
15
19
|
const { currentIndex } = createStore("currentIndex", { value: 0 });
|
|
16
20
|
const navigate = (to, { queryParams } = {}) => {
|
|
@@ -25,7 +29,7 @@ const createMemoryRouter = (routes, { page404 } = { page404: () => _jsx(_Fragmen
|
|
|
25
29
|
activeRoute.setState(active);
|
|
26
30
|
const params = extractParamsFromURL(url, active.pathname);
|
|
27
31
|
const allQueryParams = extractQueryParamsFromURL(url);
|
|
28
|
-
locationDetails.setState({ params, queryParams: allQueryParams, ...
|
|
32
|
+
locationDetails.setState({ params, queryParams: allQueryParams, ...getLatestLocationDetails(), pathname: active.pathname });
|
|
29
33
|
return;
|
|
30
34
|
}
|
|
31
35
|
const url = generateURLToNavigate(to, queryParams || {});
|
|
@@ -36,19 +40,16 @@ const createMemoryRouter = (routes, { page404 } = { page404: () => _jsx(_Fragmen
|
|
|
36
40
|
activeRoute.setState(active);
|
|
37
41
|
const params = extractParamsFromURL(to, activeRoute.pathname);
|
|
38
42
|
const allQueryParams = extractQueryParamsFromURL(url);
|
|
39
|
-
locationDetails.setState({ params, queryParams: allQueryParams, ...
|
|
43
|
+
locationDetails.setState({ params, queryParams: allQueryParams, ...getLatestLocationDetails(), pathname: active.pathname });
|
|
40
44
|
currentIndex.setState({ value: currentIndex.value + 1 });
|
|
41
45
|
};
|
|
42
|
-
const useLocationDetails = () => {
|
|
43
|
-
return locationDetails;
|
|
44
|
-
};
|
|
45
46
|
const Outlet = () => {
|
|
46
|
-
const Component =
|
|
47
|
+
const { component: Component } = useActiveRouteState();
|
|
47
48
|
return _jsx(Component, {});
|
|
48
49
|
};
|
|
49
50
|
return {
|
|
50
51
|
navigate,
|
|
51
|
-
useLocationDetails,
|
|
52
|
+
useLocationDetails: useLocationDetailsState,
|
|
52
53
|
Outlet
|
|
53
54
|
};
|
|
54
55
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { objectPath } from "../helpers/index";
|
|
3
2
|
import { createContext, useContext, useState, useEffect, } from "react";
|
|
3
|
+
import { objectPath } from "../helpers/index";
|
|
4
4
|
const TranslationsContext = createContext(null);
|
|
5
5
|
const isBrowser = typeof window !== "undefined";
|
|
6
6
|
const interpolate = (str, data) => str.replace(/{{\s*(\w+)\s*}}/g, (_, key) => key in data ? String(data[key]) : `{{${key}}}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gohanfromgoku/ui-kit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
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": {
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"author": "GohanfromGoku",
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"react": "
|
|
29
|
+
"react": ">=18"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@types/react": "
|
|
32
|
+
"@types/react": ">=18",
|
|
33
33
|
"react": "^19.2.3",
|
|
34
34
|
"tsc-alias": "^1.8.16",
|
|
35
35
|
"typescript": "^5.9.3"
|
|
@@ -11,20 +11,13 @@ declare const createBrowserRouter: (routes: Route[], { page404 }?: {
|
|
|
11
11
|
replace?: boolean;
|
|
12
12
|
}) => void;
|
|
13
13
|
useLocationDetails: () => {
|
|
14
|
-
ancestorOrigins: DOMStringList;
|
|
15
14
|
hash: string;
|
|
16
|
-
host: string;
|
|
17
|
-
hostname: string;
|
|
18
15
|
href: string;
|
|
19
|
-
toString(): string;
|
|
20
16
|
origin: string;
|
|
21
17
|
pathname: string;
|
|
22
18
|
port: string;
|
|
23
19
|
protocol: string;
|
|
24
20
|
search: string;
|
|
25
|
-
assign(url: string | URL): void;
|
|
26
|
-
reload(): void;
|
|
27
|
-
replace(url: string | URL): void;
|
|
28
21
|
params: {};
|
|
29
22
|
queryParams: {};
|
|
30
23
|
};
|
|
@@ -11,41 +11,14 @@ declare const createMemoryRouter: (routes: Route[], { page404 }?: {
|
|
|
11
11
|
}) => void;
|
|
12
12
|
useLocationDetails: () => {
|
|
13
13
|
pathname: string;
|
|
14
|
-
ancestorOrigins: DOMStringList;
|
|
15
14
|
hash: string;
|
|
16
|
-
host: string;
|
|
17
|
-
hostname: string;
|
|
18
15
|
href: string;
|
|
19
|
-
toString(): string;
|
|
20
16
|
origin: string;
|
|
21
17
|
port: string;
|
|
22
18
|
protocol: string;
|
|
23
19
|
search: string;
|
|
24
|
-
assign(url: string | URL): void;
|
|
25
|
-
reload(): void;
|
|
26
|
-
replace(url: string | URL): void;
|
|
27
20
|
params: {};
|
|
28
21
|
queryParams: {};
|
|
29
|
-
} & {
|
|
30
|
-
setState: (partial: Partial<{
|
|
31
|
-
pathname: string;
|
|
32
|
-
ancestorOrigins: DOMStringList;
|
|
33
|
-
hash: string;
|
|
34
|
-
host: string;
|
|
35
|
-
hostname: string;
|
|
36
|
-
href: string;
|
|
37
|
-
toString(): string;
|
|
38
|
-
origin: string;
|
|
39
|
-
port: string;
|
|
40
|
-
protocol: string;
|
|
41
|
-
search: string;
|
|
42
|
-
assign(url: string | URL): void;
|
|
43
|
-
reload(): void;
|
|
44
|
-
replace(url: string | URL): void;
|
|
45
|
-
params: {};
|
|
46
|
-
queryParams: {};
|
|
47
|
-
}>) => void;
|
|
48
|
-
resetState: () => void;
|
|
49
22
|
};
|
|
50
23
|
Outlet: () => import("react/jsx-runtime").JSX.Element;
|
|
51
24
|
};
|