Ctrl+K
<script> // (keep your existing recipe data and categories here) // DOM Elements const featuredRecipesContainer = document.getElementById('featured-recipes'); const categoryButtonsContainer = document.getElementById('category-buttons'); const categoryRecipesContainer = document.getElementById('category-recipes'); // Modal container (we'll add this dynamically) let modal; // Helper function to create recipe card (unchanged) function createRecipeCard(recipe) { const card = document.createElement('div'); card.className = 'recipe-card bg-white rounded-lg overflow-hidden shadow-md hover:shadow-lg'; const imageContainer = document.createElement('div'); imageContainer.className = 'h-48 overflow-hidden'; const image = document.createElement('img'); image.className = 'w-full h-full object-cover'; image.src = recipe.image.includes('placeholder.svg') ? 'https://via.placeholder.com/400x300?text=No+Image' : recipe.image; image.alt = recipe.name; imageContainer.appendChild(image); const content = document.createElement('div'); content.className = 'p-4'; const title = document.createElement('h3'); title.className = 'text-lg font-semibold text-gray-800 mb-2'; title.textContent = recipe.name; const description = document.createElement('p'); description.className = 'text-gray-600 mb-3'; description.textContent = recipe.description; const category = document.createElement('div'); category.className = 'flex justify-between items-center'; const categoryBadge = document.createElement('span'); categoryBadge.className = 'px-2 py-1 bg-blue-100 text-blue-800 text-xs font-medium rounded-full'; categoryBadge.textContent = recipe.category.charAt(0).toUpperCase() + recipe.category.slice(1); const viewButton = document.createElement('button'); viewButton.className = 'text-blue-500 hover:text-blue-700 text-sm font-medium'; viewButton.innerHTML = 'View Recipe <i class="fas fa-chevron-right ml-1"></i>'; // Open modal on click viewButton.addEventListener('click', () => { openRecipeModal(recipe); }); category.appendChild(categoryBadge); category.appendChild(viewButton); content.appendChild(title); content.appendChild(description); content.appendChild(category); card.appendChild(imageContainer); card.appendChild(content); return card; } // Render featured recipes (unchanged) function renderFeaturedRecipes() { featuredRecipesContainer.innerHTML = ''; const featured = getFeaturedRecipes(); featured.forEach(recipe => { const card = createRecipeCard(recipe); featuredRecipesContainer.appendChild(card); }); } // Render category buttons (unchanged except for a small fix to remove/add classes cleanly) function renderCategoryButtons() { categoryButtonsContainer.innerHTML = ''; categories.forEach((category, index) => { const button = document.createElement('button'); button.className = `category-btn px-4 py-2 rounded-full border border-gray-300 text-gray-700 font-medium hover:bg-gray-100 transition ${index === 0 ? 'active bg-blue-500 text-white border-blue-500' : ''}`; button.textContent = category.name; button.dataset.category = category.slug; button.addEventListener('click', () => { // Remove active class from all buttons document.querySelectorAll('.category-btn').forEach(btn => { btn.classList.remove('active', 'bg-blue-500', 'text-white'); btn.classList.add('border-gray-300', 'text-gray-700'); }); // Add active class to clicked button button.classList.add('active', 'bg-blue-500', 'text-white'); button.classList.remove('border-gray-300', 'text-gray-700'); // Render recipes for selected category, show first 8 renderCategoryRecipes(category.slug); }); categoryButtonsContainer.appendChild(button); }); } // Render recipes for selected category // Accepts optional 'all' param to show all or just 8 recipes function renderCategoryRecipes(categorySlug, showAll = false) { categoryRecipesContainer.innerHTML = ''; const category = categories.find(c => c.slug === categorySlug); if (category) { // Show all or slice to 8 const recipesToShow = showAll ? category.recipes : category.recipes.slice(0, 8); recipesToShow.forEach(recipe => { const card = createRecipeCard(recipe); categoryRecipesContainer.appendChild(card); }); // Only show "View All" button if not showing all already and there are more than 8 if (!showAll && category.recipes.length > 8) { const viewMoreContainer = document.createElement('div'); viewMoreContainer.className = 'col-span-full text-center mt-6'; const viewMoreButton = document.createElement('button'); viewMoreButton.className = 'px-6 py-3 bg-white border border-gray-300 text-gray-700 font-medium rounded-lg hover:bg-gray-50 transition'; viewMoreButton.textContent = `View All ${category.count} ${category.name} Recipes`; viewMoreButton.addEventListener('click', () => { // Render all recipes instead of alert renderCategoryRecipes(category.slug, true); }); viewMoreContainer.appendChild(viewMoreButton); categoryRecipesContainer.appendChild(viewMoreContainer); } } } // Modal functions function openRecipeModal(recipe) { // If modal exists, remove it first if (modal) modal.remove(); modal = document.createElement('div'); modal.className = 'fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50'; const modalContent = document.createElement('div'); modalContent.className = 'bg-white rounded-lg max-w-3xl w-full mx-4 overflow-auto max-h-[80vh] shadow-lg'; // Modal header const header = document.createElement('div'); header.className = 'flex justify-between items-center p-4 border-b border-gray-200'; const title = document.createElement('h3'); title.className = 'text-xl font-semibold text-gray-800'; title.textContent = recipe.name; const closeButton = document.createElement('button'); closeButton.className = 'text-gray-600 hover:text-gray-900 text-2xl font-bold'; closeButton.innerHTML = '×'; closeButton.addEventListener('click', () => { modal.remove(); }); header.appendChild(title); header.appendChild(closeButton); // Modal body const body = document.createElement('div'); body.className = 'p-6'; const image = document.createElement('img'); image.className = 'w-full h-64 object-cover rounded mb-4'; image.src = recipe.image.includes('placeholder.svg') ? 'https://via.placeholder.com/600x400?text=No+Image' : recipe.image; image.alt = recipe.name; const description = document.createElement('p'); description.className = 'mb-4 text-gray-700'; description.textContent = recipe.description; // Placeholder for more details β you can expand with ingredients, instructions, etc. const details = document.createElement('div'); details.className = 'text-gray-600'; // Example details (You can replace these with actual recipe details) details.innerHTML = ` <h4 class="font-semibold mb-2">Recipe Details</h4> <ul class="list-disc list-inside"> <li>Category: ${recipe.category.charAt(0).toUpperCase() + recipe.category.slice(1)}</li> <li>Prep Time: 30 minutes</li> <li>Servings: 4</li> <li>Ingredients: Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li> <li>Instructions: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet.</li> </ul> `; body.appendChild(image); body.appendChild(description); body.appendChild(details); modalContent.appendChild(header); modalContent.appendChild(body); modal.appendChild(modalContent); document.body.appendChild(modal); // Close modal on outside click modal.addEventListener('click', (e) => { if (e.target === modal) { modal.remove(); } }); } // Initialize the page document.addEventListener('DOMContentLoaded', () => { renderFeaturedRecipes(); renderCategoryButtons(); renderCategoryRecipes('breakfast'); }); </script> - Initial Deployment
d0ab1e2 verified