@hamur/adapter-preact 0.0.0-stage → 0.0.2

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 (68) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +35 -2
  3. package/dist/chunks/browser-index-89d71ade.js +118 -0
  4. package/dist/chunks/browser-index-ezte15pp.js +67 -0
  5. package/dist/chunks/browser-index-hsy7tnbg.js +21 -0
  6. package/dist/chunks/browser-index-k3m2axbw.js +274 -0
  7. package/dist/src/adapter.js +456 -0
  8. package/dist/src/error-page.js +391 -0
  9. package/dist/src/index.js +1506 -0
  10. package/dist/src/keepalive.js +6 -0
  11. package/dist/src/runtime/client.js +96 -0
  12. package/dist/src/runtime/island.js +33 -0
  13. package/dist/src/runtime/server.js +95 -0
  14. package/dist/types/src/activation.d.ts +15 -0
  15. package/dist/types/src/adapter.d.ts +2 -0
  16. package/dist/types/src/async-data.d.ts +13 -0
  17. package/dist/types/src/client-component.d.ts +11 -0
  18. package/dist/types/src/client-fallback.d.ts +2 -0
  19. package/dist/types/src/context.d.ts +81 -0
  20. package/dist/types/src/error-page.d.ts +1 -0
  21. package/dist/types/src/head.d.ts +5 -0
  22. package/dist/types/src/index.d.ts +429 -0
  23. package/dist/types/src/island-html.d.ts +13 -0
  24. package/dist/types/src/island.d.ts +19 -0
  25. package/dist/types/src/jsx-locator.d.ts +6 -0
  26. package/dist/types/src/keepalive.d.ts +26 -0
  27. package/dist/types/src/lazy-hydration.d.ts +12 -0
  28. package/dist/types/src/markdown-page.d.ts +9 -0
  29. package/dist/types/src/markdown.d.ts +12 -0
  30. package/dist/types/src/plugin.d.ts +36 -0
  31. package/dist/types/src/route-boundary.d.ts +41 -0
  32. package/dist/types/src/runtime/client.d.ts +10 -0
  33. package/dist/types/src/runtime/hmr.d.ts +31 -0
  34. package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
  35. package/dist/types/src/runtime/hydration.d.ts +8 -0
  36. package/dist/types/src/runtime/island.d.ts +8 -0
  37. package/dist/types/src/runtime/server.d.ts +11 -0
  38. package/dist/types/src/server-island.d.ts +10 -0
  39. package/dist/types/src/svg.d.ts +10 -0
  40. package/package.json +71 -4
  41. package/src/activation.ts +51 -0
  42. package/src/adapter.ts +35 -0
  43. package/src/async-data.ts +65 -0
  44. package/src/client-component.ts +23 -0
  45. package/src/client-fallback.ts +2 -0
  46. package/src/context.ts +372 -0
  47. package/src/env.d.ts +3 -0
  48. package/src/error-page.ts +1 -0
  49. package/src/head.ts +34 -0
  50. package/src/index.ts +1868 -0
  51. package/src/island-html.ts +58 -0
  52. package/src/island.ts +56 -0
  53. package/src/jsx-locator.ts +199 -0
  54. package/src/keepalive.ts +100 -0
  55. package/src/lazy-hydration.ts +101 -0
  56. package/src/markdown-page.ts +18 -0
  57. package/src/markdown.ts +58 -0
  58. package/src/plugin.ts +231 -0
  59. package/src/route-boundary.ts +51 -0
  60. package/src/runtime/client.ts +85 -0
  61. package/src/runtime/hmr.ts +141 -0
  62. package/src/runtime/hydration-dev.ts +8 -0
  63. package/src/runtime/hydration.ts +7 -0
  64. package/src/runtime/island.ts +37 -0
  65. package/src/runtime/server.ts +102 -0
  66. package/src/server-island.ts +35 -0
  67. package/src/svg.ts +27 -0
  68. package/src/webmcp.d.ts +18 -0
@@ -0,0 +1,1506 @@
1
+ import {
2
+ shown,
3
+ later,
4
+ RouteContext,
5
+ RouterContext,
6
+ NeverHydratedContext,
7
+ SearchContext,
8
+ BaseURLContext,
9
+ HrefContext,
10
+ DataContext,
11
+ EntryContext,
12
+ ErrorContext,
13
+ HeadContext,
14
+ StateContext,
15
+ OutletContext,
16
+ SlotsContext,
17
+ OutletValueContext,
18
+ LayoutContext,
19
+ ActivationContext,
20
+ SubmittedContext,
21
+ FormStatusContext,
22
+ AppContextContext,
23
+ NavigationContext,
24
+ LinkHrefContext
25
+ } from "../chunks/browser-index-k3m2axbw.js";
26
+ import {
27
+ ClientOnlyContext,
28
+ clientFallbackIds
29
+ } from "../chunks/browser-index-hsy7tnbg.js";
30
+ import {
31
+ defineLazyHydrationComponent2,
32
+ settledIsland
33
+ } from "../chunks/browser-index-89d71ade.js";
34
+
35
+ // packages/adapter-preact/src/index.ts
36
+ import { Component, Fragment as Fragment2, createElement as createElement2 } from "preact";
37
+ import { reportWebVitals, syncedState } from "hamur/app";
38
+ import { Suspense } from "preact/compat";
39
+ import {
40
+ asyncDataArguments,
41
+ asyncDataKey,
42
+ stateArguments,
43
+ fetchKey,
44
+ timeAttributes,
45
+ timeView,
46
+ watchTime,
47
+ createLoadingIndicator,
48
+ loadingIndicatorOptions,
49
+ paintLoadingIndicator,
50
+ createRouteAnnouncer,
51
+ createAnnouncer,
52
+ announcerActions,
53
+ announcerRole,
54
+ hideAnnouncer,
55
+ ROUTE_ANNOUNCER_ID,
56
+ ANNOUNCER_POLITE_ID,
57
+ ANNOUNCER_ASSERTIVE_ID,
58
+ isHandlerReference,
59
+ isPending,
60
+ resolveCallOptions,
61
+ settledOf
62
+ } from "hamur/adapter";
63
+ import {
64
+ useCallback,
65
+ useContext as useContext3,
66
+ useEffect,
67
+ useId as useId3,
68
+ useLayoutEffect,
69
+ useMemo,
70
+ useRef,
71
+ useState as useLocalState
72
+ } from "preact/hooks";
73
+ import {
74
+ clearAsyncData,
75
+ createForm,
76
+ enhance,
77
+ navigateTo,
78
+ redirect,
79
+ useCookie as cookieOf,
80
+ withoutBase
81
+ } from "hamur/app";
82
+ import {
83
+ callOnce as callOnceOf,
84
+ clearState as clearStateOf,
85
+ setResponseStatus as setResponseStatusOf,
86
+ useResponseHeader as responseHeaderOf,
87
+ useRequestEvent as requestEventOf,
88
+ useRequestFetch as requestFetchOf,
89
+ useRequestHeader as requestHeaderOf,
90
+ useRequestHeaders as requestHeadersOf,
91
+ useRequestURL as requestURLOf,
92
+ usePreviewMode as previewModeOf
93
+ } from "hamur/app";
94
+ import { whenAppReady } from "hamur/adapter";
95
+ import {
96
+ layoutSegments,
97
+ IDLE_FORM_STATUS,
98
+ assertFormMethod,
99
+ createActionRunner,
100
+ createFormStatus,
101
+ functionFormAction,
102
+ linkActionRunner,
103
+ restoredResult,
104
+ runFunctionForm,
105
+ publicError,
106
+ runtimeConfigOf,
107
+ siteConfigOf,
108
+ sanitizeHead,
109
+ seoMetaToHead,
110
+ createLinkSlot,
111
+ linkAttributes,
112
+ linkExtraAttributes,
113
+ linkState,
114
+ warnRemovedLinkProps,
115
+ relativeTarget,
116
+ resolveLinkTarget,
117
+ resolveRelative,
118
+ prefetchAttribute,
119
+ viewTransitionAttribute,
120
+ checkTransitionName,
121
+ optimisticSource,
122
+ navigationHook,
123
+ navigationBlocker,
124
+ routeGuard,
125
+ createBlocker,
126
+ registerSnapshot,
127
+ setLinkState,
128
+ linkPending,
129
+ LINK_DEFAULTS,
130
+ linkDefaultsOf,
131
+ linkPrefetchProps
132
+ } from "hamur/adapter";
133
+ import { diagnosticURL, noteOutlet, prehydrate } from "hamur/adapter";
134
+ import { $fetch } from "hamur/app";
135
+ import { clientFallback, markWaiting } from "hamur/adapter";
136
+
137
+ // packages/adapter-preact/src/head.ts
138
+ var mounted = new WeakMap;
139
+ var ranks = 0;
140
+ var nextRank = () => ++ranks;
141
+ function mountHead(head, rank, input) {
142
+ const live = mounted.get(head) ?? new Set;
143
+ mounted.set(head, live);
144
+ const later = [...live].filter((other) => other.rank > rank).sort((a, b) => a.rank - b.rank);
145
+ for (const other of later)
146
+ other.entry.dispose();
147
+ const self = { rank, input, entry: head.push(input) };
148
+ for (const other of later)
149
+ other.entry = head.push(other.input);
150
+ live.add(self);
151
+ return {
152
+ patch(next) {
153
+ self.input = next;
154
+ self.entry.patch(next);
155
+ },
156
+ dispose() {
157
+ self.entry.dispose();
158
+ live.delete(self);
159
+ }
160
+ };
161
+ }
162
+
163
+ // packages/adapter-preact/src/async-data.ts
164
+ import { isPreviewMode } from "hamur/adapter";
165
+ var valueOf = (value) => typeof value === "function" ? value() : value;
166
+ function requestOf({ query, body, ...rest }) {
167
+ const request = rest;
168
+ const resolvedQuery = valueOf(query);
169
+ const resolvedBody = valueOf(body);
170
+ if (resolvedQuery !== undefined)
171
+ request.query = resolvedQuery;
172
+ if (resolvedBody !== undefined)
173
+ request.body = resolvedBody;
174
+ return request;
175
+ }
176
+ function defaultGetCachedData(key, app) {
177
+ if (!app.asyncData.hydrating || isPreviewMode(app))
178
+ return;
179
+ const entry = app.asyncData.get(key);
180
+ return entry?.status === "success" ? entry.data : undefined;
181
+ }
182
+ function refreshCached(app, key, handler, options, getCachedData, cause) {
183
+ const cached = getCachedData(key, app, { cause });
184
+ if (cached !== undefined) {
185
+ app.asyncData.fetch(key, handler, { ...options, cached });
186
+ return Promise.resolve();
187
+ }
188
+ if (!app.asyncData.has(key))
189
+ app.asyncData.fetch(key, handler, { ...options, immediate: false });
190
+ return app.asyncData.refresh(key, handler);
191
+ }
192
+ var kept = new WeakMap;
193
+ function keepCached(app, key, getCachedData) {
194
+ if (getCachedData === defaultGetCachedData)
195
+ return;
196
+ let keys = kept.get(app.asyncData);
197
+ if (!keys)
198
+ kept.set(app.asyncData, keys = new Set);
199
+ if (keys.has(key))
200
+ return;
201
+ keys.add(key);
202
+ app.asyncData.subscribe(key, () => {});
203
+ }
204
+ var signalOf = (own, run) => own ? AbortSignal.any([own, run]) : run;
205
+ var callHandler = (app, fn, input) => ({ signal }) => fn(input, {
206
+ signal,
207
+ event: app.event
208
+ });
209
+
210
+ // packages/adapter-preact/src/index.ts
211
+ import {
212
+ addRouteMiddleware,
213
+ clearError,
214
+ disableScrollHandling,
215
+ forbidden,
216
+ navigateTo as navigateTo2,
217
+ notFound,
218
+ permanentRedirect,
219
+ refreshCookie,
220
+ setPageLayout,
221
+ showError,
222
+ unauthorized
223
+ } from "hamur/app";
224
+
225
+ // packages/adapter-preact/src/island.ts
226
+ import { h } from "preact";
227
+ import { useContext, useId } from "preact/hooks";
228
+ import { ISLAND_ELEMENT, renderIsland } from "hamur/adapter";
229
+ var isServer = typeof window === "undefined";
230
+ var KEPT = { __html: "" };
231
+ function Island({
232
+ component,
233
+ props,
234
+ on,
235
+ fallback,
236
+ children,
237
+ persist,
238
+ persistProps
239
+ }) {
240
+ const app = useContext(AppContextContext);
241
+ if (!app)
242
+ throw new Error("Island must be rendered inside a Hamur app");
243
+ const id = useId();
244
+ if (!isServer)
245
+ return h(ISLAND_ELEMENT, { dangerouslySetInnerHTML: KEPT });
246
+ const island = renderIsland(app, id, component, props ?? {}, on, children === undefined ? undefined : { component: () => children }, { persist, persistProps });
247
+ if (!island.html)
248
+ return h(ISLAND_ELEMENT, island.attributes, fallback);
249
+ return h(ISLAND_ELEMENT, {
250
+ ...island.attributes,
251
+ dangerouslySetInnerHTML: { __html: settledIsland(island.html) ?? "" }
252
+ });
253
+ }
254
+ // packages/adapter-preact/src/server-island.ts
255
+ import { h as h2 } from "preact";
256
+ import { useContext as useContext2, useId as useId2, useState } from "preact/hooks";
257
+ import { SERVER_ISLAND_ELEMENT, renderServerIsland, serverIslandProps } from "hamur/adapter";
258
+ var isServer2 = typeof window === "undefined";
259
+ var KEPT2 = { __html: "" };
260
+ function ServerIsland({
261
+ name,
262
+ props,
263
+ fallback
264
+ }) {
265
+ const app = useContext2(AppContextContext);
266
+ if (!app)
267
+ throw new Error("ServerIsland must be rendered inside a Hamur app");
268
+ const id = useId2();
269
+ const [kept] = useState(() => !isServer2 && app.asyncData.hydrating);
270
+ if (isServer2)
271
+ return h2(SERVER_ISLAND_ELEMENT, settledIsland(renderServerIsland(app, id, name, props).attributes) ?? {}, fallback);
272
+ if (kept)
273
+ return h2(SERVER_ISLAND_ELEMENT, { dangerouslySetInnerHTML: KEPT2 });
274
+ const given = props && Object.keys(props).length ? { props: serverIslandProps(name, props) } : {};
275
+ return h2(SERVER_ISLAND_ELEMENT, { name, ...given }, fallback);
276
+ }
277
+ // packages/adapter-preact/src/markdown.ts
278
+ import { Fragment, createElement } from "preact";
279
+ import { markdownLink, markdownProse, markdownView } from "hamur/adapter";
280
+ function render(view, raw, components) {
281
+ if (typeof view === "string")
282
+ return view;
283
+ if ("html" in view)
284
+ return createElement("hamur-html", {
285
+ style: "display:contents",
286
+ dangerouslySetInnerHTML: { __html: raw(view.html) }
287
+ });
288
+ const children = view.children.map((child) => render(child, raw, components));
289
+ if ("component" in view) {
290
+ const component = Object.hasOwn(components, view.component) ? components[view.component] : undefined;
291
+ if (component) {
292
+ const slots = Object.fromEntries(Object.entries(view.slots).map(([name, views]) => [
293
+ name,
294
+ createElement(Fragment, null, ...views.map((slot) => render(slot, raw, components)))
295
+ ]));
296
+ return createElement(component, { ...view.props, ...slots }, ...children);
297
+ }
298
+ return view.attrs ? createElement(view.component, view.attrs, ...children) : createElement(Fragment, null, ...children);
299
+ }
300
+ const link = markdownLink(view);
301
+ if (link)
302
+ return createElement(Link, { ...link.attrs, to: link.to }, ...children);
303
+ return createElement(view.tag, view.attrs, ...children);
304
+ }
305
+ function MarkdownRenderer({ value, components: given = {}, raw }) {
306
+ const { value: nodes, components } = markdownProse(value, given);
307
+ return createElement(Fragment, null, ...markdownView(nodes, { html: raw !== undefined, components }).map((view) => render(view, raw, components)));
308
+ }
309
+
310
+ // packages/adapter-preact/src/index.ts
311
+ import { matchesOf, routeDataOf } from "hamur/adapter";
312
+ import { equalSearch, isOnline, pollInterval, searchTarget, startPoll, subscribeOnline } from "hamur/adapter";
313
+ import { APP_UPDATE_OFF, createAppUpdate } from "hamur/adapter";
314
+ var NO_DEFAULTS = {};
315
+ function linkElement(props, defaults) {
316
+ return createElement2(typeof props.children === "function" ? CustomLink : AnchorLink, {
317
+ link: props,
318
+ defaults
319
+ });
320
+ }
321
+ function Link(props) {
322
+ return linkElement(props, NO_DEFAULTS);
323
+ }
324
+ function defineLink(defaults) {
325
+ return (props) => linkElement(props, defaults);
326
+ }
327
+ function useLink(to, options) {
328
+ return useLinkSlot({ to, ...options }, linkDefaultsOf(useContext3(AppContextContext)));
329
+ }
330
+ function Navigate({ to, replace }) {
331
+ const target = to;
332
+ if (isServer3)
333
+ throw redirect(target);
334
+ const started = useRef(false);
335
+ useEffect(() => {
336
+ if (started.current)
337
+ return;
338
+ started.current = true;
339
+ navigateTo(target, { replace });
340
+ }, []);
341
+ return null;
342
+ }
343
+ function useLinkTarget(props, attrs) {
344
+ warnRemovedLinkProps(props);
345
+ const { to: target, query, relative } = props;
346
+ const route = useContext3(RouteContext);
347
+ const hrefOf = useContext3(HrefContext);
348
+ const app = useContext3(AppContextContext);
349
+ const searched = route ? searchTarget(relativeTarget(target, route, relative), route, query) : target;
350
+ const { to, props: rest } = resolveLinkTarget(app, searched, attrs, hrefOf, props.external);
351
+ return { to, route, hrefOf, href: hrefOf(to), rest };
352
+ }
353
+ function AnchorLink({ link: props, defaults }) {
354
+ const d = linkDefaultsOf(useContext3(AppContextContext), defaults);
355
+ const {
356
+ to: _to,
357
+ href: _href,
358
+ noPrefetch: _noPrefetch,
359
+ query,
360
+ state: entryState,
361
+ relative: _relative,
362
+ mask,
363
+ unmaskOnReload,
364
+ viewTransition,
365
+ replace,
366
+ prefetch,
367
+ prefetchOn,
368
+ activeClass = d.activeClass,
369
+ exactActiveClass = d.exactActiveClass,
370
+ pendingClass = d.pendingClass,
371
+ ariaCurrentValue = "page",
372
+ external,
373
+ noRel,
374
+ prefetchedClass = d.prefetchedClass,
375
+ scroll,
376
+ prefetchDelay = d.prefetchDelay,
377
+ onBeforeNavigate,
378
+ children,
379
+ ...attrs
380
+ } = props;
381
+ const mode = external ? "false" : prefetchAttribute(linkPrefetchProps({ prefetch, prefetchOn }, d));
382
+ const baseURL = useContext3(BaseURLContext);
383
+ const { to, route, hrefOf, href, rest } = useLinkTarget(props, attrs);
384
+ const anchor = useRef(null);
385
+ const appRef = rest.ref;
386
+ const ref = useCallback((element) => {
387
+ anchor.current = element;
388
+ if (typeof appRef !== "function")
389
+ return void (appRef && (appRef.current = element));
390
+ const cleanup = appRef(element);
391
+ return typeof cleanup === "function" ? () => void (anchor.current = null, cleanup()) : undefined;
392
+ }, [appRef]);
393
+ const before = useRef(onBeforeNavigate);
394
+ before.current = onBeforeNavigate;
395
+ const hasBefore = onBeforeNavigate !== undefined;
396
+ useLayoutEffect(() => {
397
+ const element = anchor.current;
398
+ if (!element || entryState === undefined && !hasBefore)
399
+ return;
400
+ setLinkState(element, entryState, hasBefore ? (nav) => before.current?.(nav) : undefined);
401
+ return () => setLinkState(element, undefined);
402
+ }, [entryState, hasBefore]);
403
+ const state = route ? linkState(typeof to === "string" ? to : withoutBase(href, baseURL), route) : undefined;
404
+ const key = "className" in rest ? "className" : "class";
405
+ const classes = [rest[key], state?.active && activeClass, state?.exact && exactActiveClass].filter(Boolean).join(" ");
406
+ return createElement2("a", {
407
+ ...rest,
408
+ ref,
409
+ ...classes ? { [key]: classes } : {},
410
+ ...state?.exact && rest["aria-current"] === undefined ? { "aria-current": ariaCurrentValue } : {},
411
+ href,
412
+ "data-hamur-prefetch": mode,
413
+ "data-hamur-replace": replace ? "" : undefined,
414
+ "data-hamur-view-transition": viewTransitionAttribute(viewTransition),
415
+ "data-hamur-pending-class": pendingClass === LINK_DEFAULTS.pendingClass ? undefined : pendingClass,
416
+ "data-hamur-trim": query ? "" : undefined,
417
+ "data-hamur-mask": mask === undefined ? undefined : hrefOf(mask),
418
+ "data-hamur-unmask": mask !== undefined && unmaskOnReload === false ? "false" : undefined,
419
+ ...linkExtraAttributes({
420
+ href,
421
+ external,
422
+ noRel,
423
+ rel: rest.rel,
424
+ target: rest.target,
425
+ prefetchedClass,
426
+ scroll,
427
+ prefetchDelay,
428
+ externalRelAttribute: d.externalRelAttribute
429
+ })
430
+ }, createElement2(LinkHrefContext.Provider, { value: href }, children));
431
+ }
432
+ function CustomLink({ link: props, defaults }) {
433
+ const slot = useLinkSlot(props, linkDefaultsOf(useContext3(AppContextContext), defaults));
434
+ return createElement2(LinkHrefContext.Provider, { value: slot.href }, props.children(slot));
435
+ }
436
+ function useLinkSlot(props, d) {
437
+ const {
438
+ state: entryState,
439
+ mask,
440
+ unmaskOnReload,
441
+ viewTransition,
442
+ replace,
443
+ prefetch,
444
+ prefetchOn,
445
+ pendingClass = d.pendingClass,
446
+ ariaCurrentValue = "page",
447
+ external,
448
+ noRel,
449
+ prefetchedClass = d.prefetchedClass,
450
+ scroll,
451
+ prefetchDelay = d.prefetchDelay,
452
+ query,
453
+ rel,
454
+ target
455
+ } = props;
456
+ const baseURL = useContext3(BaseURLContext);
457
+ const router = useContext3(RouterContext);
458
+ const navigation = useNavigation();
459
+ const { to, route, hrefOf, href } = useLinkTarget(props, props);
460
+ const state = route ? linkState(typeof to === "string" ? to : withoutBase(href, baseURL), route) : undefined;
461
+ return createLinkSlot({
462
+ href,
463
+ to,
464
+ attrs: linkAttributes({
465
+ href,
466
+ external,
467
+ ...linkPrefetchProps({ prefetch, prefetchOn }, d),
468
+ replace,
469
+ viewTransition,
470
+ pendingClass,
471
+ trim: !!query,
472
+ mask: mask === undefined ? undefined : hrefOf(mask),
473
+ unmaskOnReload,
474
+ ariaCurrent: state?.exact ? ariaCurrentValue : undefined,
475
+ rel,
476
+ target,
477
+ noRel,
478
+ prefetchedClass,
479
+ scroll,
480
+ prefetchDelay,
481
+ externalRelAttribute: d.externalRelAttribute
482
+ }),
483
+ active: !!state?.active,
484
+ exact: !!state?.exact,
485
+ pending: linkPending(href, navigation, baseURL),
486
+ external,
487
+ options: {
488
+ replace,
489
+ state: entryState,
490
+ mask,
491
+ unmaskOnReload,
492
+ viewTransition,
493
+ scroll: scroll === false ? false : undefined
494
+ },
495
+ router
496
+ });
497
+ }
498
+ function Form({
499
+ action,
500
+ method,
501
+ encType,
502
+ replace,
503
+ reset,
504
+ reloadDocument,
505
+ refresh,
506
+ instance,
507
+ restore,
508
+ onSubmit,
509
+ children,
510
+ ...rest
511
+ }) {
512
+ assertFormMethod(action, method);
513
+ const route = useContext3(RouteContext);
514
+ const href = useContext3(HrefContext);
515
+ const element = useRef(null);
516
+ const callback = useRef(onSubmit);
517
+ callback.current = onSubmit;
518
+ const status = useMemo(createFormStatus, []);
519
+ const app = useContext3(AppContextContext);
520
+ const router = useContext3(RouterContext);
521
+ const fn = typeof action === "function" ? action : undefined;
522
+ useLayoutEffect(() => enhance(element.current, {
523
+ onSubmit: (args) => {
524
+ status.submitted(args.formData);
525
+ callback.current?.(args);
526
+ },
527
+ ...fn && app ? {
528
+ call: (input, formData) => runFunctionForm(app, fn, input, formData, () => router?.refresh() ?? Promise.resolve(), status, { refresh, instance })
529
+ } : {}
530
+ }), [status, fn, app, router, refresh, instance]);
531
+ const page = route ? route.fullPath.replace(/#.*$/, "") : undefined;
532
+ const target = typeof action === "function" ? functionFormAction(action, href(page ?? "/"), instance) : action !== undefined ? href(action) : page !== undefined ? href(page) : undefined;
533
+ return createElement2(FormStatusContext.Provider, { value: status }, createElement2("form", {
534
+ ...rest,
535
+ ref: element,
536
+ method: fn ? "post" : "get",
537
+ action: target,
538
+ enctype: encType,
539
+ "data-hamur-form": "",
540
+ "data-hamur-replace": replace ? "" : undefined,
541
+ "data-hamur-reset": reset ? "" : undefined,
542
+ "data-hamur-reload": reloadDocument ? "" : undefined,
543
+ "data-hamur-refresh": refresh === false ? "false" : undefined,
544
+ "data-hamur-restore": restore === false ? "false" : undefined
545
+ }, children));
546
+ }
547
+ function useFormStatus() {
548
+ const store = useContext3(FormStatusContext);
549
+ const [status, setStatus] = useLocalState(store ? store.get() : IDLE_FORM_STATUS);
550
+ useEffect(() => {
551
+ if (!store)
552
+ return;
553
+ setStatus(store.get());
554
+ return store.subscribe(() => setStatus(store.get()));
555
+ }, [store]);
556
+ return status;
557
+ }
558
+ function useForm(fn) {
559
+ const app = useContext3(AppContextContext);
560
+ if (!app)
561
+ throw new Error("useForm() must be called inside a Hamur app");
562
+ const current = useContext3(SubmittedContext);
563
+ const submitted = useRef(current);
564
+ submitted.current = current;
565
+ const form = useMemo(() => createForm(fn, { app, submitted: () => submitted.current }), [fn, app]);
566
+ const [, setVersion] = useLocalState(form.version());
567
+ useEffect(() => {
568
+ setVersion(form.version());
569
+ return form.subscribe(() => setVersion(form.version()));
570
+ }, [form]);
571
+ return form;
572
+ }
573
+ function useAction(fn, options = {}) {
574
+ const { instance } = options;
575
+ const router = useRouter();
576
+ const latest = useRef({ options, router });
577
+ latest.current = { options, router };
578
+ const { handlerState } = useContext3(SubmittedContext);
579
+ const app = useContext3(AppContextContext);
580
+ const runner = useMemo(() => createActionRunner(fn, {
581
+ app,
582
+ instance,
583
+ options: () => latest.current.options,
584
+ refresh: () => latest.current.router.refresh(),
585
+ restored: restoredResult(handlerState, fn.id, instance)
586
+ }), [fn, instance, app]);
587
+ const [snapshot, setSnapshot] = useLocalState(runner.get());
588
+ const below = useContext3(FormStatusContext);
589
+ useEffect(() => {
590
+ setSnapshot(runner.get());
591
+ const unlink = below ? below.owner.link(fn.id, runner, instance) : app ? linkActionRunner(app, fn.id, runner, instance) : undefined;
592
+ const stop = runner.subscribe(() => setSnapshot(runner.get()));
593
+ return () => {
594
+ stop();
595
+ unlink?.();
596
+ };
597
+ }, [runner, app, below, fn, instance]);
598
+ const { state, pending, error, failure } = snapshot;
599
+ return useMemo(() => ({ run: runner.run, state, pending, error, failure, reset: runner.reset }), [runner, state, pending, error, failure]);
600
+ }
601
+ function useRoute(name) {
602
+ const route = useContext3(RouteContext);
603
+ if (!route)
604
+ throw new Error("useRoute() must be called inside a Hamur app");
605
+ return route;
606
+ }
607
+ function useResolvedPath(to, { relative } = {}) {
608
+ return resolveRelative(to, useRoute(), relative);
609
+ }
610
+ function useRouter() {
611
+ const router = useContext3(RouterContext);
612
+ if (!router)
613
+ throw new Error("useRouter() must be called inside a Hamur app");
614
+ return router;
615
+ }
616
+ function useData(...unknownRoute) {
617
+ const data = useContext3(DataContext);
618
+ const key = useContext3(EntryContext);
619
+ if (key === null)
620
+ throw new Error("useData() must be called inside a Hamur app");
621
+ return data[key] ?? null;
622
+ }
623
+ function useRouteData(name) {
624
+ const route = useContext3(RouteContext);
625
+ const data = useContext3(DataContext);
626
+ if (!route)
627
+ throw new Error("useRouteData() must be called inside a Hamur app");
628
+ return routeDataOf(route, data, name);
629
+ }
630
+ function useMatches() {
631
+ const route = useContext3(RouteContext);
632
+ const data = useContext3(DataContext);
633
+ if (!route)
634
+ throw new Error("useMatches() must be called inside a Hamur app");
635
+ return useMemo(() => matchesOf(route, data), [route, data]);
636
+ }
637
+ var isDev = import.meta.dev === true;
638
+ var warnedSlots = new Set;
639
+ function Outlet({ context, name }) {
640
+ const next = useContext3(OutletContext) ?? null;
641
+ const slots = useContext3(SlotsContext);
642
+ const owner = useContext3(EntryContext);
643
+ let child = next;
644
+ if (isDev && name === undefined)
645
+ noteOutlet(owner);
646
+ if (name !== undefined) {
647
+ child = slots?.[name] ?? null;
648
+ const key = `${owner}:${name}`;
649
+ if (isDev && !(slots && (name in slots)) && !warnedSlots.has(key)) {
650
+ warnedSlots.add(key);
651
+ console.warn(`Outlet name "${name}" has no slot in ${owner}`);
652
+ }
653
+ }
654
+ return context === undefined ? child : createElement2(OutletValueContext.Provider, { value: context }, child);
655
+ }
656
+ function useOutletContext() {
657
+ return useContext3(OutletValueContext);
658
+ }
659
+ function useLayoutSegments() {
660
+ const route = useContext3(RouteContext);
661
+ const entry = useContext3(EntryContext);
662
+ if (!route)
663
+ throw new Error("useLayoutSegments() must be called inside a Hamur app");
664
+ return useMemo(() => layoutSegments(route, entry), [route, entry]);
665
+ }
666
+ function useLayoutSegment() {
667
+ return useLayoutSegments()[0] ?? null;
668
+ }
669
+ function useLayout() {
670
+ const layout = useContext3(LayoutContext);
671
+ if (!layout)
672
+ throw new Error("useLayout() must be called inside a Hamur app");
673
+ return layout;
674
+ }
675
+ function ClientOnly({ children, fallback }) {
676
+ const [hydrated, setHydrated] = useLocalState(false);
677
+ useEffect(() => setHydrated(true), []);
678
+ if (!hydrated)
679
+ return fallback ?? null;
680
+ return createElement2(ClientOnlyContext.Provider, { value: { fallback } }, children);
681
+ }
682
+ function useHydrated() {
683
+ const app = useApp();
684
+ const [hydrated, setHydrated] = useLocalState(!isServer3 && !app.asyncData.hydrating);
685
+ useEffect(() => {
686
+ const sync = () => setHydrated(!app.asyncData.hydrating);
687
+ sync();
688
+ return app.hooks.hook("app:mounted", sync);
689
+ }, [app]);
690
+ return hydrated;
691
+ }
692
+ function DevOnly({ children, fallback }) {
693
+ return isDev ? children : fallback ?? null;
694
+ }
695
+ var isThenable = (value) => typeof value?.then === "function";
696
+ var outcomes = new WeakMap;
697
+ var waits = new WeakMap;
698
+ function outcomeOf(promise) {
699
+ const known = settledOf(promise) ?? outcomes.get(promise);
700
+ if (known)
701
+ return known;
702
+ let wait = waits.get(promise);
703
+ if (!wait) {
704
+ wait = promise.then((value) => void outcomes.set(promise, { status: "fulfilled", value }), (reason) => void outcomes.set(promise, { status: "rejected", reason }));
705
+ waits.set(promise, wait);
706
+ }
707
+ throw wait;
708
+ }
709
+ var warnedAwait = false;
710
+ function Resolved({ resolve, error, children }) {
711
+ const outcome = isThenable(resolve) ? outcomeOf(resolve) : { status: "fulfilled", value: resolve };
712
+ if (outcome.status === "rejected") {
713
+ if (error)
714
+ return error(outcome.reason);
715
+ throw outcome.reason;
716
+ }
717
+ if (typeof children !== "function") {
718
+ if (isDev && !warnedAwait) {
719
+ warnedAwait = true;
720
+ console.warn("Await has nothing to render for its value");
721
+ }
722
+ return null;
723
+ }
724
+ return children(outcome.value);
725
+ }
726
+ function Await({ fallback, ...props }) {
727
+ return createElement2(Suspense, { fallback: fallback ?? null }, createElement2(Resolved, props));
728
+ }
729
+ function useDeferred(promise) {
730
+ if (!isThenable(promise))
731
+ return promise;
732
+ const outcome = outcomeOf(promise);
733
+ if (outcome.status === "rejected")
734
+ throw outcome.reason;
735
+ return outcome.value;
736
+ }
737
+ function Time(props) {
738
+ const app = useApp();
739
+ const [now, setNow] = useLocalState(undefined);
740
+ const view = timeView(app, useRoute().path, props, now);
741
+ const { interval } = view;
742
+ useEffect(() => interval === undefined ? undefined : watchTime(interval, setNow), [interval]);
743
+ return createElement2("time", { ...timeAttributes(props), datetime: view.iso, "data-locale": view.locale, "data-time-zone": view.timeZone }, view.text);
744
+ }
745
+ function useLoadingIndicator(options) {
746
+ const store = createLoadingIndicator(useApp(), options);
747
+ const source = useContext3(NavigationContext);
748
+ const [state, setState] = useLocalState(store.get());
749
+ useEffect(() => {
750
+ setState(store.get());
751
+ return store.subscribe(() => setState(store.get()));
752
+ }, [store]);
753
+ useEffect(() => source ? store.follow(source) : undefined, [store, source]);
754
+ return { ...state, start: store.start, finish: store.finish, set: store.set, clear: store.clear };
755
+ }
756
+ function useAppUpdate() {
757
+ const store = createAppUpdate(useApp());
758
+ const [state, setState] = useLocalState(APP_UPDATE_OFF);
759
+ useEffect(() => {
760
+ setState(store.get());
761
+ return store.subscribe(() => setState(store.get()));
762
+ }, [store]);
763
+ return { ...state, check: store.check };
764
+ }
765
+ function LoadingIndicator(props) {
766
+ const { color, errorColor, height } = props;
767
+ const { progress, isLoading, error } = useLoadingIndicator(loadingIndicatorOptions(props));
768
+ const element = useRef(null);
769
+ useEffect(() => {
770
+ if (element.current)
771
+ paintLoadingIndicator(element.current, { progress, isLoading, error }, { color, errorColor, height });
772
+ }, [progress, isLoading, error, color, errorColor, height]);
773
+ return createElement2("div", { ref: element, "data-hamur-loading-indicator": "", "aria-hidden": "true" });
774
+ }
775
+ function useRouteAnnouncer() {
776
+ const store = createRouteAnnouncer(useApp());
777
+ const [state, setState] = useLocalState(store.get());
778
+ useEffect(() => {
779
+ setState(store.get());
780
+ return store.subscribe(() => setState(store.get()));
781
+ }, [store]);
782
+ return { ...state, set: store.set, polite: store.polite, assertive: store.assertive };
783
+ }
784
+ function RouteAnnouncer({
785
+ politeness,
786
+ class: className,
787
+ style
788
+ }) {
789
+ const store = createRouteAnnouncer(useApp());
790
+ const { message, politeness: current } = useRouteAnnouncer();
791
+ const element = useRef(null);
792
+ const key = useId3();
793
+ const [, setTick] = useLocalState(0);
794
+ const owner = store.claim(key, () => setTick((count) => count + 1));
795
+ const styled = !!className || !!style;
796
+ useEffect(() => () => store.release(key), [store, key]);
797
+ useEffect(() => owner ? store.mount() : undefined, [store, owner]);
798
+ useEffect(() => {
799
+ if (owner && !styled && element.current)
800
+ hideAnnouncer(element.current);
801
+ }, [owner, styled]);
802
+ if (!owner)
803
+ return createElement2("div", { "aria-live": "off", "aria-atomic": "true" });
804
+ const live = politeness ?? current;
805
+ return createElement2("div", {
806
+ ref: element,
807
+ id: ROUTE_ANNOUNCER_ID,
808
+ "aria-live": live,
809
+ "aria-atomic": "true",
810
+ role: announcerRole(live),
811
+ class: className,
812
+ style
813
+ }, message);
814
+ }
815
+ function useAnnouncer({ politeness = "polite" } = {}) {
816
+ const store = createAnnouncer(useApp());
817
+ const [state, setState] = useLocalState(store.get());
818
+ useEffect(() => {
819
+ setState(store.get());
820
+ return store.subscribe(() => setState(store.get()));
821
+ }, [store]);
822
+ const actions = useMemo(() => announcerActions(store, politeness), [store, politeness]);
823
+ return { message: state.message, politeness: state.politeness, ...actions };
824
+ }
825
+ function Announcer() {
826
+ const store = createAnnouncer(useApp());
827
+ const [{ regions }, setState] = useLocalState(store.get());
828
+ const polite = useRef(null);
829
+ const assertive = useRef(null);
830
+ useEffect(() => {
831
+ setState(store.get());
832
+ return store.subscribe(() => setState(store.get()));
833
+ }, [store]);
834
+ useEffect(() => store.mount(), [store]);
835
+ useEffect(() => {
836
+ for (const element of [polite.current, assertive.current])
837
+ if (element)
838
+ hideAnnouncer(element);
839
+ }, []);
840
+ return createElement2(Fragment2, null, createElement2("div", { ref: polite, id: ANNOUNCER_POLITE_ID, "aria-live": "polite", "aria-atomic": "true", role: "status" }, regions.polite), createElement2("div", { ref: assertive, id: ANNOUNCER_ASSERTIVE_ID, "aria-live": "assertive", "aria-atomic": "true", role: "alert" }, regions.assertive));
841
+ }
842
+ function useApp() {
843
+ const app = useContext3(AppContextContext);
844
+ if (!app)
845
+ throw new Error("useApp() must be called inside a Hamur app");
846
+ return app;
847
+ }
848
+ function useRuntimeHook(name, fn) {
849
+ const app = useApp();
850
+ const latest = useRef(fn);
851
+ latest.current = fn;
852
+ useEffect(() => app.hooks.hook(name, (...args) => latest.current(...args)), [app, name]);
853
+ }
854
+ function useReportWebVitals(fn) {
855
+ const app = useApp();
856
+ const latest = useRef(fn);
857
+ latest.current = fn;
858
+ useEffect(() => reportWebVitals(app, (metric) => latest.current(metric)), [app]);
859
+ }
860
+ function onAppReady(fn) {
861
+ const app = useApp();
862
+ const latest = useRef(fn);
863
+ latest.current = fn;
864
+ const scheduled = useRef(false);
865
+ useEffect(() => {
866
+ if (scheduled.current)
867
+ return;
868
+ scheduled.current = true;
869
+ whenAppReady(app, () => latest.current());
870
+ }, [app]);
871
+ }
872
+ function onPrehydrate(fn) {
873
+ return prehydrate(useApp(), fn);
874
+ }
875
+ function useRequestEvent() {
876
+ return requestEventOf(useApp());
877
+ }
878
+ function usePreviewMode() {
879
+ return previewModeOf(useApp());
880
+ }
881
+ function useRequestHeaders(names) {
882
+ return requestHeadersOf(useApp(), names);
883
+ }
884
+ function useRequestHeader(name) {
885
+ return requestHeaderOf(useApp(), name);
886
+ }
887
+ function useRequestURL() {
888
+ return requestURLOf(useApp());
889
+ }
890
+ function useRequestFetch() {
891
+ return requestFetchOf(useApp());
892
+ }
893
+ function setResponseStatus(status, statusText) {
894
+ setResponseStatusOf(useApp(), status, statusText);
895
+ }
896
+ function useResponseHeader(name) {
897
+ const header = responseHeaderOf(useApp(), name);
898
+ return [header.get(), header.set];
899
+ }
900
+ function HttpStatusCode({ code, text }) {
901
+ setResponseStatus(code, text);
902
+ return null;
903
+ }
904
+ function HttpHeader({ name, value }) {
905
+ responseHeaderOf(useApp(), name).set(value);
906
+ return null;
907
+ }
908
+ function useAsyncDataEntry(target, input) {
909
+ const key = typeof target === "string" ? target : asyncDataKey(target, input);
910
+ const app = useApp();
911
+ const [data, setLocal] = useLocalState(() => app.asyncData.get(key)?.data);
912
+ useEffect(() => {
913
+ setLocal(app.asyncData.get(key)?.data);
914
+ return app.asyncData.subscribe(key, () => setLocal(app.asyncData.get(key)?.data));
915
+ }, [app, key]);
916
+ const setData = useCallback((next) => app.asyncData.setData(key, next), [app, key]);
917
+ return { data, setData };
918
+ }
919
+ function clearState(keys, options) {
920
+ clearStateOf(useApp(), keys, options);
921
+ }
922
+ function callOnce(key, fn, options) {
923
+ return callOnceOf(useApp(), key, fn, options);
924
+ }
925
+ function useRuntimeConfig() {
926
+ return runtimeConfigOf(useApp());
927
+ }
928
+ function useSiteConfig() {
929
+ return siteConfigOf(useApp());
930
+ }
931
+ var IDLE = { state: "idle", pending: false };
932
+ function useNavigation() {
933
+ const source = useContext3(NavigationContext);
934
+ const [navigation, setNavigation] = useLocalState(source ? source.get() : IDLE);
935
+ useEffect(() => {
936
+ if (!source)
937
+ return;
938
+ setNavigation(source.get());
939
+ return source.subscribe(() => setNavigation(source.get()));
940
+ }, [source]);
941
+ return navigation;
942
+ }
943
+ var NOT_PENDING = { pending: false };
944
+ function useLinkStatus() {
945
+ const href = useContext3(LinkHrefContext);
946
+ const baseURL = useContext3(BaseURLContext);
947
+ const state = useNavigation();
948
+ return href !== null && linkPending(href, state, baseURL) ? { pending: true } : NOT_PENDING;
949
+ }
950
+ function useNavigationHook(kind, fn) {
951
+ const latest = useRef(fn);
952
+ latest.current = fn;
953
+ useLayoutEffect(() => navigationHook(kind, (navigation) => latest.current(navigation)), [kind]);
954
+ }
955
+ var onBeforeNavigate = (fn) => useNavigationHook("before", fn);
956
+ var onNavigate = (fn) => useNavigationHook("navigate", fn);
957
+ var onAfterNavigate = (fn) => useNavigationHook("after", fn);
958
+ function onPageActivated(fn) {
959
+ const own = useContext3(ActivationContext);
960
+ const latest = useRef(fn);
961
+ latest.current = fn;
962
+ useEffect(() => {
963
+ const run = () => latest.current();
964
+ if (shown(own))
965
+ later(true, run);
966
+ own?.on.add(run);
967
+ return () => void own?.on.delete(run);
968
+ }, [own]);
969
+ }
970
+ function onPageDeactivated(fn) {
971
+ const own = useContext3(ActivationContext);
972
+ const latest = useRef(fn);
973
+ latest.current = fn;
974
+ useEffect(() => {
975
+ const run = () => latest.current();
976
+ own?.off.add(run);
977
+ return () => {
978
+ own?.off.delete(run);
979
+ if (shown(own))
980
+ later(false, run);
981
+ };
982
+ }, [own]);
983
+ }
984
+ function useRouteGuard(kind, fn) {
985
+ const entry = useContext3(EntryContext);
986
+ const latest = useRef(fn);
987
+ latest.current = fn;
988
+ useLayoutEffect(() => routeGuard(kind, entry ?? undefined, (to, from) => latest.current(to, from)), [kind, entry]);
989
+ }
990
+ var onBeforeRouteLeave = (fn) => useRouteGuard("leave", fn);
991
+ var onBeforeRouteUpdate = (fn) => useRouteGuard("update", fn);
992
+ function usePreventNavigate(fn) {
993
+ const latest = useRef(fn);
994
+ latest.current = fn;
995
+ useLayoutEffect(() => navigationBlocker((args) => latest.current(args)), []);
996
+ }
997
+ function useBlocker(options) {
998
+ const store = useRef(null);
999
+ store.current ??= createBlocker(options);
1000
+ store.current.setOptions(options);
1001
+ const blocker = store.current;
1002
+ const [value, setValue] = useLocalState(blocker.get());
1003
+ useLayoutEffect(() => {
1004
+ const unsubscribe = blocker.subscribe(() => setValue(blocker.get()));
1005
+ const stop = blocker.start();
1006
+ return () => (stop(), unsubscribe());
1007
+ }, [blocker]);
1008
+ useLayoutEffect(() => blocker.sync());
1009
+ return value;
1010
+ }
1011
+ function useSnapshot(key, options) {
1012
+ const latest = useRef(options);
1013
+ latest.current = options;
1014
+ useLayoutEffect(() => registerSnapshot(key, {
1015
+ capture: () => latest.current.capture(),
1016
+ restore: (value) => latest.current.restore(value)
1017
+ }), [key]);
1018
+ }
1019
+ function useSearch(select) {
1020
+ const store = useContext3(SearchContext);
1021
+ if (!store)
1022
+ throw new Error("useSearch() must be called inside a Hamur app");
1023
+ const read = () => select ? select(store.get()) : store.get();
1024
+ const latest = useRef(read);
1025
+ latest.current = read;
1026
+ const shown = useRef(undefined);
1027
+ const next = read();
1028
+ if (shown.current === undefined || !equalSearch(shown.current, next))
1029
+ shown.current = next;
1030
+ const [, force] = useLocalState(0);
1031
+ useEffect(() => store.subscribe(() => !equalSearch(shown.current, latest.current()) && force((count) => count + 1)), [store]);
1032
+ const set = useCallback((change, { replace = true } = {}) => {
1033
+ const update = { query: (prev) => typeof change === "function" ? change(prev) : change };
1034
+ return replace ? store.router().replace(update) : store.router().navigate(update);
1035
+ }, [store]);
1036
+ return useMemo(() => ({ value: shown.current, set }), [shown.current, set]);
1037
+ }
1038
+ function usePoll(ms, options = {}) {
1039
+ pollInterval(ms);
1040
+ const current = useRouter();
1041
+ const router = useRef(current);
1042
+ router.current = current;
1043
+ const keys = useRef(options.keys);
1044
+ keys.current = options.keys;
1045
+ useEffect(() => startPoll(ms, () => keys.current ? router.current.refresh(keys.current) : router.current.refresh()), [ms]);
1046
+ }
1047
+ function useOnline() {
1048
+ const [online, setOnline] = useLocalState(true);
1049
+ useEffect(() => {
1050
+ setOnline(isOnline());
1051
+ return subscribeOnline(setOnline);
1052
+ }, []);
1053
+ return online;
1054
+ }
1055
+ function useSyncedState(key, initial, options = {}) {
1056
+ const state = useMemo(() => syncedState(key, initial, options), [key, options.room]);
1057
+ const [value, setValue] = useLocalState(initial);
1058
+ useEffect(() => {
1059
+ setValue(() => state.get());
1060
+ return state.subscribe((next) => setValue(() => next));
1061
+ }, [state]);
1062
+ return [value, state.set];
1063
+ }
1064
+ function useOptimistic(value, reduce) {
1065
+ const app = useContext3(AppContextContext);
1066
+ const source = useMemo(() => optimisticSource(app), [app]);
1067
+ const [pending, setPending] = useLocalState(source.get);
1068
+ useEffect(() => {
1069
+ setPending(() => source.get());
1070
+ return source.subscribe(() => setPending(() => source.get()));
1071
+ }, [source]);
1072
+ return pending.length ? reduce(value, pending) : value;
1073
+ }
1074
+ function useError() {
1075
+ const { store, initial } = useContext3(ErrorContext);
1076
+ const [error, setError] = useLocalState(store ? store.get() : initial);
1077
+ useEffect(() => {
1078
+ if (!store)
1079
+ return;
1080
+ setError(store.get());
1081
+ return store.subscribe(() => setError(store.get()));
1082
+ }, [store]);
1083
+ return error;
1084
+ }
1085
+ var recordError = ({ name, message }) => Object.assign(new Error(message), { name });
1086
+ function BoundaryChildren({ boundary, children }) {
1087
+ const key = `$boundary:${useId3()}`;
1088
+ const app = useContext3(AppContextContext);
1089
+ boundary.key = key;
1090
+ boundary.app = app;
1091
+ const seeded = isServer3 ? undefined : app?.state?.get(key);
1092
+ useLayoutEffect(() => {
1093
+ if (!seeded)
1094
+ return;
1095
+ app.state.set(key, undefined);
1096
+ boundary.setState({ failed: true, error: recordError(seeded) });
1097
+ }, []);
1098
+ return seeded ? boundary.props.fallback({ error: recordError(seeded), reset: boundary.reset }) : children;
1099
+ }
1100
+
1101
+ class ErrorBoundary extends Component {
1102
+ state = { failed: false, error: undefined };
1103
+ key;
1104
+ app = null;
1105
+ static getDerivedStateFromError(error) {
1106
+ return { failed: true, error };
1107
+ }
1108
+ reset = () => this.setState({ failed: false, error: undefined });
1109
+ render() {
1110
+ if (!this.state.failed)
1111
+ return createElement2(BoundaryChildren, { boundary: this, children: this.props.children });
1112
+ if (isServer3 && this.app) {
1113
+ const error = this.state.error = publicError(this.app, this.state.error);
1114
+ if (this.key && error instanceof Error)
1115
+ this.app.state?.set(this.key, { name: error.name, message: error.message });
1116
+ }
1117
+ return this.props.fallback({ error: this.state.error, reset: this.reset });
1118
+ }
1119
+ }
1120
+ function FallbackChildren({ boundary, children }) {
1121
+ const id = useId3();
1122
+ boundary.id = id;
1123
+ boundary.app = useContext3(AppContextContext);
1124
+ const [rendered] = useLocalState(() => !isServer3 && clientFallbackIds.has(id));
1125
+ const [fresh, setFresh] = useLocalState(false);
1126
+ useLayoutEffect(() => {
1127
+ if (!rendered)
1128
+ return;
1129
+ clientFallbackIds.delete(id);
1130
+ setFresh(true);
1131
+ }, []);
1132
+ if (rendered && !fresh)
1133
+ return boundary.props.fallback ?? null;
1134
+ return rendered && boundary.props.keepFallback ? createElement2(Suspense, { fallback: boundary.props.fallback ?? null }, children) : children;
1135
+ }
1136
+
1137
+ class ClientFallback extends Component {
1138
+ state = { failed: false, error: undefined };
1139
+ id;
1140
+ app = null;
1141
+ static getDerivedStateFromError(error) {
1142
+ return { failed: true, error };
1143
+ }
1144
+ render() {
1145
+ if (!this.state.failed)
1146
+ return createElement2(FallbackChildren, { boundary: this, children: this.props.children });
1147
+ if (isServer3 && this.app && this.id)
1148
+ clientFallback(this.app, this.id, this.state.error, this.props.onSsrError);
1149
+ return this.props.fallback ?? null;
1150
+ }
1151
+ }
1152
+ function DefaultErrorPage({ error }) {
1153
+ return createElement2("main", { style: { fontFamily: "system-ui, sans-serif", textAlign: "center", padding: "4rem 1rem" } }, createElement2("h1", { style: { fontSize: "4rem", margin: 0 } }, String(error.status)), createElement2("p", null, error.message), isDev && error.code ? createElement2("p", null, createElement2("a", { href: diagnosticURL(error.code), target: "_blank", rel: "noopener" }, error.code)) : null, error.stack ? createElement2("pre", { style: { textAlign: "left", overflow: "auto" } }, error.stack) : null, createElement2(Link, { to: "/" }, "Back home"));
1154
+ }
1155
+ var pendingApply = new WeakSet;
1156
+ function scheduleApply(head) {
1157
+ if (pendingApply.has(head))
1158
+ return;
1159
+ pendingApply.add(head);
1160
+ queueMicrotask(() => {
1161
+ pendingApply.delete(head);
1162
+ head.apply();
1163
+ });
1164
+ }
1165
+ var isServer3 = typeof window === "undefined";
1166
+ function useHeadOnServer(head, input, options) {
1167
+ const entry = useRef(undefined);
1168
+ if (entry.current)
1169
+ entry.current.patch(input);
1170
+ else
1171
+ entry.current = head.push(input, options);
1172
+ }
1173
+ function useClientHead(head, input) {
1174
+ const rank = useRef(0);
1175
+ rank.current ||= nextRank();
1176
+ const entry = useRef(undefined);
1177
+ useLayoutEffect(() => {
1178
+ entry.current = mountHead(head, rank.current, input);
1179
+ scheduleApply(head);
1180
+ return () => {
1181
+ entry.current?.dispose();
1182
+ entry.current = undefined;
1183
+ scheduleApply(head);
1184
+ };
1185
+ }, [head]);
1186
+ useLayoutEffect(() => {
1187
+ entry.current?.patch(input);
1188
+ scheduleApply(head);
1189
+ });
1190
+ }
1191
+ function useHead(input) {
1192
+ const head = useContext3(HeadContext);
1193
+ if (!head)
1194
+ throw new Error("useHead() must be called inside a Hamur app");
1195
+ if (isServer3)
1196
+ useHeadOnServer(head, input);
1197
+ else
1198
+ useClientHead(head, input);
1199
+ }
1200
+ function useSeoMeta(input) {
1201
+ useHead(seoMetaToHead(input));
1202
+ }
1203
+ function useHeadSafe(input) {
1204
+ useHead(sanitizeHead(input));
1205
+ }
1206
+ function HeadTitle({ title, template }) {
1207
+ useHead({ title, titleTemplate: template });
1208
+ return null;
1209
+ }
1210
+ var HeadMeta = (props) => {
1211
+ useHead({ meta: [{ ...props }] });
1212
+ return null;
1213
+ };
1214
+ var HeadLink = (props) => {
1215
+ useHead({ link: [{ ...props }] });
1216
+ return null;
1217
+ };
1218
+ var HeadScript = (props) => {
1219
+ useHead({ script: [{ ...props }] });
1220
+ return null;
1221
+ };
1222
+ var HeadStyle = (props) => {
1223
+ useHead({ style: [{ ...props }] });
1224
+ return null;
1225
+ };
1226
+ var HtmlAttrs = (props) => {
1227
+ useHead({ htmlAttrs: { ...props } });
1228
+ return null;
1229
+ };
1230
+ var BodyAttrs = (props) => {
1231
+ useHead({ bodyAttrs: { ...props } });
1232
+ return null;
1233
+ };
1234
+ function useServerHead(input) {
1235
+ if (!isServer3)
1236
+ return;
1237
+ const head = useContext3(HeadContext);
1238
+ if (!head)
1239
+ throw new Error("useServerHead() must be called inside a Hamur app");
1240
+ useHeadOnServer(head, input, { server: true });
1241
+ }
1242
+ function useServerSeoMeta(input) {
1243
+ if (isServer3)
1244
+ useServerHead(seoMetaToHead(input));
1245
+ }
1246
+ function useState2(...args) {
1247
+ const { key, init } = stateArguments(args);
1248
+ const state = useContext3(StateContext);
1249
+ if (!state)
1250
+ throw new Error("useState() must be called inside a Hamur app");
1251
+ state.use(key, init);
1252
+ const [value, setValue] = useLocalState(() => state.get(key));
1253
+ useEffect(() => {
1254
+ setValue(state.get(key));
1255
+ return state.subscribe(key, () => setValue(state.get(key)));
1256
+ }, [state, key]);
1257
+ const set = useCallback((next) => state.set(key, typeof next === "function" ? next(state.get(key)) : next), [state, key]);
1258
+ return [value, set];
1259
+ }
1260
+ function useCookie(name, options = {}) {
1261
+ const app = useContext3(AppContextContext);
1262
+ const live = useRef({}).current;
1263
+ for (const key of Object.keys(live))
1264
+ delete live[key];
1265
+ Object.assign(live, options, app ? { app } : {});
1266
+ const cookie = useMemo(() => cookieOf(name, live), [name, app]);
1267
+ const [value, setValue] = useLocalState(() => cookie.get());
1268
+ useEffect(() => {
1269
+ setValue(() => cookie.get());
1270
+ return cookie.subscribe((next) => setValue(() => next));
1271
+ }, [cookie]);
1272
+ const set = useCallback((next) => cookie.set(next), [cookie]);
1273
+ return [value, set];
1274
+ }
1275
+ function useAsyncData(...args) {
1276
+ const [first, second, given] = asyncDataArguments("useAsyncData", args);
1277
+ const options = given ?? {};
1278
+ const app = useApp();
1279
+ if (!isHandlerReference(first))
1280
+ return keyedAsyncData(first, second, options);
1281
+ const input = valueOf(second);
1282
+ return keyedAsyncData(asyncDataKey(first, input), callHandler(app, first, input), options);
1283
+ }
1284
+ function keyedAsyncData(keyOrGetter, handler, options) {
1285
+ const key = valueOf(keyOrGetter);
1286
+ const app = useApp();
1287
+ const store = app.asyncData;
1288
+ store.neverHydrated(key, useContext3(NeverHydratedContext));
1289
+ const {
1290
+ server,
1291
+ lazy,
1292
+ default: init,
1293
+ transform,
1294
+ immediate,
1295
+ dedupe,
1296
+ pick,
1297
+ watch,
1298
+ getCachedData = defaultGetCachedData
1299
+ } = options;
1300
+ const settings = {
1301
+ server,
1302
+ lazy,
1303
+ default: init,
1304
+ transform,
1305
+ immediate,
1306
+ dedupe,
1307
+ pick
1308
+ };
1309
+ const read = () => store.get(key) ?? { data: init?.(), error: undefined, status: "idle", promise: undefined };
1310
+ const created = useRef(undefined);
1311
+ if (created.current !== key) {
1312
+ created.current = key;
1313
+ keepCached(app, key, getCachedData);
1314
+ const started = store.fetch(key, handler, {
1315
+ ...settings,
1316
+ cached: getCachedData(key, app, { cause: "initial" })
1317
+ });
1318
+ if (isServer3 && started.status === "pending")
1319
+ markWaiting(store);
1320
+ }
1321
+ const [state, setState] = useLocalState(() => ({ key, entry: read() }));
1322
+ useLayoutEffect(() => {
1323
+ const sync = () => setState((previous) => previous.key === key && previous.entry === store.get(key) ? previous : { key, entry: read() });
1324
+ const unsubscribe = store.subscribe(key, sync);
1325
+ sync();
1326
+ return unsubscribe;
1327
+ }, [store, key]);
1328
+ const entry = state.key === key ? state.entry : read();
1329
+ const latest = useRef({ handler, settings, getCachedData });
1330
+ latest.current = { handler, settings, getCachedData };
1331
+ const again = (cause) => refreshCached(app, key, latest.current.handler, latest.current.settings, latest.current.getCachedData, cause);
1332
+ const refresh = useCallback(() => again("refresh:manual"), [app, key]);
1333
+ useEffect(() => store.onRefresh(key, () => again("refresh:hook")), [store, key]);
1334
+ const first = useRef(true);
1335
+ useEffect(() => {
1336
+ if (first.current)
1337
+ first.current = false;
1338
+ else
1339
+ again("watch");
1340
+ }, watch || []);
1341
+ const clear = () => clearAsyncData(app, key);
1342
+ return {
1343
+ data: entry.data,
1344
+ error: entry.error,
1345
+ status: entry.status,
1346
+ pending: isPending(app, entry),
1347
+ refresh,
1348
+ execute: refresh,
1349
+ clear
1350
+ };
1351
+ }
1352
+ function useFetch(urlOrGetter, options = {}) {
1353
+ return fetchWith(undefined, urlOrGetter, options);
1354
+ }
1355
+ function fetchWith(client, urlOrGetter, options) {
1356
+ const {
1357
+ key,
1358
+ server,
1359
+ lazy,
1360
+ default: init,
1361
+ transform,
1362
+ immediate,
1363
+ dedupe,
1364
+ pick,
1365
+ watch,
1366
+ getCachedData,
1367
+ ...rest
1368
+ } = options;
1369
+ const url = valueOf(urlOrGetter);
1370
+ const request = requestOf(rest);
1371
+ const app = useApp();
1372
+ const fetcher = client ?? app.fetch ?? $fetch;
1373
+ return useAsyncData(key ?? fetchKey(url, request), ({ signal }) => fetcher(url, { ...request, signal: signalOf(request.signal, signal) }), { server, lazy, default: init, transform, immediate, dedupe, pick, watch, getCachedData });
1374
+ }
1375
+ function createUseFetch(defaults, { $fetch: client } = {}) {
1376
+ return (url, options) => fetchWith(client, url, resolveCallOptions(defaults, options));
1377
+ }
1378
+ function createUseAsyncData(defaults) {
1379
+ return (...args) => {
1380
+ const [first, second, options] = asyncDataArguments("useAsyncData", args);
1381
+ return useAsyncData(first, second, resolveCallOptions(defaults, options));
1382
+ };
1383
+ }
1384
+ var useLazyFetch = (url, options) => useFetch(url, { ...options, lazy: true });
1385
+ var useLazyAsyncData = (...args) => {
1386
+ const [first, second, options] = asyncDataArguments("useLazyAsyncData", args);
1387
+ return useAsyncData(first, second, {
1388
+ ...options,
1389
+ lazy: true
1390
+ });
1391
+ };
1392
+ function transitionName(name) {
1393
+ return { style: { viewTransitionName: checkTransitionName(name) } };
1394
+ }
1395
+ export {
1396
+ Announcer,
1397
+ Await,
1398
+ BodyAttrs,
1399
+ ClientFallback,
1400
+ ClientOnly,
1401
+ DefaultErrorPage,
1402
+ DevOnly,
1403
+ ErrorBoundary,
1404
+ Form,
1405
+ HeadLink,
1406
+ HeadMeta,
1407
+ HeadScript,
1408
+ HeadStyle,
1409
+ HeadTitle,
1410
+ HtmlAttrs,
1411
+ HttpHeader,
1412
+ HttpStatusCode,
1413
+ Island,
1414
+ Link,
1415
+ LoadingIndicator,
1416
+ MarkdownRenderer,
1417
+ Navigate,
1418
+ Outlet,
1419
+ RouteAnnouncer,
1420
+ ServerIsland,
1421
+ Time,
1422
+ addRouteMiddleware,
1423
+ callOnce,
1424
+ clearError,
1425
+ clearState,
1426
+ createUseAsyncData,
1427
+ createUseFetch,
1428
+ defineLazyHydrationComponent2 as defineLazyHydrationComponent,
1429
+ defineLink,
1430
+ disableScrollHandling,
1431
+ forbidden,
1432
+ navigateTo2 as navigateTo,
1433
+ notFound,
1434
+ onAfterNavigate,
1435
+ onAppReady,
1436
+ onBeforeNavigate,
1437
+ onBeforeRouteLeave,
1438
+ onBeforeRouteUpdate,
1439
+ onNavigate,
1440
+ onPageActivated,
1441
+ onPageDeactivated,
1442
+ onPrehydrate,
1443
+ permanentRedirect,
1444
+ refreshCookie,
1445
+ setPageLayout,
1446
+ setResponseStatus,
1447
+ showError,
1448
+ transitionName,
1449
+ unauthorized,
1450
+ useAction,
1451
+ useAnnouncer,
1452
+ useApp,
1453
+ useAppUpdate,
1454
+ useAsyncData,
1455
+ useAsyncDataEntry,
1456
+ useBlocker,
1457
+ useCookie,
1458
+ useData,
1459
+ useDeferred,
1460
+ useError,
1461
+ useFetch,
1462
+ useForm,
1463
+ useFormStatus,
1464
+ useHead,
1465
+ useHeadSafe,
1466
+ useHydrated,
1467
+ useId3 as useId,
1468
+ useLayout,
1469
+ useLayoutSegment,
1470
+ useLayoutSegments,
1471
+ useLazyAsyncData,
1472
+ useLazyFetch,
1473
+ useLink,
1474
+ useLinkStatus,
1475
+ useLoadingIndicator,
1476
+ useMatches,
1477
+ useNavigation,
1478
+ useOnline,
1479
+ useOptimistic,
1480
+ useOutletContext,
1481
+ usePoll,
1482
+ usePreventNavigate,
1483
+ usePreviewMode,
1484
+ useReportWebVitals,
1485
+ useRequestEvent,
1486
+ useRequestFetch,
1487
+ useRequestHeader,
1488
+ useRequestHeaders,
1489
+ useRequestURL,
1490
+ useResolvedPath,
1491
+ useResponseHeader,
1492
+ useRoute,
1493
+ useRouteAnnouncer,
1494
+ useRouteData,
1495
+ useRouter,
1496
+ useRuntimeConfig,
1497
+ useRuntimeHook,
1498
+ useSearch,
1499
+ useSeoMeta,
1500
+ useServerHead,
1501
+ useServerSeoMeta,
1502
+ useSiteConfig,
1503
+ useSnapshot,
1504
+ useState2 as useState,
1505
+ useSyncedState
1506
+ };