Cursor rules for Vue 3 with <script setup> and TypeScript: typed props and emits (3.3+/3.5 syntax), reactive data loading on prop changes, Pinia setup stores and typed composables.
.cursor/rules/vue-3-composition-api.mdc. Downloaded files need to be placed in that repository path.---
description: "Cursor rules for Vue 3 with <script setup> and TypeScript: typed props and emits (3.3+/3.5 syntax), reactive data loading on prop changes, Pinia setup stores and typed composables."
globs: "**/*"
alwaysApply: false
---
Apply these rules to Vue 3 single-file components written with `<script setup lang="ts">`.
## Component structure
```vue
<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { useUserStore } from '@/stores/user';
import type { User } from '@/types';
// Props: reactive destructure with defaults (Vue 3.5+).
// On Vue 3.3/3.4 use withDefaults(defineProps<Props>(), { showAvatar: true }).
const { userId, showAvatar = true } = defineProps<{
userId: string;
showAvatar?: boolean;
}>();
// Emits: named tuple syntax (Vue 3.3+)
const emit = defineEmits<{
update: [user: User];
delete: [id: string];
}>();
const userStore = useUserStore();
const loading = ref(false);
const error = ref<string | null>(null);
const user = ref<User | null>(null);
const fullName = computed(() =>
user.value ? `${user.value.firstName} ${user.value.lastName}` : ''
);
// Re-fetch when the prop changes, not only on mount
watch(
() => userId,
async (id) => {
loading.value = true;
error.value = null;
try {
user.value = await userStore.fetchUser(id);
} catch (e) {
error.value = e instanceof Error ? e.message : 'Failed to load user';
} finally {
loading.value = false;
}
},
{ immediate: true }
);
function handleUpdate() {
if (user.value) emit('update', user.value);
}
</script>
<template>
<div class="user-card">
<div v-if="loading" class="skeleton" aria-busy="true" />
<p v-else-if="error" role="alert">{{ error }}</p>
<template v-else-if="user">
<img v-if="showAvatar" :src="user.avatar" :alt="fullName" />
<h2>{{ fullName }}</h2>
<button type="button" @click="handleUpdate">Update</button>
</template>
</div>
</template>
<style scoped>
.user-card {
padding: 1rem;
border-radius: 0.5rem;
}
</style>
```
- Watch the prop (`() => userId`) when data depends on it; `onMounted` alone does not re-run when the parent passes a new value.
- Render loading, error and empty/success states.
## Pinia setup store
```ts
// stores/user.ts
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import type { User } from '@/types';
import { api } from '@/lib/api';
export const useUserStore = defineStore('user', () => {
const users = ref(new Map<string, User>());
const currentUserId = ref<string | null>(null);
const currentUser = computed(() =>
currentUserId.value ? users.value.get(currentUserId.value) ?? null : null
);
const userList = computed(() => [...users.value.values()]);
async function fetchUser(id: string): Promise<User> {
const cached = users.value.get(id);
if (cached) return cached;
const user = await api.get<User>(`/users/${id}`);
users.value.set(id, user);
return user;
}
async function updateUser(id: string, data: Partial<User>): Promise<User> {
const user = await api.patch<User>(`/users/${id}`, data);
users.value.set(id, user);
return user;
}
function setCurrentUser(id: string | null) {
currentUserId.value = id;
}
function $reset() {
users.value = new Map();
currentUserId.value = null;
}
// Return every piece of state so devtools, SSR and plugins can see it
return { users, currentUserId, currentUser, userList, fetchUser, updateUser, setCurrentUser, $reset };
});
```
- Setup stores must return all state refs; state that is not returned is invisible to devtools, SSR hydration and plugins.
- Setup stores have no built-in `$reset()`; define one if you need it.
- Destructure store state with `storeToRefs(store)` to keep reactivity; call actions directly on the store.
## Composables
```ts
// composables/useAsync.ts
import { ref, shallowRef, type Ref } from 'vue';
export function useAsync<T>(fn: () => Promise<T>, { immediate = true } = {}) {
const data: Ref<T | null> = shallowRef(null);
const loading = ref(false);
const error = shallowRef<Error | null>(null);
async function execute() {
loading.value = true;
error.value = null;
try {
data.value = await fn();
} catch (e) {
error.value = e instanceof Error ? e : new Error(String(e));
} finally {
loading.value = false;
}
}
if (immediate) void execute();
return { data, loading, error, execute };
}
```
- Name composables `useX`, accept refs or getters for reactive inputs (`MaybeRefOrGetter<T>` with `toValue`), and return refs so callers can destructure.
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
frontend
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A UserCard component fetches in onMounted and does not update when the parent changes userId; its Pinia setup store keeps currentUserId private, so devtools and SSR hydration lose it.
Using the Vue 3 Composition API rule, update src/components/UserCard.vue and src/stores/user.ts (Vue 3.5, Pinia 3, TypeScript).
UserCard uses destructured defineProps with a default, named-tuple defineEmits, a watch on the userId getter with immediate: true, and loading/error/success branches; the store returns currentUserId along with the other state and defines its own $reset; components use storeToRefs when destructuring state.
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/vue-3-composition-api.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 3 Composition API project conventions"
globs: "**/*"
alwaysApply: false
---
Apply these rules to Vue 3 single-file components written with `<script setup lang="ts">`.
## Component structure
```vue
<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { useUserStore } from '@/stores/user';
import type { User } from '@/types';
// Props: reactive destructure with defaults (Vue 3.5+).
// On Vue 3.3/3.4 use withDefaults(defineProps<Props>(), { showAvatar: true }).
const { userId, showAvatar = true } = defineProps<{
userId: string;
showAvatar?: boolean;
}>();
// Emits: named tuple syntax (Vue 3.3+)
const emit = defineEmits<{
update: [user: User];
delete: [id: string];
}>();
const userStore = useUserStore();
const loading = ref(false);
const error = ref<string | null>(null);
const user = ref<User | null>(null);
const fullName = computed(() =>
user.value ? `${user.value.firstName} ${user.value.lastName}` : ''
);
// Re-fetch when the prop changes, not only on mount
watch(
() => userId,
async (id) => {
loading.value = true;
error.value = null;
try {
user.value = await userStore.fetchUser(id);
} catch (e) {
error.value = e instanceof Error ? e.message : 'Failed to load user';
} finally {
loading.value = false;
}
},
{ immediate: true }
);
function handleUpdate() {
if (user.value) emit('update', user.value);
}
</script>
<template>
<div class="user-card">
<div v-if="loading" class="skeleton" aria-busy="true" />
<p v-else-if="error" role="alert">{{ error }}</p>
<template v-else-if="user">
<img v-if="showAvatar" :src="user.avatar" :alt="fullName" />
<h2>{{ fullName }}</h2>
<button type="button" @click="handleUpdate">Update</button>
</template>
</div>
</template>
<style scoped>
.user-card {
padding: 1rem;
border-radius: 0.5rem;
}
</style>
```
- Watch the prop (`() => userId`) when data depends on it; `onMounted` alone does not re-run when the parent passes a new value.
- Render loading, error and empty/success states.
## Pinia setup store
```ts
// stores/user.ts
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import type { User } from '@/types';
import { api } from '@/lib/api';
export const useUserStore = defineStore('user', () => {
const users = ref(new Map<string, User>());
const currentUserId = ref<string | null>(null);
const currentUser = computed(() =>
currentUserId.value ? users.value.get(currentUserId.value) ?? null : null
);
const userList = computed(() => [...users.value.values()]);
async function fetchUser(id: string): Promise<User> {
const cached = users.value.get(id);
if (cached) return cached;
const user = await api.get<User>(`/users/${id}`);
users.value.set(id, user);
return user;
}
async function updateUser(id: string, data: Partial<User>): Promise<User> {
const user = await api.patch<User>(`/users/${id}`, data);
users.value.set(id, user);
return user;
}
function setCurrentUser(id: string | null) {
currentUserId.value = id;
}
function $reset() {
users.value = new Map();
currentUserId.value = null;
}
// Return every piece of state so devtools, SSR and plugins can see it
return { users, currentUserId, currentUser, userList, fetchUser, updateUser, setCurrentUser, $reset };
});
```
- Setup stores must return all state refs; state that is not returned is invisible to devtools, SSR hydration and plugins.
- Setup stores have no built-in `$reset()`; define one if you need it.
- Destructure store state with `storeToRefs(store)` to keep reactivity; call actions directly on the store.
## Composables
```ts
// composables/useAsync.ts
import { ref, shallowRef, type Ref } from 'vue';
export function useAsync<T>(fn: () => Promise<T>, { immediate = true } = {}) {
const data: Ref<T | null> = shallowRef(null);
const loading = ref(false);
const error = shallowRef<Error | null>(null);
async function execute() {
loading.value = true;
error.value = null;
try {
data.value = await fn();
} catch (e) {
error.value = e instanceof Error ? e : new Error(String(e));
} finally {
loading.value = false;
}
}
if (immediate) void execute();
return { data, loading, error, execute };
}
```
- Name composables `useX`, accept refs or getters for reactive inputs (`MaybeRefOrGetter<T>` with `toValue`), and return refs so callers can destructure.
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.