agentleFS
humanspeakhumanspeak/svelte-markdown/.cursor/rules/svelte.mdc

svelte-markdown / rules

Cursor rule142 starsChanged 2 months agoMITView on GitHub

You are an expert in Svelte 5, SvelteKit, TypeScript, and modern web development. Key Principles - Write concise, technical code with accurate Svelte 5 and SvelteKit examples. - Leverage SvelteKit's server-side rendering (SSR) and static site generation (SSG) capabilities. - Prioritize performance optimization and minimal JavaScript for optimal user experience. - Use descriptive variable names and follow Svelte and SvelteKit conventions. - Organize files using SvelteKit's file-based routing system. - When exporting functions from Svelte components, always use arrow functions.…

  • Installs packages
  • Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.

    Of the Cursor rule files we read, the 1,533 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.

  • In this repository2 other files it gives its agents.

The file

Show its rules and text

We split this svelte.mdc into the rules it gives an agent

43 rules. About 0 are common, 33 are distinctive, and 10 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈33

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 10

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Write concise, technical code with accurate Svelte 5 and SvelteKit examples.

    line 9
  2. Distinctive:

    Use descriptive variable names and follow Svelte and SvelteKit conventions.

    line 12
  3. Very specific:

    When exporting functions from Svelte components, always use arrow functions.

    line 14
  4. Distinctive:

    Write concise, technical TypeScript or JavaScript code with accurate examples.

    line 51
  5. Distinctive:

    Use functional and declarative programming patterns.

    line 52
  6. Distinctive:

    Prefer iteration and modularization over code duplication.

    line 53
  7. Distinctive:

    Follow Svelte's official documentation for setup and configuration: https://svelte.dev/docs

    line 55
  8. Very specific:

    Use lowercase with hyphens for component files (e.g., components/auth-form.svelte).

    line 58
  9. Distinctive:

    Use PascalCase for component names in imports and usage.

    line 59
  10. Distinctive:

    Use camelCase for variables, functions, and props.

    line 60
  11. Distinctive:

    Use TypeScript for all code.

    line 63
  12. Distinctive:

    Avoid enums; use const objects instead.

    line 64
  13. Distinctive:

    Use functional components with TypeScript interfaces for props.

    line 65
  14. Distinctive:

    Use Tailwind CSS for utility-first styling approach.

    line 97
  15. Very specific:

    Import Shadcn components from $lib/components/ui.

    line 99
  16. Distinctive:

    Use Svelte's built-in transition and animation features.

    line 101
  17. Very specific:

    Use background and foreground convention for colors.

    line 104
  18. Distinctive:

    Use the recommended SvelteKit project structure:

    line 129
  19. Distinctive:

    Create .svelte files for Svelte components.

    line 141
  20. Very specific:

    Use .svelte.ts files for component logic and state machines.

    line 142
  21. Distinctive:

    Use Svelte's props for data passing.

    line 144
  22. Distinctive:

    Use classes for complex state management (state machines):

    line 148
  23. Distinctive:

    Use load functions for server-side data fetching and pre-rendering.

    line 184
  24. Distinctive:

    Use the adapter-auto for automatic deployment configuration.

    line 190
  25. Very specific:

    Use {#key} blocks to force re-rendering of components when needed.

    line 194
  26. Very specific:

    Use $effect.tracking() to optimize effect dependencies.

    line 197
  27. Distinctive:

    Use load functions for server-side data fetching.

    line 202
  28. Very specific:

    Create API routes in the src/routes/api/ directory.

    line 204
  29. Distinctive:

    Use SvelteKit's hooks for global API middleware.

    line 206
  30. Distinctive:

    Use Svelte:head component for adding meta information.

    line 209
  31. Distinctive:

    Create reusable SEO components for consistent meta tag management.

    line 211
  32. Distinctive:

    Use progressive enhancement for JavaScript-optional form submissions.

    line 216
  33. Very specific:

    Use Paraglide.js for internationalization: https://inlang.com/m/gerre34r/library-inlang-paraglideJs

    line 219
  34. Very specific:

    Use the t function to translate strings:

    line 222
  35. Distinctive:

    Ensure text scaling and font adjustments for accessibility.

    line 231
  36. Distinctive:

    Ensure proper semantic HTML structure in Svelte components.

    line 234
  37. Distinctive:

    Ensure keyboard navigation support for interactive elements.

    line 236
  38. Distinctive:

    Use Svelte's bind:this for managing focus programmatically.

    line 237
  39. Distinctive:

    Use SvelteKit for full-stack applications with SSR and API routes.

    line 241
  40. Distinctive:

    Use environment variables for configuration management.

    line 243
  41. Distinctive:

    Follow Svelte's best practices for component composition and state management.

    line 244
  42. Distinctive:

    Ensure cross-browser compatibility by testing on multiple platforms.

    line 245
  43. Distinctive:

    Keep your Svelte and SvelteKit versions up to date.

    line 246

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
svelte.mdc
---
description:
globs: *.svelte,*.svelte.ts
alwaysApply: false
---
You are an expert in Svelte 5, SvelteKit, TypeScript, and modern web development.

Key Principles
- Write concise, technical code with accurate Svelte 5 and SvelteKit examples.
- Leverage SvelteKit's server-side rendering (SSR) and static site generation (SSG) capabilities.
- Prioritize performance optimization and minimal JavaScript for optimal user experience.
- Use descriptive variable names and follow Svelte and SvelteKit conventions.
- Organize files using SvelteKit's file-based routing system.
- When exporting functions from Svelte components, always use arrow functions. This ensures lexical `this` binding, avoids confusion, and is the recommended style for Svelte 5 component APIs.
- **When writing JSDoc for exported functions, always provide a realistic, copy-pastable Svelte usage example. Use the following template for guidance:**

```
/**
 * Scrolls the virtual list to the item at the given index.
 *
 * @function scrollToIndex
 * @param index The index of the item to scroll to.
 * @param smoothScroll (default: true) Whether to use smooth scrolling.
 * @param shouldThrowOnBounds (default: true) Whether to throw an error if the index is out of bounds.
 *
 * @example
 * // Svelte usage example:
 * <script lang="ts">
 *   import SvelteVirtualList from '$lib/index.js';
 *   let virtualList;
 *   const items = Array.from({ length: 10000 }, (_, i) => ({ id: i, text: `Item ${i}` }));
 * </script>
 *
 * <button onclick={() => virtualList.scrollToIndex(5000)}>
 *   Scroll to 5000
 * </button>
 * <div style="height: 500px; border: 1px solid pink; padding: 10px; border-radius: 10px;">
 *   <SvelteVirtualList {items} bind:this={virtualList}>
 *     {#snippet renderItem(item)}
 *       <div>{item.text}</div>
 *     {/snippet}
 *   </SvelteVirtualList>
 * </div>
 *
 * @returns {void}
 * @throws {Error} If the index is out of bounds and shouldThrowOnBounds is true
 */
```

Code Style and Structure
- Write concise, technical TypeScript or JavaScript code with accurate examples.
- Use functional and declarative programming patterns; avoid unnecessary classes except for state machines.
- Prefer iteration and modularization over code duplication.
- Structure files: component logic, markup, styles, helpers, types.
- Follow Svelte's official documentation for setup and configuration: https://svelte.dev/docs

Naming Conventions
- Use lowercase with hyphens for component files (e.g., `components/auth-form.svelte`).
- Use PascalCase for component names in imports and usage.
- Use camelCase for variables, functions, and props.

TypeScript Usage
- Use TypeScript for all code; prefer interfaces over types.
- Avoid enums; use const objects instead.
- Use functional components with TypeScript interfaces for props.
- Enable strict mode in TypeScript for better type safety.

Svelte Runes
- `$state`: Declare reactive state
  ```typescript
  let count = $state(0);
  ```
- `$derived`: Compute derived values
  ```typescript
  let doubled = $derived(count * 2);
  ```
- `$effect`: Manage side effects and lifecycle
  ```typescript
  $effect(() => {
    console.log(`Count is now ${count}`);
  });
  ```
- `$props`: Declare component props
  ```typescript
  let { optionalProp = 42, requiredProp } = $props();
  ```
- `$bindable`: Create two-way bindable props
  ```typescript
  let { bindableProp = $bindable() } = $props();
  ```
- `$inspect`: Debug reactive state (development only)
  ```typescript
  $inspect(count);
  ```

UI and Styling
- Use Tailwind CSS for utility-first styling approach.
- Leverage Shadcn components for pre-built, customizable UI elements.
- Import Shadcn components from `$lib/components/ui`.
- Organize Tailwind classes using the `cn()` utility from `$lib/utils`.
- Use Svelte's built-in transition and animation features.

Shadcn Color Conventions
- Use `background` and `foreground` convention for colors.
- Define CSS variables without color space function:
  ```css
  --primary: 222.2 47.4% 11.2%;
  --primary-foreground: 210 40% 98%;
  ```
- Usage example:
  ```svelte
  <div class="bg-primary text-primary-foreground">Hello</div>
  ```
- Key color variables:
  - `--background`, `--foreground`: Default body colors
  - `--muted`, `--muted-foreground`: Muted backgrounds
  - `--card`, `--card-foreground`: Card backgrounds
  - `--popover`, `--popover-foreground`: Popover backgrounds
  - `--border`: Default border color
  - `--input`: Input border color
  - `--primary`, `--primary-foreground`: Primary button colors
  - `--secondary`, `--secondary-foreground`: Secondary button colors
  - `--accent`, `--accent-foreground`: Accent colors
  - `--destructive`, `--destructive-foreground`: Destructive action colors
  - `--ring`: Focus ring color
  - `--radius`: Border radius for components

SvelteKit Project Structure
- Use the recommended SvelteKit project structure:
  ```
  - src/
    - lib/
    - routes/
    - app.html
  - static/
  - svelte.config.js
  - vite.config.js
  ```

Component Development
- Create .svelte files for Svelte components.
- Use .svelte.ts files for component logic and state machines.
- Implement proper component composition and reusability.
- Use Svelte's props for data passing.
- Leverage Svelte's reactive declarations for local state management.

State Management
- Use classes for complex state management (state machines):
  ```typescript
  // counter.svelte.ts
  class Counter {
    count = $state(0);
    incrementor = $state(1);

    increment() {
      this.count += this.incrementor;
    }

    resetCount() {
      this.count = 0;
    }

    resetIncrementor() {
      this.incrementor = 1;
    }
  }

  export const counter = new Counter();
  ```
- Use in components:
  ```svelte
  <script lang="ts">
  import { counter } from './counter.svelte.ts';
  </script>

  <button onclick={() => counter.increment()}>
    Count: {counter.count}
  </button>
  ```

Routing and Pages
- Utilize SvelteKit's file-based routing system in the src/routes/ directory.
- Implement dynamic routes using [slug] syntax.
- Use load functions for server-side data fetching and pre-rendering.
- Implement proper error handling with +error.svelte pages.

Server-Side Rendering (SSR) and Static Site Generation (SSG)
- Leverage SvelteKit's SSR capabilities for dynamic content.
- Implement SSG for static pages using prerender option.
- Use the adapter-auto for automatic deployment configuration.

Performance Optimization
- Leverage Svelte's compile-time optimizations.
- Use `{#key}` blocks to force re-rendering of components when needed.
- Implement code splitting using dynamic imports for large applications.
- Profile and monitor performance using browser developer tools.
- Use `$effect.tracking()` to optimize effect dependencies.
- Minimize use of client-side JavaScript; leverage SvelteKit's SSR and SSG.
- Implement proper lazy loading for images and other assets.

Data Fetching and API Routes
- Use load functions for server-side data fetching.
- Implement proper error handling for data fetching operations.
- Create API routes in the src/routes/api/ directory.
- Implement proper request handling and response formatting in API routes.
- Use SvelteKit's hooks for global API middleware.

SEO and Meta Tags
- Use Svelte:head component for adding meta information.
- Implement canonical URLs for proper SEO.
- Create reusable SEO components for consistent meta tag management.

Forms and Actions
- Utilize SvelteKit's form actions for server-side form handling.
- Implement proper client-side form validation using Svelte's reactive declarations.
- Use progressive enhancement for JavaScript-optional form submissions.

Internationalization (i18n) with Paraglide.js
- Use Paraglide.js for internationalization: https://inlang.com/m/gerre34r/library-inlang-paraglideJs
- Install Paraglide.js: `npm install @inlang/paraglide-js`
- Set up language files in the `languages` directory.
- Use the `t` function to translate strings:
  ```svelte
  <script>
  import { t } from '@inlang/paraglide-js';
  </script>

  <h1>{t('welcome_message')}</h1>
  ```
- Support multiple languages and RTL layouts.
- Ensure text scaling and font adjustments for accessibility.

Accessibility
- Ensure proper semantic HTML structure in Svelte components.
- Implement ARIA attributes where necessary.
- Ensure keyboard navigation support for interactive elements.
- Use Svelte's bind:this for managing focus programmatically.

Key Conventions
1. Embrace Svelte's simplicity and avoid over-engineering solutions.
2. Use SvelteKit for full-stack applications with SSR and API routes.
3. Prioritize Web Vitals (LCP, FID, CLS) for performance optimization.
4. Use environment variables for configuration management.
5. Follow Svelte's best practices for component composition and state management.
6. Ensure cross-browser compatibility by testing on multiple platforms.
7. Keep your Svelte and SvelteKit versions up to date.

Documentation
- Svelte 5 Runes: https://svelte-5-preview.vercel.app/docs/runes
- Svelte Documentation: https://svelte.dev/docs
- SvelteKit Documentation: https://kit.svelte.dev/docs
- Paraglide.js Documentation: https://inlang.com/m/gerre34r/library-inlang-paraglideJs/usage

Refer to Svelte, SvelteKit, and Paraglide.js documentation for detailed information on components, internationalization, and best practices.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.