salla-slider

Swiper-based slider for banners, product carousels, testimonials and product image galleries with thumbnails.

Swiper-based slider for banners, product carousels, testimonials and product image galleries with thumbnails. Use it whenever a theme needs a swipeable list of items. Put the slides as children of one <div slot="items">; each child becomes a slide. For type="thumbs", add the thumbnails as children of a <div slot="thumbs">. Important: the arrows and the pagination find their elements through the host id, so set an id on salla-slider if you use show-controls or pagination. Set static-when-single to skip the slider when there is only one slide.

Accessibility: the arrow buttons are real button elements with aria-labels ("Previous Slide" and "Next Slide"), and pagination bullets are clickable.

Use it

In HTML or JSX, use the tag <salla-slider> with the Attribute names below. In React, the wrapper takes the Property names (camelCase).

This component has a native React version. Use it instead of the raw tag:

import { SallaSlider } from '@salla.sa/twilight-components-react';

Try it

web-components/salla-slider

Example

import { SallaSlider } from '@salla.sa/twilight-components-react';

export function Example() {
  return (
    <SallaSlider id="home-slider" type="carousel" blockTitle="New arrivals" displayAllUrl="/products" pagination>
      <div slot="items">
        <div style={{ width: '240px' }}>Slide 1</div>
        <div style={{ width: '240px' }}>Slide 2</div>
        <div style={{ width: '240px' }}>Slide 3</div>
      </div>
    </SallaSlider>
  );
}
import { SallaSlider } from '@salla.sa/twilight-components-react';

export function Example() {
  return (
    <SallaSlider id="product-gallery" type="thumbs" showThumbsControls={false}>
      <div slot="items"><img src="/img/1.jpg" alt="" /><img src="/img/2.jpg" alt="" /></div>
      <div slot="thumbs"><img src="/img/1-thumb.jpg" alt="" /><img src="/img/2-thumb.jpg" alt="" /></div>
    </SallaSlider>
  );
}

Properties

PropertyAttributeDescriptionTypeDefault
arrowsCenteredarrows-centeredAdds the s-slider-v-centered class to the host. The arrow position is styled by the theme CSS.booleanfalse
autoHeightauto-heightLets the slider height follow the height of the active slide.booleanfalse
autoPlayauto-playStarts autoplay with a 10 second delay between slides. Pass a Swiper autoplay options object through slider-config to use a different delay.booleanfalse
blockSubtitleblock-subtitleSubtitle shown under the title. It is inserted as HTML. Only shown when block-title is set.string''
blockTitleblock-titleTitle text shown above the slider as an h2. When empty, no title is rendered.string''
centeredcenteredCenters the active slide (Swiper centeredSlides).booleanfalse
controlsOutercontrols-outerAdds the s-slider-controls-outer class so the theme CSS can place the arrows outside the slider.booleanfalse
directiondirectionReading direction of the slider, rtl or ltr. Defaults to document.documentElement.dir.string
displayAllUrldisplay-all-urlURL for a "Display all" link shown beside the arrows. When empty, no link is rendered.string''
gridThumbsgrid-thumbsAdds the s-slider-grid-thumbs class to the thumbs wrapper so the theme CSS can show the thumbs as a grid. Only used with type="thumbs".booleanfalse
listenToThumbnailsOptionlisten-to-thumbnails-optionWhen true, the slider jumps to a slide when a product option of type thumbnail or color is changed (the product-options::change event). Each slide needs data-img-id (the gallery image id) and data-slid-index (the slide index) attributes. Read once at load.booleanfalse
looploopRun slider in loop, Don't use it for slides with custom components inside it, because it may cause re-render issuebooleanfalse
paginationpaginationShows clickable pagination bullets. Only works when the slider host has an id attribute.booleanfalse
showControlsshow-controlsShows the previous and next arrow buttons. They only work when the slider host has an id attribute.booleantrue
showThumbsControlsshow-thumbs-controlsShows the previous and next arrows of the thumbs slider. Only used with type="thumbs", and the arrows only work when the slider host has an id attribute.booleantrue
sliderConfigslider-configExtra Swiper parameters, as an object or a JSON string. They are merged over the component defaults and the type preset, so they win. See https://swiperjs.com/swiper-api#parameters.any
slidesPerViewslides-per-viewValue passed to Swiper slidesPerView: auto or a number of slides shown at once.string"auto"
staticWhenSinglestatic-when-singleWhen enabled and there is exactly one slide in the items slot, render without initializing Swiper (single slide mode).booleanfalse
thumbsConfigthumbs-configExtra Swiper parameters for the thumbs slider, as an object or a JSON string, merged over its defaults. Only used with type="thumbs" and when the thumbs slot has content. See https://swiperjs.com/swiper-api#parameters.any
typetypePreset for the slider. carousel shows slides at their natural width; fullscreen is a vertical slider with mouse wheel navigation; thumbs is a main slider synced with a thumbnails slider from the thumbs slot (typical for product galleries); testimonials shows 1 slide (2 from 1024px); blog loops with centered slides; fullwidth uses a slower parallax transition. default, hero and an empty value apply no preset (hero and default only add a CSS class)."" | "blog" | "carousel" | "default" | "fullscreen" | "fullwidth" | "hero" | "testimonials" | "thumbs"''
verticalverticalMakes the main slider vertical from 768px viewport width and up (it is horizontal on smaller screens). The slider container is rendered with dir="ltr" when this is on.booleanfalse
verticalThumbsvertical-thumbsLays out the thumbs slider vertically from 768px viewport width and up. Only used with type="thumbs".booleanfalse

sliderConfig example

let slider = document.querySelector('salla-slider');
slider.sliderConfig = {
  slidesPerView: 1,
  spaceBetween: 30,
}

thumbsConfig example

let slider = document.querySelector('salla-slider');
slider.thumbsConfig = {
  slidesPerView: 3,
  spaceBetween: 10,
}

Events

Listen with addEventListener, or the on prop of the React wrapper (for example onModalBack).

EventDescriptionDetail
afterInitFires once Swiper has been initialized. event.detail is the Swiper instance.any
reachBeginningFires when the slider reaches the first slide. event.detail is the Swiper instance.any
reachEndFires when the slider reaches the last slide. event.detail is the Swiper instance.any
slideChangeFires when the active slide changes. event.detail is the Swiper instance.any
slideChangeTransitionEndFires after the animation to another slide (next or previous) ends. event.detail is the Swiper instance.any
slideChangeTransitionStartFires when the animation to another slide (next or previous) starts. event.detail is the Swiper instance.any
slideNextTransitionEndSame as slideChangeTransitionEnd but for forward movement only. event.detail is the Swiper instance.any
slideNextTransitionStartSame as slideChangeTransitionStart but for forward movement only. event.detail is the Swiper instance.any
slidePrevTransitionEndSame as slideChangeTransitionEnd but for backward movement only. event.detail is the Swiper instance.any
slidePrevTransitionStartSame as slideChangeTransitionStart but for backward movement only. event.detail is the Swiper instance.any
sliderMoveFires while the user drags the slider with a finger or mouse. event.detail is the Swiper instance.any
sliderTransitionEndFires after a transition ends. event.detail is the Swiper instance.any
sliderTransitionStartFires when a transition starts. event.detail is the Swiper instance.any
touchSliderEndFires when the user releases the slider after touching it. event.detail is the Swiper instance.any
touchSliderMoveFires when the user touches the slider and moves their finger. event.detail is the Swiper instance.any
touchSliderStartFires when the user touches the slider. event.detail is the Swiper instance.any

Methods

Call these on the element or a React ref.

getSlides() => Promise<any>

Returns the slide elements of the main slider.

Returns Promise<any>: A Promise that resolves to the Swiper slides array, or undefined before the slider has initialized.

getThumbsSlides() => Promise<any>

Returns the slide elements of the thumbs slider.

Returns Promise<any>: A Promise that resolves to the thumbs Swiper slides array, or undefined if there is no thumbs slider.

getThumbsUpdateSize() => Promise<any>

Recalculates the size of the thumbs slider container. Does nothing if there is no thumbs slider.

Returns Promise<any>: A Promise that resolves when the update is done.

slideNext(speed?: number, runCallbacks?: boolean) => Promise<void>

Slides to the next slide. Does nothing until the slider has initialized.

ParameterTypeDescription
speednumberTransition duration in milliseconds.
runCallbacksbooleanSet to false to skip the transition events. Defaults to true.

slideNextLoop(speed?: number, runCallbacks?: boolean) => Promise<void>

Same as slideNext, for sliders with loop on. Does nothing until the slider has initialized.

ParameterTypeDescription
speednumberTransition duration in milliseconds.
runCallbacksbooleanSet to false to skip the transition events. Defaults to true.

slidePrev(speed?: number, runCallbacks?: boolean) => Promise<void>

Slides to the previous slide. Does nothing until the slider has initialized.

ParameterTypeDescription
speednumberTransition duration in milliseconds.
runCallbacksbooleanSet to false to skip the transition events. Defaults to true.

slidePrevLoop(speed?: number, runCallbacks?: boolean) => Promise<void>

Same as slidePrev, for sliders with loop on. Does nothing until the slider has initialized.

ParameterTypeDescription
speednumberTransition duration in milliseconds.
runCallbacksbooleanSet to false to skip the transition events. Defaults to true.

slideReset(speed?: number, runCallbacks?: boolean) => Promise<void>

Returns the slider to the position of the currently active slide. Does nothing until the slider has initialized.

ParameterTypeDescription
speednumberTransition duration in milliseconds.
runCallbacksbooleanSet to false to skip the transition events. Defaults to true.

slideTo(index: number, speed?: number, runCallbacks?: boolean) => Promise<any>

Slides to the slide at the given index. Does nothing until the slider has initialized.

ParameterTypeDescription
indexnumberSlide index, starting at 0.
speednumberTransition duration in milliseconds.
runCallbacksbooleanSet to false to skip the transition events. Defaults to true.

Returns Promise<any>: A Promise that resolves to what Swiper slideTo returns.

slideToClosest(speed?: number, runCallbacks?: boolean) => Promise<void>

Moves the slider to the closest slide position. Does nothing until the slider has initialized.

ParameterTypeDescription
speednumberTransition duration in milliseconds.
runCallbacksbooleanSet to false to skip the transition events. Defaults to true.

slideToLoop(index: number, speed?: number, runCallbacks?: boolean) => Promise<void>

Same as slideTo, for sliders with loop on: slides to the slide whose real index (the index in the original slides) matches. Does nothing until the slider has initialized.

ParameterTypeDescription
indexnumberReal slide index, starting at 0.
speednumberTransition duration in milliseconds.
runCallbacksbooleanSet to false to skip the transition events. Defaults to true.

sliderInit() => Promise<any>

Calls Swiper init() on the main slider. Does nothing until the slider has initialized.

Returns Promise<any>: A Promise that resolves when it has finished.

sliderInstance() => Promise<any>

Returns the Swiper instance of the main slider so you can use the Swiper API directly.

Returns Promise<any>: A Promise that resolves to the Swiper instance, or undefined before the slider has initialized.

thumbsSlideTo(index: number, speed?: number, runCallbacks?: boolean) => Promise<any>

Slides the thumbs slider to the slide at the given index. Does nothing if there is no thumbs slider.

ParameterTypeDescription
indexnumberSlide index, starting at 0.
speednumberTransition duration in milliseconds.
runCallbacksbooleanSet to false to skip the transition events. Defaults to true.

Returns Promise<any>: A Promise that resolves to what Swiper slideTo returns.

thumbsSliderInit() => Promise<any>

Calls Swiper init() on the thumbs slider. Only call it with type="thumbs" after the slider has initialized, otherwise it throws.

Returns Promise<any>: A Promise that resolves when it has finished.

thumbsSliderInstance() => Promise<any>

Returns the Swiper instance of the thumbs slider (only exists with type="thumbs").

Returns Promise<any>: A Promise that resolves to the thumbs Swiper instance, or undefined.

thumbsSliderUpdate() => Promise<any>

Runs Swiper update() on the thumbs slider. Only call it with type="thumbs" after the slider has initialized, otherwise it throws.

Returns Promise<any>: A Promise that resolves when the update is done.

thumbsSliderUpdateSlides() => Promise<any>

Runs Swiper updateSlides() on the thumbs slider. Only call it with type="thumbs" after the slider has initialized, otherwise it throws.

Returns Promise<any>: A Promise that resolves when the update is done.

update() => Promise<void>

Recalculates the slider (size, slides, progress and classes). Call it after you add or remove slides in JavaScript, or after showing a slider that was hidden. Does nothing until the slider has initialized.

updateAutoHeight(speed?: number) => Promise<void>

Recalculates the slider height to match the active slide. Useful with auto-height. Does nothing until the slider has initialized.

ParameterTypeDescription
speednumberTransition duration in milliseconds.

updateProgress() => Promise<void>

Recalculates the slider progress value. Does nothing until the slider has initialized.

updateSize() => Promise<any>

Recalculates the size of the slider container. Call it after the container size changed. Does nothing until the slider has initialized.

Returns Promise<any>: A Promise that resolves when the update is done.

updateSlides() => Promise<void>

Recalculates the number of slides and their offsets. Call it after you add or remove slides in JavaScript. Does nothing until the slider has initialized.

updateSlidesClasses() => Promise<void>

Updates the active, previous and next classes on the slides and pagination bullets. Does nothing until the slider has initialized.

Slots

SlotDescription
(default)Default slot, rendered inside the slider container before the slides.
itemsSlider items. Wrap them in one <div slot="items">; each child becomes a slide.
thumbsThumbs slider items, used when type="thumbs". Wrap them in one <div slot="thumbs">.

Used inside: <salla-attachment-view>, <salla-cart-coupons>, <salla-gifting>, <salla-loyalty>, <salla-loyalty-panel>, <salla-loyalty-program>, <salla-multiple-bundle-product-options-modal>, <salla-multiple-bundle-product-slider>, <salla-offer>, <salla-offer-modal>, <salla-products-slider>, <salla-reviews>, <salla-reward-details>.

On this page