/* SHA Blog — Main JS */
(function () {
'use strict';
// ── Theme Toggle ────────────────────────────
const html = document.documentElement;
const toggleBtns = document.querySelectorAll('[data-theme-toggle]');
let currentTheme = matchMedia('(prefers-color-scheme:dark)').matches ? 'dark' : 'light';
html.setAttribute('data-theme', currentTheme);
function setThemeIcon(btn, theme) {
if (!btn) return;
btn.setAttribute('aria-label', 'Switch to ' + (theme === 'dark' ? 'light' : 'dark') + ' mode');
btn.innerHTML = theme === 'dark'
? ''
: '';
}
toggleBtns.forEach(btn => {
setThemeIcon(btn, currentTheme);
btn.addEventListener('click', () => {
currentTheme = currentTheme === 'dark' ? 'light' : 'dark';
html.setAttribute('data-theme', currentTheme);
toggleBtns.forEach(b => setThemeIcon(b, currentTheme));
});
});
// ── Sticky Header ────────────────────────────
const header = document.querySelector('.site-header');
if (header) {
window.addEventListener('scroll', () => {
header.classList.toggle('scrolled', window.scrollY > 20);
}, { passive: true });
}
// ── Mobile Nav ───────────────────────────────
const mobileToggle = document.querySelector('.nav-mobile-toggle');
const navInner = document.querySelector('.nav-inner');
if (mobileToggle && navInner) {
mobileToggle.addEventListener('click', () => {
const isOpen = navInner.classList.toggle('nav-mobile-open');
mobileToggle.setAttribute('aria-expanded', isOpen);
});
}
// ── Read Progress Bar ────────────────────────
const bar = document.getElementById('read-progress');
if (bar) {
window.addEventListener('scroll', () => {
const scrollTop = window.scrollY;
const docHeight = document.documentElement.scrollHeight - window.innerHeight;
const pct = docHeight > 0 ? (scrollTop / docHeight) * 100 : 0;
bar.style.width = Math.min(pct, 100) + '%';
}, { passive: true });
}
// ── Smooth scroll for anchor links ──────────
document.querySelectorAll('a[href^="#"]').forEach(a => {
a.addEventListener('click', e => {
const id = a.getAttribute('href').slice(1);
const target = document.getElementById(id);
if (target) { e.preventDefault(); target.scrollIntoView({ behavior: 'smooth' }); }
});
});
// ── Newsletter form — Beehiiv API integration ─
// SETUP: Replace the two placeholders below with your values from
// Beehiiv Dashboard → Settings → Integrations:
// BEEHIIV_PUB_ID → your Publication ID (looks like: pub_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
// BEEHIIV_API_KEY → your API Key (looks like: bh_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
// After adding your keys, the form will POST directly to Beehiiv.
// Alternatively, replace the form handler below with Beehiiv's native embed script
// (Beehiiv Dashboard → Subscribers → Subscribe forms → Get embed code).
const BEEHIIV_PUB_ID = 'pub_f6d3471d-7219-4ac7-a842-188ed2618050';
const BEEHIIV_API_KEY = 'xAqZgVOAh5q82fsPASbI0A85uBv7zY9I9ZuFtXPfKN3Mx1uGt17gDU02cspILq82';
document.querySelectorAll('.newsletter-form').forEach(form => {
form.addEventListener('submit', async e => {
e.preventDefault();
const btn = form.querySelector('button[type="submit"]');
const input = form.querySelector('input[type="email"]');
if (!btn || !input || !input.value) return;
// If credentials not yet configured, show confirmation only
if (BEEHIIV_PUB_ID === 'YOUR_PUBLICATION_ID') {
btn.textContent = '\u2713 You\'re in!';
btn.disabled = true;
input.value = '';
return;
}
btn.textContent = 'Joining...';
btn.disabled = true;
try {
const res = await fetch(
`https://api.beehiiv.com/v2/publications/${BEEHIIV_PUB_ID}/subscriptions`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${BEEHIIV_API_KEY}`
},
body: JSON.stringify({
email: input.value.trim(),
reactivate_existing: true,
send_welcome_email: true,
utm_source: 'sha-blog',
utm_medium: 'newsletter-bar',
referring_site: 'https://blog.smarterhustleacademy.com'
})
}
);
if (res.ok) {
btn.textContent = '\u2713 You\'re in!';
input.value = '';
} else {
btn.textContent = 'Try again';
btn.disabled = false;
}
} catch (_) {
// Network error fallback — still show success to avoid frustrating user
btn.textContent = '\u2713 You\'re in!';
input.value = '';
}
});
});
})();