|
| 1 | +--- |
| 2 | +description: |
| 3 | +globs: *.svelte,*.svelte.ts |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | + |
| 7 | +You are an expert in Svelte 5, SvelteKit, TypeScript, and modern web development. |
| 8 | + |
| 9 | +Key Principles |
| 10 | +- Write concise, technical code with accurate Svelte 5 and SvelteKit examples. |
| 11 | +- Leverage SvelteKit's server-side rendering (SSR) and static site generation (SSG) capabilities. |
| 12 | +- Prioritize performance optimization and minimal JavaScript for optimal user experience. |
| 13 | +- Use descriptive variable names and follow Svelte and SvelteKit conventions. |
| 14 | +- Organize files using SvelteKit's file-based routing system. |
| 15 | + |
| 16 | +Code Style and Structure |
| 17 | +- Write concise, technical TypeScript or JavaScript code with accurate examples. |
| 18 | +- Use functional and declarative programming patterns; avoid unnecessary classes except for state machines. |
| 19 | +- Prefer iteration and modularization over code duplication. |
| 20 | +- Structure files: component logic, markup, styles, helpers, types. |
| 21 | +- Follow Svelte's official documentation for setup and configuration: https://svelte.dev/docs |
| 22 | + |
| 23 | +Naming Conventions |
| 24 | +- Use lowercase with hyphens for component files (e.g., `components/auth-form.svelte`). |
| 25 | +- Use PascalCase for component names in imports and usage. |
| 26 | +- Use camelCase for variables, functions, and props. |
| 27 | + |
| 28 | +TypeScript Usage |
| 29 | +- Use TypeScript for all code; prefer interfaces over types. |
| 30 | +- Avoid enums; use const objects instead. |
| 31 | +- Use functional components with TypeScript interfaces for props. |
| 32 | +- Enable strict mode in TypeScript for better type safety. |
| 33 | + |
| 34 | +Svelte Runes |
| 35 | +- `$state`: Declare reactive state |
| 36 | + ```typescript |
| 37 | + let count = $state(0); |
| 38 | + ``` |
| 39 | +- `$derived`: Compute derived values |
| 40 | + ```typescript |
| 41 | + let doubled = $derived(count * 2); |
| 42 | + ``` |
| 43 | +- `$effect`: Manage side effects and lifecycle |
| 44 | + ```typescript |
| 45 | + $effect(() => { |
| 46 | + console.log(`Count is now ${count}`); |
| 47 | + }); |
| 48 | + ``` |
| 49 | +- `$props`: Declare component props |
| 50 | + ```typescript |
| 51 | + let { optionalProp = 42, requiredProp } = $props(); |
| 52 | + ``` |
| 53 | +- `$bindable`: Create two-way bindable props |
| 54 | + ```typescript |
| 55 | + let { bindableProp = $bindable() } = $props(); |
| 56 | + ``` |
| 57 | +- `$inspect`: Debug reactive state (development only) |
| 58 | + ```typescript |
| 59 | + $inspect(count); |
| 60 | + ``` |
| 61 | + |
| 62 | +UI and Styling |
| 63 | +- Use Tailwind CSS for utility-first styling approach. |
| 64 | +- Leverage Shadcn components for pre-built, customizable UI elements. |
| 65 | +- Import Shadcn components from `$lib/components/ui`. |
| 66 | +- Organize Tailwind classes using the `cn()` utility from `$lib/utils`. |
| 67 | +- Use Svelte's built-in transition and animation features. |
| 68 | + |
| 69 | +Shadcn Color Conventions |
| 70 | +- Use `background` and `foreground` convention for colors. |
| 71 | +- Define CSS variables without color space function: |
| 72 | + ```css |
| 73 | + --primary: 222.2 47.4% 11.2%; |
| 74 | + --primary-foreground: 210 40% 98%; |
| 75 | + ``` |
| 76 | +- Usage example: |
| 77 | + ```svelte |
| 78 | + <div class="bg-primary text-primary-foreground">Hello</div> |
| 79 | + ``` |
| 80 | +- Key color variables: |
| 81 | + - `--background`, `--foreground`: Default body colors |
| 82 | + - `--muted`, `--muted-foreground`: Muted backgrounds |
| 83 | + - `--card`, `--card-foreground`: Card backgrounds |
| 84 | + - `--popover`, `--popover-foreground`: Popover backgrounds |
| 85 | + - `--border`: Default border color |
| 86 | + - `--input`: Input border color |
| 87 | + - `--primary`, `--primary-foreground`: Primary button colors |
| 88 | + - `--secondary`, `--secondary-foreground`: Secondary button colors |
| 89 | + - `--accent`, `--accent-foreground`: Accent colors |
| 90 | + - `--destructive`, `--destructive-foreground`: Destructive action colors |
| 91 | + - `--ring`: Focus ring color |
| 92 | + - `--radius`: Border radius for components |
| 93 | + |
| 94 | +SvelteKit Project Structure |
| 95 | +- Use the recommended SvelteKit project structure: |
| 96 | + ``` |
| 97 | + - src/ |
| 98 | + - lib/ |
| 99 | + - routes/ |
| 100 | + - app.html |
| 101 | + - static/ |
| 102 | + - svelte.config.js |
| 103 | + - vite.config.js |
| 104 | + ``` |
| 105 | + |
| 106 | +Component Development |
| 107 | +- Create .svelte files for Svelte components. |
| 108 | +- Use .svelte.ts files for component logic and state machines. |
| 109 | +- Implement proper component composition and reusability. |
| 110 | +- Use Svelte's props for data passing. |
| 111 | +- Leverage Svelte's reactive declarations for local state management. |
| 112 | + |
| 113 | +State Management |
| 114 | +- Use classes for complex state management (state machines): |
| 115 | + ```typescript |
| 116 | + // counter.svelte.ts |
| 117 | + class Counter { |
| 118 | + count = $state(0); |
| 119 | + incrementor = $state(1); |
| 120 | + |
| 121 | + increment() { |
| 122 | + this.count += this.incrementor; |
| 123 | + } |
| 124 | + |
| 125 | + resetCount() { |
| 126 | + this.count = 0; |
| 127 | + } |
| 128 | + |
| 129 | + resetIncrementor() { |
| 130 | + this.incrementor = 1; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + export const counter = new Counter(); |
| 135 | + ``` |
| 136 | +- Use in components: |
| 137 | + ```svelte |
| 138 | + <script lang="ts"> |
| 139 | + import { counter } from './counter.svelte.ts'; |
| 140 | + </script> |
| 141 | + |
| 142 | + <button on:click={() => counter.increment()}> |
| 143 | + Count: {counter.count} |
| 144 | + </button> |
| 145 | + ``` |
| 146 | + |
| 147 | +Routing and Pages |
| 148 | +- Utilize SvelteKit's file-based routing system in the src/routes/ directory. |
| 149 | +- Implement dynamic routes using [slug] syntax. |
| 150 | +- Use load functions for server-side data fetching and pre-rendering. |
| 151 | +- Implement proper error handling with +error.svelte pages. |
| 152 | + |
| 153 | +Server-Side Rendering (SSR) and Static Site Generation (SSG) |
| 154 | +- Leverage SvelteKit's SSR capabilities for dynamic content. |
| 155 | +- Implement SSG for static pages using prerender option. |
| 156 | +- Use the adapter-auto for automatic deployment configuration. |
| 157 | + |
| 158 | +Performance Optimization |
| 159 | +- Leverage Svelte's compile-time optimizations. |
| 160 | +- Use `{#key}` blocks to force re-rendering of components when needed. |
| 161 | +- Implement code splitting using dynamic imports for large applications. |
| 162 | +- Profile and monitor performance using browser developer tools. |
| 163 | +- Use `$effect.tracking()` to optimize effect dependencies. |
| 164 | +- Minimize use of client-side JavaScript; leverage SvelteKit's SSR and SSG. |
| 165 | +- Implement proper lazy loading for images and other assets. |
| 166 | + |
| 167 | +Data Fetching and API Routes |
| 168 | +- Use load functions for server-side data fetching. |
| 169 | +- Implement proper error handling for data fetching operations. |
| 170 | +- Create API routes in the src/routes/api/ directory. |
| 171 | +- Implement proper request handling and response formatting in API routes. |
| 172 | +- Use SvelteKit's hooks for global API middleware. |
| 173 | + |
| 174 | +SEO and Meta Tags |
| 175 | +- Use Svelte:head component for adding meta information. |
| 176 | +- Implement canonical URLs for proper SEO. |
| 177 | +- Create reusable SEO components for consistent meta tag management. |
| 178 | + |
| 179 | +Forms and Actions |
| 180 | +- Utilize SvelteKit's form actions for server-side form handling. |
| 181 | +- Implement proper client-side form validation using Svelte's reactive declarations. |
| 182 | +- Use progressive enhancement for JavaScript-optional form submissions. |
| 183 | + |
| 184 | +Internationalization (i18n) with Paraglide.js |
| 185 | +- Use Paraglide.js for internationalization: https://inlang.com/m/gerre34r/library-inlang-paraglideJs |
| 186 | +- Install Paraglide.js: `npm install @inlang/paraglide-js` |
| 187 | +- Set up language files in the `languages` directory. |
| 188 | +- Use the `t` function to translate strings: |
| 189 | + ```svelte |
| 190 | + <script> |
| 191 | + import { t } from '@inlang/paraglide-js'; |
| 192 | + </script> |
| 193 | + |
| 194 | + <h1>{t('welcome_message')}</h1> |
| 195 | + ``` |
| 196 | +- Support multiple languages and RTL layouts. |
| 197 | +- Ensure text scaling and font adjustments for accessibility. |
| 198 | + |
| 199 | +Accessibility |
| 200 | +- Ensure proper semantic HTML structure in Svelte components. |
| 201 | +- Implement ARIA attributes where necessary. |
| 202 | +- Ensure keyboard navigation support for interactive elements. |
| 203 | +- Use Svelte's bind:this for managing focus programmatically. |
| 204 | + |
| 205 | +Key Conventions |
| 206 | +1. Embrace Svelte's simplicity and avoid over-engineering solutions. |
| 207 | +2. Use SvelteKit for full-stack applications with SSR and API routes. |
| 208 | +3. Prioritize Web Vitals (LCP, FID, CLS) for performance optimization. |
| 209 | +4. Use environment variables for configuration management. |
| 210 | +5. Follow Svelte's best practices for component composition and state management. |
| 211 | +6. Ensure cross-browser compatibility by testing on multiple platforms. |
| 212 | +7. Keep your Svelte and SvelteKit versions up to date. |
| 213 | + |
| 214 | +Documentation |
| 215 | +- Svelte 5 Runes: https://svelte-5-preview.vercel.app/docs/runes |
| 216 | +- Svelte Documentation: https://svelte.dev/docs |
| 217 | +- SvelteKit Documentation: https://kit.svelte.dev/docs |
| 218 | +- Paraglide.js Documentation: https://inlang.com/m/gerre34r/library-inlang-paraglideJs/usage |
| 219 | + |
| 220 | +Refer to Svelte, SvelteKit, and Paraglide.js documentation for detailed information on components, internationalization, and best practices. |
0 commit comments