Skip to content

How to add slides via slots in Astro with Svelte? #7885

Answered by MLNW
MLNW asked this question in Q&A
Discussion options

You must be logged in to vote

I was able to solve the issue like this:

<script lang="ts">
  import { onMount } from "svelte";
  import { register } from "swiper/element/bundle";

  let swiper: HTMLElement;

  onMount(() => {
    let astroSlot = swiper.children.item(0);

    // Astro wraps slot elements within an astro-slot element. SwiperJS expects
    // swiper-slide elements to be direct children of the swiper-container. We
    // therefore move all slides into the swiper-container.
    if (astroSlot && swiper) {
      swiper.append(...astroSlot.children);
      astroSlot.remove();
    }

    register();
  });
</script>

<swiper-container
  bind:this={swiper}
  slides-per-view="1"
  autoplay="false"
  css-mode="false"

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by MLNW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant