|
5 | 5 | import HomepageCarouselItem from "@/components/HomepageCarouselItem.svelte"; |
6 | 6 | interface Props { |
7 | 7 | slides: { |
8 | | - title: string; |
9 | 8 | image: { |
10 | | - path:string; |
11 | | - alt?:string; |
| 9 | + path: string; |
| 10 | + alt?: string; |
12 | 11 | }; |
| 12 | + title: string; |
13 | 13 | text: string; |
14 | 14 | actionLabel: string; |
15 | 15 | actionTo: string; |
16 | 16 | }[]; |
17 | 17 | } |
18 | | - let {slides}: Props = $props(); |
19 | | -</script> |
| 18 | + let { slides }: Props = $props(); |
20 | 19 |
|
21 | | -<Carousel.Root opts={{ |
22 | | - loop: true, |
23 | | - }} class="w-full h-fit"> |
| 20 | + let currentSlide: number = $state(0); // TODO: make check for actual value here. Could break if not starting at 0 |
| 21 | +
|
| 22 | + // Theres probably a less hacky way but my ts only started working after I had written all this so hey. |
| 23 | + // Type safety by "Trust me bro" |
| 24 | + //@ts-ignore |
| 25 | + let api: CarouselAPI = $state(); |
| 26 | +
|
| 27 | + $effect(() => { |
| 28 | + if (api) { |
| 29 | + api.on("scroll", (el) => { // Yes this gets spammed as the slide slides, but `settle` from the embla docs doesnt seem to be working. |
| 30 | + currentSlide = el.slidesInView()[0] ?? 0 // default to 0 if no slides in view (something has gone horrible wrong) |
| 31 | + // console.log(currentSlide) |
| 32 | + }); |
| 33 | + } |
| 34 | + }); |
| 35 | +</script> |
| 36 | +<Carousel.Root |
| 37 | + opts={{ |
| 38 | + loop: true, |
| 39 | + }} |
| 40 | + class="w-full h-fit" |
| 41 | + bind:api |
| 42 | +> |
24 | 43 | <Carousel.Content class="bg-red-400"> |
25 | 44 | {#each slides as slide} |
26 | | - <HomepageCarouselItem src={slide.image.path} alt={slide.image.alt} /> |
| 45 | + <HomepageCarouselItem |
| 46 | + src={slide.image.path} |
| 47 | + alt={slide.image.alt ?? ""} |
| 48 | + /> |
27 | 49 | {/each} |
28 | 50 | </Carousel.Content> |
29 | 51 | <div |
30 | | - class="w-4/5 sm:w-72 bg-background absolute bottom-12 left-8 px-4 py-2" |
| 52 | + class="m-12 w-auto sm:w-72 bg-background absolute bottom-0 sm:bottom-4 px-8 py-6" |
31 | 53 | > |
32 | | - <h1 class="font-display text-lg">Title</h1> |
33 | | - <div class="text-xs">body text</div> |
34 | | - <Button class="h-6">Action</Button> |
| 54 | + <h1 class="font-display text-xl">{slides[currentSlide]?.title ?? "Error"}</h1> |
| 55 | + <div class="text-base font-sans">{slides[currentSlide]?.text ?? "It looks like something went wrong. Try refreshing"}</div> |
| 56 | + <Button class="h-8 mt-2 text-lg p-5 font-mono font-semibold" href={slides[currentSlide]?.actionTo ?? "/"}>{slides[currentSlide]?.actionLabel ?? ":("}</Button> |
35 | 57 | </div> |
36 | | - <Carousel.Previous class="top-auto -bottom-2 scale-75 left-10" /> |
| 58 | + <Carousel.Previous class="top-auto -bottom-0 left-14 hidden sm:flex" /> |
37 | 59 | <Carousel.Next |
38 | | - class="top-auto -bottom-2 scale-75 right-auto left-[4.5rem]" |
| 60 | + class="top-auto -bottom-0 right-auto left-[6.6rem] hidden sm:flex" |
39 | 61 | /> |
40 | 62 | </Carousel.Root> |
0 commit comments