Skip to content

Commit

Permalink
fix(Carousel): next and prev buttons disabled (#1619)
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijsubedi committed Apr 5, 2024
1 parent 5e84fd0 commit e909884
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/runtime/components/elements/Carousel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</template>
<script lang="ts">
import { ref, toRef, toRefs, computed, defineComponent } from 'vue'
import { ref, toRef, computed, defineComponent } from 'vue'
import type { PropType } from 'vue'
import { twMerge } from 'tailwind-merge'
import { mergeConfig } from '../../utils'
Expand Down Expand Up @@ -112,10 +112,9 @@ export default defineComponent({
const carouselRef = ref<HTMLElement>()
const itemWidth = ref(0)
const { x, arrivedState } = useScroll(carouselRef, { behavior: 'smooth' })
const { width: carouselWidth } = useElementSize(carouselRef)
const { x } = useScroll(carouselRef, { behavior: 'smooth' })
const { left: isFirst, right: isLast } = toRefs(arrivedState)
const { width: carouselWidth } = useElementSize(carouselRef)
useCarouselScroll(carouselRef)
Expand All @@ -125,7 +124,13 @@ export default defineComponent({
itemWidth.value = entry?.target?.firstElementChild?.clientWidth || 0
})
const currentPage = computed(() => Math.round(x.value / itemWidth.value) + 1)
const currentPage = computed(() => {
if (!itemWidth.value) {
return 0
}
return Math.round(x.value / itemWidth.value) + 1
})
const pages = computed(() => {
if (!itemWidth.value) {
Expand All @@ -135,6 +140,9 @@ export default defineComponent({
return props.items.length - Math.round(carouselWidth.value / itemWidth.value) + 1
})
const isFirst = computed(() => currentPage.value <= 1)
const isLast = computed(() => currentPage.value === pages.value)
function onClickNext () {
x.value += itemWidth.value
}
Expand Down

0 comments on commit e909884

Please sign in to comment.