feat: Implement infinite scroll with preloader
This commit is contained in:
@@ -6,9 +6,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
// Remove 'hidden' class from html to prevent FOUC
|
||||
htmlElement.classList.remove('hidden');
|
||||
|
||||
// Staggered fade-in animation for post grid
|
||||
const gridItems = document.querySelectorAll('.post-grid-item');
|
||||
gridItems.forEach((item, index) => {
|
||||
// Staggered fade-in animation for post grid (initial load)
|
||||
const initialGridItems = document.querySelectorAll('.post-grid-item');
|
||||
initialGridItems.forEach((item, index) => {
|
||||
item.style.animationDelay = `${index * 100}ms`;
|
||||
item.classList.add('animate-fadeInUp');
|
||||
});
|
||||
@@ -19,16 +19,18 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
const lightboxImage = document.getElementById('lightbox-image');
|
||||
const lightboxClose = document.getElementById('lightbox-close');
|
||||
|
||||
const images = document.querySelectorAll('.kg-image-card img, .post-content img');
|
||||
|
||||
images.forEach(image => {
|
||||
const setupLightboxImage = (image) => {
|
||||
image.style.cursor = 'pointer';
|
||||
image.addEventListener('click', () => {
|
||||
lightbox.classList.remove('hidden');
|
||||
lightbox.classList.add('show');
|
||||
lightboxImage.src = image.src;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Initial setup for existing images
|
||||
document.querySelectorAll('.kg-image-card img, .post-content img').forEach(setupLightboxImage);
|
||||
|
||||
|
||||
lightboxClose.addEventListener('click', () => {
|
||||
lightbox.classList.add('hidden');
|
||||
@@ -90,4 +92,107 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
const ghostApiKeyMeta = document.querySelector('meta[name="ghost-api-key"]');
|
||||
const ghostApiUrlMeta = document.querySelector('meta[name="ghost-api-url"]');
|
||||
|
||||
if (!ghostApiKeyMeta || !ghostApiUrlMeta) {
|
||||
console.error('Ghost Content API Key or URL meta tag not found. Infinite scroll will not work.');
|
||||
return;
|
||||
}
|
||||
|
||||
const GHOST_API_KEY = ghostApiKeyMeta.content;
|
||||
const GHOST_API_URL = ghostApiUrlMeta.content;
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
<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">
|
||||
<meta name="ghost-api-key" content="YOUR_GHOST_CONTENT_API_KEY"> <!-- IMPORTANT: Replace with your actual Ghost Content API Key -->
|
||||
<meta name="ghost-api-url" content="{{@site.url}}">
|
||||
{{ghost_head}}
|
||||
</head>
|
||||
<body class="{{body_class}} font-sans antialiased">
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
<div class="container mx-auto px-4">
|
||||
{{#if posts}}
|
||||
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6">
|
||||
<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">
|
||||
@@ -34,7 +34,11 @@
|
||||
{{/foreach}}
|
||||
</div>
|
||||
|
||||
{{pagination}}
|
||||
<div id="loading-spinner" class="text-center py-8 hidden">
|
||||
<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>
|
||||
|
||||
{{else}}
|
||||
<p class="text-center text-[var(--text-secondary)] text-2xl py-20">No posts found.</p>
|
||||
{{/if}}
|
||||
|
||||
Reference in New Issue
Block a user