@void/svelte 0.22.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -37,7 +37,7 @@ Main runtime APIs live at `@void/svelte`:
37
37
  - `useParams()`
38
38
  - `useNavigation()`
39
39
  - `useShared()`
40
- - `useForm()`
40
+ - `Form` and `useForm()`
41
41
  - `useIslandForm()`
42
42
  - `action()`
43
43
 
@@ -109,6 +109,12 @@ Export `ssr = false` from a page's `.server.ts` file to skip server-rendered
109
109
  component HTML for that route while still running its loader on the first
110
110
  request.
111
111
 
112
+ ## Form
113
+
114
+ Use `Form` with `useForm()` to display action and validation errors automatically. It preserves controlled input after a failed submission and announces errors accessibly. Structured usage-limit responses display the application's message and an optional retry time. Submissions are blocked while pending and before that deadline; inputs remain editable and requests are never retried automatically.
115
+
116
+ `method` selects `post` (the default), `put`, `patch`, or `delete`. Use `renderError` to customize action-error presentation (a snippet in Svelte). Native forms remain available when you manage error presentation yourself.
117
+
112
118
  ## Tests
113
119
 
114
120
  - `test/integration/svelte-pages-client.test.ts`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@void/svelte",
3
- "version": "0.22.0",
3
+ "version": "0.23.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/voidzero-dev/void.git",
@@ -37,12 +37,12 @@
37
37
  "svelte": "^5.57.1",
38
38
  "vite": "npm:@voidzero-dev/vite-plus-core@1.0.0",
39
39
  "vite-plus": "1.0.0",
40
- "void": "0.22.0"
40
+ "void": "0.23.0"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "svelte": "^5.0.0",
44
44
  "vite": "^8.0.0",
45
- "void": "0.22.0"
45
+ "void": "0.23.0"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "vp pack",
@@ -0,0 +1,28 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte';
3
+ import { formErrorMessage, formRetryMessage, formSubmissionBlocked, scheduleFormRetry, type FormModel, type FormMethod, type VoidActionError } from 'void/pages-client';
4
+ let { form, method='post', renderError, children, ...rest }: {
5
+ form: FormModel;
6
+ method?: FormMethod;
7
+ renderError?: Snippet<[VoidActionError]>;
8
+ children?: Snippet;
9
+ [key: string]: unknown;
10
+ } = $props();
11
+ let now=$state(Date.now());
12
+ const message=$derived(formErrorMessage(form));
13
+ const retry=$derived(formRetryMessage(form.error));
14
+ $effect(()=>scheduleFormRetry(form.error,()=>{now=Date.now();}));
15
+ </script>
16
+
17
+ <form {...rest} aria-busy={form.pending} onsubmit={async event=>{
18
+ event.preventDefault();
19
+ if (!formSubmissionBlocked(form,now)) await form[method]();
20
+ }}>
21
+ {#if message}
22
+ <div role="alert" data-void-form-error>
23
+ {#if form.error && renderError}{@render renderError(form.error)}{:else}{message}{/if}
24
+ {#if retry}<p>{retry}</p>{/if}
25
+ </div>
26
+ {/if}
27
+ {@render children?.()}
28
+ </form>
@@ -1,50 +1,6 @@
1
- import { submitAction, type ActionResult, type VoidRouter } from 'void/pages-client';
2
- import type { ActionUrl, ResolveActionBody, ResolveActionParams } from 'void/routes';
1
+ import { createActionClient, type ActionClient } from 'void/pages-client';
3
2
 
4
- type ActionMethod = 'POST' | 'PUT' | 'PATCH' | 'DELETE';
3
+ const client = createActionClient();
5
4
 
6
- type HasParams<U extends string> = [ResolveActionParams<U>] extends [never] ? false : true;
7
-
8
- type ActionOptions<U extends string> = {
9
- data?: ResolveActionBody<U>;
10
- method?: ActionMethod;
11
- } & (HasParams<U> extends true ? { params: ResolveActionParams<U> } : { params?: never });
12
-
13
- let _router: VoidRouter | null = null;
14
-
15
- /** Called during app initialization to store the router instance */
16
- export function setActionRouter(router: VoidRouter): void {
17
- _router = router;
18
- }
19
-
20
- /** Programmatic one-shot page action call with Inertia page update */
21
- export async function action<U extends ActionUrl & string>(
22
- url: U,
23
- ...args: HasParams<U> extends true ? [options: ActionOptions<U>] : [options?: ActionOptions<U>]
24
- ): Promise<ActionResult> {
25
- if (!_router) {
26
- throw new Error('action(): called before router initialization.');
27
- }
28
-
29
- const options = args[0] as ActionOptions<U> | undefined;
30
- const data = options?.data;
31
- const method = options?.method || 'POST';
32
-
33
- let resolvedUrl: string = url;
34
- let actionQuery = '';
35
-
36
- const qIdx = resolvedUrl.indexOf('?');
37
- if (qIdx !== -1) {
38
- actionQuery = resolvedUrl.slice(qIdx);
39
- resolvedUrl = resolvedUrl.slice(0, qIdx);
40
- }
41
-
42
- if (options?.params) {
43
- for (const [key, value] of Object.entries(options.params)) {
44
- resolvedUrl = resolvedUrl.replace(`:${key}`, encodeURIComponent(value as string));
45
- }
46
- }
47
-
48
- resolvedUrl = resolvedUrl + actionQuery;
49
- return submitAction(_router, resolvedUrl, { method, data });
50
- }
5
+ export const action: ActionClient['action'] = client.action;
6
+ export const setActionRouter: ActionClient['setActionRouter'] = client.setActionRouter;
@@ -1,3 +1,4 @@
1
+ export { default as Form } from './Form.svelte';
1
2
  export { default as Link } from './Link.svelte';
2
3
  export { useRouter } from './use-router.ts';
3
4
  export { useParams } from './use-params.ts';