class TourGuide extends HTMLElement { constructor() { super(); this.steps = [ { target: '.step-1', content: 'This is the main dashboard.', }, { target: '.step-2', content: 'Here you can see your recent activity.', }, { target: '.step-3', content: 'Click here to create a new item.', } ]; this.currentStep = 0; this.isRunning = false; } connectedCallback() { this.render(); this.setupEventListeners(); } render() { this.innerHTML = ` `; } setupEventListeners() { this.querySelector('.tour-button').addEventListener('click', () => this.startTour()); } startTour() { this.isRunning = true; this.currentStep = 0; this.showStep(); } showStep() { // Remove any existing tooltips this.removeTooltips(); const step = this.steps[this.currentStep]; const targetElement = document.querySelector(step.target); if (!targetElement) { console.warn(`Target element not found: ${step.target}`); this.nextStep(); return; } // Create overlay const overlay = document.createElement('div'); overlay.className = 'tour-overlay'; document.body.appendChild(overlay); // Position tooltip const rect = targetElement.getBoundingClientRect(); const tooltip = document.createElement('div'); tooltip.className = 'tour-tooltip'; tooltip.style.top = `${rect.bottom + window.scrollY + 10}px`; tooltip.style.left = `${rect.left + window.scrollX}px`; tooltip.innerHTML = `
${step.content}
`; document.body.appendChild(tooltip); // Highlight target targetElement.style.boxShadow = '0 0 0 3px rgba(59, 130, 246, 0.5)'; targetElement.style.position = 'relative'; targetElement.style.zIndex = '10000'; // Add event listeners tooltip.querySelector('.next-button').addEventListener('click', () => this.nextStep()); tooltip.querySelector('.skip-button').addEventListener('click', () => this.endTour()); } nextStep() { this.currentStep++; if (this.currentStep < this.steps.length) { this.showStep(); } else { this.endTour(); } } endTour() { this.isRunning = false; this.removeTooltips(); // Remove highlights this.steps.forEach(step => { const element = document.querySelector(step.target); if (element) { element.style.boxShadow = ''; element.style.zIndex = ''; } }); } removeTooltips() { const tooltips = document.querySelectorAll('.tour-tooltip, .tour-overlay'); tooltips.forEach(tooltip => tooltip.remove()); } } customElements.define('tour-guide', TourGuide);