Update Svelte instructions with Svelte 5 best practices#580
Conversation
- Added latest Svelte 5 runes best practices (.by, .pre, untrack) - Added type-safe context with createContext() pattern - Added transitions and animations section - Emphasized effect best practices (prefer over ) - Added function bindings for two-way binding - Restructured document for better readability with logical groupings - Organized into clear sections: Core Concepts, Reactivity, SvelteKit, UI, TypeScript, Production - Consolidated related topics (data loading, forms, tooling) - Updated to align with official Svelte 5 and SvelteKit documentation
There was a problem hiding this comment.
Pull request overview
This PR updates the Svelte instructions file to align with Svelte 5 and SvelteKit best practices, incorporating advanced features and improved organizational structure based on official documentation.
Changes:
- Added advanced Svelte 5 runes (
$derived.by(),$effect.pre(),untrack(),$effect.tracking(),$effect.pending()) and best practices - Reorganized content into logical sections: Core Concepts, Reactivity and State, SvelteKit Patterns, UI and Styling, TypeScript and Tooling, and Production Readiness
- Enhanced guidance on effects, context API, SSR safety, TypeScript prop annotations, and transitions/animations
| ### State Management | ||
| - Use `$state()` for local component state | ||
| - Implement context API with `setContext`/`getContext` for shared state | ||
| - Implement type-safe context with `createContext()` helper over raw `setContext`/`getContext` |
There was a problem hiding this comment.
The createContext() helper is mentioned but lacks explanation. Consider adding a brief example or description of how this pattern differs from raw setContext/getContext to help developers understand when and how to use it.
| - Implement type-safe context with `createContext()` helper over raw `setContext`/`getContext` | |
| - Implement type-safe context with a `createContext()` helper over raw `setContext`/`getContext`. A typical helper wraps Svelte's context APIs, fixes the key and value type in one place, and returns typed setter/getter functions: | |
| ```ts | |
| // context/user-context.ts | |
| // createContext<T>() returns [set, get] tied to a specific key and type | |
| const [setUserContext, getUserContext] = createContext<User>('user'); | |
| // in parent component | |
| setUserContext({ id: '123', name: 'Ada' }); | |
| // in child component | |
| const user = getUserContext(); // strongly typed User |
- Prefer this pattern so components call
getUserContext()instead of repeating the context key and types around rawsetContext/getContext, reducing bugs and improving type safety.
| - Type-safe context with `createContext()` for component tree state sharing | ||
| - Progressive enhancement for forms and interactive features with `use:enhance` | ||
| - Server-side rendering with client-side hydration for optimal performance | ||
| - Function bindings (`bind:value={() => value, setValue}`) for two-way binding |
There was a problem hiding this comment.
The function binding syntax shown appears incomplete or unclear. The pattern bind:value={() => value, setValue} needs clarification—typically function bindings use a different syntax. Consider providing a clearer example or verifying this syntax against Svelte 5 documentation.
| - Function bindings (`bind:value={() => value, setValue}`) for two-way binding | |
| - Function bindings and `bind:value` (e.g. `<input bind:value={value} />`) for two-way binding |
Summary
Updates the Svelte instructions file to reflect the latest Svelte 5 and SvelteKit best practices based on the official documentation.
Changes Made
New Content
$derived.by(),$effect.pre(),untrack(),$effect.tracking()createContext()helper$statemodules)Enhanced Best Practices
$derived()over$effect()for state synchronization{#snippet}block documentationStructural Improvements
Documentation Source
All updates verified against official Svelte 5 and SvelteKit documentation via the Svelte MCP server to ensure accuracy and currency.
File Stats