Patterns
Nice pattern structure of code
Project Structure
This project follows a combination of Atomic Design principles and feature-based organization to maintain a scalable and maintainable codebase.
Next.js Implementation Guide
To implement this structure in Next.js, we need to adapt it to Next.js conventions while maintaining Atomic Design principles:
project-root/
├── src/
│ ├── app/ # Next.js App Router
│ │ ├── (auth)/ # Auth route group
│ │ │ ├── login/ # Login route
│ │ │ └── signup/ # Signup route
│ │ ├── dashboard/ # Dashboard route
│ │ ├── reports/ # Reports route
│ │ ├── profile/ # Profile route
│ │ ├── layout.tsx # Root layout
│ │ └── page.tsx # Home page
│ ├── components/ # Atomic Design components
│ │ ├── atoms/ # Basic UI elements
│ │ ├── molecules/ # Composite components
│ │ ├── organisms/ # Complex components
│ │ ├── templates/ # Page layouts and templates
│ │ └── layouts/ # General layouts
│ ├── features/ # Feature-based logic
│ │ ├── dashboard/
│ │ │ ├── api/ # Dashboard API handlers
│ │ │ ├── hooks/ # Custom hooks
│ │ │ └── utils/ # Utility functions
│ │ ├── reports/
│ │ └── profile/
│ ├── lib/ # Shared utilities
│ │ ├── api/ # API utilities
│ │ └── db/ # Database utilities
│ ├── styles/ # Global styles
│ └── themes/ # Theme configuration
├── public/ # Static assets
└── next.config.js # Next.js configuration
Key Implementation Details
-
App Router Structure
// src/app/layout.tsx import { ThemeProvider } from '@/themes' export default function RootLayout({ children, }: { children: React.ReactNode }) { return ( <html lang="en"> <ThemeProvider> <body>{children}</body> </ThemeProvider> </html> ) } -
Component Organization
// src/components/atoms/Button/index.tsx export const Button = ({ children, ...props }) => { return <button {...props}>{children}</button> } // src/components/molecules/SearchForm/index.tsx import { Button } from '@/components/atoms/Button' import { Input } from '@/components/atoms/Input' export const SearchForm = () => { return ( <form> <Input /> <Button>Search</Button> </form> ) } -
Feature Implementation
// src/features/dashboard/api/getData.ts export async function getDashboardData() { // API logic } // src/app/dashboard/page.tsx import { DashboardTemplate } from '@/components/templates/Dashboard' import { getDashboardData } from '@/features/dashboard/api/getData' export default async function DashboardPage() { const data = await getDashboardData() return <DashboardTemplate data={data} /> } -
Theme Implementation
// src/themes/baseTheme.ts export const baseTheme = { colors: { primary: '#007AFF', // ... }, } // src/themes/index.ts 'use client' import { ThemeProvider as NextThemeProvider } from 'next-themes'
Next.js-Specific Best Practices
-
Route Organization
- Use route groups
(folderName)for logical grouping - Implement layouts per section using
layout.tsx - Keep page components thin, delegate to templates
- Use route groups
-
Data Fetching
- Use Server Components by default
- Place API calls in feature directories
- Implement loading UI with
loading.tsx - Handle errors with
error.tsx
-
State Management
- Use React Server Components where possible
- Implement client-side state in
features/[feature]/hooks - Keep global state minimal
-
Performance Optimization
- Implement proper component boundaries
- Use Image component for images
- Leverage Next.js built-in optimizations
Directory Structure Overview
project-root/
├── src/ # Source code
│ ├── components/ # Atomic Design components
│ │ ├── atoms/ # Basic UI elements (buttons, inputs, labels, etc.)
│ │ ├── molecules/ # Composite components (search form)
│ │ ├── organisms/ # Complex components (header, footer)
│ │ ├── templates/ # Page layouts and templates (home, login, etc.)
│ │ └── layouts/ # General layouts (main, auth, etc.)
│ ├── features/ # Feature-based organization
│ │ ├── dashboard/ # Dashboard feature
│ │ ├── reports/ # Reports feature
│ │ └── profile/ # Profile feature
│ ├── themes/ # Theme configuration
│ │ ├── baseTheme.ts
│ │ ├── customer1Theme.ts
│ │ ├── customer2Theme.ts
│ │ └── index.ts
│ ├── pages/ # Main application pages
│ │ ├── HomePage.tsx
│ │ ├── LoginPage.tsx
│ │ └── ErrorPage.tsx
│ └── App.tsx # Main application entry point
└── public/ # Static assets
└── assets/ # Images, icons, etc.
Detailed Structure Explanation
Components (Atomic Design)
The components directory follows Atomic Design methodology:
- Atoms: Basic building blocks like buttons, inputs, labels
- Molecules: Simple combinations of atoms (e.g., search form with input and button)
- Organisms: Complex UI components (e.g., header, footer, navigation)
- Templates: Page-level component layouts
- Layouts: Reusable layout structures
Feature-based Organization
The features directory contains feature-specific modules:
- Dashboard: Dashboard-related components and logic
- Reports: Reporting functionality
- Profile: User profile management
Theming System
The themes directory manages application styling:
baseTheme.ts: Default theme configurationcustomer1Theme.ts&customer2Theme.ts: Customer-specific themesindex.ts: Theme selection and management
Pages
The pages directory contains main application views:
HomePage.tsx: Landing pageLoginPage.tsx: Authentication pageErrorPage.tsx: Error handling page
Static Assets
The public/assets directory stores static resources:
- Images
- Icons
- Logos
- Other static files
Best Practices
- Keep components atomic and reusable
- Maintain feature isolation
- Follow consistent naming conventions
- Use proper type definitions
- Keep theme configurations separate
- Centralize routing in pages directory
This structure promotes:
- Code reusability
- Maintainability
- Scalability
- Clear separation of concerns
- Easy theme customization
- Feature isolation