Index of every page on this site: llms.txt
All skills
spences10 avatar

/svelte-components

@5fe4989
Run it yourself

Svelte component patterns. Use for web components, component libraries (Bits UI, Ark UI, Melt UI), form patterns, or third-party integration.

Use this Skill: https://skilld.dev/gh/spences10/svelte-skills-kit/svelte-components

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesform-patterns.md

≈764 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Form Patterns

Form Attribute Trick

When you can't nest a form (e.g., inside tables), use the form attribute:

<form id="add-item" action="?/add" method="POST"></form>

<table>
	<tbody>
		{#each items as item}
			<tr>
				<td>{item.name}</td>
				<td>{item.price}</td>
			</tr>
		{/each}
		<tr>
			<td><input form="add-item" name="name" required /></td>
			<td
				><input
					form="add-item"
					name="price"
					type="number"
					required
				/></td
			>
			<td><button form="add-item">Add</button></td>
		</tr>
	</tbody>
</table>

Benefits:

  • Form can be anywhere in the document
  • Submit with Enter works
  • FormData collection works
  • Accessible by default

Default Values and Reset

Forms support defaultValue for easy resets:

<script>
	let name = $state('');
</script>

<form onreset={() => (name = '')}>
	<input bind:value={name} defaultValue="" />
	<button type="submit">Save</button>
	<button type="reset">Reset</button>
</form>

Progressive Enhancement

<script>
	import { enhance } from '$app/forms';

	let submitting = $state(false);
</script>

<form
	method="POST"
	use:enhance={() => {
		submitting = true;
		return async ({ update }) => {
			await update();
			submitting = false;
		};
	}}
>
	<input name="email" type="email" required />
	<button disabled={submitting}>
		{submitting ? 'Saving...' : 'Save'}
	</button>
</form>

Form Validation with Valibot

// +page.server.ts
import * as v from 'valibot';
import { fail } from '@sveltejs/kit';

const ContactSchema = v.object({
	email: v.pipe(v.string(), v.email()),
	message: v.pipe(v.string(), v.minLength(10)),
});

export const actions = {
	default: async ({ request }) => {
		const formData = await request.formData();
		const data = Object.fromEntries(formData);

		const result = v.safeParse(ContactSchema, data);

		if (!result.success) {
			return fail(400, {
				data,
				errors: v.flatten(result.issues),
			});
		}

		// Process valid data
		await saveContact(result.output);
	},
};
<!-- +page.svelte -->
<script>
	let { form } = $props();
</script>

<form method="POST">
	<label>
		Email
		<input
			name="email"
			type="email"
			value={form?.data?.email ?? ''}
		/>
		{#if form?.errors?.nested?.email}
			<span class="error">{form.errors.nested.email[0]}</span>
		{/if}
	</label>

	<label>
		Message
		<textarea name="message">{form?.data?.message ?? ''}</textarea>
		{#if form?.errors?.nested?.message}
			<span class="error">{form.errors.nested.message[0]}</span>
		{/if}
	</label>

	<button>Send</button>
</form>

Multiple Forms on One Page

<form action="?/subscribe" method="POST">
	<input name="email" type="email" />
	<button>Subscribe</button>
</form>

<form action="?/contact" method="POST">
	<input name="message" />
	<button>Send</button>
</form>
// +page.server.ts
export const actions = {
	subscribe: async ({ request }) => {
		// Handle subscription
	},
	contact: async ({ request }) => {
		// Handle contact
	},
};

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning2mo4 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    This skill provides educational content and best practices for building web components and forms using Svelte. It includes references to popular, well-maintained UI libraries like Bits UI, Ark UI, and Melt UI. No security risks were identified.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

  • Runlayer9mo

    4/4 files flagged

Signed by skilld at 5fe4989. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago

README badge

README badge for spences10/svelte-skills-kit/svelte-components