#client side rendering JS
57 messages · Page 1 of 1 (latest)
is:inline to make the script tag run on the client?
My Js is in a script tag at the bottom of my html. Styles are imported.
Does this answer your question?
Can you send an actual code snippet or a stackblitz reproduction?
Just Js?
Probably your astro file?
<script>
document.addEventListener("astro:page-load", () => {
const buttons = document.querySelectorAll("[data-carousel-button]");
buttons.forEach(button => {
button.addEventListener("click", () => {
const offset = button.dataset.carouselButton === "next" ? 1 : -1;
const carousel = button.closest("[data-carousel]"); // Find closest carousel container
const slides = carousel.querySelector("[data-slides]");
const activeSlide = slides.querySelector("[data-active]");
let newIndex = (slides.children.length + [...slides.children].indexOf(activeSlide) + offset) % slides.children.length; // Improved slide selection and looping
slides.children[newIndex].dataset.active = true;
if (activeSlide) {
delete activeSlide.dataset.active; // Remove active state from previous slide
}
});
});
})
</script>
If you know that those elements will always be there you can cast as Element
const carousel = button.closest("[data-carousel]") as Element
No so that's the same line as you have where you're defining carousel
<script>
document.addEventListener("astro:page-load", () => {
const buttons = document.querySelectorAll("[data-carousel-button]");
buttons.forEach(button => {
button.addEventListener("click", () => {
const offset = button.dataset.carouselButton === "next" ? 1 : -1;
// Added the Typescript casts below 👇
const carousel = button.closest("[data-carousel]") as Element; // Adding as Element here
const slides = carousel.querySelector("[data-slides]") as Element; // Adding as Element here
const activeSlide = slides.querySelector("[data-active]") as Element; // Adding as Element here
let newIndex = (slides.children.length + [...slides.children].indexOf(activeSlide) + offset) % slides.children.length; // Improved slide selection and looping
slides.children[newIndex].dataset.active = true;
if (activeSlide) {
delete activeSlide.dataset.active; // Remove active state from previous slide
}
});
});
})
</script>
Thanks, it stilldoesn't recognize dataset
I'd probably do something like
const slideChild = slides.children[newIndex] as Element
slideChild.dataset.active = true
Slides, newIndex and dataset are all complaining
Document is not defined it says
document.addEventListener("astro:page-load", () => {
const buttons = document.querySelectorAll("[data-carousel-button]");
const slideChild = slides.children[newIndex] as Element
slideChild.dataset.active = true
buttons.forEach(button => {
button.addEventListener("click", () => {
const offset = button.dataset.carouselButton === "next" ? 1 : -1;
// Added the Typescript casts below 👇
const carousel = button.closest("[data-carousel]") as Element; // Adding as Element here
const slides = carousel.querySelector("[data-slides]") as Element; // Adding as Element here
const activeSlide = slides.querySelector("[data-active]") as Element; // Adding as Element here
let newIndex = (slides.children.length + [...slides.children].indexOf(activeSlide) + offset) % slides.children.length; // Improved slide selection and looping
slides.children[newIndex].dataset.active = true;
if (activeSlide) {
delete activeSlide.dataset.active; // Remove active state from previous slide
}
});
});
})
</script>```
Are you able to screenshot or copy and paste the errors you're getting?
can you see the screenshots that I uploaded, they don't appear to be in the chat.
now they appear to be in the chat
I need to run an errand this morning, I will check back in. a couple of hours. I appreciate your help!
Maybe I should explore server side rendering for the carousel??
The error about document not existing is coming form CustomCarousel.js. Where are you importing that script and what's on line 2 of it?
This is what I am importing: There's no JS file
Before, all the pictures were stacked, but would not scroll.
I found this list of errors. I'm not sure what to do with it:
document.addEventListener("astro:page-load", () => {
const buttons = document.querySelectorAll("[data-carousel-button]");
buttons.forEach(button => {
button.addEventListener("click", () => {
const offset = button.dataset.carouselButton === "next" ? 1 : -1;
// Added the Typescript casts below 👇
const carousel = button.closest("[data-carousel]") as Element; // Adding as Element here
const slides = carousel.querySelector("[data-slides]") as Element; // Adding as Element here
const activeSlide = slides.querySelector("[data-active]") as Element; // Adding as Element here
let newIndex = (slides.children.length + [...slides.children].indexOf(activeSlide) + offset) % slides.children.length; // Improved slide selection and looping
slides.children[newIndex].dataset.active = true;
if (activeSlide) {
delete activeSlide.dataset.active; // Remove active state from previous slide
}
});
});
})
</script>```. here's the file
Try typing them as HTMLElement instead of Element, I think maybe dataset is only on HTMLElement?
how do I do that?
Change where it says as Element to as HTMLElement. So this is called "casting" in Typescript, where we are telling Typescript that something is a specific type.
So we're telling it that your carousel variable, actually has the type HTMLElement
If that doesn't work, it might be worth making a stackblitz reproduction of this so we can actually see what you're trying to do!
You can use this template and add your code so we can see what you're doing https://stackblitz.com/github/withastro/astro/tree/latest/examples/basics?file=README.md
Thank you. I want to try a couple more things first
Did you mean to send your updated one? This is just the boilerplate example link 😅
Sorry I didn’t communicate well. Family came and I needed to entertain them. I’ll do that now.
Here is the link. I hope I did it right. I just copied and pasted the code into the program in the right spot. If I need to do more, let me know. https://stackblitz.com/edit/withastro-astro-habeqb?file=src%2Fpages%2Findex.astro
everyone is sleeping
Ok, so... That repo doesn't seem to be working, there's some components and stuff missing... What exactly was your error again? Was it just the type error of the carousel.dataset ?
I have had time to fix the code so that I now have the same experience as on my computer
Sorry for sending before it was ready
Ok, so that previous link still has some errors. But tbh you're not really trying to rewrite your whole site! If you can create a reproduction of your issue with the minimum amount of code the better!
So I think your error was a typing issue, correct?
try this link. It works for me. I saved the data, and here is the link. https://stackblitz.com/edit/withastro-astro-habeqb?file=src%2Fcomponents%2Fcarousel.astro
this is what I see
Let me know if you see something else. how I fix it. Thanks,
I see this: "The collection "blog" does not exist or is empty. Ensure a collection directory with this name exists."
But I don't see the error messages you mentioned earlier
The problem is that the js does not work.
The photos should sycle with clicking the arrows
Help me out, why does yours work, and mine does not. I have looked line by line in the js and html. What have I missed?
found it, thank you so much!!
Yeah so I'm glad it works. Wasn't 100% sure if that's what you were going for but it's a simple fix. You were using the astro:page-load lifecycle event. But that doesn't work unless you import and use View Transitions.
So in Layout.astro line 3 I added:
import { ViewTransitions } from 'astro:transitions';
Then on line 38 I added: <ViewTransitions />
I took that from the docs here: https://docs.astro.build/en/guides/view-transitions/
You also had two <head> so I removed one
I appreciate your help. still working on that attention to detail thing!!
Yeah it's terrible. Forget one word and your entire app breaks. If want to you can go through the astroJS tutorial and it has a section for ViewTransitions. It will explain it better than I can
I can't even find it in the tutorial now that I look. Maybe just the docs then 🙂
Do you have the link? I would love to know more about JS and astro!