Vue.js Composition API style guide: <script setup>, naming, ref vs reactive, computed and watchers, prop immutability, lifecycle hooks and error handling.
.cursor/rules/vuejs-composition-api-style-guide.mdc. Downloaded files need to be placed in that repository path.--- description: "Vue.js Composition API style guide: <script setup>, naming, ref vs reactive, computed and watchers, prop immutability, lifecycle hooks and error handling." globs: "**/*" alwaysApply: false --- Apply this style guide to Vue 3 code written with the Composition API. ## Principles - Keep components focused on one concern and reusable where it makes sense; prefer readable, consistent code over clever abstractions. - Follow the official Vue style guide's essential rules; this page adds Composition API conventions. ## Naming - Components: PascalCase, multi-word (`UserProfile.vue`). - Variables and functions: camelCase. Composables: `useSomething`. - Module-level constants: `UPPER_SNAKE_CASE`. - Events: camelCase in `defineEmits`, kebab-case or camelCase listeners in templates (both work). ## Typing - Use `<script setup lang="ts">`. Type props and emits with type-based `defineProps<...>()` / `defineEmits<...>()`; annotate function parameters and refs that start empty (`ref<User | null>(null)`); let inference handle the rest. ## Composition API - Use `<script setup>` for single-file components; the plain `setup()` function is for cases that need it (render functions, non-SFC components). - `ref` for state by default; `reactive` only for an object you never reassign or destructure (destructuring a reactive object loses reactivity; use `toRefs`). - `computed` for derived values; `watch` for side effects on specific sources, `watchEffect` when the effect should track what it reads. ## State - Never mutate props; emit an event or use `defineModel()` for two-way bindings. - Mutate local `ref`/`reactive` state directly; there is no special update method. For shared state, change it through store actions so rules live in one place. ## Component structure - Order `<script setup>` contents consistently: imports, props/emits/model, composables and stores, local state, computed, watchers, functions, lifecycle hooks. - Split large components by concern (extract child components or composables) and keep services and utilities in separate files. ## Lifecycle hooks - Use `onMounted`/`onUnmounted` for DOM access, subscriptions and cleanup; register them synchronously in setup. Load data with a watcher on its inputs (or the router/framework data loader) rather than only in `onMounted`. ## Error handling - Wrap async calls in `try`/`catch` and expose the error to the template. Capture unexpected errors centrally with `app.config.errorHandler` (send them to logging) and use `onErrorCaptured` for error boundaries around sections. ## Pitfalls - Many separate watchers for related values: watch them together with an array source or derive with `computed`. - Watchers that only copy state: replace with `computed`. - Missing types on props and emits. - Components with several unrelated responsibilities.
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
coding
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A team's Vue codebase mixes setup() functions and <script setup>, mutates props in child components, destructures reactive() objects, and has no central error reporting.
Using the Vue.js Composition API Style Guide rule, review src/components/OrderForm.vue and src/main.ts.
Review comments converting to <script setup lang="ts"> with typed props/emits, replacing prop mutation with defineModel or an emit, fixing reactive destructuring with ref or toRefs, consolidating watchers or replacing them with computed, and adding app.config.errorHandler in main.ts.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/vuejs-composition-api-style-guide.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
--- description: "Vue.js Composition API Style Guide project conventions" globs: "**/*" alwaysApply: false --- Apply this style guide to Vue 3 code written with the Composition API. ## Principles - Keep components focused on one concern and reusable where it makes sense; prefer readable, consistent code over clever abstractions. - Follow the official Vue style guide's essential rules; this page adds Composition API conventions. ## Naming - Components: PascalCase, multi-word (`UserProfile.vue`). - Variables and functions: camelCase. Composables: `useSomething`. - Module-level constants: `UPPER_SNAKE_CASE`. - Events: camelCase in `defineEmits`, kebab-case or camelCase listeners in templates (both work). ## Typing - Use `<script setup lang="ts">`. Type props and emits with type-based `defineProps<...>()` / `defineEmits<...>()`; annotate function parameters and refs that start empty (`ref<User | null>(null)`); let inference handle the rest. ## Composition API - Use `<script setup>` for single-file components; the plain `setup()` function is for cases that need it (render functions, non-SFC components). - `ref` for state by default; `reactive` only for an object you never reassign or destructure (destructuring a reactive object loses reactivity; use `toRefs`). - `computed` for derived values; `watch` for side effects on specific sources, `watchEffect` when the effect should track what it reads. ## State - Never mutate props; emit an event or use `defineModel()` for two-way bindings. - Mutate local `ref`/`reactive` state directly; there is no special update method. For shared state, change it through store actions so rules live in one place. ## Component structure - Order `<script setup>` contents consistently: imports, props/emits/model, composables and stores, local state, computed, watchers, functions, lifecycle hooks. - Split large components by concern (extract child components or composables) and keep services and utilities in separate files. ## Lifecycle hooks - Use `onMounted`/`onUnmounted` for DOM access, subscriptions and cleanup; register them synchronously in setup. Load data with a watcher on its inputs (or the router/framework data loader) rather than only in `onMounted`. ## Error handling - Wrap async calls in `try`/`catch` and expose the error to the template. Capture unexpected errors centrally with `app.config.errorHandler` (send them to logging) and use `onErrorCaptured` for error boundaries around sections. ## Pitfalls - Many separate watchers for related values: watch them together with an array source or derive with `computed`. - Watchers that only copy state: replace with `computed`. - Missing types on props and emits. - Components with several unrelated responsibilities.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.