feat: enhance site with HTMX for AJAX navigation and Alpine.js for reactivity
All checks were successful
Deploy Theme / deploy (push) Successful in 1m6s

This commit is contained in:
2026-02-19 18:07:46 +01:00
parent 3129f982df
commit 64a3c1d583
7 changed files with 117 additions and 242 deletions

View File

@@ -36,6 +36,8 @@ html[data-theme='light'] {
--text-tertiary: var(--color-gray-700);
}
[x-cloak] { display: none !important; }
@layer base {
body {
background-color: var(--bg-primary);
@@ -66,6 +68,10 @@ html[data-theme='light'] {
}
}
.kg-image-card img, .post-content img {
@apply cursor-pointer transition-opacity duration-200 hover:opacity-90;
}
@keyframes fadeInUp {
from {
opacity: 0;

View File

@@ -1,216 +1,35 @@
// Main JavaScript file for Palina theme
// Using HTMX and Alpine.js for enhanced functionality
document.addEventListener('DOMContentLoaded', () => {
console.log('Palina theme loaded!');
const htmlElement = document.documentElement;
// Remove 'hidden' class from html to prevent FOUC
htmlElement.classList.remove('hidden');
// Staggered fade-in animation for post grid (initial load)
const initialGridItems = document.querySelectorAll('.post-grid-item');
initialGridItems.forEach((item, index) => {
// Staggered fade-in animation for post grid
const animateGridItems = (container = document) => {
const gridItems = container.querySelectorAll('.post-grid-item:not(.animated)');
gridItems.forEach((item, index) => {
item.style.animationDelay = `${index * 100}ms`;
item.classList.add('animate-fadeInUp');
});
// Lightbox for post images
const lightbox = document.getElementById('lightbox');
if (lightbox) {
const lightboxImage = document.getElementById('lightbox-image');
const lightboxClose = document.getElementById('lightbox-close');
const setupLightboxImage = (image) => {
image.style.cursor = 'pointer';
image.addEventListener('click', () => {
lightbox.classList.remove('hidden');
lightbox.classList.add('show');
lightboxImage.src = image.src;
item.classList.add('animated');
});
};
// Initial setup for existing images
document.querySelectorAll('.kg-image-card img, .post-content img').forEach(setupLightboxImage);
// Initial animation
animateGridItems();
// Re-run animation and other setup when HTMX settles new content
document.body.addEventListener('htmx:afterSettle', (event) => {
// If the settled element is the posts-container or contains grid items
if (event.detail.target.id === 'posts-container' || event.detail.target.querySelector('.post-grid-item')) {
animateGridItems(event.detail.target);
}
lightboxClose.addEventListener('click', () => {
lightbox.classList.add('hidden');
lightbox.classList.remove('show');
lightboxImage.src = '';
});
lightbox.addEventListener('click', (e) => {
if (e.target.id !== 'lightbox-image') {
lightbox.classList.add('hidden');
lightbox.classList.remove('show');
lightboxImage.src = '';
// If it was a full page boost, we might need to reset some things
if (event.detail.boosted) {
animateGridItems();
}
});
}
// Theme Switcher
const themeToggle = document.getElementById('theme-toggle');
const sunIcon = document.getElementById('theme-toggle-sun-icon');
const moonIcon = document.getElementById('theme-toggle-moon-icon');
const setThemeIcons = (theme) => {
if (sunIcon && moonIcon) {
if (theme === 'dark') {
sunIcon.classList.remove('hidden');
moonIcon.classList.add('hidden');
} else {
sunIcon.classList.add('hidden');
moonIcon.classList.remove('hidden');
}
}
};
const currentTheme = localStorage.getItem('theme');
if (currentTheme) {
htmlElement.setAttribute('data-theme', currentTheme);
setThemeIcons(currentTheme);
} else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches) {
// Default to light theme if system preference is light
htmlElement.setAttribute('data-theme', 'light');
setThemeIcons('light');
} else {
// Default to dark theme if no preference
htmlElement.setAttribute('data-theme', 'dark');
setThemeIcons('dark');
}
if (themeToggle) {
themeToggle.addEventListener('click', () => {
let newTheme = htmlElement.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
htmlElement.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
setThemeIcons(newTheme);
});
}
// Mobile Menu
const mobileMenuToggle = document.getElementById('mobile-menu-toggle');
const mobileMenuClose = document.getElementById('mobile-menu-close');
const mobileMenu = document.getElementById('mobile-menu');
if (mobileMenuToggle && mobileMenu && mobileMenuClose) {
mobileMenuToggle.addEventListener('click', () => {
mobileMenu.classList.remove('-translate-x-full');
mobileMenu.classList.add('translate-x-0');
});
mobileMenuClose.addEventListener('click', () => {
mobileMenu.classList.remove('translate-x-0');
mobileMenu.classList.add('-translate-x-full');
});
// Close menu if a link is clicked
const mobileNavLinks = mobileMenu.querySelectorAll('a');
mobileNavLinks.forEach(link => {
link.addEventListener('click', () => {
mobileMenu.classList.remove('translate-x-0');
mobileMenu.classList.add('-translate-x-full');
});
});
}
// Infinite Scroll
const postsContainer = document.getElementById('posts-container');
const loadingSpinner = document.getElementById('loading-spinner');
if (postsContainer && loadingSpinner) {
let currentPage = 1;
let isLoading = false;
let hasMorePosts = true;
// Retrieve API key and URL from global GhostConfig object
if (typeof window.GhostConfig === 'undefined' || !window.GhostConfig.ghostApiKey || !window.GhostConfig.ghostApiUrl) {
console.error('Ghost Content API Key or URL not found in window.GhostConfig. Infinite scroll will not work.');
return;
}
const GHOST_API_KEY = window.GhostConfig.ghostApiKey;
const GHOST_API_URL = window.GhostConfig.ghostApiUrl;
const fetchPosts = async () => {
if (isLoading || !hasMorePosts) return;
isLoading = true;
loadingSpinner.classList.remove('hidden');
currentPage++;
const postsPerPage = 12; // Matches posts_per_page in package.json config
const url = `${GHOST_API_URL}/ghost/api/content/posts/?key=${GHOST_API_KEY}&limit=${postsPerPage}&page=${currentPage}&include=tags,authors`;
try {
const response = await fetch(url);
const data = await response.json();
const newPosts = data.posts;
const pagination = data.meta.pagination;
if (newPosts.length > 0) {
newPosts.forEach((post, index) => {
const article = document.createElement('article');
article.className = 'post-grid-item opacity-0 relative bg-[var(--bg-secondary)] rounded-lg shadow-lg overflow-hidden group';
// Basic rendering of a post for infinite scroll.
// This should ideally match the structure in index.hbs
// For simplicity, I'm reconstructing it here.
article.innerHTML = `
<a href="${post.url}" class="block">
${post.feature_image ? `
<img
class="w-full h-72 object-cover transition-transform duration-300 ease-in-out group-hover:scale-105"
src="${post.feature_image}"
alt="${post.title}"
loading="lazy"
>
` : `
<div class="w-full h-72 flex items-center justify-center bg-[var(--bg-tertiary)] text-[var(--text-tertiary)] text-2xl">No Image</div>
`}
<div class="absolute inset-0 bg-black bg-opacity-50 flex items-end p-4 opacity-0 group-hover:opacity-100 transition-opacity duration-300 ease-in-out">
<h2 class="text-[var(--text-primary)] text-xl font-semibold leading-tight">${post.title}</h2>
</div>
</a>
`;
postsContainer.appendChild(article);
// Apply staggered animation to newly added posts
// Calculate delay based on total items
const totalItems = postsContainer.children.length;
article.style.animationDelay = `${(totalItems - newPosts.length + index) * 100}ms`;
article.classList.add('animate-fadeInUp');
// If lightbox is present, set up new images for lightbox
if (lightbox) {
article.querySelectorAll('.kg-image-card img, .post-content img').forEach(setupLightboxImage);
}
});
} else {
hasMorePosts = false;
}
if (pagination && pagination.next === null) {
hasMorePosts = false;
}
} catch (error) {
console.error('Error fetching posts:', error);
hasMorePosts = false; // Stop trying to fetch if there's an error
} finally {
isLoading = false;
loadingSpinner.classList.add('hidden');
}
};
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting && hasMorePosts && !isLoading) {
fetchPosts();
}
});
}, { threshold: 0.1 }); // Trigger when 10% of the spinner is visible
observer.observe(loadingSpinner);
}
// Handle Lightbox for post images specifically (if needed beyond the global Alpine listener)
// The global listener in default.hbs should handle most cases.
});

View File

@@ -1,16 +1,39 @@
<!DOCTYPE html>
<html lang="{{@site.lang}}" data-theme="dark" class="hidden">
<html lang="{{@site.lang}}"
x-data="{
theme: localStorage.getItem('theme') || (window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'),
init() {
$watch('theme', val => {
localStorage.setItem('theme', val);
document.documentElement.setAttribute('data-theme', val);
});
document.documentElement.setAttribute('data-theme', this.theme);
document.documentElement.classList.remove('hidden');
}
}"
:data-theme="theme"
class="hidden">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{meta_title}}</title>
<link rel="stylesheet" href="{{asset "built/screen.css"}}">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&family=Playfair+Display:wght@700&display=swap" rel="stylesheet">
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<script src="https://unpkg.com/htmx.org@2.0.0"></script>
{{ghost_head}}
</head>
<body class="{{body_class}} font-sans antialiased">
<body class="{{body_class}} font-sans antialiased" hx-boost="true">
<div class="min-h-screen flex flex-col">
<div class="min-h-screen flex flex-col"
x-data="{ mobileMenuOpen: false, lightboxOpen: false, lightboxImage: '' }"
@click="if ($event.target.closest('.kg-image-card img, .post-content img')) {
$event.preventDefault();
lightboxImage = $event.target.src;
lightboxOpen = true;
}">
{{> header}}
<main class="flex-grow">
@@ -18,13 +41,18 @@
</main>
{{> footer}}
</div>
{{> mobile-menu}}
<div id="lightbox" class="hidden fixed inset-0 bg-black bg-opacity-80 z-50 flex items-center justify-center">
<button id="lightbox-close" class="absolute top-4 right-4 text-white text-3xl">&times;</button>
<img id="lightbox-image" src="" alt="Lightbox image" class="max-w-full max-h-full">
<div id="lightbox"
x-show="lightboxOpen"
x-transition
@click="lightboxOpen = false"
class="fixed inset-0 bg-black bg-opacity-80 z-50 flex items-center justify-center"
x-cloak>
<button @click="lightboxOpen = false" class="absolute top-4 right-4 text-white text-3xl">&times;</button>
<img :src="lightboxImage" alt="Lightbox image" class="max-w-full max-h-full" @click.stop>
</div>
</div>
{{ghost_foot}}

View File

@@ -9,32 +9,23 @@
{{#if posts}}
<div id="posts-container" class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6">
{{#foreach posts}}
<article class="post-grid-item opacity-0 relative bg-[var(--bg-secondary)] rounded-lg shadow-lg overflow-hidden group">
<a href="{{url}}" class="block">
{{#if feature_image}}
<img
class="w-full h-72 object-cover transition-transform duration-300 ease-in-out group-hover:scale-105"
src="{{img_url feature_image size="m"}}"
srcset="{{img_url feature_image size="s"}} 400w,
{{img_url feature_image size="m"}} 600w,
{{img_url feature_image size="l"}} 1000w,
{{img_url feature_image size="xl"}} 2000w"
sizes="(max-width: 800px) 400px, (max-width: 1200px) 600px, 1000px"
alt="{{title}}"
loading="lazy"
>
{{else}}
<div class="w-full h-72 flex items-center justify-center bg-[var(--bg-tertiary)] text-[var(--text-tertiary)] text-2xl">No Image</div>
{{/if}}
<div class="absolute inset-0 bg-black bg-opacity-50 flex items-end p-4 opacity-0 group-hover:opacity-100 transition-opacity duration-300 ease-in-out">
<h2 class="text-[var(--text-primary)] text-xl font-semibold leading-tight">{{title}}</h2>
</div>
</a>
</article>
{{> "post-card"}}
{{/foreach}}
{{#if pagination.next}}
<div id="pagination-trigger"
hx-get="{{page_url pagination.next}}"
hx-trigger="revealed"
hx-select="#posts-container > *"
hx-target="this"
hx-swap="outerHTML"
hx-indicator="#loading-spinner"
class="col-span-full h-1">
</div>
{{/if}}
</div>
<div id="loading-spinner" class="text-center py-8 hidden">
<div id="loading-spinner" class="htmx-indicator text-center py-8">
<div class="inline-block animate-spin rounded-full h-8 w-8 border-4 border-[var(--brand-primary)] border-t-transparent"></div>
<p class="text-[var(--text-secondary)] mt-2">Loading more posts...</p>
</div>

View File

@@ -7,17 +7,17 @@
<div class="flex items-center">
<nav class="hidden md:flex flex-wrap items-center justify-center">
{{navigation type="primary"}}
<button id="theme-toggle" class="ml-4 p-2 rounded-full bg-[var(--bg-secondary)] stroke-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] transition-colors duration-200">
<svg id="theme-toggle-sun-icon" class="theme-toggle-dark-icon w-5 h-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<button @click="theme = (theme === 'dark' ? 'light' : 'dark')" class="ml-4 p-2 rounded-full bg-[var(--bg-secondary)] stroke-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] transition-colors duration-200">
<svg x-show="theme === 'dark'" class="theme-toggle-dark-icon w-5 h-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M12 3v2.25m6.364.386-1.591 1.591M21 12h-2.25m-.386 6.364-1.591-1.591M12 18.75V21m-4.773-4.227-1.591 1.591M5.25 12H3m4.227-4.773L5.636 5.636M15.75 12a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0Z" />
</svg>
<svg id="theme-toggle-moon-icon" class="theme-toggle-light-icon w-5 h-5 hidden" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<svg x-show="theme === 'light'" class="theme-toggle-light-icon w-5 h-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" x-cloak>
<path stroke-linecap="round" stroke-linejoin="round" d="M21.752 15.002A9.72 9.72 0 0 1 18 15.75c-5.385 0-9.75-4.365-9.75-9.75 0-1.33.266-2.597.748-3.752A9.753 9.753 0 0 0 3 11.25C3 16.635 7.365 21 12.75 21a9.753 9.753 0 0 0 9.002-5.998Z" />
</svg>
</button>
</nav>
<button id="mobile-menu-toggle" class="md:hidden p-2 rounded-full bg-[var(--bg-secondary)] text-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] transition-colors duration-200">
<button @click="mobileMenuOpen = true" class="md:hidden p-2 rounded-full bg-[var(--bg-secondary)] text-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] transition-colors duration-200">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
</svg>

View File

@@ -1,12 +1,21 @@
<div id="mobile-menu" class="fixed inset-0 z-40 bg-[var(--bg-primary)] transform -translate-x-full transition-transform duration-300 ease-in-out md:hidden">
<div id="mobile-menu"
x-show="mobileMenuOpen"
x-transition:enter="transition ease-out duration-300"
x-transition:enter-start="-translate-x-full"
x-transition:enter-end="translate-x-0"
x-transition:leave="transition ease-in duration-300"
x-transition:leave-start="translate-x-0"
x-transition:leave-end="-translate-x-full"
class="fixed inset-0 z-40 bg-[var(--bg-primary)] transform md:hidden"
x-cloak>
<div class="flex justify-end p-5">
<button id="mobile-menu-close" class="p-2 rounded-full bg-[var(--bg-secondary)] text-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] transition-colors duration-200">
<button @click="mobileMenuOpen = false" class="p-2 rounded-full bg-[var(--bg-secondary)] text-[var(--text-primary)] hover:bg-[var(--bg-tertiary)] transition-colors duration-200">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<nav class="flex flex-col items-center justify-center h-full space-y-8 text-2xl">
<nav class="flex flex-col items-center justify-center h-full space-y-8 text-2xl" @click="mobileMenuOpen = false">
{{navigation type="primary"}}
</nav>
</div>

22
partials/post-card.hbs Normal file
View File

@@ -0,0 +1,22 @@
<article class="post-grid-item opacity-0 relative bg-[var(--bg-secondary)] rounded-lg shadow-lg overflow-hidden group">
<a href="{{url}}" class="block">
{{#if feature_image}}
<img
class="w-full h-72 object-cover transition-transform duration-300 ease-in-out group-hover:scale-105"
src="{{img_url feature_image size="m"}}"
srcset="{{img_url feature_image size="s"}} 400w,
{{img_url feature_image size="m"}} 600w,
{{img_url feature_image size="l"}} 1000w,
{{img_url feature_image size="xl"}} 2000w"
sizes="(max-width: 800px) 400px, (max-width: 1200px) 600px, 1000px"
alt="{{title}}"
loading="lazy"
>
{{else}}
<div class="w-full h-72 flex items-center justify-center bg-[var(--bg-tertiary)] text-[var(--text-tertiary)] text-2xl">No Image</div>
{{/if}}
<div class="absolute inset-0 bg-black bg-opacity-50 flex items-end p-4 opacity-0 group-hover:opacity-100 transition-opacity duration-300 ease-in-out">
<h2 class="text-[var(--text-primary)] text-xl font-semibold leading-tight">{{title}}</h2>
</div>
</a>
</article>