πŸš€ VanillaJS Router

Enterprise-grade routing in 200 lines of vanilla JavaScript

NEW: Completely UI-agnostic with event-based architecture!

πŸ“ Current Route

Loading...

πŸ“‹ Navigation Status

πŸ“Š Typed Parameters

{}

πŸ“œ Scroll Position

0, 0

Welcome to VanillaJS Router Demo!

Use the navigation buttons above to test all features. This router demonstrates:

🎯 NEW Event System: The router now uses a clean event system instead of DOM manipulation:
// Subscribe to navigation status updates MyRouter.onStatus((status, type, context) => { console.log(status); // "πŸ”„ Navigating...", "βœ… Navigation complete" }); // Subscribe to scroll events MyRouter.onScroll((scrollData) => { console.log(scrollData); // { type: 'update', winX: 0, winY: 100, ... } });

πŸ“œ Enhanced Scroll Test Area

Scroll down in this area, then navigate to other pages and back. The router remembers your scroll position!

πŸ“ Demo Section 1 - UI-Agnostic Architecture

The router is now completely decoupled from UI concerns. Instead of assuming DOM elements exist, it emits events:

// OLD: Router directly manipulated DOM updateNavStatus('βœ… Navigation complete'); // NEW: Router emits events, application handles UI MyRouter.onStatus((status, type) => { // Application decides how to display status document.getElementById('status').textContent = status; });
πŸ’‘ Benefits: This makes the router reusable across different UI frameworks and eliminates assumptions about HTML structure.

πŸ“ Demo Section 2 - Custom Scroll Handling

Applications can now add custom scroll data through the event system:

MyRouter.onScroll((scrollData) => { if (scrollData.type === 'capture') { // Add custom scroll positions (e.g., specific containers) const customContainer = document.getElementById('my-container'); scrollData.customY = customContainer.scrollTop; } if (scrollData.type === 'restore') { // Restore custom scroll positions if (scrollData.customY !== undefined) { document.getElementById('my-container').scrollTop = scrollData.customY; } } });

πŸ“ Demo Section 3 - Event-Driven Status Updates

The router provides rich status information through events:

MyRouter.onStatus((status, type, context) => { // status: The human-readable status message // type: 'info', 'loading', 'success', 'error' // context: { route, prevRoute } - current navigation context const statusEl = document.getElementById('nav-status'); statusEl.textContent = status; statusEl.className = `status-value ${type}`; });
🎨 Dynamic Styling: Applications can apply different CSS classes based on the status type, creating rich visual feedback.

πŸ“ Demo Section 4 - Production Readiness

This event-driven architecture makes the router truly production-ready:

  • Framework agnostic - Works with React, Vue, Angular, or vanilla JS
  • No DOM assumptions - Can run in Node.js environments for SSR
  • Testable - Easy to unit test without DOM manipulation
  • Extensible - Applications can add custom data to events

The clean separation of concerns makes the router reusable across different projects and teams.

πŸ’­ Scroll to the very bottom to test the full scroll restoration effect!