@kiln-js/client 0.0.0-stage → 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/client.d.ts +11 -0
- package/dist/entry.d.ts +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +150 -0
- package/dist/router.d.ts +12 -0
- package/dist/routes.d.ts +2 -0
- package/dist/types.d.ts +23 -0
- package/package.json +31 -5
- package/README.md +0 -3
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type AnchorHTMLAttributes } from "react";
|
|
2
|
+
import type { ClientRoute } from "./types";
|
|
3
|
+
export declare function navigate(to: string, push?: boolean): Promise<void>;
|
|
4
|
+
export declare function Link(props: AnchorHTMLAttributes<HTMLAnchorElement> & {
|
|
5
|
+
href: string;
|
|
6
|
+
}): import("react").JSX.Element;
|
|
7
|
+
export declare function createClient(options?: {
|
|
8
|
+
routes?: ClientRoute[];
|
|
9
|
+
}): Promise<{
|
|
10
|
+
start(): Promise<void>;
|
|
11
|
+
}>;
|
package/dist/entry.d.ts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// src/client.tsx
|
|
2
|
+
import { hydrateRoot, createRoot } from "react-dom/client";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
|
|
5
|
+
// ../core/dist/index.js
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
import { createContext, useContext } from "react";
|
|
8
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
9
|
+
import { createContext as createContext2, useContext as useContext2, useEffect, useLayoutEffect } from "react";
|
|
10
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
11
|
+
function matchRoute(routes, pathname) {
|
|
12
|
+
const parts = pathname.split("/").filter(Boolean);
|
|
13
|
+
for (const route of routes) {
|
|
14
|
+
const segs = route.pattern.split("/").filter(Boolean);
|
|
15
|
+
const params = {};
|
|
16
|
+
let ok = true;
|
|
17
|
+
let i = 0;
|
|
18
|
+
for (const seg of segs) {
|
|
19
|
+
if (seg.startsWith("*")) {
|
|
20
|
+
params[seg.slice(1)] = parts.slice(i).map(decodeURIComponent).join("/");
|
|
21
|
+
i = parts.length;
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
if (i >= parts.length) {
|
|
25
|
+
ok = false;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
if (seg.startsWith(":"))
|
|
29
|
+
params[seg.slice(1)] = decodeURIComponent(parts[i]);
|
|
30
|
+
else if (seg !== parts[i]) {
|
|
31
|
+
ok = false;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
i++;
|
|
35
|
+
}
|
|
36
|
+
if (ok && i === parts.length)
|
|
37
|
+
return { route, params };
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
function compose(page, layouts) {
|
|
42
|
+
let tree = page;
|
|
43
|
+
for (const Layout of [...layouts].reverse())
|
|
44
|
+
tree = /* @__PURE__ */ jsx(Layout, {
|
|
45
|
+
children: tree
|
|
46
|
+
});
|
|
47
|
+
return tree;
|
|
48
|
+
}
|
|
49
|
+
var RouteContext = createContext({ pathname: "/", params: {} });
|
|
50
|
+
function RouteProvider({ value, children }) {
|
|
51
|
+
return /* @__PURE__ */ jsx2(RouteContext.Provider, {
|
|
52
|
+
value,
|
|
53
|
+
children
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
var usePathname = () => useContext(RouteContext).pathname;
|
|
57
|
+
var useParams = () => useContext(RouteContext).params;
|
|
58
|
+
var HeadContext = createContext2(null);
|
|
59
|
+
|
|
60
|
+
// src/routes.ts
|
|
61
|
+
function getInjectedRoutes() {
|
|
62
|
+
const routes = globalThis.__KILN_ROUTES__;
|
|
63
|
+
if (!routes) {
|
|
64
|
+
throw new Error("No routes found. Pass createClient({ routes }) or run through the Kiln plugin.");
|
|
65
|
+
}
|
|
66
|
+
return routes;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/client.tsx
|
|
70
|
+
import { jsx as jsx4 } from "react/jsx-runtime";
|
|
71
|
+
var routes = [];
|
|
72
|
+
var setCurrent = () => {};
|
|
73
|
+
async function resolve(url, data) {
|
|
74
|
+
const match = matchRoute(routes, url.pathname);
|
|
75
|
+
if (!match?.route.load)
|
|
76
|
+
throw new Error(`No client route for ${url.pathname}`);
|
|
77
|
+
const [mod, ...layouts] = await Promise.all([
|
|
78
|
+
match.route.load(),
|
|
79
|
+
...match.route.layouts.map((l) => l())
|
|
80
|
+
]);
|
|
81
|
+
return {
|
|
82
|
+
Page: mod.default,
|
|
83
|
+
layouts: layouts.map((m) => m.default),
|
|
84
|
+
params: match.params,
|
|
85
|
+
pathname: url.pathname,
|
|
86
|
+
data
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function App({ initial }) {
|
|
90
|
+
const [current, set] = useState(initial);
|
|
91
|
+
setCurrent = set;
|
|
92
|
+
const { Page, layouts, params, pathname, data } = current;
|
|
93
|
+
return /* @__PURE__ */ jsx4(RouteProvider, {
|
|
94
|
+
value: { pathname, params },
|
|
95
|
+
children: compose(/* @__PURE__ */ jsx4(Page, {
|
|
96
|
+
params,
|
|
97
|
+
data
|
|
98
|
+
}), layouts)
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
async function navigate(to, push = true) {
|
|
102
|
+
const url = new URL(to, location.href);
|
|
103
|
+
const match = matchRoute(routes, url.pathname);
|
|
104
|
+
if (!match || match.route.mode === "server") {
|
|
105
|
+
location.href = to;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const data = match.route.mode === "client" ? null : await fetch(`/__kiln/data?path=${encodeURIComponent(url.pathname)}`).then((r) => r.json());
|
|
109
|
+
setCurrent(await resolve(url, data));
|
|
110
|
+
if (push)
|
|
111
|
+
history.pushState({}, "", to);
|
|
112
|
+
window.scrollTo(0, 0);
|
|
113
|
+
}
|
|
114
|
+
function Link(props) {
|
|
115
|
+
return /* @__PURE__ */ jsx4("a", {
|
|
116
|
+
...props,
|
|
117
|
+
onClick: (e) => {
|
|
118
|
+
if (e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0)
|
|
119
|
+
return;
|
|
120
|
+
e.preventDefault();
|
|
121
|
+
navigate(props.href);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
async function createClient(options = {}) {
|
|
126
|
+
routes = options.routes ?? getInjectedRoutes();
|
|
127
|
+
return {
|
|
128
|
+
async start() {
|
|
129
|
+
window.addEventListener("popstate", () => navigate(location.pathname + location.search, false));
|
|
130
|
+
const boot = window.__KILN__ ?? { mode: "universal", pattern: "", data: null };
|
|
131
|
+
const container = document.getElementById("root");
|
|
132
|
+
const initial = await resolve(new URL(location.href), boot.data);
|
|
133
|
+
if (boot.mode === "client")
|
|
134
|
+
createRoot(container).render(/* @__PURE__ */ jsx4(App, {
|
|
135
|
+
initial
|
|
136
|
+
}));
|
|
137
|
+
else
|
|
138
|
+
hydrateRoot(container, /* @__PURE__ */ jsx4(App, {
|
|
139
|
+
initial
|
|
140
|
+
}));
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
export {
|
|
145
|
+
Link,
|
|
146
|
+
createClient,
|
|
147
|
+
navigate,
|
|
148
|
+
useParams,
|
|
149
|
+
usePathname
|
|
150
|
+
};
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
interface RouteState {
|
|
3
|
+
pathname: string;
|
|
4
|
+
params: Record<string, string>;
|
|
5
|
+
}
|
|
6
|
+
export declare function RouteProvider({ value, children }: {
|
|
7
|
+
value: RouteState;
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
}): import("react").JSX.Element;
|
|
10
|
+
export declare const usePathname: () => string;
|
|
11
|
+
export declare const useParams: () => Record<string, string>;
|
|
12
|
+
export {};
|
package/dist/routes.d.ts
ADDED
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ComponentType } from "react";
|
|
2
|
+
export type RenderMode = "universal" | "server" | "client";
|
|
3
|
+
export interface ClientRoute {
|
|
4
|
+
pattern: string;
|
|
5
|
+
mode: RenderMode;
|
|
6
|
+
load: (() => Promise<{
|
|
7
|
+
default: ComponentType<any>;
|
|
8
|
+
loader?: (...args: any[]) => unknown;
|
|
9
|
+
}>) | null;
|
|
10
|
+
layouts: (() => Promise<{
|
|
11
|
+
default: ComponentType<any>;
|
|
12
|
+
}>)[];
|
|
13
|
+
}
|
|
14
|
+
export interface KilnBoot {
|
|
15
|
+
mode: RenderMode;
|
|
16
|
+
pattern: string;
|
|
17
|
+
data: unknown;
|
|
18
|
+
}
|
|
19
|
+
declare global {
|
|
20
|
+
interface Window {
|
|
21
|
+
__KILN__?: KilnBoot;
|
|
22
|
+
}
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
"
|
|
5
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@kiln-js/client",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"import": "./dist/index.js"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "bun build ./src/index.ts --outdir ./dist --target browser --format esm --external react --external react-dom \u0026\u0026 tsc -p tsconfig.json --emitDeclarationOnly",
|
|
17
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@kiln/core": "*"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": "\u003e=18",
|
|
24
|
+
"react-dom": "\u003e=18"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/react": "^18.3.0",
|
|
28
|
+
"@types/react-dom": "^18.3.0",
|
|
29
|
+
"react": "^18.3.1",
|
|
30
|
+
"react-dom": "^18.3.1"
|
|
31
|
+
}
|
|
6
32
|
}
|
package/README.md
DELETED