--- name: svelte description: Svelte and SvelteKit development with built-in reactivity, stores, SSR/SSG, and modern web patterns. allowed-tools: Read, Write, Edit, Bash, Glob, Grep --- # Svelte Skill Expert assistance for building applications with Svelte and SvelteKit. ## Capabilities - Create Svelte components with reactive declarations - Implement Svelte stores for state management - Configure SvelteKit for SSR/SSG/SPA - Build API routes and form actions - Set up load functions for data fetching - Implement transitions and animations ## Usage Invoke this skill when you need to: - Build Svelte/SvelteKit applications - Create reactive components - Implement server-side rendering - Set up form handling with actions - Configure deployment ## Inputs | Parameter | Type | Required | Description | |-----------|------|----------|-------------| | componentName | string | Yes | Component name | | sveltekit | boolean | No | Using SvelteKit (default: true) | | typescript | boolean | No | Use TypeScript (default: true) | | ssr | boolean | No | Enable SSR (default: true) | ## Component Patterns ### Basic Component ```svelte
{#if user.avatar} {user.name} {:else} {initials} {/if}
{#if isEditing} e.key === 'Enter' && save()} /> {:else}

{user.name}

{user.email}

{#if editable} {/if} {/if}
``` ### Svelte Stores ```typescript // src/lib/stores/user.ts import { writable, derived, readable } from 'svelte/store'; interface User { id: string; name: string; email: string; } // Writable store function createUserStore() { const { subscribe, set, update } = writable(null); return { subscribe, login: async (email: string, password: string) => { const response = await fetch('/api/auth/login', { method: 'POST', body: JSON.stringify({ email, password }), }); const user = await response.json(); set(user); }, logout: () => set(null), updateProfile: (data: Partial) => update(user => user ? { ...user, ...data } : null), }; } export const user = createUserStore(); // Derived store export const isAuthenticated = derived(user, $user => !!$user); // Readable store (external data) export const time = readable(new Date(), (set) => { const interval = setInterval(() => set(new Date()), 1000); return () => clearInterval(interval); }); ``` ### SvelteKit Page with Load ```typescript // src/routes/users/+page.server.ts import type { PageServerLoad, Actions } from './$types'; import { fail, redirect } from '@sveltejs/kit'; import { db } from '$lib/server/db'; export const load: PageServerLoad = async ({ locals }) => { if (!locals.user) { throw redirect(302, '/login'); } const users = await db.user.findMany(); return { users, }; }; export const actions: Actions = { create: async ({ request }) => { const data = await request.formData(); const name = data.get('name') as string; const email = data.get('email') as string; if (!name || !email) { return fail(400, { error: 'Name and email required' }); } const user = await db.user.create({ data: { name, email }, }); return { success: true, user }; }, delete: async ({ request }) => { const data = await request.formData(); const id = data.get('id') as string; await db.user.delete({ where: { id } }); return { success: true }; }, }; ``` ```svelte

Users

{#if form?.error}

{form.error}

{/if}
``` ### API Routes ```typescript // src/routes/api/users/+server.ts import { json, error } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; import { db } from '$lib/server/db'; export const GET: RequestHandler = async ({ url }) => { const search = url.searchParams.get('search'); const users = await db.user.findMany({ where: search ? { name: { contains: search } } : undefined, }); return json(users); }; export const POST: RequestHandler = async ({ request }) => { const body = await request.json(); if (!body.name || !body.email) { throw error(400, 'Name and email required'); } const user = await db.user.create({ data: body, }); return json(user, { status: 201 }); }; ``` ### Transitions and Animations ```svelte {#if showModal} {/if} ``` ## Best Practices - Use reactive declarations ($:) for derived values - Leverage SvelteKit's load functions for data - Use form actions for mutations - Keep stores simple and focused - Use TypeScript for type safety ## Target Processes - svelte-application-development - sveltekit-full-stack - jamstack-development - frontend-architecture