Merge pull request #77 from cadiyak/carousel

Add basic carousel component (Issue #55)
This commit is contained in:
Ilya Artamonov
2022-12-13 13:45:35 +03:00
committed by GitHub
9 changed files with 314 additions and 25 deletions

View File

@@ -1,8 +1,17 @@
<script setup>
import CarouselExample from './examples/CarouselExample.vue'
import CarouselDefaultExample from './examples/CarouselDefaultExample.vue';
import CarouselNoControlsExample from './examples/CarouselNoControlsExample.vue';
import CarouselNoIndicatorsExample from './examples/CarouselNoIndicatorsExample.vue';
import CarouselSlideExample from './examples/CarouselSlideExample.vue';
import CarouselSlideIntervalExample from './examples/CarouselSlideIntervalExample.vue';
import CarouselPicturesExample from './examples/CarouselPicturesExample.vue';
</script>
# Vue Carousel Component - Flowbite
Use the carousel component to slide through multiple elements and images using custom controls, indicators, intervals, and options
## Default slider
```vue
<script setup>
import { Carousel } from 'flowbite-vue'
@@ -12,4 +21,149 @@ import { Carousel } from 'flowbite-vue'
</template>
```
<CarouselExample />
<CarouselDefaultExample />
## Prop - controls
```typescript
const props = defineProps({
controls: {
type: Boolean,
default: true,
},
})
```
<CarouselNoControlsExample />
```vue
<script setup>
import { Carousel } from 'flowbite-vue'
</script>
<template>
<Carousel :controls="false"></Carousel>
</template>
```
## Prop - indicators
```typescript
const props = defineProps({
indicators: {
type: Boolean,
default: true,
},
})
```
<CarouselNoIndicatorsExample />
```vue
<script setup>
import { Carousel } from 'flowbite-vue'
</script>
<template>
<Carousel :indicators="false"></Carousel>
</template>
```
## Prop - slide
```typescript
const props = defineProps({
slide: {
type: Boolean,
default: false,
},
})
```
<CarouselSlideExample />
```vue
<script setup>
import { Carousel } from 'flowbite-vue'
</script>
<template>
<Carousel :slide="true"></Carousel>
</template>
```
## Prop - slide interval
```typescript
const props = defineProps({
slideInterval: {
type: Number,
default: 3000,
},
})
```
<CarouselSlideIntervalExample />
```vue
<script setup>
import { Carousel } from 'flowbite-vue'
</script>
<template>
<Carousel :slide="true" :slide-interval="1000"></Carousel>
</template>
```
## Prop - pictures
```typescript
const props = defineProps({
pictures: {
type: Array,
default() {
return [
{
'src': 'https://flowbite.com/docs/images/carousel/carousel-1.svg',
'alt': 'Picture 1',
},
{
'src': 'https://flowbite.com/docs/images/carousel/carousel-2.svg',
'alt': 'Picture 2',
},
{
'src': 'https://flowbite.com/docs/images/carousel/carousel-3.svg',
'alt': 'Picture 3',
},
]
},
},
})
```
<CarouselPicturesExample />
```vue
<script setup>
import { Carousel } from 'flowbite-vue'
const pictures = [
{
'src': 'https://i.picsum.photos/id/221/1920/1080.jpg?grayscale&hmac=GWPBzHGEhEh-BrPn1i-PuximCxLtUpKHcNwyACiTRHk',
'alt': 'Picture 1',
},
{
'src': 'https://i.picsum.photos/id/608/1920/1080.jpg?grayscale&hmac=kkVc8-M3ovbO-sFr02WmRbrHx5YcfWmOtkP8rNF9fO4',
'alt': 'Picture 2',
},
{
'src': 'https://i.picsum.photos/id/1012/1920/1080.jpg?grayscale&hmac=Zy9JmZ_sl88DXTF502k_0QqaeCyqjhkn60DjyE5F8Eo',
'alt': 'Picture 3',
},
{
'src': 'https://i.picsum.photos/id/1004/1920/1080.jpg?grayscale&hmac=48C-T9K-36l8jpzJntPTu40OiDuXflIlG_YB-lQWYZI',
'alt': 'Picture 4',
},
{
'src': 'https://i.picsum.photos/id/244/1920/1080.jpg?grayscale&hmac=lLcfHciI-EqaOIHGB_Gab7OpUjVSmDLw6GOq6B5THak',
'alt': 'Picture 5',
},]
</script>
<template>
<Carousel :pictures="pictures"></Carousel>
</template>
```

View File

@@ -0,0 +1,8 @@
<template>
<div class="vp-raw flex flex-col">
<Carousel :slide="true"></Carousel>
</div>
</template>
<script setup>
import { Carousel } from '../../../../src/index'
</script>

View File

@@ -0,0 +1,8 @@
<template>
<div class="vp-raw flex flex-col">
<Carousel></Carousel>
</div>
</template>
<script setup>
import { Carousel } from '../../../../src/index'
</script>

View File

@@ -0,0 +1,8 @@
<template>
<div class="vp-raw flex flex-col">
<Carousel :controls="false"></Carousel>
</div>
</template>
<script setup>
import { Carousel } from '../../../../src/index'
</script>

View File

@@ -0,0 +1,8 @@
<template>
<div class="vp-raw flex flex-col">
<Carousel :indicators="false"></Carousel>
</div>
</template>
<script setup>
import { Carousel } from '../../../../src/index'
</script>

View File

@@ -0,0 +1,30 @@
<template>
<div class="vp-raw flex flex-col">
<Carousel :pictures="pictures"></Carousel>
</div>
</template>
<script setup>
import { Carousel } from '../../../../src/index'
const pictures = [
{
'src': 'https://i.picsum.photos/id/221/1920/1080.jpg?grayscale&hmac=GWPBzHGEhEh-BrPn1i-PuximCxLtUpKHcNwyACiTRHk',
'alt': 'Picture 1',
},
{
'src': 'https://i.picsum.photos/id/608/1920/1080.jpg?grayscale&hmac=kkVc8-M3ovbO-sFr02WmRbrHx5YcfWmOtkP8rNF9fO4',
'alt': 'Picture 2',
},
{
'src': 'https://i.picsum.photos/id/1012/1920/1080.jpg?grayscale&hmac=Zy9JmZ_sl88DXTF502k_0QqaeCyqjhkn60DjyE5F8Eo',
'alt': 'Picture 3',
},
{
'src': 'https://i.picsum.photos/id/1004/1920/1080.jpg?grayscale&hmac=48C-T9K-36l8jpzJntPTu40OiDuXflIlG_YB-lQWYZI',
'alt': 'Picture 4',
},
{
'src': 'https://i.picsum.photos/id/244/1920/1080.jpg?grayscale&hmac=lLcfHciI-EqaOIHGB_Gab7OpUjVSmDLw6GOq6B5THak',
'alt': 'Picture 5',
},
]
</script>

View File

@@ -0,0 +1,8 @@
<template>
<div class="vp-raw flex flex-col">
<Carousel :slide="true"></Carousel>
</div>
</template>
<script setup>
import { Carousel } from '../../../../src/index'
</script>

View File

@@ -0,0 +1,8 @@
<template>
<div class="vp-raw flex flex-col">
<Carousel :slide="true" :slide-interval="1000"></Carousel>
</div>
</template>
<script setup>
import { Carousel } from '../../../../src/index'
</script>

View File

@@ -1,35 +1,26 @@
<template>
<div id="default-carousel" class="relative" data-carousel="static">
<div id="default-carousel" class="relative">
<!-- Carousel wrapper -->
<div class="overflow-hidden relative h-56 rounded-lg sm:h-64 xl:h-80 2xl:h-96">
<!-- Item 1 -->
<div class="hidden duration-700 ease-in-out" data-carousel-item>
<span class="absolute top-1/2 left-1/2 text-2xl font-semibold text-white -translate-x-1/2 -translate-y-1/2 sm:text-3xl dark:text-gray-800">First Slide</span>
<!-- <img src="/docs/images/carousel/carousel-1.svg" class="block absolute top-1/2 left-1/2 w-full -translate-x-1/2 -translate-y-1/2" alt="...">-->
</div>
<!-- Item 2 -->
<div class="hidden duration-700 ease-in-out" data-carousel-item>
<!-- <img src="/docs/images/carousel/carousel-2.svg" class="block absolute top-1/2 left-1/2 w-full -translate-x-1/2 -translate-y-1/2" alt="...">-->
</div>
<!-- Item 3 -->
<div class="hidden duration-700 ease-in-out" data-carousel-item>
<!-- <img src="/docs/images/carousel/carousel-3.svg" class="block absolute top-1/2 left-1/2 w-full -translate-x-1/2 -translate-y-1/2" alt="...">-->
<!-- duration-700 ease-in-out-->
<div :class="index === currentPicture ? 'z-30' : 'z-0'"
v-for="(picture, index) in pictures" :key="picture" class="absolute inset-0 -translate-y-0">
<img :src="picture.src" class="block absolute top-1/2 left-1/2 w-full -translate-x-1/2 -translate-y-1/2" :alt="picture.alt">
</div>
</div>
<!-- Slider indicators -->
<div class="flex absolute bottom-5 left-1/2 z-30 space-x-3 -translate-x-1/2">
<button type="button" class="w-3 h-3 rounded-full" aria-current="false" aria-label="Slide 1" data-carousel-slide-to="0"></button>
<button type="button" class="w-3 h-3 rounded-full" aria-current="false" aria-label="Slide 2" data-carousel-slide-to="1"></button>
<button type="button" class="w-3 h-3 rounded-full" aria-current="false" aria-label="Slide 3" data-carousel-slide-to="2"></button>
<div v-if="indicators" class="flex absolute bottom-5 left-1/2 z-30 space-x-3 -translate-x-1/2">
<button v-for="(picture, index) in pictures" :key="picture" type="button" :class="index === currentPicture ? 'bg-white' : 'bg-white/50'" class="w-3 h-3 rounded-full bg-white" aria-current="false" :aria-label="'Slide ' + index" @click.prevent="slideTo(index)"></button>
</div>
<!-- Slider controls -->
<button type="button" class="flex absolute top-0 left-0 z-30 justify-center items-center px-4 h-full cursor-pointer group focus:outline-none" data-carousel-prev>
<button v-if="controls" @click.prevent="previousPicture" type="button" class="flex absolute top-0 left-0 z-30 justify-center items-center px-4 h-full cursor-pointer group focus:outline-none" data-carousel-prev>
<span class="inline-flex justify-center items-center w-8 h-8 rounded-full sm:w-10 sm:h-10 bg-white/30 dark:bg-gray-800/30 group-hover:bg-white/50 dark:group-hover:bg-gray-800/60 group-focus:ring-4 group-focus:ring-white dark:group-focus:ring-gray-800/70 group-focus:outline-none">
<svg class="w-5 h-5 text-white sm:w-6 sm:h-6 dark:text-gray-800" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path></svg>
<span class="hidden">Previous</span>
</span>
</button>
<button type="button" class="flex absolute top-0 right-0 z-30 justify-center items-center px-4 h-full cursor-pointer group focus:outline-none" data-carousel-next>
<button v-if="controls" @click.prevent="nextPicture" type="button" class="flex absolute top-0 right-0 z-30 justify-center items-center px-4 h-full cursor-pointer group focus:outline-none" data-carousel-next>
<span class="inline-flex justify-center items-center w-8 h-8 rounded-full sm:w-10 sm:h-10 bg-white/30 dark:bg-gray-800/30 group-hover:bg-white/50 dark:group-hover:bg-gray-800/60 group-focus:ring-4 group-focus:ring-white dark:group-focus:ring-gray-800/70 group-focus:outline-none">
<svg class="w-5 h-5 text-white sm:w-6 sm:h-6 dark:text-gray-800" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path></svg>
<span class="hidden">Next</span>
@@ -38,28 +29,94 @@
</div>
</template>
<script lang="ts" setup>
import { computed, toRefs } from 'vue'
import type { PropType } from 'vue'
import { onMounted, ref } from 'vue'
const props = defineProps({
children: {
pictures: {
type: Array,
default() {
return []
return [
{
'src': 'https://flowbite.com/docs/images/carousel/carousel-1.svg',
'alt': 'Picture 1',
},
{
'src': 'https://flowbite.com/docs/images/carousel/carousel-2.svg',
'alt': 'Picture 2',
},
{
'src': 'https://flowbite.com/docs/images/carousel/carousel-3.svg',
'alt': 'Picture 3',
},
]
},
},
indicators: {
type: Boolean,
default: true,
},
slide: {
controls: {
type: Boolean,
default: true,
},
slide: {
type: Boolean,
default: false,
},
slideInterval: {
type: Number,
default: 3000,
},
animation: {
type: Boolean,
default: false,
},
})
const currentPicture = ref(0)
const direction = ref('')
const interval = ref()
const automaticSlide = () => {
interval.value = setInterval(function() {
nextPicture()
}, props.slideInterval)
}
const resetInterval = () => {
clearInterval(interval.value)
automaticSlide()
}
const slideTo = (index: number) => {
currentPicture.value = index
resetInterval()
}
const nextPicture = () => {
if (currentPicture.value !== props.pictures.length - 1) {
currentPicture.value ++
} else {
currentPicture.value = 0
}
direction.value = 'right'
resetInterval()
}
const previousPicture = () => {
if (currentPicture.value !== 0) {
currentPicture.value --
} else {
currentPicture.value = props.pictures.length -1
}
direction.value = 'left'
resetInterval()
}
onMounted(() => {
if (props.slide) {
automaticSlide()
}
})
</script>