agentleFS
girijashankarjgirijashankarj/cursor-handbook/.cursor/rules/frontend/component-patterns.mdc

cursor-handbook / frontend

Cursor rule31 starsChanged 3 months agoMITView on GitHub

Frontend component architecture and patterns. Apply when creating or editing UI components.

The file

Show its rules and text

We split this component-patterns.mdc into the rules it gives an agent

15 rules. About 0 are common, 11 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈11

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use functional components with hooks — no class components

    line 37
  2. Very specific:

    Use React.FC or explicit return types

    line 38
  3. Distinctive:

    Use descriptive prop names

    line 43
  4. Distinctive:

    Document complex props with JSDoc comments

    line 45
  5. Very specific:

    Use React.memo for pure presentational components

    line 87
  6. Distinctive:

    Use CSS Modules, Tailwind CSS, or styled-components — pick one, be consistent

    line 92
  7. Distinctive:

    No inline styles except for dynamic values

    line 93
  8. Distinctive:

    Follow design system tokens for colors, spacing, typography

    line 94
  9. Very specific:

    Don't use any for props — define explicit interface

    line 99
  10. Distinctive:

    Don't fetch in useEffect without cleanup — cancel on unmount

    line 108
  11. Distinctive:

    Don't put server data in global state — use React Query

    line 109
  12. Distinctive:

    Don't use index as key when list can reorder — use stable id

    line 110
  13. Distinctive:

    Don't inline object/array in JSX — causes unnecessary re-renders

    line 111
  14. Distinctive:

    List virtualization: Use when >100 items; set item height for scroll

    line 126
  15. Very specific:

    Use @component-creation skill for new components

    line 129

≈ “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
component-patterns.mdc
---
description: "Frontend component architecture and patterns. Apply when creating or editing UI components."
alwaysApply: false
globs: "**/*.tsx,**/*.jsx,**/components/**/*,**/Components/**/*"
---

# Frontend Component Patterns

## Component Structure
```
src/components/
├── ui/                    # Atomic/base components
│   ├── Button/
│   │   ├── Button.tsx
│   │   ├── Button.test.tsx
│   │   ├── Button.stories.tsx
│   │   └── index.ts
│   ├── Input/
│   └── Modal/
├── features/              # Feature-specific components
│   ├── Auth/
│   ├── Dashboard/
│   └── Settings/
├── layout/                # Layout components
│   ├── Header/
│   ├── Sidebar/
│   └── Footer/
└── shared/                # Shared/utility components
    ├── ErrorBoundary/
    ├── LoadingSpinner/
    └── ProtectedRoute/
```

## Component Guidelines

### Functional Components Only
- Use functional components with hooks — no class components
- Use `React.FC` or explicit return types
- Destructure props at the function signature level

### Props
- Define props interface above the component
- Use descriptive prop names
- Provide default values where appropriate
- Document complex props with JSDoc comments

```tsx
interface ButtonProps {
  /** Button label text */
  label: string;
  /** Visual style variant */
  variant?: 'primary' | 'secondary' | 'danger';
  /** Disabled state */
  disabled?: boolean;
  /** Click handler */
  onClick: () => void;
}

export const Button: React.FC<ButtonProps> = ({
  label,
  variant = 'primary',
  disabled = false,
  onClick,
}) => {
  return (
    <button
      className={`btn btn-${variant}`}
      disabled={disabled}
      onClick={onClick}
    >
      {label}
    </button>
  );
};
```

### State Management
- Local state: `useState` for component-scoped state
- Shared state: Context API or state library (Zustand, Redux Toolkit)
- Server state: React Query / TanStack Query
- Form state: React Hook Form or Formik
- **NEVER** prop-drill more than 2 levels — use context or state management

### Performance
- Memoize expensive computations with `useMemo`
- Memoize callbacks with `useCallback` when passing to child components
- Use `React.memo` for pure presentational components
- Lazy load routes and heavy components with `React.lazy`
- Virtualize long lists (react-window or react-virtuoso)

## Styling Rules
- Use CSS Modules, Tailwind CSS, or styled-components — pick one, be consistent
- No inline styles except for dynamic values
- Follow design system tokens for colors, spacing, typography
- Mobile-first responsive design
- Support dark mode via CSS variables or theme context

## Anti-patterns
- Don't use `any` for props — define explicit interface

```tsx
// BAD
const Button = (props: any) => ...

// GOOD
interface ButtonProps { label: string; onClick: () => void; }
```
- Don't fetch in useEffect without cleanup — cancel on unmount
- Don't put server data in global state — use React Query
- Don't use index as key when list can reorder — use stable id
- Don't inline object/array in JSX — causes unnecessary re-renders

## Examples
```tsx
// BAD: New object every render
<Child onClick={() => ({})} />

// GOOD: Stable callback
const noop = useCallback(() => {}, []);
<Child onClick={noop} />
```

## Edge Cases
- **Loading states**: Skeleton > spinner > empty; handle error state
- **Form validation**: Show errors on blur or submit; don't block typing
- **List virtualization**: Use when >100 items; set item height for scroll

## Related
- Use `@component-creation` skill for new components
- See `@accessibility` for a11y requirements

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.