Skip to content

luigi043/counter-app

Repository files navigation

React Counter App

A professional, feature-rich counter application built with React and Vite. This project demonstrates modern React patterns, clean component architecture, and production-ready code practices.

React Counter App Vite JavaScript

alt text

Features

Core Functionality

  • Counter Operations: Increment, decrement, and reset
  • Value Limits: Minimum (0) and maximum (10) boundaries
  • Visual Feedback: Color-coded display and status messages
  • Disabled States: Buttons disable at limits for better UX

Advanced Features

  • LocalStorage Persistence: Counter value persists across page reloads
  • Keyboard Navigation:
    • Arrow Up → Increment
    • Arrow Down → Decrement
    • Escape → Reset
  • Responsive Design: Fully responsive across all device sizes
  • Accessibility: ARIA labels and keyboard support
  • CSS Animations: Smooth transitions and visual feedback

Project Structure

counter-app/
├── src/
│   ├── components/          # React components
│   │   ├── Counter.jsx     # Main counter logic
│   │   ├── CounterDisplay.jsx
│   │   ├── CounterControls.jsx
│   │   └── CounterMessage.jsx
│   ├── styles/             # CSS styles
│   │   ├── global.css
│   │   └── Counter.css
│   ├── hooks/              # Custom React hooks
│   ├── utils/              # Utility functions
│   ├── App.jsx             # Root component
│   └── main.jsx            # Application entry point
├── public/                 # Static assets
├── package.json
├── vite.config.js
└── README.md

website

Getting Started

Prerequisites

  • Node.js (v16 or higher)
  • npm or yarn

Installation

  1. Clone the repository
git clone https://github.com/yourusername/counter-app.git
cd counter-app
  1. Install dependencies
npm install
# or
yarn install
  1. Start the development server
npm run dev
# or
yarn dev
  1. Open your browser Navigate to http://localhost:5173

Available Scripts

  • npm run dev - Start development server
  • npm run build - Build for production
  • npm run preview - Preview production build
  • npm run lint - Run ESLint
  • npm run format - Format code with Prettier

Project Architecture

Component Breakdown

1. Counter.jsx (Main Component)

  • Manages counter state with useState
  • Implements LocalStorage persistence with useEffect
  • Handles keyboard events
  • Contains all business logic

2. CounterDisplay.jsx (Presentational)

  • Displays current count
  • Applies dynamic styling based on value
  • Pure visual component

3. CounterControls.jsx (Interactive)

  • Contains action buttons
  • Manages button disabled states
  • Handles user interactions

4. CounterMessage.jsx (Feedback)

  • Shows contextual messages
  • Displays limit warnings
  • Conditional rendering

State Management

// Single source of truth
const [count, setCount] = useState(() => {
  // Initialize from LocalStorage
  const savedCount = localStorage.getItem('counter-value')
  return savedCount ? parseInt(savedCount, 10) : 0
})

Persistence Layer

// Save to LocalStorage on every change
useEffect(() => {
  localStorage.setItem('counter-value', count.toString())
}, [count])

Styling System

CSS Variables

:root {
  --primary-color: #2563eb;
  --spacing-md: 1rem;
  --border-radius-md: 0.5rem;
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
}

Responsive Design

  • Mobile-first approach
  • Flexbox and Grid layouts
  • Media queries for tablet/desktop

Animations

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

Code Quality

PropTypes

CounterControls.propTypes = {
  onIncrement: PropTypes.func.isRequired,
  isDecrementDisabled: PropTypes.bool.isRequired,
  // ... other prop validations
}

ESLint Configuration

  • React Hooks rules
  • React refresh plugin
  • Best practices enforcement

Code Organization

  • Clean separation of concerns
  • Single responsibility principle
  • Reusable components

Customization

Changing Limits

Edit MIN_VALUE and MAX_VALUE constants in Counter.jsx:

const MIN_VALUE = 0
const MAX_VALUE = 10

Modifying Colors

Update CSS variables in src/styles/global.css:

:root {
  --primary-color: #your-color;
  --success-color: #your-color;
  --danger-color: #your-color;
}

Adding Features

  1. Multiple Counters: See CounterList.jsx example
  2. Undo/Redo: Implement with stack-based state
  3. Themes: Add theme switching with Context API

Responsive Design

Device Layout Features
Mobile Single column Touch-friendly buttons
Tablet Compact layout Optimized spacing
Desktop Full layout Keyboard shortcuts

Accessibility

  • ARIA Labels: All interactive elements have descriptive labels
  • Keyboard Navigation: Full keyboard support
  • Focus Management: Visible focus indicators
  • Screen Reader: Semantic HTML structure

Performance

  • Code Splitting: Vite automatically handles this
  • Lazy Loading: Ready for future component splitting
  • Optimized Build: Production builds are minified and optimized
  • Efficient Renders: Memoization-ready component structure

Testing (Future Enhancement)

The project is structured to easily add tests:

// Example test structure
describe('Counter', () => {
  it('should increment count', () => {
    // Test implementation
  })
  
  it('should not exceed max value', () => {
    // Test implementation
  })
})

Deployment

Vercel

# Install Vercel CLI
npm i -g vercel

# Deploy
vercel

Netlify

# Build project
npm run build

# Deploy build folder
netlify deploy --prod --dir=dist

GitHub Pages

# Install gh-pages
npm install --save-dev gh-pages

# Add to package.json
"homepage": "https://username.github.io/counter-app",
"scripts": {
  "predeploy": "npm run build",
  "deploy": "gh-pages -d dist"
}

# Deploy
npm run deploy

Learning Resources

This project demonstrates:

React Concepts

  • ✅ State management with useState
  • ✅ Side effects with useEffect
  • ✅ Component composition
  • ✅ Props and PropTypes
  • ✅ Event handling
  • ✅ Conditional rendering

Modern JavaScript

  • ✅ ES6+ features
  • ✅ Arrow functions
  • ✅ Template literals
  • ✅ Destructuring
  • ✅ Modules

Web Development

  • ✅ CSS Variables
  • ✅ Flexbox/Grid
  • ✅ Responsive design
  • ✅ Accessibility
  • ✅ LocalStorage API

Built with to demonstrate professional React development practices


If you find this project helpful, please give it a ⭐️!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published