React Splide

Introduction

React Splide is a React component for a Splide slider/carousel.

You are reading documentation for v0.7.0 or newer.

Installation

Get the latest version by NPM:

$ npm install @splidejs/react-splide

Usage

Components

Import Splide and SplideSlide components:

import { Splide, SplideSlide } from '@splidejs/react-splide';
JavaScript

...and render them like this:

<Splide aria-label="My Favorite Images">
<SplideSlide>
<img src="image1.jpg" alt="Image 1"/>
</SplideSlide>
<SplideSlide>
<img src="image2.jpg" alt="Image 2"/>
</SplideSlide>
</Splide>
JSX

If you have the visible heading for the carousel, use aria-labelledby instead of aria-label. See this page for more details.

CSS

Select a CSS file you want to use, and import it:

// Default theme
import '@splidejs/react-splide/css';
// or other themes
import '@splidejs/react-splide/css/skyblue';
import '@splidejs/react-splide/css/sea-green';
// or only core styles
import '@splidejs/react-splide/css/core';
JavaScript

Custom Structure

Although <Splide> renders a track element by default, you can handle them respectively with the hasTrack prop and the <SplideTrack> component. In a nutshell, following 2 components render the same HTML:

import { Splide, SplideTrack, SplideSlide } from '@splidejs/react-splide';
<Splide>
<SplideSlide>...</SplideSlide>
</Splide>
<Splide hasTrack={ false }>
<SplideTrack>
<SplideSlide>...</SplideSlide>
</SplideTrack>
</Splide>
JSX

Separating <SplideTrack> from <Splide> allows you to place arrows, pagination or other controls anywhere outside the track in the similar way of vanilla Splide. For example, Splide renders arrows before a track by default, but you are able to specify the location with a placeholder:

<Splide hasTrack={ false } aria-label="...">
<div className="custom-wrapper">
<SplideTrack>
<SplideSlide>...</SplideSlide>
</SplideTrack>
<div className="splide__arrows" />
</div>
</Splide>
JSX

...or with custom arrows:

<Splide hasTrack={ false } aria-label="...">
<SplideTrack>
<SplideSlide>...</SplideSlide>
</SplideTrack>
<div className="splide__arrows">
<button className="splide__arrow splide__arrow--prev">Prev</button>
<button className="splide__arrow splide__arrow--next">Next</button>
</div>
</Splide>
JSX

In the same way, you can add an autoplay toggle button and progress bar like so:

<Splide hasTrack={ false } aria-label="...">
<SplideTrack>
<SplideSlide>...</SplideSlide>
</SplideTrack>
<div className="splide__progress">
<div className="splide__progress__bar" />
</div>
<button class="splide__toggle" type="button">
<span class="splide__toggle__play">Play</span>
<span class="splide__toggle__pause">Pause</span>
</button>
</Splide>
JSX

...or:

<Splide hasTrack={ false } aria-label="...">
<div className="custom-wrapper">
<button class="splide__toggle" type="button">
<span class="splide__toggle__play">Play</span>
<span class="splide__toggle__pause">Pause</span>
</button>
<div className="splide__progress">
<div className="splide__progress__bar" />
</div>
<SplideTrack>
<SplideSlide>...</SplideSlide>
</SplideTrack>
</div>
</Splide>
JSX

Props

<Splide> accepts HTMLAttributes that will be assigned to a carousel root element, except for DOMAttributes. For instance, className and 'aria-label' are acceptable:

<Splide className="my-carousel" aria-label="My Favorite Images">
</Splide>
JSX

Additionally, it takes a few more props.


options

options: Options

Splide options as an object:

<Splide
options={ {
rewind: true,
width : 800,
gap : '1rem',
} }
>
</Splide>
JSX

This property is reactive, which means if you change values, the component will also update the carousel. But do not change readonly options, or the carousel will be broken.


tag

tag: 'div' | 'section' | 'header' | 'footer' | 'nav' = 'div'

Allows you to specify the tag name used for the root element. Although the default value is div for backward compatibility, 'section' is recommended.

If you are using header, footer, or nav, you have to provide the most appropriate landmark role together. Otherwise, your accessibility tree will be invalid.

<Splide tag="section"></Splide>
JSX

extensions

extensions: Record<string, ComponentConstructor>

Registers extensions as an object literal.


transition

transition: ComponentConstructor

Registers the custom transition component.


hasTrack

hasTrack: boolean = true

Determines whether to render a track or not.


Events

You can listen to all Splide events through the Splide component. The name of the callback function is generated by the original name with adding an "on" prefix, converting the format to the camelcase and removing colons. For example, "arrows:mounted" becomes "onArrowsMounted". The event list is available in this file.

<Splide onArrowsMounted={ ( splide, prev, next ) => { console.log( prev, next ) } }>
JSX

Note that the handler always takes the splide instance as the first argument, and original arguments after it.

Accessing Splide Instance

You can access the splide instance though a Ref object by a React.createRef method or an useRef hook.

<Splide ref={ ref }>
...
</Splide>
JSX

After React mounts the Splide component, the splide instance is available on ref.current.splide.

componentDidMount() {
if ( this.ref.current ) {
console.log( this.ref.current.splide.length );
}
}
// or
useEffect( () => {
if ( ref.current ) {
console.log( ref.current.splide.length );
}
} );
JSX

Example

Here is a small example:

import React from 'react';
import { Splide, SplideSlide } from '@splidejs/react-splide';
export default () => {
return (
<Splide
options={ {
rewind: true,
gap : '1rem',
} }
aria-label="My Favorite Images"
>
<SplideSlide>
<img src="image1.jpg" alt="Image 1"/>
</SplideSlide>
<SplideSlide>
<img src="image2.jpg" alt="Image 2"/>
</SplideSlide>
<SplideSlide>
<img src="image3.jpg" alt="Image 3"/>
</SplideSlide>
</Splide>
);
}
JSX

You can see working examples in this page and their sources here:

Migrating from v3

To migrate from v3 to v4:

  1. Read Breaking Changes
  2. If those changes may affect your carousel, modify your code according to the migration guide except for "Slider Element"
  3. If you are using removed props below, restructure your component as described here
  4. Rename the Extensions prop to extensions and the Transition prop to transition
  5. Make sure to update CSS (now short paths are available, but old ones still work)
  6. Optionally, translate newly added texts by the i18n option
  7. Optionally, add aria-label or aria-labelledby to each carousel

Here is the list of removed props:

  • hasSliderWrapper
  • hasAutoplayProgress
  • hasAutoplayControls
  • playButtonLabel
  • pauseButtonLabel
  • renderControls