I'm using svelte 5 instead of svelte 4 here is an overview of the changes. #### Overview Svelte 5 introduces runes, a set of advanced primitives for controlling reactivity. The runes replace certain non-runes features and provide more explicit control over state and effects. #### $state - **Purpose:** Declare reactive state. - **Usage:** ```javascript ``` - **Replaces:** Top-level `let` declarations in non-runes mode. - **Class Fields:** ```javascript class Todo { done = $state(false); text = $state(); constructor(text) { this.text = text; } } ``` - **Deep Reactivity:** Only plain objects and arrays become deeply reactive. #### $state.raw - **Purpose:** Declare state that cannot be mutated, only reassigned. - **Usage:** ```javascript ``` - **Performance:** Improves with large arrays and objects. #### $state.snapshot - **Purpose:** Take a static snapshot of $state. - **Usage:** ```javascript ``` #### $derived - **Purpose:** Declare derived state. - **Usage:** ```javascript ``` - **Replaces:** Reactive variables computed using `$:` in non-runes mode. #### $derived.by - **Purpose:** Create complex derivations with a function. - **Usage:** ```javascript ``` #### $effect - **Purpose:** Run side-effects when values change. - **Usage:** ```javascript ``` - **Replacements:** $effect replaces a substantial part of `$: {}` blocks triggering side-effects. #### $effect.pre - **Purpose:** Run code before the DOM updates. - **Usage:** ```javascript ``` - **Replaces:** beforeUpdate. #### $effect.tracking - **Purpose:** Check if code is running inside a tracking context. - **Usage:** ```javascript ``` #### $props - **Purpose:** Declare component props. - **Usage:** ```javascript ``` - **Replaces:** export let syntax for declaring props. #### $bindable - **Purpose:** Declare bindable props. - **Usage:** ```javascript ``` #### $inspect - **Purpose:** Equivalent to `console.log` but re-runs when its argument changes. - **Usage:** ```javascript ``` #### $host - **Purpose:** Retrieve the this reference of the custom element. - **Usage:** ```javascript ``` - **Note:** Only available inside custom element components on the client-side. #### Overview of snippets in svelte 5 Snippets, along with render tags, help create reusable chunks of markup inside your components, reducing duplication and enhancing maintainability. #### Snippets Usage - **Definition:** Use the `#snippet` syntax to define reusable markup sections. - **Basic Example:** ```javascript {#snippet figure(image)}
{image.caption}
{image.caption}
{/snippet} ``` - **Invocation:** Render predefined snippets with `@render`: ```javascript {@render figure(image)} ``` - **Destructuring Parameters:** Parameters can be destructured for concise usage: ```javascript {#snippet figure({ src, caption, width, height })}
{caption}
{caption}
{/snippet} ``` #### Snippet Scope - **Scope Rules:** Snippets have lexical scoping rules; they are visible to everything in the same lexical scope: ```javascript
{#snippet x()} {#snippet y()}...{/snippet} {@render y()} {/snippet} {@render y()}
{@render x()} ``` - **Recursive References:** Snippets can self-reference or reference other snippets: ```javascript {#snippet blastoff()} 🚀 {/snippet} {#snippet countdown(n)} {#if n > 0} {n}... {@render countdown(n - 1)} {:else} {@render blastoff()} {/if} {/snippet} {@render countdown(10)} ``` #### Passing Snippets to Components - **Direct Passing as Props:** ```javascript {#snippet header()} fruit qty price total {/snippet} {#snippet row(fruit)} {fruit.name} {fruit.qty} {fruit.price} {fruit.qty * fruit.price} {/snippet} ``` - **Implicit Binding:** ```html
{#snippet header()} {/snippet} {#snippet row(fruit)} {/snippet}
fruit qty price total{fruit.name} {fruit.qty} {fruit.price} {fruit.qty * fruit.price}
``` - **Children Snippet:** Non-snippet content defaults to the `children` snippet: ```html
fruit qty price total
{@render children()}
``` - **Avoid Conflicts:** Do not use a prop named `children` if also providing content inside the component. #### Typing Snippets - **TypeScript Integration:** ```typescript ``` - **Generics for Improved Typing:** ```typescript ``` #### Creating Snippets Programmatically - **Advanced Use:** Create snippets programmatically using `createRawSnippet` where necessary. #### Snippets and Slots - **Mixing with Slots:** Slots are deprecated but still work. Snippets provide more flexibility and power. - **Custom Elements:** Continue using `` for custom elements as usual. Sure! Here are the succinct instructions for handling Event Handlers in Svelte 5, tailored for the AI-integrated code editor to help it understand and utilize these features effectively. --- ### Custom Instructions for Svelte 5 Event Handlers in Cursor AI #### Overview In Svelte 5, event handlers are treated as properties, simplifying their use and integrating them more closely with the rest of the properties in the component. #### Basic Event Handlers - **Declaration:** Use properties to attach event handlers. ```javascript ``` - **Shorthand Syntax:** ```javascript ``` - **Deprecation:** The traditional `on:` directive is deprecated. #### Component Events - **Replacing createEventDispatcher:** Components should accept callback props instead of using `createEventDispatcher`. ```javascript { size += power; if (size > 75) burst = true; }} deflate={(power) => { if (size > 0) size -= power; }} /> {#if burst} 💥 {:else} 🎈 {/if} ``` #### Bubbling Events - **Accept Callback Props:** ```javascript ``` - **Spreading Props:** ```javascript ``` #### Event Modifiers - **Avoiding Modifiers:** Modifiers like `|once`, `|preventDefault`, etc., are not supported. Use wrapper functions instead. - **Example Wrapper Functions:** ```javascript ``` - **Special Modifiers:** For `capture`: ```javascript ``` #### Multiple Event Handlers - **Combining Handlers:** Instead of using multiple handlers, combine them into one. ```javascript ``` --- ### examples old vs new #### Counter Example - **Svelte 4 vs. Svelte 5:** - **Before:** ```html ``` - **After:** ```html ``` #### Tracking Dependencies - **Svelte 4 vs. Svelte 5:** - **Before:** ```html

{a} + {b} = {sum}

``` - **After:** ```html

{a} + {b} = {sum}

``` #### Untracking Dependencies - **Svelte 4 vs. Svelte 5:** - **Before:** ```html

{a} + {b} = {sum}

``` - **After:** ```html

{a} + {b} = {sum}

``` #### Simple Component Props - **Svelte 5:** ```html {count} ``` #### Advanced Component Props - **Svelte 5:** ```html
    {JSON.stringify(others)}
  
``` #### Autoscroll Example - **Svelte 4 vs. Svelte 5:** - **Before:** ```html
{#each messages as message}

{message}

{/each}
``` - **After:** ```html
{#each messages as message}

{message}

{/each}
``` #### Forwarding Events - **Svelte 5:** ```html ``` #### Passing UI Content to a Component - **Passing content using snippets:** ```html ``` I'm also using sveltekit 2 which also has some changes I'd like you to keep in mind ### Redirect and Error Handling In SvelteKit 2, it is no longer necessary to throw the results of `error(...)` and `redirect(...)`. Simply calling them is sufficient. **SvelteKit 1:** ```javascript import { error } from '@sveltejs/kit'; function load() { throw error(500, 'something went wrong'); } ``` **SvelteKit 2:** ```javascript import { error } from '@sveltejs/kit'; function load() { error(500, 'something went wrong'); } ``` **Distinguish Errors:** Use `isHttpError` and `isRedirect` to differentiate known errors from unexpected ones. ```javascript import { isHttpError, isRedirect } from '@sveltejs/kit'; try { // some code } catch (err) { if (isHttpError(err) || isRedirect(err)) { // handle error } } ``` ### Cookie Path Requirement Cookies now require a specified path when set, deleted, or serialized. **SvelteKit 1:** ```javascript export function load({ cookies }) { cookies.set(name, value); return { response }; } ``` **SvelteKit 2:** ```javascript export function load({ cookies }) { cookies.set(name, value, { path: '/' }); return { response }; } ``` ### Top-Level Promise Handling Promises in `load` functions are no longer awaited automatically. **Single Promise:** **SvelteKit 1:** ```javascript export function load({ fetch }) { return { response: fetch(...).then(r => r.json()) }; } ``` **SvelteKit 2:** ```javascript export async function load({ fetch }) { const response = await fetch(...).then(r => r.json()); return { response }; } ``` **Multiple Promises:** **SvelteKit 1:** ```javascript export function load({ fetch }) { return { a: fetch(...).then(r => r.json()), b: fetch(...).then(r => r.json()) }; } ``` **SvelteKit 2:** ```javascript export async function load({ fetch }) { const [a, b] = await Promise.all([ fetch(...).then(r => r.json()), fetch(...).then(r => r.json()) ]); return { a, b }; } ``` ### `goto` Changes `goto(...)` no longer accepts external URLs. Use `window.location.href = url` for external navigation. ### Relative Paths Default Paths are now relative by default, ensuring portability across different environments. The `paths.relative` config option manages this behavior. ### Deprecated Settings and Functions - **Server Fetches** are no longer trackable. - **`preloadCode` Arguments:** Must be prefixed with the base path. - **`resolvePath` Replacement:** Use `resolveRoute` instead. ```javascript import { resolveRoute } from '$app/paths'; const path = resolveRoute('/blog/[slug]', { slug: 'hello' }); ``` ### Improved Error Handling Errors trigger the `handleError` hook with `status` and `message` properties for better discernment. ### Dynamic Environment Variables Dynamic environment variables cannot be used during prerendering. Use static modules instead. ### `use:enhance` Callback Changes The properties `form` and `data` have been removed from `use:enhance` callbacks, replaced by `formElement` and `formData`. ### Forms with File Inputs Forms containing `` must use `enctype="multipart/form-data"`. With these adjusted guidelines, your AI can now generate SvelteKit 2 code accurately while considering the migration changes.