466 lines
10 KiB
Plaintext
466 lines
10 KiB
Plaintext
<script>
|
|
import _ from 'lodash'
|
|
import Carousel from '../components/Carousel/Carousel.svelte'
|
|
import Color from './components/Color.svelte'
|
|
import AlbumsPreview from './components/AlbumsPreview/AlbumsPreview.svelte'
|
|
import Divider from './components/Divider.svelte'
|
|
import CustomDot from './components/CustomDot.svelte'
|
|
import images from './data/images.json'
|
|
import colors from './data/colors.json'
|
|
</script>
|
|
|
|
# Features
|
|
|
|
## Single item
|
|
<Carousel>
|
|
{#each colors as { color, text } (color)}
|
|
<Color {color} {text} />
|
|
{/each}
|
|
</Carousel>
|
|
|
|
```jsx
|
|
<Carousel>
|
|
{#each colors as { color, text } (color)}
|
|
<Color {color} {text} />
|
|
{/each}
|
|
</Carousel>
|
|
```
|
|
|
|
<Divider />
|
|
|
|
## Multiple items
|
|
<Carousel>
|
|
{#each _.chunk(colors, 3) as colorsChunk, chunkIndex (chunkIndex)}
|
|
<div style="display: flex;">
|
|
{#each colorsChunk as { color, text } (color)}
|
|
<Color {color} {text} />
|
|
{/each}
|
|
</div>
|
|
{/each}
|
|
</Carousel>
|
|
|
|
```jsx
|
|
<Carousel>
|
|
{#each _.chunk(colors, 3) as colorsChunk, chunkIndex (chunkIndex)}
|
|
<div style="display: flex;">
|
|
{#each colorsChunk as { color, text } (color)}
|
|
<Color {color} {text} />
|
|
{/each}
|
|
</div>
|
|
{/each}
|
|
</Carousel>
|
|
```
|
|
|
|
<Divider />
|
|
|
|
## Autoplay
|
|
<Carousel
|
|
autoplay={true}
|
|
autoplayDuration={2000}
|
|
>
|
|
{#each colors as { color, text } (color)}
|
|
<Color {color} {text} />
|
|
{/each}
|
|
</Carousel>
|
|
|
|
```jsx
|
|
<Carousel
|
|
autoplay={true}
|
|
autoplayDuration={2000}
|
|
>
|
|
{#each colors as { color, text } (color)}
|
|
<Color {color} {text} />
|
|
{/each}
|
|
</Carousel>
|
|
```
|
|
|
|
<Divider />
|
|
|
|
## Lazy loading of images
|
|
<Carousel
|
|
let:loaded
|
|
>
|
|
{#each images as src, imageIndex (src)}
|
|
<div class="img-container">
|
|
{#if loaded.includes(imageIndex)}
|
|
<img {src} alt="nature" />
|
|
{/if}
|
|
</div>
|
|
{/each}
|
|
</Carousel>
|
|
|
|
```jsx
|
|
<Carousel
|
|
let:loaded
|
|
>
|
|
{#each images as src, imageIndex (src)}
|
|
<div class="img-container">
|
|
{#if loaded.includes(imageIndex)}
|
|
<img {src} alt="nature" />
|
|
{/if}
|
|
</div>
|
|
{/each}
|
|
</Carousel>
|
|
```
|
|
|
|
<Divider />
|
|
|
|
## Arrows customization
|
|
<Carousel
|
|
let:showPrevPage
|
|
let:showNextPage
|
|
>
|
|
<div slot="prev" on:click={showPrevPage} class="custom-arrow custom-arrow-prev">
|
|
<i />
|
|
</div>
|
|
{#each colors as { color, text } (color)}
|
|
<Color {color} {text} />
|
|
{/each}
|
|
<div slot="next" on:click={showNextPage} class="custom-arrow custom-arrow-next">
|
|
<i />
|
|
</div>
|
|
</Carousel>
|
|
|
|
```jsx
|
|
<Carousel
|
|
let:showPrevPage
|
|
let:showNextPage
|
|
>
|
|
<div slot="prev" on:click={showPrevPage} class="custom-arrow custom-arrow-prev">
|
|
<i />
|
|
</div>
|
|
{#each colors as { color, text } (color)}
|
|
<Color {color} {text} />
|
|
{/each}
|
|
<div slot="next" on:click={showNextPage} class="custom-arrow custom-arrow-next">
|
|
<i />
|
|
</div>
|
|
</Carousel>
|
|
```
|
|
|
|
<Divider />
|
|
|
|
## Dots customization
|
|
<Carousel
|
|
let:currentPageIndex
|
|
let:pagesCount
|
|
let:showPage
|
|
>
|
|
<div slot="dots" class="custom-dots">
|
|
{#each Array(pagesCount) as _, pageIndex (pageIndex)}
|
|
<CustomDot
|
|
symbol={pageIndex + 1}
|
|
active={currentPageIndex === pageIndex}
|
|
on:click={() => showPage(pageIndex)}
|
|
></CustomDot>
|
|
{/each}
|
|
</div>
|
|
{#each colors as { color, text } (color)}
|
|
<Color {color} {text} />
|
|
{/each}
|
|
</Carousel>
|
|
|
|
```jsx
|
|
<Carousel
|
|
let:currentPageIndex
|
|
let:pagesCount
|
|
let:showPage
|
|
>
|
|
<div slot="dots" class="custom-dots">
|
|
{#each Array(pagesCount) as _, pageIndex (pageIndex)}
|
|
<CustomDot
|
|
symbol={pageIndex + 1}
|
|
active={currentPageIndex === pageIndex}
|
|
on:click={() => showPage(pageIndex)}
|
|
></CustomDot>
|
|
{/each}
|
|
</div>
|
|
{#each colors as { color, text } (color)}
|
|
<Color {color} {text} />
|
|
{/each}
|
|
</Carousel>
|
|
```
|
|
|
|
<Divider />
|
|
|
|
## Use case
|
|
<AlbumsPreview />
|
|
|
|
<Divider />
|
|
|
|
# Installation
|
|
```bash
|
|
yarn add svelte-carousel
|
|
```
|
|
```bash
|
|
npm install svelte-carousel
|
|
```
|
|
|
|
Import component
|
|
```jsx
|
|
<script>
|
|
import Carousel from 'svelte-carousel'
|
|
// ...
|
|
</script>
|
|
```
|
|
|
|
<Divider />
|
|
|
|
# Props
|
|
|
|
| Prop | Type | Default | Description |
|
|
|----------------------|------------|-----------------|-----------------------------------------------|
|
|
| `arrows` | `boolean` | `true` | Enable Next/Prev arrows |
|
|
| `infinite` | `boolean` | `true` | Infinite looping |
|
|
| `initialPageIndex` | `number` | `0` | Page to start on |
|
|
| `duration` | `number` | `500` | Transition duration (ms) |
|
|
| `autoplay` | `boolean` | `false` | Enables auto play of pages |
|
|
| `autoplayDuration` | `number` | `3000` | Auto play change interval (ms) |
|
|
| `autoplayDirection` | `string` | `'next'` | Auto play change direction (`next` or `prev`) |
|
|
| `dots` | `boolean` | `true` | Current page indicator dots |
|
|
| `pauseOnFocus` | `boolean` | `false` | Pause autoplay on focus |
|
|
| `timingFunction` | `string` | `'ease-in-out'` | CSS animation timing function |
|
|
|
|
<Divider />
|
|
|
|
# Events
|
|
|
|
## `pageChange`
|
|
Is dispatched on page change
|
|
|
|
| Payload field | Type | Description |
|
|
|--------------------|-------------|---------------------------------------|
|
|
| `event.detail` | `number` | Current page index |
|
|
|
|
```jsx
|
|
<Carousel
|
|
on:pageChange={
|
|
event => console.log(`Current page index: ${event.detail}`)
|
|
}
|
|
>
|
|
<!-- -->
|
|
</Carousel>
|
|
```
|
|
|
|
<Divider />
|
|
|
|
# Slots
|
|
|
|
## `prev` and `next`
|
|
They are used for customizing prev and next buttons.
|
|
|
|
Slot props:
|
|
|
|
| Prop | Type | Description |
|
|
|--------------------|-------------|---------------------------------------|
|
|
| `showPrevPage` | `function` | Call it to switch to the previos page |
|
|
| `showNextPage` | `function` | Call it to switch to the next page |
|
|
|
|
```jsx
|
|
<Carousel
|
|
let:showPrevPage
|
|
let:showNextPage
|
|
>
|
|
<div slot="prev">
|
|
<!-- -->
|
|
</div>
|
|
<div slot="next">
|
|
<!-- -->
|
|
</div>
|
|
<!-- -->
|
|
</Carousel>
|
|
```
|
|
|
|
## `dots`
|
|
|
|
This slot is used for customizing dots appearance.
|
|
|
|
Slot props:
|
|
|
|
| Prop | Type | Description |
|
|
|---------------------|---------- --|----------------------------------------------|
|
|
| `currentPageIndex` | `number` | Represents current page index (start from 0) |
|
|
| `pagesCount` | `number` | Total pages amount |
|
|
| `showPage` | `function` | Takes index as page to be shown |
|
|
|
|
```jsx
|
|
<Carousel
|
|
let:currentPageIndex
|
|
let:pagesCount
|
|
let:showPage
|
|
>
|
|
<div slot="dots">
|
|
<!-- -->
|
|
</div>
|
|
<!-- -->
|
|
</Carousel>
|
|
```
|
|
|
|
## Default slot
|
|
|
|
This slot takes content for the carousel.
|
|
|
|
Slot props:
|
|
|
|
| Prop | Type | Description |
|
|
|-------------------|------------|----------------------------------------------------------------------|
|
|
| `loaded` | `number[]` | Contains indexes of pages to be loaded. Can be used for lazy loading |
|
|
|
|
```jsx
|
|
<Carousel
|
|
let:loaded
|
|
>
|
|
<div>
|
|
<!-- -->
|
|
</div>
|
|
<!-- -->
|
|
</Carousel>
|
|
```
|
|
|
|
<Divider />
|
|
|
|
# Methods
|
|
|
|
## `goTo`
|
|
Navigates to a page by index
|
|
|
|
Arguments:
|
|
|
|
| Argument | Type | Default | Description |
|
|
|--------------------|-------------|---------|---------------------------------------|
|
|
| `pageIndex` | `number` | | Page number |
|
|
| `options.animated` | `boolean` | `true` | Should it be animated or not |
|
|
|
|
```jsx
|
|
<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
|
|
|
|
Arguments:
|
|
|
|
| Argument | Type | Default | Description |
|
|
|--------------------|-------------|---------|---------------------------------------|
|
|
| `options.animated` | `boolean` | `true` | Should it be animated or not |
|
|
|
|
```jsx
|
|
<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
|
|
|
|
Arguments:
|
|
|
|
| Argument | Type | Default | Description |
|
|
|--------------------|-------------|---------|---------------------------------------|
|
|
| `options.animated` | `boolean` | `true` | Should it be animated or not |
|
|
|
|
```jsx
|
|
<script>
|
|
// ...
|
|
let carousel;
|
|
function goToNextPage() {
|
|
carousel.goToNext({ animated: false })
|
|
}
|
|
</script>
|
|
|
|
<Carousel
|
|
bind:this={carousel}
|
|
>
|
|
<!-- -->
|
|
</Carousel>
|
|
<button class="button" on:click={goToNextPage}>Go</button>
|
|
```
|
|
|
|
<Divider />
|
|
|
|
<style>
|
|
.img-container {
|
|
display: block;
|
|
width: 100%;
|
|
height: 200px;
|
|
}
|
|
.img-container > img {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
-webkit-user-drag: none;
|
|
}
|
|
|
|
/* custom arrows */
|
|
.custom-arrow {
|
|
width: 20px;
|
|
background-color: #000000;
|
|
opacity: 0.3;
|
|
position: absolute;
|
|
top: 0;
|
|
bottom: 0;
|
|
z-index: 1;
|
|
transition: opacity 150ms ease;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
cursor: pointer;
|
|
-webkit-tap-highlight-color: transparent;
|
|
}
|
|
.custom-arrow:hover {
|
|
opacity: 0.5;
|
|
}
|
|
.custom-arrow > i {
|
|
border: solid #1e1e1e;
|
|
border-width: 0 5px 5px 0;
|
|
padding: 5px;
|
|
position: relative;
|
|
}
|
|
.custom-arrow-prev {
|
|
left: 0;
|
|
}
|
|
.custom-arrow-prev > i {
|
|
transform: rotate(135deg);
|
|
right: -4px;
|
|
}
|
|
.custom-arrow-next {
|
|
right: 0;
|
|
}
|
|
.custom-arrow-next > i {
|
|
transform: rotate(-45deg);
|
|
left: -4px;
|
|
}
|
|
|
|
/* custom dots */
|
|
.custom-dots {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 0 20px;
|
|
}
|
|
</style> |