To achieve the desired behavior, you can modify your Astro file to include a client-side script that updates the displayed events when the "See More" button is clicked. Here's an updated version of your Astro file:
---
import UpcomingEvents from './UpcomingEvents.astro';
import Layout from './Layout.astro';
---
<Layout>
<div id="events-container">
{{upcomingEvents.slice(0, 4).map((data) => (
<UpcomingEvents key={{data.id}} title={{data.eventTitle}} />
))}}
</div>
<button id="see-more">See More</button>
</Layout>
<script>
let pressed = false;
function buttonPress() {
pressed = !pressed;
console.log("Clicked:", pressed);
const eventsContainer = document.getElementById("events-container");
eventsContainer.innerHTML = '';
const eventsToDisplay = pressed ? upcomingEvents : upcomingEvents.slice(0, 4);
eventsToDisplay.forEach((data) => {
const eventElement = document.createElement('astro-upcoming-events');
eventElement.setAttribute('key', data.id);
eventElement.setAttribute('title', data.eventTitle);
eventsContainer.appendChild(eventElement);
});
}
const seeMore = document.getElementById("see-more");
seeMore.addEventListener("click", buttonPress);
</script>
In this example, I've updated the <script> block to clear the events-container and then create and append new astro-upcoming-events elements based on the pressed state. When the button is clicked, it toggles the pressed state and updates the displayed events accordingly.
Please note that this approach uses vanilla JavaScript to manipulate the DOM. If you prefer to use a UI framework like React, Vue, or Svelte, you can follow the respective integration guides in the Astro documentation: