---
title: "Svelte and SvelteKit"
description: "Use Blossom Carousel in Svelte and SvelteKit with keyed each blocks and global stylesheet imports."
canonical_url: "https://www.blossom-carousel.com/docs/framework-guides/svelte-sveltekit"
---

> Index of every page on this site: [llms.txt](https://www.blossom-carousel.com/llms.txt)

## Install

<code-group>

```bash [pnpm]
pnpm add @blossom-carousel/svelte
```

```bash [bun]
bun add @blossom-carousel/svelte
```

```bash [yarn]
yarn add @blossom-carousel/svelte
```

```bash [npm]
npm install @blossom-carousel/svelte
```

</code-group>

## Svelte usage

<note color="neutral" icon="i-lucide-lightbulb">

`@blossom-carousel/svelte` `2.x` requires Svelte 5 (runes + snippets) and drops Svelte 4 support. If you're on Svelte 4, stay on `@blossom-carousel/svelte@1`.

</note>

```svelte
<script>
  import { BlossomCarousel } from "@blossom-carousel/svelte";
  import "@blossom-carousel/svelte/style.css";

  const slides = Array.from({ length: 12 }, (_, index) => index + 1);
</script>

<BlossomCarousel>
  {#each slides as slide (slide)}
    <div>Slide {slide}</div>
  {/each}
</BlossomCarousel>
```

## SvelteKit setup

Import the stylesheet from the root layout.

```svelte [src/routes/+layout.svelte]
<script>
  import "@blossom-carousel/svelte/style.css";

  let { children } = $props();
</script>

{@render children()}
```

Then import the component in the page or component that renders the carousel.

```svelte
<script>
  import { BlossomCarousel } from "@blossom-carousel/svelte";
</script>

<BlossomCarousel>{/* slides */}</BlossomCarousel>
```

## Semantic root element

Use `as="ul"` for list-like carousels.

```svelte
<BlossomCarousel as="ul">
  {#each slides as slide (slide)}
    <li>Slide {slide}</li>
  {/each}
</BlossomCarousel>
```

## Props

<table>
<thead>
  <tr>
    <th>
      Prop
    </th>
    
    <th>
      Type
    </th>
    
    <th>
      Default
    </th>
    
    <th>
      Description
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        as
      </code>
    </td>
    
    <td>
      <code>
        string
      </code>
    </td>
    
    <td>
      <code>
        "div"
      </code>
    </td>
    
    <td>
      Element rendered as the carousel root.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        load
      </code>
    </td>
    
    <td>
      <code>
        "conditional" | "always"
      </code>
    </td>
    
    <td>
      <code>
        "conditional"
      </code>
    </td>
    
    <td>
      <code>
        conditional
      </code>
      
       loads the drag engine only on devices that match <code>
        (hover: hover) and (pointer: fine)
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        repeat
      </code>
    </td>
    
    <td>
      <code>
        boolean
      </code>
    </td>
    
    <td>
      <code>
        false
      </code>
    </td>
    
    <td>
      Experimental looping mode. See the <a href="/docs/examples/experimental/repeat">
        Repeat example
      </a>
      
      .
    </td>
  </tr>
</tbody>
</table>

## Navigation controls

Place previous, next, and dot controls outside the carousel with `<BlossomPrev>`, `<BlossomNext>`, and `<BlossomDots>`. Link them to the carousel with an `id` on `<BlossomCarousel>`, and a matching `forId` prop on each control and mark slides with `data-blossom-slide`.

```svelte
<BlossomCarousel id="my-carousel">
  {#each slides as slide (slide)}
    <div data-blossom-slide>Slide {slide}</div>
  {/each}
</BlossomCarousel>

<BlossomPrev forId="my-carousel" />
<BlossomDots forId="my-carousel" />
<BlossomNext forId="my-carousel" />
```

<note color="neutral" icon="i-lucide-lightbulb">

Place navigation components *after* their carousel in the markup, as above. `BlossomDots` renders one button per marked slide server-side, before any client JS runs, by reading the slide count that `BlossomCarousel` registers when it renders. Controls placed before the carousel start at zero dots and fill in on the client.

</note>

#### Prev/Next Buttons

`<BlossomPrev>` and `<BlossomNext>` are aware of configured scroll-snap and will navigate between snap points. When no scroll-snap is configured, they will slide the carousel proportionally.

Slot your own content to replace the default button icon.

```svelte
<BlossomPrev forId="my-carousel">
  <span>Previous</span>
</BlossomPrev>
```

#### Dots

`<BlossomDots>` renders one button per slide marked with `data-blossom-slide`.
Default styles can be themed with CSS custom properties on the component or any ancestor:

```css
/* defaults */
{
  --blossom-dot-size: 0.625rem;
  --blossom-dot-radius: 50%;
  --blossom-dot-color: currentColor;
  --blossom-dot-opacity: 0.35;
  --blossom-dot-hover-opacity: 0.6;
  --blossom-dot-active-opacity: 1;
}
```

To bring your own dots, provide a default slot and render `<BlossomDot>` inside it. This will configure the dot as a `<button>` with navigation wired up.
Now you can style the dot as you please and attach any button attributes you need.

```svelte
<BlossomDots forId="my-carousel">
  {#snippet children({ index, active })}
    <BlossomDot
      class="my-dot"
      data-active={active}
      aria-label="Photo {index + 1}"
    >
      {index + 1}
    </BlossomDot>
  {/snippet}
</BlossomDots>
```

#### Scroll behavior

Every navigation control scrolls smoothly by default. Set the `behavior` prop to `"instant"` or `"auto"` to change that. On `<BlossomDots>` it applies to every dot, and a `<BlossomDot>` can override it.

```svelte
<BlossomPrev forId="my-carousel" behavior="instant" />
<BlossomDots forId="my-carousel" behavior="instant" />
<BlossomNext forId="my-carousel" behavior="instant" />
```

#### Custom navigation UI

`useBlossomNavigation` returns the same state the built-in controls use: `activeIndex`, `count`, `canPrev`, and `canNext`. Call it during component initialization with the carousel id, or with a getter such as `() => id` to follow a changing id. `activeIndex` is `-1` until the carousel has been measured.

```svelte
<script>
  import { useBlossomNavigation } from "@blossom-carousel/svelte";

  const nav = useBlossomNavigation("my-carousel");
</script>

<span>{nav.activeIndex + 1} / {nav.count}</span>
```

Read the properties from the returned object, as above. Destructuring it loses reactivity.

#### Listening for commands

Listen for `command` events on the carousel to know when any navigation control is triggered or a [method](#methods) is called:

- previous (`--blossom-prev`)
- next (`--blossom-next`)
- dot (`--blossom-goto-{index}`).

These events are not fired by drag or free scrolling. Read `event.command` (or `event.detail.command` where the Invoker Commands polyfill applies).

```svelte
<script>
  function handleCommand(event) {
    const command = event?.command || event?.detail?.command;
  }
</script>

<BlossomCarousel oncommand={handleCommand}>
  {#each slides as slide (slide)}
    <div data-blossom-slide>Slide {slide}</div>
  {/each}
</BlossomCarousel>
```

See the [Buttons](https://www.blossom-carousel.com/docs/examples/basic/buttons) and [Dots](https://www.blossom-carousel.com/docs/examples/basic/dots) examples for live demos.

## Methods

Bind the component instance with `bind:this` to navigate from your own code:

- `prev(options?)` scrolls to the previous snap point or page.
- `next(options?)` scrolls to the next snap point or page.
- `goTo(index, options?)` scrolls to the slide at a zero-based `index`, counting slides marked with `data-blossom-slide`.

`options.behavior` accepts `"smooth"` (default), `"instant"`, or `"auto"`. The methods use native scrolling, so they also work on touch devices where the drag engine isn't loaded.

```svelte
<script>
  import { BlossomCarousel } from "@blossom-carousel/svelte";

  let carousel;
</script>

<BlossomCarousel bind:this={carousel}>
  {#each slides as slide (slide)}
    <div data-blossom-slide>Slide {slide}</div>
  {/each}
</BlossomCarousel>

<button type="button" onclick={() => carousel.goTo(0, { behavior: "instant" })}>
  Back to start
</button>
```

## Overscroll API

Tap into Blossom's drag engine's overscroll behavior to create your own style.

```svelte
<script>
  function onOverscroll(event) {
    // prevent and overwrite Blossom's default rubberbanding effect
    event.preventDefault();

    const overScroll = event.detail.left;

    Array.from(event.currentTarget.children).forEach((slide) => {
      slide.style.transform = `scale(${1 - overScroll * 0.1})`;
    });
  }
</script>

<BlossomCarousel onoverscroll={onOverscroll}>
  {#each Array.from({ length: 12 }, (_, index) => index + 1) as slide (slide)}
    <div>Slide {slide}</div>
  {/each}
</BlossomCarousel>
```

<note color="neutral" icon="i-lucide-lightbulb">

`BlossomCarousel` forwards unrecognized props to its root element, so `onoverscroll` attaches directly via `addEventListener`. There's no exposed ref to the root DOM node, so read the carousel off the event (`event.currentTarget`) rather than binding a ref.

</note>

## Sitemap

See the full [sitemap](https://www.blossom-carousel.com/sitemap.md) for all pages.
