Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

svelte-carousel

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svelte-carousel

Svelte carousel

  • 1.0.25
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

svelte-carousel

npm npm
GitHub repo GitHub followers

The awesome carousel component for Svelte 3

Demo

Installation

yarn add svelte-carousel
npm install svelte-carousel

Import component

<script>
  import Carousel from 'svelte-carousel'
  // ...
</script>

SvelteKit support

There are several things to keep in mind when svelte-carousel is used with SvelteKit. This is because svelte-carousel is a client-side library and depends on document and window. See more in SvelteKit FAQ.

  1. Install svelte-carousel as a dev dependency. Why as a dev dependency?
yarn add svelte-carousel -D
npm install svelte-carousel -D
  1. Extend kit in svelte.config.js to include the vite property
const config = {
  // existing props
  kit: {
    // existing props
    vite: {
      optimizeDeps: {
        include: ['lodash.get', 'lodash.isequal', 'lodash.clonedeep']
      }
      // plugins: []
    }
  }
}
  1. Import and use it:

<script>
  import Carousel from 'svelte-carousel';
  import { browser } from '$app/environment';

  let carousel; // for calling methods of the carousel instance
  
  const handleNextClick = () => {
    carousel.goToNext()
  }
</script>

{#if browser}
  <Carousel
    bind:this={carousel}
  >
    <div>1</div>
    <div>2</div>
    <div>3</div>
  </Carousel>
{/if}

<button on:click={handleNextClick}>Next</button>

Vite support

  1. Extend optimizeDeps.include in vite.config.js
export default defineConfig({
  optimizeDeps: {
    include: ['lodash.get', 'lodash.isequal', 'lodash.clonedeep']
  }
})
  1. Import and use it:

<script>
  import Carousel from 'svelte-carousel'

  let carousel; // for calling methods of the carousel instance
  const handleNextClick = () => {
    carousel.goToNext()
  }
</script>

<Carousel
  bind:this={carousel}
>
  <div>1</div>
  <div>2</div>
  <div>3</div>
</Carousel>

<button on:click={handleNextClick}>Next</button>

Props

PropTypeDefaultDescription
arrowsbooleantrueEnables next/prev arrows
infinitebooleantrueInfinite looping
initialPageIndexnumber0Page to start on
durationnumber500Transition duration (ms)
autoplaybooleanfalseEnables autoplay of pages
autoplayDurationnumber3000Autoplay change interval (ms)
autoplayDirectionstring'next'Autoplay change direction (next or prev)
pauseOnFocusbooleanfalsePauses autoplay on focus (for touchable devices - tap the carousel to toggle the autoplay, for non-touchable devices - hover over the carousel to pause the autoplay)
autoplayProgressVisiblebooleanfalseShows autoplay duration progress indicator
dotsbooleantrueCurrent page indicator dots
timingFunctionstring'ease-in-out'CSS animation timing function
swipingbooleantrueEnables swiping
particlesToShownumber1Number of elements to show
particlesToScrollnumber1Number of elements to scroll

Events

pageChange

It is dispatched on page change

Payload fieldTypeDescription
event.detailnumberCurrent page index
<Carousel
  on:pageChange={
    event => console.log(`Current page index: ${event.detail}`)
  }
>
  <!-- -->
</Carousel>

Slots

prev and next

They are used for customizing prev and next buttons.

Slot props:

PropTypeDescription
showPrevPagefunctionCall it to switch to the previos page
showNextPagefunctionCall it to switch to the next page
<Carousel
  let:showPrevPage
  let:showNextPage
>
  <div slot="prev">
    <!-- -->
  </div>
  <div slot="next">
    <!-- -->
  </div>
  <!-- -->
</Carousel>

dots

This slot is used for customizing how dots look like.

Slot props:

PropTypeDescription
currentPageIndexnumberRepresents current page index (start from 0)
pagesCountnumberTotal pages amount
showPagefunctionTakes index as page to be shown
<Carousel
  let:currentPageIndex
  let:pagesCount
  let:showPage
>
  <div slot="dots">
    <!-- -->
  </div>
  <!-- -->
</Carousel>

Default slot

This slot takes content for the carousel.

Slot props:

PropTypeDescription
loadednumber[]Contains indexes of pages to be loaded. Can be used for lazy loading
currentPageIndexnumberRepresents current page index (start from 0)
<Carousel
  let:loaded
>
  <div>
    <!-- -->
  </div>
  <!-- -->
</Carousel>

Methods

goTo

Navigates to a page by index. (pageIndex, options) => Promise<void>.

Arguments:

ArgumentTypeDefaultDescription
pageIndexnumberPage number
options.animatedbooleantrueShould it be animated or not
<script>
  // ...
  let carousel;
  function goToStartPage() {
    carousel.goTo(0, { animated: false })
  }
</script>

<Carousel
  bind:this={carousel}
>
  <!--  -->
</Carousel>
<button class="button" on:click={goToStartPage}>Go</button>

goToPrev

Navigates to the previous page. (options) => Promise<void>.

Arguments:

ArgumentTypeDefaultDescription
options.animatedbooleantrueShould it be animated or not
<script>
  // ...
  let carousel;
  function goToPrevPage() {
    carousel.goToPrev({ animated: false })
  }
</script>

<Carousel
  bind:this={carousel}
>
  <!--  -->
</Carousel>
<button class="button" on:click={goToPrevPage}>Go</button>

goToNext

Navigates to the next page. (options) => Promise<void>.

Arguments:

ArgumentTypeDefaultDescription
options.animatedbooleantrueShould it be animated or not
<script>
  // ...
  let carousel;
  function goToNextPage() {
    carousel.goToNext({ animated: false })
  }
</script>

<Carousel
  bind:this={carousel}
>
  <!--  -->
</Carousel>
<button class="button" on:click={goToNextPage}>Go</button>

Keywords

FAQs

Package last updated on 03 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc