/** * WP Modern Blog — Ana JavaScript * * Mobil menü, arama overlay, header scroll shadow, copy link butonu. */ ( function () { 'use strict'; // ─── Yardımcı: DOM hazır ────────────────────────────────────────────────── function ready( fn ) { if ( document.readyState !== 'loading' ) fn(); else document.addEventListener( 'DOMContentLoaded', fn ); } ready( function () { // ─── Mobil Menü ──────────────────────────────────────────────────────── const hamburger = document.getElementById( 'mobile-menu-toggle' ); const mobileMenu = document.getElementById( 'mobile-menu' ); const hamburgerIcon = document.getElementById( 'hamburger-icon' ); const closeIcon = document.getElementById( 'close-icon' ); if ( hamburger && mobileMenu ) { hamburger.addEventListener( 'click', function () { const isOpen = mobileMenu.classList.toggle( 'hidden' ) === false; hamburger.setAttribute( 'aria-expanded', isOpen ? 'true' : 'false' ); if ( hamburgerIcon ) hamburgerIcon.classList.toggle( 'hidden', isOpen ); if ( closeIcon ) closeIcon.classList.toggle( 'hidden', ! isOpen ); document.body.classList.toggle( 'overflow-hidden', isOpen ); } ); // ESC tuşu ile kapat document.addEventListener( 'keydown', function ( e ) { if ( e.key === 'Escape' && ! mobileMenu.classList.contains( 'hidden' ) ) { mobileMenu.classList.add( 'hidden' ); hamburger.setAttribute( 'aria-expanded', 'false' ); if ( hamburgerIcon ) hamburgerIcon.classList.remove( 'hidden' ); if ( closeIcon ) closeIcon.classList.add( 'hidden' ); document.body.classList.remove( 'overflow-hidden' ); hamburger.focus(); } } ); } // ─── Arama Overlay ───────────────────────────────────────────────────── const searchToggle = document.getElementById( 'search-toggle' ); const searchOverlay = document.getElementById( 'search-overlay' ); const searchClose = document.getElementById( 'search-close' ); const searchInput = searchOverlay ? searchOverlay.querySelector( 'input[type="search"]' ) : null; function openSearch() { if ( ! searchOverlay ) return; searchOverlay.classList.remove( 'hidden' ); searchOverlay.setAttribute( 'aria-hidden', 'false' ); document.body.classList.add( 'overflow-hidden' ); requestAnimationFrame( function () { if ( searchInput ) searchInput.focus(); } ); } function closeSearch() { if ( ! searchOverlay ) return; searchOverlay.classList.add( 'hidden' ); searchOverlay.setAttribute( 'aria-hidden', 'true' ); document.body.classList.remove( 'overflow-hidden' ); if ( searchToggle ) searchToggle.focus(); } if ( searchToggle ) searchToggle.addEventListener( 'click', openSearch ); if ( searchClose ) searchClose.addEventListener( 'click', closeSearch ); if ( searchOverlay ) { // Backdrop tıklaması searchOverlay.addEventListener( 'click', function ( e ) { if ( e.target === searchOverlay ) closeSearch(); } ); // ESC ile kapat document.addEventListener( 'keydown', function ( e ) { if ( e.key === 'Escape' && ! searchOverlay.classList.contains( 'hidden' ) ) { closeSearch(); } } ); } // ─── Header Scroll Shadow ────────────────────────────────────────────── const header = document.getElementById( 'site-header' ); if ( header ) { let ticking = false; window.addEventListener( 'scroll', function () { if ( ! ticking ) { requestAnimationFrame( function () { header.classList.toggle( 'shadow-md', window.scrollY > 8 ); ticking = false; } ); ticking = true; } }, { passive: true } ); } // ─── Copy Link Butonları ─────────────────────────────────────────────── document.addEventListener( 'click', function ( e ) { const btn = e.target.closest( '[data-copy-url]' ); if ( ! btn ) return; const url = btn.dataset.copyUrl || window.location.href; if ( ! navigator.clipboard ) return; navigator.clipboard.writeText( url ).then( function () { const label = btn.querySelector( '[data-copy-label]' ); const original = label ? label.textContent : ''; if ( label ) { label.textContent = window.wpmbData ? window.wpmbData.i18n.copied : 'Kopyalandı!'; } btn.classList.add( 'text-green-600' ); setTimeout( function () { if ( label ) label.textContent = original; btn.classList.remove( 'text-green-600' ); }, 2000 ); } ); } ); // ─── Mobil: Alt Menü Akordeonu ───────────────────────────────────────── document.addEventListener( 'click', function ( e ) { const toggle = e.target.closest( '.mobile-submenu-toggle' ); if ( ! toggle ) return; const submenu = toggle.nextElementSibling; if ( ! submenu ) return; submenu.classList.toggle( 'hidden' ); toggle.setAttribute( 'aria-expanded', submenu.classList.contains( 'hidden' ) ? 'false' : 'true' ); } ); } ); } )();