component-organization
Frontend Component Organization
This is a reference pattern. Learn from the approach, adapt to your context — don't copy verbatim.
Problem: Frontend projects grow into disorganized directories where related files are hard to find and shared code has no clear home.
Solution: Organize by type (components, hooks, lib, types), with subdirectories for grouping related items within each type.
Pattern
frontend/
├── app/ # Framework routing (Next.js app dir, Nuxt pages, etc.)
│ ├── layout.tsx
│ └── page.tsx
├── components/ # All components
│ ├── ui/ # Base primitives (Button, Input, Modal)
│ ├── UserProfile.tsx
│ ├── DashboardChart.tsx
│ └── PostCard.tsx
├── hooks/ # Custom hooks
│ ├── use-auth.ts
│ ├── use-dashboard.ts
│ └── use-debounce.ts
├── lib/ # Utilities, services, business logic
│ ├── api/ # API client, fetch helpers
│ ├── auth/ # Auth context, cookie handling
│ └── utils/ # Pure helper functions
├── queries/ # Data fetching (GraphQL queries, API calls)
├── types/ # Shared type definitions
│ ├── user.ts
│ └── post.ts
└── public/ # Static assets
Key Rules:
- Components live in
components/, grouped by subdirectory when related (e.g.,ui/) - Hooks live in
hooks/, one hook per file - Business logic lives in
lib/, organized by domain subdirectory - Types shared across files live in
types/ - Base UI primitives (buttons, inputs, modals) live in
components/ui/
Why This Pattern?
Benefits:
- Predictable: Know exactly where to look — all hooks in
hooks/, all components incomponents/ - Reusability: Components used across multiple features are easy to find and share
- Scalability: Adding a new hook or component has an obvious home
- Natural for component libraries: Matches how shared UI kits are structured
When to use subdirectories within a type:
components/ui/— base primitives reused everywherelib/auth/— multiple files for one domain (context + utils + types)lib/api/— API client, interceptors, helpers
When to Split
- New file in
components/: When UI is reused or the parent component exceeds ~200 lines - New file in
hooks/: When stateful logic is reused or clutters a component - New subdirectory in
lib/: When a domain has 2+ related files (service + utils, context + helpers) - Move to
components/ui/: When a component is purely presentational with no business logic
Progressive Improvement
If the developer corrects a behavior that this skill should have prevented, suggest a specific amendment to this skill to prevent the same correction in the future.
More from loxosceles/ai-dev
static-frontend-hosting
S3 + CloudFront + Lambda@Edge for low-cost global hosting with edge authentication. Apply when setting up frontend hosting infrastructure.
59github-actions-oidc-aws
Secure GitHub Actions to AWS authentication using OIDC without long-lived credentials. CRITICAL PATTERN. Apply when setting up CI/CD pipelines that deploy to AWS.
48code-review
Multi-perspective code review strategy covering architecture, security, performance, and quality. Follow when reviewing code or analyzing changes.
46frontend-code-quality
Essential guidelines for clear, maintainable frontend code. Follow when writing or reviewing frontend components, composables, or pages.
46command-execution
Guidelines for executing commands and running scripts. Follow when running shell commands, installing packages, or using project scripts.
45cdk-bootstrap-configuration
CDK synth-time configuration pattern without context caching. Apply when working on CDK infrastructure code or adding new configuration parameters.
45