Skip to content

Loubal70/AlpinePerfFormation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ AlpineJS Component Architecture Project

A modern, lightweight web application built with Alpine.js, Vite, and TailwindCSS that demonstrates advanced component architecture patterns, lazy loading, and inter-component communication.


πŸ› οΈ Technologies Used

Technology Purpose Version
Alpine.js πŸ”οΈ Reactive frontend framework 3.14.9
Vite ⚑ Build tool and dev server 6.3.5
TailwindCSS 🎨 Utility-first CSS framework 4.1.10
Alpine Intersect πŸ‘οΈ Intersection Observer plugin 3.14.9
HTML Inject Plugin πŸ”§ Component template injection 1.1.2

πŸ“ Project Structure

alpineJS/
β”œβ”€β”€ πŸ“„ index.html                  # Main entry point
β”œβ”€β”€ πŸ“¦ package.json               # Dependencies & scripts
β”œβ”€β”€ βš™οΈ vite.config.mjs            # Vite configuration
└── src/
    β”œβ”€β”€ 🎨 assets/
    β”‚   β”œβ”€β”€ css/style.css          # Global styles
    β”‚   └── js/
    β”‚       β”œβ”€β”€ app.js             # Main app initialization
    β”‚       └── components/        # Alpine.js components
    β”‚           β”œβ”€β”€ slider.js      # Image slider component
    β”‚           β”œβ”€β”€ sliderDisplay.js # Slider status display
    β”‚           └── lightbox.js    # Image lightbox modal
    β”œβ”€β”€ πŸ“Š data/
    β”‚   └── slides.json           # Slider content data
    └── 🧩 parts/
        β”œβ”€β”€ header.html           # Header template
        β”œβ”€β”€ slider.html           # Slider template
        └── lightbox.html         # Lightbox template

🧩 Component System Architecture

πŸ”§ Component Registration

All components are registered in src/assets/js/app.js:

import Alpine from 'alpinejs'
import Slider from './components/slider.js'
import SliderDisplay from './components/sliderDisplay.js'
import Lightbox from './components/lightbox.js'

// Register components globally
Alpine.data('slider', Slider)
Alpine.data('sliderDisplay', SliderDisplay)
Alpine.data('lightbox', Lightbox)

πŸ“€ Component Factory Pattern

Each component follows a factory function pattern that returns an Alpine.js data object:

export default function ComponentName(parameters) {
    return {
        // Reactive properties
        property: initialValue,
        
        // Lifecycle method
        init() {
            // Component initialization logic
        },
        
        // Component methods
        method() {
            // Component functionality
        }
    };
}

πŸ—οΈ Template Injection System

Templates are modularized using the HTML Inject Plugin:

  • βœ… Separation of Concerns: HTML structure separated from logic
  • βœ… Reusability: Components can be used across different pages
  • βœ… Maintainability: Each component has its own template file
<!-- index.html -->
<load src="src/parts/slider.html" />
<load src="src/parts/lightbox.html" />

πŸš€ Lazy Loading System

πŸ–ΌοΈ Image Lazy Loading

The project implements intelligent image lazy loading using Alpine's Intersect plugin:

<img x-intersect.once="$el.src = slide.image"
     :alt="slide.title"
     class="w-full h-full object-cover">

How it works:

  1. πŸ‘€ Intersection Observer: Monitors when image containers enter viewport
  2. ⚑ On-Demand Loading: Images load only when visible
  3. 🎯 Once Modifier: Each image loads only once to prevent re-triggering
  4. πŸ“ˆ Performance Boost: Reduces initial page load time and bandwidth

πŸ“Š Data Lazy Loading

Slider data is fetched asynchronously:

async loadSlides() {
    try {
        const response = await fetch(sliderJson);
        this.slides = await response.json();
        this.isLoading = false;
    } catch (error) {
        console.error('Error loading slides:', error);
        this.isLoading = false;
    }
}

Benefits:

  • ⏱️ Non-blocking: UI remains responsive during data loading
  • πŸ”„ Loading States: Visual feedback with loading indicators
  • πŸ›‘οΈ Error Handling: Graceful degradation on fetch failures

πŸ“‘ Dispatch & Inter-Component Communication

🎯 Event-Driven Architecture

The project uses Alpine's custom event system for component communication:

πŸ“€ Event Dispatching (Publisher)

// Slider component dispatches events
this.$dispatch('slider-ready', { 
    current: this.current, 
    total: this.slides.length 
});

this.$dispatch('slider-changed', { 
    current: nextSlideEq, 
    total: this.slides.length 
});

this.$dispatch('open-lightbox', {
    image: this.slides[index]
});

πŸ“₯ Event Listening (Subscriber)

Method 1: Component-based listening

// SliderDisplay component
init() {
    window.addEventListener('slider-ready', this.handleSliderEvent.bind(this));
    window.addEventListener('slider-changed', this.handleSliderEvent.bind(this));
}

Method 2: Template-based listening

<div x-data="{ current: 0, total: 0 }"
     @slider-ready.window="current = $event.detail.current; total = $event.detail.total"
     @slider-changed.window="current = $event.detail.current; total = $event.detail.total">
</div>

Method 3: Cross-component communication

<div x-data="lightbox()"
     @open-lightbox.window="open($event.detail.image)">
</div>

πŸŽͺ Event Flow Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    slider-ready      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Slider    β”‚ ───────────────────► β”‚  SliderDisplay   β”‚
β”‚ Component   β”‚                      β”‚   Component      β”‚
β”‚             β”‚    slider-changed    β”‚                  β”‚
β”‚             β”‚ ───────────────────► β”‚                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β”‚
      β”‚ open-lightbox
      β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Lightbox   β”‚
β”‚ Component   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”„ Communication Patterns

  1. 🎯 Publisher-Subscriber: Slider broadcasts state changes
  2. 🎭 Command Pattern: Lightbox receives open commands
  3. πŸ“Š State Synchronization: Multiple displays stay in sync
  4. πŸ”— Loose Coupling: Components don't directly reference each other

πŸš€ Getting Started

πŸ“‹ Prerequisites

  • Node.js (v18+)
  • npm or yarn

πŸ”§ Installation

# Clone the repository
git clone https://github.com/Loubal70/AlpinePerfFormation
cd alpineJS

# Install dependencies
npm install
# or
yarn install

πŸƒβ€β™‚οΈ Development

# Start development server
npm run dev
# or
yarn dev

πŸ—οΈ Build

# Build for production
npm run build
# or
yarn build

🎨 Features Showcase

πŸ–ΌοΈ Image Slider

  • ⚑ Smooth Transitions: CSS-based animations
  • πŸ–±οΈ Multiple Controls: Arrows, dots, and keyboard navigation
  • πŸ“± Responsive Design: Mobile-friendly interface
  • πŸ”„ Auto-loop: Infinite scrolling capability

πŸ” Lightbox Modal

  • ⌨️ Keyboard Support: Space bar to close
  • 🎭 Elegant Animations: Smooth open/close transitions
  • πŸ“ Content Display: Title and description overlay
  • 🚫 Body Scroll Lock: Prevents background scrolling

πŸ“Š Status Display

  • πŸ”„ Real-time Updates: Shows current slide position
  • πŸ“‘ Event-driven: Updates via custom events
  • 🎯 Multiple Instances: Different display methods demonstrated

🎯 Key Benefits

  • ⚑ Performance: Lazy loading and minimal bundle size
  • 🧩 Modularity: Component-based architecture
  • πŸ”„ Reactivity: Real-time UI updates
  • πŸ“± Responsive: Mobile-first design approach
  • πŸ› οΈ Developer Experience: Hot reload and modern tooling
  • 🎨 Maintainable: Clean separation of concerns

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

πŸ“„ License

This project is open source and available under the MIT License.


Built with ❀️ using Alpine.js and modern web technologies

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published