toiljs 0.0.116 → 0.0.118

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.
Files changed (337) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +16 -7
  3. package/build/backend/.tsbuildinfo +1 -1
  4. package/build/cli/.tsbuildinfo +1 -1
  5. package/build/cli/index.js +2375 -258
  6. package/build/client/.tsbuildinfo +1 -1
  7. package/build/client/auth.d.ts +3 -0
  8. package/build/client/auth.js +19 -10
  9. package/build/client/dev/devtools.js +1 -1
  10. package/build/client/dev/error-overlay.js +1 -1
  11. package/build/client/index.d.ts +1 -1
  12. package/build/client/index.js +1 -1
  13. package/build/client/navigation/navigation.js +28 -3
  14. package/build/client/routing/Router.js +5 -2
  15. package/build/client/routing/mount.js +1 -1
  16. package/build/compiler/.tsbuildinfo +1 -1
  17. package/build/compiler/auth-emails.js +7 -3
  18. package/build/compiler/emails.js +1 -1
  19. package/build/compiler/index.d.ts +1 -0
  20. package/build/compiler/index.js +26 -5
  21. package/build/compiler/pages.js +2 -3
  22. package/build/compiler/prerender.d.ts +3 -8
  23. package/build/compiler/prerender.js +88 -127
  24. package/build/compiler/ssg.js +3 -4
  25. package/build/compiler/template-build.js +3 -4
  26. package/build/compiler/toil-docs.generated.js +8 -7
  27. package/build/compiler/vite.js +4 -6
  28. package/build/devserver/.tsbuildinfo +1 -1
  29. package/build/devserver/db/database.js +7 -3
  30. package/build/devserver/db/types.js +1 -1
  31. package/build/devserver/production.js +4 -3
  32. package/build/devserver/runtime/crypto.d.ts +1 -1
  33. package/build/io/.tsbuildinfo +1 -1
  34. package/build/logger/.tsbuildinfo +1 -1
  35. package/build/shared/.tsbuildinfo +1 -1
  36. package/examples/basic/client/routes/analytics.tsx +7 -5
  37. package/examples/basic/client/routes/pq.tsx +2 -1
  38. package/examples/basic/server/tsconfig.json +1 -3
  39. package/package.json +45 -44
  40. package/presets/no-uint8array-tostring.js +120 -153
  41. package/presets/oxlint.d.ts +5 -0
  42. package/presets/oxlint.js +224 -0
  43. package/server/auth/AuthController.ts +53 -14
  44. package/server/auth/FactorSettings.ts +41 -0
  45. package/server/auth/SessionEpochs.ts +58 -0
  46. package/server/auth/SessionRevocations.ts +13 -0
  47. package/server/globals/auth.ts +27 -2
  48. package/server/runtime/tsconfig.json +1 -3
  49. package/tsconfig.json +2 -11
  50. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  51. package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
  52. package/.github/ISSUE_TEMPLATE/config.yml +0 -8
  53. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  54. package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
  55. package/.github/changelog-config.json +0 -45
  56. package/.github/dependabot.yml +0 -27
  57. package/.github/workflows/ci.yml +0 -222
  58. package/.vscode/settings.json +0 -10
  59. package/docs/README.md +0 -107
  60. package/docs/auth/README.md +0 -104
  61. package/docs/auth/configuration.md +0 -105
  62. package/docs/auth/emails.md +0 -92
  63. package/docs/auth/extending.md +0 -202
  64. package/docs/auth/how-it-works.md +0 -138
  65. package/docs/auth/usage.md +0 -188
  66. package/docs/backend/README.md +0 -143
  67. package/docs/backend/data.md +0 -351
  68. package/docs/backend/rest.md +0 -402
  69. package/docs/backend/rpc.md +0 -226
  70. package/docs/background/README.md +0 -114
  71. package/docs/background/daemons.md +0 -276
  72. package/docs/background/derive.md +0 -179
  73. package/docs/cli/README.md +0 -379
  74. package/docs/concepts/ai-guide.md +0 -132
  75. package/docs/concepts/config.md +0 -416
  76. package/docs/concepts/decorators.md +0 -127
  77. package/docs/concepts/security.md +0 -108
  78. package/docs/concepts/tiers.md +0 -166
  79. package/docs/concepts/types.md +0 -216
  80. package/docs/database/README.md +0 -143
  81. package/docs/database/capacity.md +0 -350
  82. package/docs/database/counters.md +0 -174
  83. package/docs/database/documents.md +0 -277
  84. package/docs/database/events.md +0 -307
  85. package/docs/database/membership.md +0 -246
  86. package/docs/database/setup.md +0 -155
  87. package/docs/database/unique.md +0 -216
  88. package/docs/database/views.md +0 -246
  89. package/docs/frontend/README.md +0 -104
  90. package/docs/frontend/components.md +0 -286
  91. package/docs/frontend/data-fetching.md +0 -260
  92. package/docs/frontend/images.md +0 -148
  93. package/docs/frontend/metadata.md +0 -344
  94. package/docs/frontend/navigation.md +0 -296
  95. package/docs/frontend/rendering.md +0 -236
  96. package/docs/frontend/routing.md +0 -346
  97. package/docs/frontend/scripts.md +0 -118
  98. package/docs/frontend/search.md +0 -191
  99. package/docs/frontend/styling.md +0 -147
  100. package/docs/frontend/toil-global.md +0 -216
  101. package/docs/getting-started/README.md +0 -81
  102. package/docs/getting-started/create-project.md +0 -131
  103. package/docs/getting-started/deploy.md +0 -101
  104. package/docs/getting-started/first-app.md +0 -215
  105. package/docs/getting-started/installation.md +0 -124
  106. package/docs/getting-started/migrating.md +0 -125
  107. package/docs/getting-started/project-structure.md +0 -163
  108. package/docs/introduction/README.md +0 -66
  109. package/docs/introduction/design-principles.md +0 -81
  110. package/docs/introduction/distributed.md +0 -91
  111. package/docs/introduction/how-it-works.md +0 -121
  112. package/docs/introduction/hyperscale.md +0 -70
  113. package/docs/introduction/modern-stack.md +0 -85
  114. package/docs/introduction/vs-other-frameworks.md +0 -64
  115. package/docs/introduction/why-toil.md +0 -99
  116. package/docs/llms.txt +0 -95
  117. package/docs/realtime/README.md +0 -102
  118. package/docs/realtime/channels.md +0 -211
  119. package/docs/realtime/streams.md +0 -369
  120. package/docs/services/README.md +0 -60
  121. package/docs/services/analytics.md +0 -279
  122. package/docs/services/caching.md +0 -175
  123. package/docs/services/cookies.md +0 -235
  124. package/docs/services/crypto.md +0 -209
  125. package/docs/services/email.md +0 -289
  126. package/docs/services/environment.md +0 -141
  127. package/docs/services/ratelimit.md +0 -174
  128. package/docs/services/time.md +0 -85
  129. package/eslint.config.js +0 -57
  130. package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
  131. package/presets/eslint.js +0 -88
  132. package/src/backend/index.ts +0 -204
  133. package/src/cli/configure.ts +0 -363
  134. package/src/cli/create.ts +0 -924
  135. package/src/cli/db.ts +0 -158
  136. package/src/cli/diagnostics.ts +0 -665
  137. package/src/cli/doctor.ts +0 -982
  138. package/src/cli/features.ts +0 -170
  139. package/src/cli/index.ts +0 -326
  140. package/src/cli/notify.ts +0 -120
  141. package/src/cli/proc.ts +0 -56
  142. package/src/cli/ui.ts +0 -169
  143. package/src/cli/update.ts +0 -248
  144. package/src/cli/updates.ts +0 -92
  145. package/src/cli/validate.ts +0 -31
  146. package/src/cli/version-check.ts +0 -125
  147. package/src/client/auth.ts +0 -869
  148. package/src/client/channel/channel.ts +0 -146
  149. package/src/client/components/Form.tsx +0 -65
  150. package/src/client/components/Image.tsx +0 -160
  151. package/src/client/components/Script.tsx +0 -113
  152. package/src/client/components/Slot.tsx +0 -21
  153. package/src/client/dev/devtools.tsx +0 -1089
  154. package/src/client/dev/error-overlay.tsx +0 -225
  155. package/src/client/errors.ts +0 -11
  156. package/src/client/head/head.ts +0 -182
  157. package/src/client/head/metadata.ts +0 -110
  158. package/src/client/index.ts +0 -130
  159. package/src/client/navigation/Link.tsx +0 -108
  160. package/src/client/navigation/NavLink.tsx +0 -86
  161. package/src/client/navigation/navigation.ts +0 -235
  162. package/src/client/navigation/prefetch.ts +0 -169
  163. package/src/client/navigation/scroll.ts +0 -53
  164. package/src/client/routing/Router.tsx +0 -216
  165. package/src/client/routing/action.ts +0 -122
  166. package/src/client/routing/error-boundary.tsx +0 -43
  167. package/src/client/routing/hooks.ts +0 -140
  168. package/src/client/routing/lazy.ts +0 -77
  169. package/src/client/routing/loader.ts +0 -381
  170. package/src/client/routing/match.ts +0 -47
  171. package/src/client/routing/mount.tsx +0 -90
  172. package/src/client/routing/params-context.ts +0 -10
  173. package/src/client/routing/slot-context.ts +0 -7
  174. package/src/client/rpc.ts +0 -97
  175. package/src/client/search/search.ts +0 -189
  176. package/src/client/search/use-page-search.ts +0 -73
  177. package/src/client/ssr/markers.tsx +0 -172
  178. package/src/client/stream/client.ts +0 -237
  179. package/src/client/types.ts +0 -73
  180. package/src/compiler/auth-emails.ts +0 -312
  181. package/src/compiler/config.ts +0 -369
  182. package/src/compiler/docs.ts +0 -127
  183. package/src/compiler/email-preview.ts +0 -314
  184. package/src/compiler/emails.ts +0 -404
  185. package/src/compiler/fonts.ts +0 -89
  186. package/src/compiler/generate.ts +0 -471
  187. package/src/compiler/image-blur.ts +0 -42
  188. package/src/compiler/image-report.ts +0 -88
  189. package/src/compiler/index.ts +0 -1362
  190. package/src/compiler/pages.ts +0 -49
  191. package/src/compiler/plugin.ts +0 -391
  192. package/src/compiler/prerender.ts +0 -263
  193. package/src/compiler/routes.ts +0 -170
  194. package/src/compiler/seo.ts +0 -415
  195. package/src/compiler/sri.ts +0 -144
  196. package/src/compiler/ssg.ts +0 -177
  197. package/src/compiler/ssr-codegen.ts +0 -85
  198. package/src/compiler/template-build.ts +0 -676
  199. package/src/compiler/template.ts +0 -262
  200. package/src/compiler/toil-docs.generated.ts +0 -77
  201. package/src/compiler/vite.ts +0 -245
  202. package/src/devserver/analytics/index.ts +0 -317
  203. package/src/devserver/config/dotenv.ts +0 -102
  204. package/src/devserver/config/env.ts +0 -30
  205. package/src/devserver/config/ratelimit.ts +0 -123
  206. package/src/devserver/daemon/catalog.ts +0 -120
  207. package/src/devserver/daemon/cron.ts +0 -87
  208. package/src/devserver/daemon/host.ts +0 -134
  209. package/src/devserver/daemon/index.ts +0 -369
  210. package/src/devserver/daemon/runtime.ts +0 -48
  211. package/src/devserver/db/catalog.ts +0 -109
  212. package/src/devserver/db/database.ts +0 -1834
  213. package/src/devserver/db/derives.ts +0 -121
  214. package/src/devserver/db/index.ts +0 -20
  215. package/src/devserver/db/routeKinds.ts +0 -191
  216. package/src/devserver/db/types.ts +0 -168
  217. package/src/devserver/email/caps.ts +0 -0
  218. package/src/devserver/email/config.ts +0 -141
  219. package/src/devserver/email/console.ts +0 -348
  220. package/src/devserver/email/index.ts +0 -155
  221. package/src/devserver/email/providers.ts +0 -131
  222. package/src/devserver/email/status.ts +0 -23
  223. package/src/devserver/email/validate.ts +0 -37
  224. package/src/devserver/email/wire.ts +0 -55
  225. package/src/devserver/http/cache.ts +0 -0
  226. package/src/devserver/http/envelope.ts +0 -157
  227. package/src/devserver/http/proxy.ts +0 -172
  228. package/src/devserver/http/runtime.ts +0 -387
  229. package/src/devserver/index.ts +0 -43
  230. package/src/devserver/production-ipc.ts +0 -63
  231. package/src/devserver/production-worker.ts +0 -83
  232. package/src/devserver/production.ts +0 -718
  233. package/src/devserver/runtime/crypto.ts +0 -587
  234. package/src/devserver/runtime/host.ts +0 -427
  235. package/src/devserver/runtime/module.ts +0 -442
  236. package/src/devserver/server.ts +0 -251
  237. package/src/devserver/ssr.ts +0 -208
  238. package/src/devserver/stream/catalog.ts +0 -106
  239. package/src/devserver/stream/host.ts +0 -42
  240. package/src/devserver/stream/index.ts +0 -301
  241. package/src/devserver/stream/manager.ts +0 -167
  242. package/src/devserver/stream/router.ts +0 -101
  243. package/src/devserver/stream/wire.ts +0 -58
  244. package/src/devserver/stream/ws.ts +0 -78
  245. package/src/devserver/wasm/sections.ts +0 -59
  246. package/src/devserver/wasm/surface.ts +0 -115
  247. package/src/io/FastMap.ts +0 -151
  248. package/src/io/FastSet.ts +0 -113
  249. package/src/io/codec.ts +0 -362
  250. package/src/io/index.ts +0 -10
  251. package/src/io/lengths.ts +0 -14
  252. package/src/io/types.ts +0 -19
  253. package/src/logger/index.ts +0 -22
  254. package/src/shared/index.ts +0 -46
  255. package/test/analytics-dev.test.ts +0 -113
  256. package/test/assembly/aspect-shim.ts +0 -33
  257. package/test/assembly/cookie.spec.ts +0 -303
  258. package/test/assembly/example.spec.ts +0 -22
  259. package/test/assembly/ssr.spec.ts +0 -95
  260. package/test/assembly.test.ts +0 -59
  261. package/test/auth-emails.test.ts +0 -118
  262. package/test/built-ssr.test.ts +0 -148
  263. package/test/channel.test.ts +0 -21
  264. package/test/config-host-port.test.ts +0 -59
  265. package/test/configure.test.ts +0 -94
  266. package/test/daemon-build.test.ts +0 -278
  267. package/test/daemon-catalog.test.ts +0 -349
  268. package/test/daemon-emulation.test.ts +0 -256
  269. package/test/db.test.ts +0 -0
  270. package/test/devserver-database.test.ts +0 -1102
  271. package/test/devserver-email.test.ts +0 -306
  272. package/test/devserver-pqauth.test.ts +0 -93
  273. package/test/devserver-secrets.test.ts +0 -63
  274. package/test/devserver.test.ts +0 -259
  275. package/test/doctor.test.ts +0 -298
  276. package/test/dom/Image.test.tsx +0 -144
  277. package/test/dom/Link.test.tsx +0 -67
  278. package/test/dom/NavLink.test.tsx +0 -37
  279. package/test/dom/Script.test.tsx +0 -48
  280. package/test/dom/action.test.tsx +0 -146
  281. package/test/dom/error-overlay.test.tsx +0 -44
  282. package/test/dom/loader.test.tsx +0 -121
  283. package/test/dom/navigation.test.ts +0 -59
  284. package/test/dom/revalidate.test.tsx +0 -38
  285. package/test/dom/route-head.test.tsx +0 -56
  286. package/test/dom/router-loading.test.tsx +0 -44
  287. package/test/dom/scroll.test.ts +0 -56
  288. package/test/dom/slot.test.tsx +0 -131
  289. package/test/dom/use-metadata.test.tsx +0 -58
  290. package/test/dom/view-transitions.test.tsx +0 -53
  291. package/test/dynamic-routing.test.ts +0 -132
  292. package/test/email-console.test.ts +0 -86
  293. package/test/email-preview.test.ts +0 -73
  294. package/test/emails.test.ts +0 -58
  295. package/test/errors.test.ts +0 -21
  296. package/test/example-guestbook.test.ts +0 -123
  297. package/test/features.test.ts +0 -149
  298. package/test/fixtures/bignum-wire/spec.ts +0 -25
  299. package/test/fixtures/daemon-app.ts +0 -65
  300. package/test/fixtures/stream-echo.ts +0 -26
  301. package/test/fixtures/stream-gate.ts +0 -28
  302. package/test/fixtures/stream-trap.ts +0 -18
  303. package/test/fixtures/stream-typed.ts +0 -41
  304. package/test/fonts.test.ts +0 -28
  305. package/test/global-setup.ts +0 -17
  306. package/test/head.test.ts +0 -42
  307. package/test/image-blur.test.ts +0 -50
  308. package/test/io.test.ts +0 -117
  309. package/test/metadata.test.ts +0 -40
  310. package/test/navlink.test.ts +0 -28
  311. package/test/pages.test.ts +0 -105
  312. package/test/placeholder.test.ts +0 -9
  313. package/test/pqauth-e2e.test.ts +0 -216
  314. package/test/pqauth-email.test.ts +0 -801
  315. package/test/prerender.test.ts +0 -116
  316. package/test/prettier-plugin.test.ts +0 -46
  317. package/test/routes.test.ts +0 -76
  318. package/test/rpc-bignum-wire.test.ts +0 -164
  319. package/test/rpc-dispatch.test.ts +0 -132
  320. package/test/rpc-kinds.test.ts +0 -18
  321. package/test/rpc.test.ts +0 -66
  322. package/test/search.test.ts +0 -114
  323. package/test/seo.test.ts +0 -175
  324. package/test/slot-layouts.test.ts +0 -69
  325. package/test/sri.test.ts +0 -129
  326. package/test/ssg.test.ts +0 -36
  327. package/test/ssr-hydration.test.tsx +0 -122
  328. package/test/ssr-render.test.ts +0 -197
  329. package/test/ssr-template.test.tsx +0 -482
  330. package/test/stream-emulation.test.ts +0 -479
  331. package/test/ui.test.ts +0 -40
  332. package/test/update.test.ts +0 -58
  333. package/test/validate.test.ts +0 -42
  334. package/test/version-check.test.ts +0 -130
  335. package/tests/data-parity/generated-parity.ts +0 -99
  336. package/tests/data-parity/parity.ts +0 -80
  337. package/tests/data-parity/spec.ts +0 -46
@@ -1,146 +0,0 @@
1
- /**
2
- * Client for the toil backend's WebSocket channel (served by the hyper-express/uWS backend at
3
- * `/_toil`). Supports text and binary (`ArrayBuffer`) frames, auto-reconnect, and a React hook.
4
- */
5
- import { useCallback, useEffect, useRef, useState } from 'react';
6
-
7
- /** A frame received from / sent to the channel. */
8
- export type ChannelData = string | ArrayBuffer;
9
-
10
- /** Whatever `WebSocket.send` accepts (string / BufferSource / Blob), per the DOM lib. */
11
- export type SendData = Parameters<WebSocket['send']>[0];
12
-
13
- export interface ChannelOptions {
14
- /** Channel path on the toil backend. Default `/_toil`. */
15
- readonly path?: string;
16
- /** Full `ws(s)://` URL override (takes precedence over `path`). */
17
- readonly url?: string;
18
- /** Auto-reconnect after an unexpected close. Default `true`. */
19
- readonly reconnect?: boolean;
20
- /** Reconnect delay in ms. Default `1000`. */
21
- readonly reconnectDelay?: number;
22
- }
23
-
24
- export interface Channel {
25
- /** Sends a text or binary frame (no-op until the socket is open). */
26
- send(data: SendData): void;
27
- /** Closes the channel and stops reconnecting. */
28
- close(): void;
29
- }
30
-
31
- /** Derives the channel's `ws(s)://` URL from the current page location. */
32
- export function resolveChannelUrl(
33
- path: string = '/_toil',
34
- location: { protocol: string; host: string } = window.location,
35
- ): string {
36
- const scheme = location.protocol === 'https:' ? 'wss:' : 'ws:';
37
- const normalized = path.startsWith('/') ? path : `/${path}`;
38
- return `${scheme}//${location.host}${normalized}`;
39
- }
40
-
41
- /**
42
- * Opens a channel to the backend, invoking `onMessage` for each frame. Reconnects on unexpected
43
- * close unless disabled. Returns a handle to `send()` and `close()`.
44
- */
45
- export function connectChannel(
46
- onMessage: (data: ChannelData) => void,
47
- options: ChannelOptions = {},
48
- ): Channel {
49
- const url = options.url ?? resolveChannelUrl(options.path);
50
- const reconnect = options.reconnect ?? true;
51
- const delay = options.reconnectDelay ?? 1000;
52
-
53
- let socket: WebSocket | null = null;
54
- let stopped = false;
55
- let timer: ReturnType<typeof setTimeout> | undefined;
56
-
57
- const open = (): void => {
58
- const ws = new WebSocket(url);
59
- ws.binaryType = 'arraybuffer';
60
- socket = ws;
61
- ws.addEventListener('message', (event: MessageEvent) => {
62
- if (typeof event.data === 'string') onMessage(event.data);
63
- else if (event.data instanceof ArrayBuffer) onMessage(event.data);
64
- });
65
- ws.addEventListener('close', () => {
66
- if (!stopped && reconnect) timer = setTimeout(open, delay);
67
- });
68
- };
69
- open();
70
-
71
- return {
72
- send: (data: SendData): void => {
73
- if (socket && socket.readyState === WebSocket.OPEN) socket.send(data);
74
- },
75
- close: (): void => {
76
- stopped = true;
77
- if (timer !== undefined) clearTimeout(timer);
78
- socket?.close();
79
- },
80
- };
81
- }
82
-
83
- export interface ChannelHook {
84
- /** Whether the socket is currently open. */
85
- readonly connected: boolean;
86
- /** Frames received so far, in order. */
87
- readonly messages: ChannelData[];
88
- /** Sends a text or binary frame. */
89
- send: (data: SendData) => void;
90
- }
91
-
92
- /**
93
- * React hook wrapping {@link connectChannel}: connects on mount, tracks `connected` state and the
94
- * received `messages`, and cleans up on unmount.
95
- */
96
- export function useChannel(options: ChannelOptions = {}): ChannelHook {
97
- const { path, url, reconnect, reconnectDelay } = options;
98
- const [connected, setConnected] = useState<boolean>(false);
99
- const [messages, setMessages] = useState<ChannelData[]>([]);
100
- const socketRef = useRef<WebSocket | null>(null);
101
-
102
- useEffect(() => {
103
- const target = url ?? resolveChannelUrl(path);
104
- const shouldReconnect = reconnect ?? true;
105
- const delay = reconnectDelay ?? 1000;
106
- let stopped = false;
107
- let timer: ReturnType<typeof setTimeout> | undefined;
108
-
109
- const open = (): void => {
110
- const ws = new WebSocket(target);
111
- ws.binaryType = 'arraybuffer';
112
- socketRef.current = ws;
113
- ws.addEventListener('open', () => {
114
- if (!stopped) setConnected(true);
115
- });
116
- ws.addEventListener('message', (event: MessageEvent) => {
117
- if (typeof event.data === 'string') {
118
- const data = event.data;
119
- setMessages((prev) => [...prev, data]);
120
- } else if (event.data instanceof ArrayBuffer) {
121
- const data = event.data;
122
- setMessages((prev) => [...prev, data]);
123
- }
124
- });
125
- ws.addEventListener('close', () => {
126
- if (stopped) return;
127
- setConnected(false);
128
- if (shouldReconnect) timer = setTimeout(open, delay);
129
- });
130
- };
131
- open();
132
-
133
- return () => {
134
- stopped = true;
135
- if (timer !== undefined) clearTimeout(timer);
136
- socketRef.current?.close();
137
- };
138
- }, [path, url, reconnect, reconnectDelay]);
139
-
140
- const send = useCallback((data: SendData): void => {
141
- const socket = socketRef.current;
142
- if (socket && socket.readyState === WebSocket.OPEN) socket.send(data);
143
- }, []);
144
-
145
- return { connected, messages, send };
146
- }
@@ -1,65 +0,0 @@
1
- import { type ReactNode, type SyntheticEvent, useRef } from 'react';
2
-
3
- import { type ActionState, type RevalidateTarget, useAction } from '../routing/action.js';
4
-
5
- /** Props for {@link Form}. */
6
- export interface FormProps {
7
- /** Handles the submission, receiving the form's `FormData`. May be async. */
8
- action: (data: FormData) => void | Promise<void>;
9
- /** Loader data to revalidate after a successful submit. Default `true` (the current route). */
10
- revalidate?: RevalidateTarget;
11
- /** Called after a successful submit. */
12
- onSuccess?: () => void;
13
- /** Called when the action throws. */
14
- onError?: (error: unknown) => void;
15
- /** Reset the form fields after a successful submit. Default `false`. */
16
- resetOnSuccess?: boolean;
17
- className?: string;
18
- /**
19
- * Form contents. Pass a render function to receive live submit state, e.g. to disable the
20
- * button while pending: `{({ pending }) => <button disabled={pending}>Save</button>}`.
21
- */
22
- children?: ReactNode | ((state: ActionState<void>) => ReactNode);
23
- }
24
-
25
- /**
26
- * A `<form>` that runs an {@link useAction} on submit (no page reload) and revalidates loader data
27
- * on success, the write half of the loader/action data loop. Tracks pending/error state, which a
28
- * render-function child can read.
29
- */
30
- export function Form({
31
- action,
32
- revalidate,
33
- onSuccess,
34
- onError,
35
- resetOnSuccess = false,
36
- className,
37
- children,
38
- }: FormProps): ReactNode {
39
- const formRef = useRef<HTMLFormElement | null>(null);
40
- const handle = useAction((data: FormData) => action(data), {
41
- revalidate,
42
- onError,
43
- onSuccess: () => {
44
- if (resetOnSuccess) formRef.current?.reset();
45
- onSuccess?.();
46
- },
47
- });
48
-
49
- const onSubmit = (event: SyntheticEvent<HTMLFormElement>): void => {
50
- event.preventDefault();
51
- formRef.current = event.currentTarget;
52
- void handle.run(new FormData(event.currentTarget));
53
- };
54
-
55
- return (
56
- <form
57
- ref={formRef}
58
- className={className}
59
- onSubmit={onSubmit}>
60
- {typeof children === 'function'
61
- ? children({ pending: handle.pending, error: handle.error, data: handle.data })
62
- : children}
63
- </form>
64
- );
65
- }
@@ -1,160 +0,0 @@
1
- import { type ComponentPropsWithRef, type CSSProperties, type ReactNode, useState } from 'react';
2
-
3
- /**
4
- * Props for {@link Image}: every standard `<img>` attribute, plus toil's layout/loading controls.
5
- * `src` and `alt` are required (`alt` is enforced for accessibility, pass `alt=""` for decorative
6
- * images). `width`/`height` (or `fill`) reserve space to prevent layout shift.
7
- */
8
- /** A toil image source carrying an auto-generated blur LQIP + intrinsic size, produced by importing an
9
- * image with the `?toil` flag (`import hero from './hero.webp?toil'`). Pass it straight to `Image`. */
10
- export interface ToilImageSource {
11
- src: string;
12
- width?: number;
13
- height?: number;
14
- blurDataURL?: string;
15
- }
16
-
17
- export interface ImageProps extends Omit<
18
- ComponentPropsWithRef<'img'>,
19
- 'loading' | 'placeholder' | 'width' | 'height' | 'src'
20
- > {
21
- /** The image URL, or a `?toil` import object that auto-fills the blur placeholder + aspect-ratio. */
22
- src: string | ToilImageSource;
23
- alt: string;
24
- /** Intrinsic width in px. Set together with `height` to reserve space (avoids layout shift). */
25
- width?: number;
26
- /** Intrinsic height in px. Set together with `width` to reserve space (avoids layout shift). */
27
- height?: number;
28
- /**
29
- * Make the image fill the width of a box toil wraps around it (a block-level `<span>`), scaling to
30
- * its natural height — or, if you size the box (`width`/`height`, or `style` like
31
- * `aspectRatio: '16/9'`), the image covers that box, cropped per `objectFit` (default `cover`).
32
- * The image flows in-block (never absolutely positioned), so it can't escape to fill the page or
33
- * collapse to nothing. `className`/`style` apply to the box, not the `<img>`.
34
- */
35
- fill?: boolean;
36
- /** `object-fit` for the rendered image (handy with `fill`). */
37
- objectFit?: CSSProperties['objectFit'];
38
- /**
39
- * Mark this as a high-priority (LCP) image: eager load + `fetchPriority="high"` and no lazy
40
- * loading. Use for above-the-fold hero images; everything else stays lazy. Default `false`.
41
- */
42
- priority?: boolean;
43
- /**
44
- * Placeholder shown until the image loads: `'empty'` (default) or `'blur'`. `'blur'` paints the
45
- * `blurDataURL` (auto-filled from a `?toil` import, or passed) blurred, or a neutral skeleton
46
- * shimmer if there's none. It needs a reserved size (`width`+`height`, or `fill`) to paint into —
47
- * which also prevents layout shift (the placeholder is cosmetic; the reserved size is what holds).
48
- */
49
- placeholder?: 'empty' | 'blur';
50
- /**
51
- * A tiny base64 image shown blurred while the real image loads (with `placeholder="blur"`).
52
- * Auto-generated when you import via `?toil` (`import hero from './hero.webp?toil'`); pass it
53
- * explicitly only for a string `src`. Omit it to fall back to a neutral skeleton shimmer.
54
- */
55
- blurDataURL?: string;
56
- }
57
-
58
- /**
59
- * A drop-in `<img>` replacement that prevents layout shift and lazy-loads by default. It reserves
60
- * space from `width`/`height` (or fills its container with `fill`), decodes async, lazy-loads unless
61
- * `priority`, and can fade in from a `blur` placeholder. This is a client-only component, there is
62
- * no server-side resizing; pass an already-optimized `src` (Vite hashes imported assets for you).
63
- */
64
- export function Image(props: ImageProps): ReactNode {
65
- const {
66
- src: srcProp,
67
- alt,
68
- width: widthProp,
69
- height: heightProp,
70
- fill = false,
71
- objectFit,
72
- priority = false,
73
- placeholder = 'empty',
74
- blurDataURL: blurDataURLProp,
75
- className: userClassName,
76
- style,
77
- onLoad,
78
- ...rest
79
- } = props;
80
-
81
- // `src` may be a plain URL or a `?toil` import object; unpack it and let explicit props win, so a
82
- // `?toil` import auto-fills the blur placeholder + aspect-ratio with no extra props.
83
- const source = typeof srcProp === 'string' ? null : srcProp;
84
- const src = typeof srcProp === 'string' ? srcProp : srcProp.src;
85
- const width = widthProp ?? source?.width;
86
- const height = heightProp ?? source?.height;
87
- const blurDataURL = blurDataURLProp ?? source?.blurDataURL;
88
-
89
- const [loaded, setLoaded] = useState(false);
90
- // The placeholder paints while the real image loads, then drops on load. With a `blurDataURL`
91
- // (auto-filled by a `?toil` import, or passed) it's that tiny image blurred; otherwise it's a
92
- // neutral skeleton shimmer — so `placeholder="blur"` is never a silent no-op. It only shows if
93
- // there's a reserved box to paint into (give `width`+`height`, or use `fill` in a sized parent).
94
- const showPlaceholder = placeholder === 'blur' && !loaded;
95
- const placeholderClass = !showPlaceholder
96
- ? undefined
97
- : blurDataURL !== undefined
98
- ? 'toil-img-blur'
99
- : 'toil-img-skeleton';
100
- // Reserve space so the image can't shift layout when it loads: an explicit aspect-ratio derived
101
- // from width+height survives responsive `width:100%` CSS (bare width/height attributes do not).
102
- const aspectRatio =
103
- width !== undefined && height !== undefined ? `${width} / ${height}` : undefined;
104
-
105
- // Layout + placeholder come from toil's shipped CSS classes (see `buildHtml`'s `<style id="toil-
106
- // base">`) not inline styles, so they're SSR-safe AND overridable. Only per-instance bits stay
107
- // inline: `objectFit`, the aspect-ratio, and the blur image URL. For a non-`fill` image the
108
- // caller's `className`/`style` ride on the <img>; for `fill` they go on the wrapper box below.
109
- const imgClass =
110
- [fill ? 'toil-img-fill' : userClassName, placeholderClass].filter(Boolean).join(' ') ||
111
- undefined;
112
- const imgStyle: CSSProperties = {
113
- ...(objectFit !== undefined ? { objectFit } : {}),
114
- ...(showPlaceholder && blurDataURL !== undefined
115
- ? { backgroundImage: `url(${blurDataURL})` }
116
- : {}),
117
- ...(fill || aspectRatio === undefined ? {} : { aspectRatio }),
118
- ...(fill ? {} : style),
119
- };
120
-
121
- const img = (
122
- <img
123
- {...rest}
124
- className={imgClass}
125
- src={src}
126
- alt={alt}
127
- width={fill ? undefined : width}
128
- height={fill ? undefined : height}
129
- loading={priority ? 'eager' : 'lazy'}
130
- decoding="async"
131
- fetchPriority={priority ? 'high' : 'auto'}
132
- onLoad={(event) => {
133
- setLoaded(true);
134
- onLoad?.(event);
135
- }}
136
- style={Object.keys(imgStyle).length > 0 ? imgStyle : undefined}
137
- />
138
- );
139
-
140
- if (!fill) return img;
141
-
142
- // `fill`: the image flows in-block at 100% of its box's width (scaling to natural height), or
143
- // covers the box if the caller sizes it (`width`/`height`, or `style` like `aspectRatio`). It is
144
- // NEVER absolutely positioned, so it can't escape to fill the page nor collapse to a zero-height
145
- // box. We still wrap it so the caller's `width`/`height`/`style` size the box, not the raw `<img>`.
146
- const boxStyle: CSSProperties = {
147
- ...(aspectRatio !== undefined ? { aspectRatio } : {}),
148
- ...(width !== undefined ? { width } : {}),
149
- ...(height !== undefined ? { height } : {}),
150
- ...style,
151
- };
152
- return (
153
- <span
154
- className={[userClassName, 'toil-img-fill-box'].filter(Boolean).join(' ') || undefined}
155
- style={Object.keys(boxStyle).length > 0 ? boxStyle : undefined}
156
- >
157
- {img}
158
- </span>
159
- );
160
- }
@@ -1,113 +0,0 @@
1
- import { type ReactNode, useEffect } from 'react';
2
-
3
- /**
4
- * When a {@link Script} is injected, relative to the app becoming interactive:
5
- * - `afterInteractive` (default), on mount, once the app is running. Good for analytics, widgets.
6
- * - `lazyOnload`, deferred until the browser is idle (after `window.load`). For low-priority scripts.
7
- * - `beforeInteractive`, as early as possible. In a client-only SPA there is no SSR, so this still
8
- * runs after hydration, but synchronously on first mount with high fetch priority.
9
- */
10
- export type ScriptStrategy = 'beforeInteractive' | 'afterInteractive' | 'lazyOnload';
11
-
12
- /** Props for {@link Script}. Provide either `src` (external) or inline `children` (script body). */
13
- export interface ScriptProps {
14
- /** URL of an external script. Omit when providing an inline script body via `children`. */
15
- src?: string;
16
- /** When to load the script. Default `'afterInteractive'`. */
17
- strategy?: ScriptStrategy;
18
- /** Stable identity for dedup (required for inline scripts; defaults to `src` for external ones). */
19
- id?: string;
20
- /** `type` attribute (e.g. `'module'`, `'application/json'`). */
21
- type?: string;
22
- /** Fired once the script has loaded (external) or been inserted (inline). */
23
- onLoad?: () => void;
24
- /** Fired after load, and on every later mount once the script is already loaded. */
25
- onReady?: () => void;
26
- /** Fired if an external script fails to load. */
27
- onError?: (error: unknown) => void;
28
- /** Inline script body. Mutually exclusive with `src`. */
29
- children?: string;
30
- }
31
-
32
- type LoadState = 'loading' | 'ready';
33
- /** Module-level registry so a given script is injected/executed at most once across the app. */
34
- const registry = new Map<string, LoadState>();
35
-
36
- function inject(props: ScriptProps, key: string): void {
37
- const { src, type, onLoad, onReady, onError, children } = props;
38
- const el = document.createElement('script');
39
- el.dataset.toilScript = key;
40
- if (type !== undefined) el.type = type;
41
-
42
- if (src !== undefined) {
43
- el.src = src;
44
- el.async = true;
45
- el.addEventListener('load', () => {
46
- registry.set(key, 'ready');
47
- onLoad?.();
48
- onReady?.();
49
- });
50
- el.addEventListener('error', (event) => {
51
- registry.delete(key); // allow a later remount to retry
52
- onError?.(event);
53
- });
54
- document.head.appendChild(el);
55
- } else {
56
- el.textContent = children ?? '';
57
- document.head.appendChild(el);
58
- registry.set(key, 'ready');
59
- onLoad?.();
60
- onReady?.();
61
- }
62
- }
63
-
64
- /**
65
- * Loads an external or inline `<script>` with a load `strategy`, deduplicated across the app so the
66
- * same script never executes twice. Renders nothing. Mirrors the ergonomics of Next.js `next/script`
67
- * for a client-only SPA.
68
- */
69
- export function Script(props: ScriptProps): ReactNode {
70
- const { src, id, strategy = 'afterInteractive', onReady } = props;
71
- const key = id ?? src;
72
-
73
- useEffect(() => {
74
- if (key === undefined) {
75
- // No id and no src: nothing to dedup or load (an inline script needs at least an id).
76
- return;
77
- }
78
-
79
- const state = registry.get(key);
80
- if (state === 'ready') {
81
- onReady?.();
82
- return;
83
- }
84
- if (state === 'loading') {
85
- return; // another instance is already injecting it
86
- }
87
-
88
- registry.set(key, 'loading');
89
- const run = (): void => {
90
- inject(props, key);
91
- };
92
-
93
- if (strategy === 'lazyOnload') {
94
- if (document.readyState === 'complete') {
95
- const idle = window.requestIdleCallback?.bind(window);
96
- if (idle) idle(run);
97
- else setTimeout(run, 0);
98
- } else {
99
- window.addEventListener('load', run, { once: true });
100
- }
101
- return () => {
102
- window.removeEventListener('load', run);
103
- };
104
- }
105
-
106
- // beforeInteractive + afterInteractive: inject now (on mount).
107
- run();
108
- // Intentionally keyed on identity only: inject once per script key; later prop changes
109
- // (handlers, body) are read at inject time and must not re-run/re-inject the script.
110
- }, [key, strategy]);
111
-
112
- return null;
113
- }
@@ -1,21 +0,0 @@
1
- import { type ReactNode, useContext } from 'react';
2
-
3
- import { SlotContext } from '../routing/slot-context.js';
4
-
5
- /** Props for {@link Slot}. */
6
- export interface SlotProps {
7
- /** The parallel-slot name, the `@name` directory under `routes/` (without the `@`). */
8
- name: string;
9
- /** Rendered when the slot has no match for the current URL. Default `null`. */
10
- fallback?: ReactNode;
11
- }
12
-
13
- /**
14
- * Renders the parallel-route slot named `name` for the current URL. Place it in a layout or page to
15
- * show an `@name` route tree alongside the main content (e.g. a persistent sidebar, or a modal that
16
- * an intercepting route fills). Renders `fallback` (default nothing) when no slot route matches.
17
- */
18
- export function Slot({ name, fallback = null }: SlotProps): ReactNode {
19
- const slots = useContext(SlotContext);
20
- return slots[name] ?? fallback;
21
- }