// Sustainability Page JavaScript let isQuickMenuOpen = false; // Scroll-to-top functionality function handleScroll() { const scrollToTopBtn = document.getElementById('scrollToTopBtn'); if (scrollToTopBtn) { if (window.pageYOffset > 300) { scrollToTopBtn.classList.remove('opacity-0', 'translate-y-4', 'pointer-events-none'); scrollToTopBtn.classList.add('opacity-100', 'translate-y-0', 'pointer-events-auto'); } else { scrollToTopBtn.classList.add('opacity-0', 'translate-y-4', 'pointer-events-none'); scrollToTopBtn.classList.remove('opacity-100', 'translate-y-0', 'pointer-events-auto'); } } } function scrollToTop() { window.scrollTo({ top: 0, behavior: 'smooth' }); } function handleScrollToTopClick() { scrollToTop(); } // Quick Menu functionality function toggleQuickMenu() { const panel = document.getElementById('quickMenuPanel'); const btn = document.getElementById('quickMenuBtn'); if (isQuickMenuOpen) { panel.classList.remove('opacity-100', 'translate-x-0', 'pointer-events-auto'); panel.classList.add('opacity-0', 'translate-x-4', 'pointer-events-none'); btn.classList.remove('rotate-45'); } else { panel.classList.remove('opacity-0', 'translate-x-4', 'pointer-events-none'); panel.classList.add('opacity-100', 'translate-x-0', 'pointer-events-auto'); btn.classList.add('rotate-45'); } isQuickMenuOpen = !isQuickMenuOpen; } function closeQuickMenu() { const panel = document.getElementById('quickMenuPanel'); const btn = document.getElementById('quickMenuBtn'); if (panel && btn) { panel.classList.remove('opacity-100', 'translate-x-0', 'pointer-events-auto'); panel.classList.add('opacity-0', 'translate-x-4', 'pointer-events-none'); btn.classList.remove('rotate-45'); isQuickMenuOpen = false; } } function handleQuickMenuClick() { toggleQuickMenu(); } function handleQuickMenuClose() { closeQuickMenu(); } function handleOutsideClick(event) { const panel = document.getElementById('quickMenuPanel'); const btn = document.getElementById('quickMenuBtn'); if (isQuickMenuOpen && panel && btn && !panel.contains(event.target) && !btn.contains(event.target)) { closeQuickMenu(); } } function handleKeyDown(event) { if (event.key === 'Escape' && isQuickMenuOpen) { closeQuickMenu(); } } function init() { // Scroll-to-top functionality const scrollToTopBtn = document.getElementById('scrollToTopBtn'); if (scrollToTopBtn) { scrollToTopBtn.addEventListener('click', handleScrollToTopClick); } // Scroll event listener for showing/hiding scroll-to-top button window.addEventListener('scroll', handleScroll); // Initial check for scroll position handleScroll(); // Quick Menu event listeners const quickMenuBtn = document.getElementById('quickMenuBtn'); const quickMenuClose = document.getElementById('quickMenuClose'); if (quickMenuBtn) { quickMenuBtn.addEventListener('click', handleQuickMenuClick); } if (quickMenuClose) { quickMenuClose.addEventListener('click', handleQuickMenuClose); } // Close menu when clicking outside document.addEventListener('click', handleOutsideClick); // Close menu when pressing Escape key document.addEventListener('keydown', handleKeyDown); } function teardown() { // Remove scroll-to-top event listeners const scrollToTopBtn = document.getElementById('scrollToTopBtn'); if (scrollToTopBtn) { scrollToTopBtn.removeEventListener('click', handleScrollToTopClick); } window.removeEventListener('scroll', handleScroll); // Remove quick menu event listeners const quickMenuBtn = document.getElementById('quickMenuBtn'); const quickMenuClose = document.getElementById('quickMenuClose'); if (quickMenuBtn) { quickMenuBtn.removeEventListener('click', handleQuickMenuClick); } if (quickMenuClose) { quickMenuClose.removeEventListener('click', handleQuickMenuClose); } document.removeEventListener('click', handleOutsideClick); document.removeEventListener('keydown', handleKeyDown); // Reset state isQuickMenuOpen = false; } export { init, teardown };