Skip to main content
Carousel
@coinbase/cds-web@8.34.1
A flexible carousel component for displaying sequences of content with navigation and pagination options.
Import
import { Carousel } from '@coinbase/cds-web/carousel/Carousel'
SourceView source codeStorybookView StorybookFigmaView Figma (internal only)
View as Markdown

Basic Example

Carousels are a great way to showcase a list of items in a compact and engaging way. By default, Carousels have navigation and pagination enabled. You can also add a title to the Carousel by setting title prop.

You simply wrap each child in a CarouselItem component, and can optionally set the width prop to control the width of the item.

You can also set the styles prop to control the styles of the carousel, such as the gap between items.

Images

Images inside of the carousel have pointer-events disabled by default.

Loading...

Item Sizing

Items by default take their natural width while in the carousel, such as from our example above. However, you can set the width prop of CarouselItem to control the width of the item.

Dynamic Sizing

Items can be given a width proportional to the carousel width.

Tip

If you have a gap between items, you should account for that in the width. For example, if you have a gap of 8px, and you want to show 2 items per page, you should give each item a width of calc(50% - 4px).

Loading...

Responsive Sizing

You can also use responsive props to change the number of items visible based on the carousel width. The carousel below will show per page 1 item on mobile, 2 items on tablet, and 3 items on desktop.

Loading...

Varied Sizing

Not all carousel items need to be the same size. You can provide CarouselItems of varying widths as well.

Loading...

Drag

You can set the drag prop to snap (default), free, or none. When set to snap, upon release the carousel will snap to either the nearest item or page (depending on snapMode).

Loading...

Snap Mode

You can set the snapMode to page (default) or item. When set to page, the carousel will automatically group items into pages. When set to item, the carousel will snap to the nearest item.

Loading...

Overflow

By default, the carousel's inner overflow is visible. This means that you can apply padding to the inner carousel element (such as styles={{ carousel: { paddingInline: 'var(--space-3)' } }}) and it will not be clipped. You can pair this with modifying the spacing of the inner carousel to match the padding of your page (along with a wrapping div to negate any default spacing). This creates a seamless experience.

Tip

If you want to have the next item be shown at the edge of the screen, make sure your carousel padding is larger than your gap.

Loading...

Accessibility

The carousel is accessible by default.

You need to use accessibilityLabel or accessibilityLabelledBy props to provide a label for the carousel items.

If you have elements that are focusable, you can use isVisible render prop to disable focus when the item is not visible.

<Carousel>
<CarouselItem id="btc" accessibilityLabel="Bitcoin">
<SquareAssetCard imageUrl={assets.btc.imageUrl} name={assets.btc.symbol} />
</CarouselItem>
<CarouselItem id="recurring-buy" width="100%" accessibilityLabelledBy="recurring-buy-label">
{({ isVisible }) => (
<UpsellCard
action={
<Button
compact
flush="start"
numberOfLines={1}
onClick={NoopFn}
tabIndex={isVisible ? undefined : -1}
variant="secondary"
>
Get started
</Button>
}
description="Want to add funds to your card every week or month?"
media={
<Box bottom={6} position="relative" right={24}>
<Pictogram dimension="64x64" name="recurringPurchases" />
</Box>
}
minWidth="0"
title={
<Text as="h3" font="headline" id="recurring-buy-label">
Recurring Buy
</Text>
}
width="100%"
/>
)}
</CarouselItem>
</Carousel>

Customization

Custom Components

You can customize the navigation and pagination components of the carousel using the NavigationComponent and PaginationComponent props. You can also modify the title by providing a ReactNode for the title prop.

Loading...

Custom Styles

You can use the classNames and styles props to customize different parts of the carousel.

Loading...

Dynamic Content

You can dynamically add and remove items from the carousel.

Loading...

You can also animate items as they enter or leave the viewport.

Loading...

You can even change the size or content of items. In the example below, click an asset to highlight it.

Loading...

Hide Navigation and Pagination

You can hide the navigation and pagination components of the carousel if desired (using hideNavigation and hidePagination props).

Note that this can prevent proper accessibility for the carousel. If hiding pagination, it's recommended instead to pass in DefaultCarouselNavigation with hideUnlessFocused prop. Alternatively, you can ensure that the carousel is navigable by keyboard through other means.

Loading...

Animated Pagination

You can create smooth pagination animations by customizing the pagination dots. This example shows how to create expanding dots that smoothly transition between active and inactive states.

Loading...

Imperative API

You can control the carousel programmatically using a ref. The carousel exposes methods to navigate to specific pages and access the current page index.

Loading...

Callbacks

You can use the onChangePage, onDragStart, and onDragEnd callbacks to listen for user interaction in the carousel.

<Carousel
onChangePage={(pageIndex: number) => console.log('Page changed', activePageIndex)}
onDragStart={() => console.log('Drag started')}
onDragEnd={() => console.log('Drag ended')}
>
...
</Carousel>

Is this page useful?

Coinbase Design is an open-source, adaptable system of guidelines, components, and tools that aid the best practices of user interface design for crypto products.