---
title: "React and Next.js"
description: "Use Blossom Carousel in React and Next.js apps with tsx, global styles, and semantic root elements."
canonical_url: "https://www.blossom-carousel.com/docs/framework-guides/react-nextjs"
---

> 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/react
```

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

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

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

</code-group>

## React usage

```tsx
import { BlossomCarousel } from "@blossom-carousel/react";
import "@blossom-carousel/react/style.css";

export function App() {
  return (
    <BlossomCarousel>
      {Array.from({ length: 12 }, (_, index) => (
        <div key={index}>Slide {index + 1}</div>
      ))}
    </BlossomCarousel>
  );
}
```

## Next.js setup

Import the stylesheet from a global entry point.

```tsx [app/layout.tsx]
import "@blossom-carousel/react/style.css";

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}
```

Then render the carousel from a component. In the App Router, files that render Blossom components must be Client Components, so start them with `"use client"`.

```tsx [FeaturedCarousel.tsx]
"use client";

import { BlossomCarousel } from "@blossom-carousel/react";

export function FeaturedCarousel() {
  return (
    <BlossomCarousel>
      {Array.from({ length: 12 }, (_, index) => (
        <div key={index}>Slide {index + 1}</div>
      ))}
    </BlossomCarousel>
  );
}
```

## Semantic root element

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

```tsx
<BlossomCarousel as="ul">
  {Array.from({ length: 12 }, (_, index) => (
    <li key={index}>Slide {index + 1}</li>
  ))}
</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>
        ElementType
      </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 `for` prop on each control and mark slides with `data-blossom-slide`.

```tsx
<BlossomCarousel id="my-carousel">
  {Array.from({ length: 12 }, (_, index) => (
    <div key={index} data-blossom-slide>
      Slide {index + 1}
    </div>
  ))}
</BlossomCarousel>

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

#### 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.

```tsx
<BlossomPrev for="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.

```tsx
<BlossomDots for="my-carousel">
  {({ index, active }) => (
    <BlossomDot
      className="my-dot"
      data-active={active}
      aria-label={`Photo ${index + 1}`}
    >
      {index + 1}
    </BlossomDot>
  )}
</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.

```tsx
<BlossomPrev for="my-carousel" behavior="instant" />
<BlossomDots for="my-carousel" behavior="instant" />
<BlossomNext for="my-carousel" behavior="instant" />
```

#### Custom navigation UI

`useBlossomNavigation` returns the same state the built-in controls use: `activeIndex`, `count`, `canPrev`, and `canNext`. It only re-renders when one of them changes. `activeIndex` is `-1` until the carousel has been measured.

```tsx
import { useBlossomNavigation } from "@blossom-carousel/react";

function SlideCounter() {
  const { activeIndex, count } = useBlossomNavigation("my-carousel");
  return (
    <span>
      {activeIndex + 1} / {count}
    </span>
  );
}
```

#### 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).

```tsx
import { BlossomCarousel, type BlossomCommandEvent } from "@blossom-carousel/react";

<BlossomCarousel onCommand={handleCommand}>
  {Array.from({ length: 12 }, (_, index) => (
    <div key={index} data-blossom-slide>
      Slide {index + 1}
    </div>
  ))}
</BlossomCarousel>;

function handleCommand(event: BlossomCommandEvent) {
  const command = event.command ?? event.detail?.command;
}
```

<note>

`onCommand` and `onOverscroll` require `@blossom-carousel/react` 1.8.0 or later. Earlier versions passed them to the DOM, where React ignored them.

</note>

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

Pass a `ref` to `<BlossomCarousel>` to navigate from your own code. The ref receives a `BlossomCarouselHandle`:

- `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`.
- `element` is the carousel's root element.

`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.

```tsx
import { useRef } from "react";
import { BlossomCarousel, type BlossomCarouselHandle } from "@blossom-carousel/react";

export function Gallery() {
  const carousel = useRef<BlossomCarouselHandle>(null);

  return (
    <>
      <BlossomCarousel ref={carousel}>
        {Array.from({ length: 12 }, (_, index) => (
          <div key={index} data-blossom-slide>
            Slide {index + 1}
          </div>
        ))}
      </BlossomCarousel>

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

## Overscroll API

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

```tsx
import { BlossomCarousel, type BlossomOverscrollEvent } from "@blossom-carousel/react";

export function App() {
  function onOverscroll(event: BlossomOverscrollEvent) {
    // prevent and overwrite Blossom's default rubberbanding effect
    event.preventDefault();

    const overScroll = event.detail.left;
    const carousel = event.currentTarget as HTMLElement;

    Array.from(carousel.children).forEach((slide) => {
      (slide as HTMLElement).style.transform = `scale(${1 - overScroll * 0.1})`;
    });
  }

  return (
    <BlossomCarousel onOverscroll={onOverscroll}>
      {Array.from({ length: 12 }, (_, index) => (
        <div key={index}>Slide {index + 1}</div>
      ))}
    </BlossomCarousel>
  );
}
```

## Sitemap

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