spinning-router 0.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.
@@ -0,0 +1,2 @@
1
+ /// <reference types="react" />
2
+ export declare const Location: import("react").Context<string>;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Location = void 0;
4
+ const react_1 = require("react");
5
+ exports.Location = (0, react_1.createContext)("");
@@ -0,0 +1,8 @@
1
+ /// <reference types="react" />
2
+ export type AsyncComponent = (arg: any) => Promise<JSX.Element>;
3
+ export type Route = {
4
+ path: string;
5
+ routes?: Routes;
6
+ component?: AsyncComponent;
7
+ };
8
+ export type Routes = Route[];
package/dist/Routes.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,16 @@
1
+ import React from "react";
2
+ import { Routes } from "./Routes";
3
+ type Parameters = {
4
+ [key: string]: any;
5
+ };
6
+ export declare const doMatchRoute: (routes: Routes, path: string[], parentParameters: Parameters) => Promise<JSX.Element>;
7
+ export declare const matchRoute: (routes: Routes, path: string) => Promise<JSX.Element | undefined>;
8
+ export declare const SpinningRouter: React.FC<{
9
+ routes: Routes;
10
+ errorPage?: React.FC<{
11
+ error: any;
12
+ }>;
13
+ notFoundPage?: React.ComponentType;
14
+ loadingIndicator?: React.ComponentType;
15
+ }>;
16
+ export {};
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.SpinningRouter = exports.matchRoute = exports.doMatchRoute = void 0;
13
+ const jsx_runtime_1 = require("react/jsx-runtime");
14
+ const react_1 = require("react");
15
+ const Location_1 = require("./Location");
16
+ const DefaultLoadingIndicator_1 = require("./components/DefaultLoadingIndicator");
17
+ const DefaultErrorPage_1 = require("./components/DefaultErrorPage");
18
+ const DefaultNotFoundPage_1 = require("./components/DefaultNotFoundPage");
19
+ const doMatchRoute = (routes, path, parentParameters) => __awaiter(void 0, void 0, void 0, function* () {
20
+ for (let r of routes) {
21
+ const rPath = r.path.split("/").filter(e => e != "");
22
+ if ((r.routes && rPath.length <= path.length) || rPath.length == path.length) {
23
+ let parameters = Object.assign({}, parentParameters);
24
+ let match = true;
25
+ for (let i in rPath) {
26
+ if (rPath[i].startsWith(":")) {
27
+ parameters[rPath[i].substring(1)] = path[i];
28
+ }
29
+ else {
30
+ if (rPath[i] != path[i]) {
31
+ match = false;
32
+ }
33
+ }
34
+ }
35
+ if (match) {
36
+ let children = undefined;
37
+ if (r.routes) {
38
+ children = yield (0, exports.doMatchRoute)(r.routes, path.slice(rPath.length), parameters);
39
+ }
40
+ if (r.component) {
41
+ parameters["children"] = children;
42
+ return r.component(parameters);
43
+ }
44
+ else {
45
+ if (children) {
46
+ return children;
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ throw "Not Found";
53
+ });
54
+ exports.doMatchRoute = doMatchRoute;
55
+ const matchRoute = (routes, path) => __awaiter(void 0, void 0, void 0, function* () {
56
+ try {
57
+ return (0, exports.doMatchRoute)(routes, path.split("/").filter(e => e != ""), {});
58
+ }
59
+ catch (error) {
60
+ if (error == "Not Found") {
61
+ return undefined;
62
+ }
63
+ else {
64
+ throw error;
65
+ }
66
+ }
67
+ });
68
+ exports.matchRoute = matchRoute;
69
+ const SpinningRouter = ({ routes, errorPage = DefaultErrorPage_1.DefaultErrorPage, notFoundPage = DefaultNotFoundPage_1.DefaultNotFoundPage, loadingIndicator = DefaultLoadingIndicator_1.DefaultLoadingIndicator }) => {
70
+ const [hash, setHash] = (0, react_1.useState)(() => location.hash.substring(1));
71
+ const [element, setElement] = (0, react_1.useState)(undefined);
72
+ const [overlay, setOverlay] = (0, react_1.useState)(undefined);
73
+ const invokeRoute = (path) => __awaiter(void 0, void 0, void 0, function* () {
74
+ setOverlay(loadingIndicator);
75
+ try {
76
+ const element = yield (0, exports.matchRoute)(routes, path);
77
+ setElement(element);
78
+ if (element) {
79
+ setElement(element);
80
+ }
81
+ else {
82
+ setElement(notFoundPage);
83
+ }
84
+ }
85
+ catch (error) {
86
+ console.log(error);
87
+ setElement(errorPage({ error }));
88
+ }
89
+ finally {
90
+ setOverlay(undefined);
91
+ }
92
+ });
93
+ (0, react_1.useEffect)(() => {
94
+ invokeRoute(hash);
95
+ }, []);
96
+ (0, react_1.useEffect)(() => {
97
+ const updateHash = () => __awaiter(void 0, void 0, void 0, function* () {
98
+ setHash(location.hash.substring(1));
99
+ yield invokeRoute(location.hash.substring(1));
100
+ });
101
+ window.addEventListener("hashchange", updateHash);
102
+ window.addEventListener("softRefresh", updateHash);
103
+ return () => {
104
+ window.removeEventListener("hashchange", updateHash);
105
+ window.removeEventListener("softRefresh", updateHash);
106
+ };
107
+ }, []);
108
+ return ((0, jsx_runtime_1.jsxs)(Location_1.Location.Provider, Object.assign({ value: hash }, { children: [element, overlay] })));
109
+ };
110
+ exports.SpinningRouter = SpinningRouter;
@@ -0,0 +1,4 @@
1
+ import React from "react";
2
+ export declare const DefaultErrorPage: React.FC<{
3
+ error: any;
4
+ }>;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefaultErrorPage = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const DefaultErrorPage = ({ error }) => (0, jsx_runtime_1.jsxs)("p", { children: ["Unhandled error: ", "" + error] });
6
+ exports.DefaultErrorPage = DefaultErrorPage;
@@ -0,0 +1,2 @@
1
+ import React from "react";
2
+ export declare const DefaultLoadingIndicator: React.FC;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefaultLoadingIndicator = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const DefaultLoadingIndicator = () => ((0, jsx_runtime_1.jsx)("div", Object.assign({ style: {
6
+ position: "fixed",
7
+ top: "40%",
8
+ left: "50%",
9
+ transform: "translate(-50%, -50%)",
10
+ opacity: 1
11
+ } }, { children: (0, jsx_runtime_1.jsx)("svg", Object.assign({ width: "200", height: "200", viewBox: "0 0 50 50" }, { children: (0, jsx_runtime_1.jsxs)("path", Object.assign({ fill: "#000000", d: "M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z" }, { children: [(0, jsx_runtime_1.jsx)("animate", { attributeName: "opacity", values: "0; 0.1; 1.0", dur: "0.5s" }), (0, jsx_runtime_1.jsx)("animateTransform", { attributeType: "xml", attributeName: "transform", type: "rotate", from: "0 25 25", to: "360 25 25", dur: "1s", repeatCount: "indefinite" })] })) })) })));
12
+ exports.DefaultLoadingIndicator = DefaultLoadingIndicator;
@@ -0,0 +1,2 @@
1
+ import React from "react";
2
+ export declare const DefaultNotFoundPage: React.FC;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefaultNotFoundPage = void 0;
4
+ const jsx_runtime_1 = require("react/jsx-runtime");
5
+ const DefaultNotFoundPage = () => (0, jsx_runtime_1.jsx)("p", { children: "Not found" });
6
+ exports.DefaultNotFoundPage = DefaultNotFoundPage;
@@ -0,0 +1,5 @@
1
+ export { link } from "./link";
2
+ export { navigate } from "./navigate";
3
+ export { AsyncComponent, Route, Routes } from "./Routes";
4
+ export { softRefresh } from "./softRefresh";
5
+ export { SpinningRouter } from "./SpinningRouter";
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SpinningRouter = exports.softRefresh = exports.navigate = exports.link = void 0;
4
+ var link_1 = require("./link");
5
+ Object.defineProperty(exports, "link", { enumerable: true, get: function () { return link_1.link; } });
6
+ var navigate_1 = require("./navigate");
7
+ Object.defineProperty(exports, "navigate", { enumerable: true, get: function () { return navigate_1.navigate; } });
8
+ var softRefresh_1 = require("./softRefresh");
9
+ Object.defineProperty(exports, "softRefresh", { enumerable: true, get: function () { return softRefresh_1.softRefresh; } });
10
+ var SpinningRouter_1 = require("./SpinningRouter");
11
+ Object.defineProperty(exports, "SpinningRouter", { enumerable: true, get: function () { return SpinningRouter_1.SpinningRouter; } });
package/dist/link.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const link: (strings: TemplateStringsArray, ...values: any[]) => string;
package/dist/link.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.link = void 0;
4
+ const link = (strings, ...values) => {
5
+ const s = String.raw({ raw: strings }, ...values);
6
+ if (s.startsWith("~")) {
7
+ const id = document.location.hash.split("/")[1];
8
+ if (id) {
9
+ return `#/${id}${s.substring(1)}`;
10
+ }
11
+ else {
12
+ return `#${s.substring(1)}`;
13
+ }
14
+ }
15
+ if (s.startsWith("/")) {
16
+ return "#" + s;
17
+ }
18
+ else {
19
+ return "#/" + s;
20
+ }
21
+ };
22
+ exports.link = link;
@@ -0,0 +1 @@
1
+ export declare const navigate: (strings: TemplateStringsArray, ...values: any[]) => void;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.navigate = void 0;
4
+ const link_1 = require("./link");
5
+ const navigate = (strings, ...values) => {
6
+ window.location.hash = (0, link_1.link)(strings, ...values);
7
+ };
8
+ exports.navigate = navigate;
@@ -0,0 +1 @@
1
+ export declare const softRefresh: () => void;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.softRefresh = void 0;
4
+ const softRefresh = () => {
5
+ window.dispatchEvent(new Event("softRefresh"));
6
+ };
7
+ exports.softRefresh = softRefresh;
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "spinning-router",
3
+ "version": "0.0.1",
4
+ "description": "n/a",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "keywords": [],
8
+ "author": "",
9
+ "license": "UNLICENSED",
10
+ "files": [
11
+ "/dist"
12
+ ],
13
+ "peerDependencies": {
14
+ "react": "^18.2.0 || >19"
15
+ }
16
+ }