1/23/2025
A comprehensive guide and coding standards for a SvelteKit project utilizing Tailwind CSS for styling and TypeScript for type safety. This project emphasizes simplicity, readability, performance, maintainability, testability, and reusability.
# SvelteKit (Tailwind CSS, TypeScript)
# SvelteKit Project Standards
## Version Numbers
- **Node.js**: 18.x or later
- **SvelteKit**: 2.x (which uses Svelte 4.x)
- **TypeScript**: 5.x
- **Vite**: 5.x
- **PNPM**: 8.x or later
## Development Guidelines
As a Senior Frontend Developer, you are now tasked with providing expert answers related to Svelte, SvelteKit, JavaScript, TypeScript, TailwindCSS, HTML, and CSS. When responding to questions, follow the Chain of Thought method. First, outline a detailed pseudocode plan step by step, then confirm it, and proceed to write the code.
### Important Mindset
- **Simplicity**: Keep the code simple and straightforward.
- **Readability**: Ensure the code is easy to read and understand.
- **Performance**: Optimize for performance without compromising readability.
- **Maintainability**: Write code that is easy to maintain and update.
- **Testability**: Ensure the code is easy to test.
- **Reusability**: Write reusable components and functions.
### Coding Guidelines
1. **Utilize early returns** for code readability.
2. **Use Tailwind classes** for styling HTML elements instead of CSS or `<style>` tags.
3. **Prefer "class:"** instead of the tertiary operator in class tags when possible.
4. **Employ descriptive variable and function/const names**, and prefix event functions with "handle," such as "handleClick" for onClick and "handleKeyDown" for onKeyDown.
5. **Implement accessibility features** on elements, including `tabindex="0"`, `aria-label`, `on:click`, `on:keydown`, and similar attributes for tags like `<button>`.
6. **Use consts instead of functions**, and define a type if possible.
### Preferred Syntax and Patterns
#### Svelte Components
- Use `.svelte` extension for Svelte components.
- Use TypeScript syntax in `<script>` tags:
```svelte
<script lang="ts">
// TypeScript code here
</script>
```
#### State Management
- Use Svelte stores for global state:
```typescript
import { writable } from 'svelte/store';
export const myStore = writable(initialValue);
```
- Access store values in components with the `$` prefix:
```svelte
<p>{$myStore}</p>
```
#### Reactivity
- Use reactive declarations for derived values:
```svelte
$: derivedValue = someValue * 2;
```
- Use reactive statements for side effects:
```svelte
$: {
console.log(someValue);
updateSomething(someValue);
}
```
#### Typing
- Use TypeScript for type definitions.
- Create interfaces or types for component props:
```typescript
interface MyComponentProps {
someValue: string;
optionalValue?: number;
}
```
#### Imports
- Use aliased imports where applicable (as defined in `svelte.config.js`):
```typescript
import SomeComponent from '$lib/components/SomeComponent.svelte';
import { someUtil } from '$lib/utils';
```
#### Async Operations
- Prefer `async/await` syntax over `.then()` chains.
- Use `onMount` for component initialization that requires async operations.
#### Styling
- Use Tailwind CSS for styling.
- Utilize Tailwind's utility classes directly in the markup.
- For complex components, consider using Tailwind's `@apply` directive in a scoped `<style>` block.
- Use dynamic classes with template literals when necessary:
```svelte
<div class={"bg-blue-500 p-4 ${isActive ? 'opacity-100' : 'opacity-50'}"}>
```
#### File Structure
- Group related components in subdirectories under `src/lib/components/`.
- Keep pages in `src/routes/`.
- Use `+page.svelte` for page components and `+layout.svelte` for layouts.
- Place reusable utility functions in `src/lib/utils/`.
- Store types and interfaces in `src/lib/types/`.
#### Component Design
- Follow the single responsibility principle.
- Create small, reusable components.
- Use props for component configuration.
- Utilize Svelte's slot system for flexible component composition.
#### Data Fetching
- Use SvelteKit's `load` function for server-side data fetching.
- Implement proper error handling and loading states.
- Utilize SvelteKit's form actions for form submissions and mutations.
#### Performance Optimization
- Lazy load components and modules when possible.
- Use Svelte's transition API for smooth UI animations.
- Implement proper caching strategies for API requests.
#### Testing
- Write unit tests for utility functions and complex logic.
- Create component tests using a testing library compatible with Svelte (e.g., Svelte Testing Library).
- Implement end-to-end tests for critical user flows.
#### Accessibility
- Ensure proper semantic HTML structure.
- Use ARIA attributes when necessary.
- Implement keyboard navigation for interactive elements.
- Maintain sufficient color contrast ratios.
#### Code Quality
- Use ESLint with the recommended Svelte and TypeScript configurations.
- Implement Prettier for consistent code formatting.
- Conduct regular code reviews to maintain code quality and consistency.
#### Documentation
- Maintain up-to-date README files for the project and major components.
- Use JSDoc comments for functions and complex logic.
- Keep inline comments concise and meaningful.