Scope Cursor Rules to React Components

In a repository with React UI and server code, a file pattern can keep component guidance with the UI files it describes. Start with one observable rule so you can see whether the scope works.

Worked example

A file-scoped rule for React form fields

---
globs: src/components/**/*.tsx
alwaysApply: false
---
Give each input a visible label with matching htmlFor and id.
Use the project's existing field component when one is available.

Steps

  1. Save the sample as .cursor/rules/react-components.mdc. Match the glob to the real component directory before sharing the rule.
  2. In one Cursor chat, attach a matching file such as src/components/SearchPanel.tsx and ask for a search input. Check that the proposed input has an accessible label.
  3. Start a separate chat with only src/server/search.ts in context and check whether the component rule is attached. Inspect the rule status if it appears in both chats.

What to check

  • For the matching file, review the proposed label and input IDs in the diff. A placeholder alone should not be treated as a label.
  • A chat can still contain other project instructions. Test with fresh chats and only the intended files attached when troubleshooting the glob.

See the Cursor tool profile

References