Skip to content

Commit d53da4c

Browse files
committed
feat: Add CCarousel and CCarosuelItem components
1 parent 1753e17 commit d53da4c

File tree

5 files changed

+163
-6
lines changed

5 files changed

+163
-6
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# @coreui/vue
1+
# @coreui/vue
22

33
[![Npm badge](https://img.shields.io/npm/v/@coreui/vue.svg)][npm]
44
[![NPM downloads][npm-download]][npm]
@@ -9,9 +9,9 @@
99
[npm]: https://www.npmjs.com/package/@coreui/vue
1010
[npm-download]: https://img.shields.io/npm/dm/@coreui/vue.svg?style=flat-square
1111

12-
> A [@coreui/vue](https://coreui.io/vue) `v2` library project
12+
> A [@coreui/vue](https://coreui.io/vue) `v3` library project
1313
14-
for use with [CoreUI](https://coreui.io/vue/) `v2` Open Source Bootstrap Admin Template
14+
for use with [CoreUI](https://coreui.io/vue/) `v3-alpha` Open Source Bootstrap Admin Template
1515

1616
## Installation
1717
```
@@ -21,17 +21,17 @@ npm install @coreui/vue
2121

2222
When in non-modular environment, @coreui/vue will register all the components to vue by itself.</p>
2323

24-
### ES6
24+
### ES6
2525
```js
2626
//
2727
// You can register a component manually
2828
//
29-
import { Switch } from '@coreui/vue';
29+
import { CSwitch } from '@coreui/vue';
3030

3131
export default {
3232
...
3333
components: {
34-
Switch
34+
CSwitch
3535
},
3636
...
3737
};

src/components/Carousel/CCarousel.vue

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<template>
2+
<div class="carousel slide" >
3+
<ol v-if="!noIndicators" :class="indicatorClasses">
4+
<li
5+
v-for="(index, key) in itemsLength"
6+
@click="setSlide(key)"
7+
:class="{'active' : active === key}"
8+
:key="key"
9+
></li>
10+
</ol>
11+
<div class="carousel-inner">
12+
<slot></slot>
13+
</div>
14+
<template v-if="!noArrows">
15+
<a class="carousel-control-prev" @click="previousSlide">
16+
<span class="carousel-control-prev-icon"></span>
17+
<span class="sr-only">Previous</span>
18+
</a>
19+
<a class="carousel-control-next" @click="nextSlide">
20+
<span class="carousel-control-next-icon"></span>
21+
<span class="sr-only">Next</span>
22+
</a>
23+
</template>
24+
</div>
25+
</template>
26+
27+
<script>
28+
export default {
29+
name: 'CCarousel',
30+
props: {
31+
interval: {
32+
type: Number,
33+
default: 4000
34+
},
35+
noAnimation: Boolean,
36+
noIndicators: Boolean,
37+
noArrows: Boolean,
38+
indicatorClasses: {
39+
type: [String, Array],
40+
default: 'carousel-indicators'
41+
},
42+
},
43+
data () {
44+
return {
45+
active: 0,
46+
itemsLength: null,
47+
currentInterval: null,
48+
transitioning: null
49+
}
50+
},
51+
mounted () {
52+
this.itemsLength = this.$children.length
53+
this.$children.forEach((item, index) => item.index = index)
54+
const activeItem = this.$children.filter(item => item.active)
55+
if (activeItem[0]) {
56+
this.active = activeItem[0].index || 0
57+
} else {
58+
this.$children[0].classes = 'active'
59+
}
60+
this.menageInterval()
61+
},
62+
methods: {
63+
menageInterval () {
64+
if (this.noAnimation) return
65+
66+
clearInterval(this.currentInterval)
67+
this.currentInterval = setInterval(() => {
68+
this.nextSlide()
69+
}, this.interval)
70+
},
71+
nextSlide () {
72+
let index = this.active === this.itemsLength - 1 ? 0 : this.active + 1
73+
this.setSlide(index, 'left')
74+
},
75+
previousSlide () {
76+
let index = this.active === 0 ? this.itemsLength -1 : this.active - 1
77+
this.setSlide(index, 'right')
78+
},
79+
setSlide (index, direction = null) {
80+
if (index === this.active) {
81+
return this.menageInterval()
82+
}
83+
direction === null ? direction = this.active < index ? 'left' : 'right' : ''
84+
if (this.noAnimation) {
85+
this.$children[this.active].classes = ''
86+
this.$children[index].classes = 'active'
87+
this.active = index
88+
} else if (!this.transitioning) {
89+
this.slide(index, direction)
90+
}
91+
},
92+
slide (index, direction) {
93+
this.menageInterval()
94+
let oldIndex = this.active
95+
this.active = index
96+
const order = direction === 'right' ? 'prev' : 'next'
97+
const orderClass = `carousel-item-${order}`
98+
const directionClass = `carousel-item-${direction}`
99+
const activeEl = this.$children[oldIndex].$el
100+
const nextEl = this.$children[index].$el
101+
setTimeout(() =>{
102+
nextEl.classList.add(orderClass)
103+
nextEl.offsetHeight
104+
nextEl.classList.add(directionClass)
105+
activeEl.classList.add(directionClass)
106+
}, 0)
107+
108+
this.transitioning = setTimeout(() => {
109+
this.transitioning = null
110+
nextEl.classList.remove(orderClass)
111+
nextEl.classList.remove(directionClass)
112+
activeEl.classList.remove(directionClass)
113+
this.$children[oldIndex].classes = ''
114+
this.$children[index].classes = 'active'
115+
}, 600)
116+
}
117+
}
118+
119+
}
120+
</script>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<template>
2+
<div class="carousel-item" :class="classes">
3+
<img v-if="imgSrc" :src="imgSrc" class="d-block w-100 h-100 img-fluid"/>
4+
<slot>
5+
<div class="carousel-caption">
6+
<h3>{{caption}}</h3>
7+
<p>{{text}}</p>
8+
</div>
9+
</slot>
10+
</div>
11+
</template>
12+
13+
<script>
14+
import CImage from '../Image/CImage'
15+
export default {
16+
name: 'CCarouselItem',
17+
props: {
18+
imgSrc: String,
19+
caption: String,
20+
text: String,
21+
active: Boolean,
22+
},
23+
data () {
24+
return {
25+
classes: this.active ? 'active' : ''
26+
}
27+
}
28+
}
29+
</script>

src/components/Carousel/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import CCarousel from './CCarousel'
2+
import CCarouselItem from './CCarouselItem'
3+
4+
export {
5+
CCarousel,
6+
CCarouselItem
7+
}

src/components/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './Badge'
44
export * from './Breadcrumb'
55
export * from './Button'
66
export * from './Callout'
7+
export * from './Carousel'
78
export * from './Card'
89
export * from './Charts'
910
export * from './Collapse'

0 commit comments

Comments
 (0)