Add dots
This commit is contained in:
65
src/Dots.svelte
Normal file
65
src/Dots.svelte
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
<script>
|
||||||
|
import { createEventDispatcher } from 'svelte'
|
||||||
|
const dispatch = createEventDispatcher()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Amount of pages (amount of dots)
|
||||||
|
*/
|
||||||
|
export let pagesCount = 1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Index of the current page
|
||||||
|
*/
|
||||||
|
export let currentPageIndex = 0
|
||||||
|
|
||||||
|
function handleDotClick(pageIndex) {
|
||||||
|
dispatch('pageChange', pageIndex)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="main-container">
|
||||||
|
{#each Array(pagesCount) as _, pageIndex}
|
||||||
|
<div class="dot-container">
|
||||||
|
<div
|
||||||
|
class="dot"
|
||||||
|
class:current="{currentPageIndex === pageIndex}"
|
||||||
|
on:click={() => handleDotClick(pageIndex)}
|
||||||
|
></div>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
:root {
|
||||||
|
--dot-size: 10px;
|
||||||
|
}
|
||||||
|
.main-container {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.dot-container {
|
||||||
|
height: calc(var(--dot-size) + 10px);
|
||||||
|
width: calc(var(--dot-size) + 10x);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
.dot {
|
||||||
|
height: var(--dot-size);
|
||||||
|
width: var(--dot-size);
|
||||||
|
background-color: #aaa;
|
||||||
|
border-radius: 50%;
|
||||||
|
display: inline-block;
|
||||||
|
opacity: 0.5;
|
||||||
|
margin: 3px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.dot:hover {
|
||||||
|
opacity: 0.9;
|
||||||
|
}
|
||||||
|
.current {
|
||||||
|
height: calc(var(--dot-size) + 3px);
|
||||||
|
width: calc(var(--dot-size) + 3px);
|
||||||
|
opacity: 0.7;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
getSlideSize,
|
getSlideSize,
|
||||||
getIsNotCompletePage
|
getIsNotCompletePage
|
||||||
} from './utils/size'
|
} from './utils/size'
|
||||||
|
import Dots from './Dots.svelte'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable Next/Prev arrows
|
* Enable Next/Prev arrows
|
||||||
@@ -51,6 +52,11 @@
|
|||||||
*/
|
*/
|
||||||
export let autoplayDirection = 'next'
|
export let autoplayDirection = 'next'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current page indicator dots
|
||||||
|
*/
|
||||||
|
export let dots = true
|
||||||
|
|
||||||
let pagesCount = 0
|
let pagesCount = 0
|
||||||
let contentContainerWidth = 0
|
let contentContainerWidth = 0
|
||||||
let offset
|
let offset
|
||||||
@@ -106,6 +112,10 @@
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
function handlePageChange(event) {
|
||||||
|
showPage(event.detail)
|
||||||
|
}
|
||||||
|
|
||||||
function applyOffset() {
|
function applyOffset() {
|
||||||
offset = -$store.currentItemIndex * contentContainerWidth
|
offset = -$store.currentItemIndex * contentContainerWidth
|
||||||
}
|
}
|
||||||
@@ -125,6 +135,7 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="main-container">
|
<div class="main-container">
|
||||||
|
<div class="carousel-container">
|
||||||
{#if arrows}
|
{#if arrows}
|
||||||
<div class="side-container">
|
<div class="side-container">
|
||||||
<span
|
<span
|
||||||
@@ -156,11 +167,25 @@
|
|||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
{#if dots}
|
||||||
|
<Dots
|
||||||
|
{pagesCount}
|
||||||
|
currentPageIndex={$store.currentItemIndex}
|
||||||
|
on:pageChange={handlePageChange}
|
||||||
|
></Dots>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
.main-container {
|
.main-container {
|
||||||
display: flex;
|
display: flex;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
.carousel-container {
|
||||||
|
display: flex;
|
||||||
|
width: 100%;
|
||||||
}
|
}
|
||||||
.content-container {
|
.content-container {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
|
|||||||
13
src/stories/Dots.stories.js
Normal file
13
src/stories/Dots.stories.js
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import DotsView from './DotsView.svelte';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
title: 'Dots',
|
||||||
|
component: DotsView
|
||||||
|
};
|
||||||
|
|
||||||
|
const Template = ({ ...args }) => ({
|
||||||
|
Component: DotsView,
|
||||||
|
props: args
|
||||||
|
});
|
||||||
|
|
||||||
|
export const Primary = Template.bind({});
|
||||||
24
src/stories/DotsView.svelte
Normal file
24
src/stories/DotsView.svelte
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
<script>
|
||||||
|
import Dots from '../Dots.svelte'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Amount of pages (amount of dots)
|
||||||
|
*/
|
||||||
|
export let pagesCount = 5
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Index of the current page
|
||||||
|
*/
|
||||||
|
export let currentPageIndex = 3
|
||||||
|
|
||||||
|
function handlePageChange(event) {
|
||||||
|
currentPageIndex = event.detail
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Dots
|
||||||
|
{pagesCount}
|
||||||
|
{currentPageIndex}
|
||||||
|
on:pageChange={handlePageChange}
|
||||||
|
>
|
||||||
|
</Dots>
|
||||||
@@ -2,18 +2,12 @@ import ImageCarouselView from './ImageCarouselView.svelte';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
title: 'ImageCarousel',
|
title: 'ImageCarousel',
|
||||||
component: ImageCarouselView,
|
component: ImageCarouselView
|
||||||
argTypes: {
|
|
||||||
arrows: { control: 'boolean' },
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Template = ({ ...args }) => ({
|
const Template = ({ ...args }) => ({
|
||||||
Component: ImageCarouselView,
|
Component: ImageCarouselView,
|
||||||
props: args,
|
props: args
|
||||||
});
|
});
|
||||||
|
|
||||||
export const Primary = Template.bind({});
|
export const Primary = Template.bind({});
|
||||||
Primary.args = {
|
|
||||||
arrows: true
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -41,6 +41,11 @@
|
|||||||
*/
|
*/
|
||||||
export let autoplayDirection = 'next'
|
export let autoplayDirection = 'next'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current page indicator dots
|
||||||
|
*/
|
||||||
|
export let dots = true
|
||||||
|
|
||||||
const colors = [
|
const colors = [
|
||||||
'#e5f9f0',
|
'#e5f9f0',
|
||||||
'#ccf3e2',
|
'#ccf3e2',
|
||||||
@@ -65,6 +70,7 @@
|
|||||||
{autoplay}
|
{autoplay}
|
||||||
{autoplaySpeed}
|
{autoplaySpeed}
|
||||||
{autoplayDirection}
|
{autoplayDirection}
|
||||||
|
{dots}
|
||||||
>
|
>
|
||||||
{#each colors as color (color)}
|
{#each colors as color (color)}
|
||||||
<div
|
<div
|
||||||
|
|||||||
Reference in New Issue
Block a user