@fluxonjs/router 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.
@@ -0,0 +1,15 @@
1
+ type RouteConfig = {
2
+ path: string;
3
+ element: any;
4
+ };
5
+ export declare function Router(props: {
6
+ routes: RouteConfig[];
7
+ fallback?: any;
8
+ }): DocumentFragment;
9
+ export declare function Link(props: {
10
+ to: string;
11
+ children?: any;
12
+ class?: string;
13
+ }): import("@fluxonjs/core").JSX.Element;
14
+ export {};
15
+ //# sourceMappingURL=components.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../src/components.tsx"],"names":[],"mappings":"AAIA,KAAK,WAAW,GAAG;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,GAAG,CAAC;CACd,CAAC;AAEF,wBAAgB,MAAM,CAAC,KAAK,EAAE;IAAE,MAAM,EAAE,WAAW,EAAE,CAAC;IAAC,QAAQ,CAAC,EAAE,GAAG,CAAA;CAAE,oBA6CtE;AAED,wBAAgB,IAAI,CAAC,KAAK,EAAE;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,GAAG,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,wCAazE"}
@@ -0,0 +1,4 @@
1
+ export { location, params, navigate } from "./router";
2
+ export { Router, Link } from "./components";
3
+ export type { Location } from "./router";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAC5C,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,109 @@
1
+ // src/router.ts
2
+ import { signal } from "@fluxonjs/core";
3
+ var [location, setLocation] = signal({
4
+ pathname: window.location.pathname,
5
+ search: window.location.search,
6
+ hash: window.location.hash
7
+ });
8
+ window.addEventListener("popstate", () => {
9
+ setLocation({
10
+ pathname: window.location.pathname,
11
+ search: window.location.search,
12
+ hash: window.location.hash
13
+ });
14
+ });
15
+ function navigate(to, options = {}) {
16
+ if (options.replace) {
17
+ window.history.replaceState(null, "", to);
18
+ } else {
19
+ window.history.pushState(null, "", to);
20
+ }
21
+ setLocation({
22
+ pathname: window.location.pathname,
23
+ search: window.location.search,
24
+ hash: window.location.hash
25
+ });
26
+ }
27
+ var [params, setParams] = signal({});
28
+
29
+ // src/components.tsx
30
+ import { effect } from "@fluxonjs/core";
31
+
32
+ // src/match.ts
33
+ function matchPath(pattern, path) {
34
+ const patternParts = pattern.split("/").filter(Boolean);
35
+ const pathParts = path.split("/").filter(Boolean);
36
+ if (patternParts.length !== pathParts.length) return null;
37
+ const params2 = {};
38
+ for (let i = 0; i < patternParts.length; i++) {
39
+ const p = patternParts[i];
40
+ const u = pathParts[i];
41
+ if (p.startsWith(":")) {
42
+ params2[p.slice(1)] = decodeURIComponent(u);
43
+ } else if (p !== u) {
44
+ return null;
45
+ }
46
+ }
47
+ return params2;
48
+ }
49
+
50
+ // src/components.tsx
51
+ import { jsx } from "@fluxonjs/core/jsx-runtime";
52
+ function Router(props) {
53
+ const fragment = document.createDocumentFragment();
54
+ const startMarker = document.createComment("router-start");
55
+ const endMarker = document.createComment("router-end");
56
+ fragment.appendChild(startMarker);
57
+ fragment.appendChild(endMarker);
58
+ let currentNode = null;
59
+ effect(() => {
60
+ const currentPath = location().pathname;
61
+ const parent = startMarker.parentNode;
62
+ if (!parent) return;
63
+ if (currentNode && currentNode.parentNode) {
64
+ currentNode.parentNode.removeChild(currentNode);
65
+ currentNode = null;
66
+ }
67
+ let element = props.fallback ?? null;
68
+ for (const route of props.routes) {
69
+ const matchedParams = matchPath(route.path, currentPath);
70
+ if (matchedParams) {
71
+ setParams(matchedParams);
72
+ element = route.element;
73
+ break;
74
+ }
75
+ }
76
+ if (!element) {
77
+ setParams({});
78
+ return;
79
+ }
80
+ const node = typeof element === "function" ? element() : element;
81
+ if (node instanceof Node) {
82
+ currentNode = node;
83
+ parent.insertBefore(node, endMarker);
84
+ }
85
+ });
86
+ return fragment;
87
+ }
88
+ function Link(props) {
89
+ return /* @__PURE__ */ jsx(
90
+ "a",
91
+ {
92
+ href: props.to,
93
+ class: props.class,
94
+ onClick: (e) => {
95
+ e.preventDefault();
96
+ navigate(props.to);
97
+ },
98
+ children: props.children
99
+ }
100
+ );
101
+ }
102
+ export {
103
+ Link,
104
+ Router,
105
+ location,
106
+ navigate,
107
+ params
108
+ };
109
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/router.ts","../src/components.tsx","../src/match.ts"],"sourcesContent":["import { signal } from \"@fluxonjs/core\";\r\n\r\nexport type Location = {\r\n pathname: string;\r\n search: string;\r\n hash: string;\r\n};\r\n\r\ntype NavigateOptions = {\r\n replace?: boolean;\r\n};\r\n\r\nconst [location, setLocation] = signal<Location>({\r\n pathname: window.location.pathname,\r\n search: window.location.search,\r\n hash: window.location.hash,\r\n});\r\n\r\nwindow.addEventListener(\"popstate\", () => {\r\n setLocation({\r\n pathname: window.location.pathname,\r\n search: window.location.search,\r\n hash: window.location.hash,\r\n });\r\n});\r\n\r\nexport function navigate(to: string, options: NavigateOptions = {}) {\r\n if (options.replace) {\r\n window.history.replaceState(null, \"\", to);\r\n } else {\r\n window.history.pushState(null, \"\", to);\r\n }\r\n\r\n setLocation({\r\n pathname: window.location.pathname,\r\n search: window.location.search,\r\n hash: window.location.hash,\r\n });\r\n}\r\n\r\nconst [params, setParams] = signal<Record<string, string>>({});\r\n\r\nexport { location, params, setParams };","import { effect } from \"@fluxonjs/core\";\r\nimport { location, setParams, navigate } from \"./router\";\r\nimport { matchPath } from \"./match\";\r\n\r\ntype RouteConfig = {\r\n path: string;\r\n element: any;\r\n};\r\n\r\nexport function Router(props: { routes: RouteConfig[]; fallback?: any }) {\r\n const fragment = document.createDocumentFragment();\r\n const startMarker = document.createComment(\"router-start\");\r\n const endMarker = document.createComment(\"router-end\");\r\n\r\n fragment.appendChild(startMarker);\r\n fragment.appendChild(endMarker);\r\n\r\n let currentNode: Node | null = null;\r\n\r\n effect(() => {\r\n const currentPath = location().pathname;\r\n const parent = startMarker.parentNode;\r\n if (!parent) return;\r\n\r\n if (currentNode && currentNode.parentNode) {\r\n currentNode.parentNode.removeChild(currentNode);\r\n currentNode = null;\r\n }\r\n\r\n let element: any = props.fallback ?? null;\r\n\r\n for (const route of props.routes) {\r\n const matchedParams = matchPath(route.path, currentPath);\r\n if (matchedParams) {\r\n setParams(matchedParams);\r\n element = route.element;\r\n break;\r\n }\r\n }\r\n\r\n if (!element) {\r\n setParams({});\r\n return;\r\n }\r\n\r\n const node = typeof element === \"function\" ? element() : element;\r\n\r\n if (node instanceof Node) {\r\n currentNode = node;\r\n parent.insertBefore(node, endMarker);\r\n }\r\n });\r\n\r\n return fragment;\r\n}\r\n\r\nexport function Link(props: { to: string; children?: any; class?: string }) {\r\n return (\r\n <a\r\n href={props.to}\r\n class={props.class}\r\n onClick={(e: MouseEvent) => {\r\n e.preventDefault();\r\n navigate(props.to);\r\n }}\r\n >\r\n {props.children}\r\n </a>\r\n );\r\n}","export function matchPath(pattern: string, path: string): Record<string, string> | null {\r\n const patternParts = pattern.split(\"/\").filter(Boolean);\r\n const pathParts = path.split(\"/\").filter(Boolean);\r\n\r\n if (patternParts.length !== pathParts.length) return null;\r\n\r\n const params: Record<string, string> = {};\r\n\r\n for (let i = 0; i < patternParts.length; i++) {\r\n const p = patternParts[i];\r\n const u = pathParts[i];\r\n\r\n if (p.startsWith(\":\")) {\r\n params[p.slice(1)] = decodeURIComponent(u);\r\n } else if (p !== u) {\r\n return null;\r\n }\r\n }\r\n\r\n return params;\r\n}"],"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A;AACT;;;ADsCI;AAjDG,SAAS,OAAO,OAAkD;AACvE,QAAM,WAAW,SAAS,uBAAuB;AACjD,QAAM,cAAc,SAAS,cAAc,cAAc;AACzD,QAAM,YAAY,SAAS,cAAc,YAAY;AAErD,WAAS,YAAY,WAAW;AAChC,WAAS,YAAY,SAAS;AAE9B,MAAI,cAA2B;AAE/B,SAAO,MAAM;AACX,UAAM,cAAc,SAAS,EAAE;AAC/B,UAAM,SAAS,YAAY;AAC3B,QAAI,CAAC,OAAQ;AAEb,QAAI,eAAe,YAAY,YAAY;AACzC,kBAAY,WAAW,YAAY,WAAW;AAC9C,oBAAc;AAAA,IAChB;AAEA,QAAI,UAAe,MAAM,YAAY;AAErC,eAAW,SAAS,MAAM,QAAQ;AAChC,YAAM,gBAAgB,UAAU,MAAM,MAAM,WAAW;AACvD,UAAI,eAAe;AACjB,kBAAU,aAAa;AACvB,kBAAU,MAAM;AAChB;AAAA,MACF;AAAA,IACF;AAEA,QAAI,CAAC,SAAS;AACZ,gBAAU,CAAC,CAAC;AACZ;AAAA,IACF;AAEA,UAAM,OAAO,OAAO,YAAY,aAAa,QAAQ,IAAI;AAEzD,QAAI,gBAAgB,MAAM;AACxB,oBAAc;AACd,aAAO,aAAa,MAAM,SAAS;AAAA,IACrC;AAAA,EACF,CAAC;AAED,SAAO;AACT;AAEO,SAAS,KAAK,OAAuD;AAC1E,SACE;AAAA,IAAC;AAAA;AAAA,MACC,MAAM,MAAM;AAAA,MACZ,OAAO,MAAM;AAAA,MACb,SAAS,CAAC,MAAkB;AAC1B,UAAE,eAAe;AACjB,iBAAS,MAAM,EAAE;AAAA,MACnB;AAAA,MAEC,gBAAM;AAAA;AAAA,EACT;AAEJ;","names":["params"]}
@@ -0,0 +1,2 @@
1
+ export declare function matchPath(pattern: string, path: string): Record<string, string> | null;
2
+ //# sourceMappingURL=match.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"match.d.ts","sourceRoot":"","sources":["../src/match.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,CAoBtF"}
@@ -0,0 +1,13 @@
1
+ export type Location = {
2
+ pathname: string;
3
+ search: string;
4
+ hash: string;
5
+ };
6
+ type NavigateOptions = {
7
+ replace?: boolean;
8
+ };
9
+ declare const location: import("@fluxonjs/core").SignalGetter<Location>;
10
+ export declare function navigate(to: string, options?: NavigateOptions): void;
11
+ declare const params: import("@fluxonjs/core").SignalGetter<Record<string, string>>, setParams: (newValue: Record<string, string> | ((prev: Record<string, string>) => Record<string, string>)) => void;
12
+ export { location, params, setParams };
13
+ //# sourceMappingURL=router.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,QAAQ,GAAG;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,KAAK,eAAe,GAAG;IACrB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF,QAAA,MAAO,QAAQ,iDAIb,CAAC;AAUH,wBAAgB,QAAQ,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,GAAE,eAAoB,QAYjE;AAED,QAAA,MAAO,MAAM,iEAAE,SAAS,yGAAsC,CAAC;AAE/D,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@fluxonjs/router",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Official router for FluxonJS",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md"
18
+ ],
19
+ "peerDependencies": {
20
+ "@fluxonjs/core": "^0.1.7"
21
+ },
22
+ "devDependencies": {
23
+ "@fluxonjs/core": "0.1.7",
24
+ "tsup": "^8.5.1",
25
+ "typescript": "^5.0.0"
26
+ },
27
+ "license": "MIT",
28
+ "scripts": {
29
+ "build": "tsup && tsc --emitDeclarationOnly"
30
+ }
31
+ }