fix(Carousel): arrows & indicators are broken in RTL (#2251)

This commit is contained in:
Malik-Jouda
2024-10-02 12:16:00 +03:00
committed by GitHub
parent 474accbefb
commit db5e5c4907
3 changed files with 17 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
<template>
<div :class="ui.wrapper" v-bind="attrs">
<div :class="ui.wrapper" v-bind="attrs" :dir="dir">
<div ref="carouselRef" :class="ui.container" class="no-scrollbar">
<div
v-for="(item, index) in items"
@@ -89,6 +89,10 @@ export default defineComponent({
type: Boolean,
default: false
},
dir: {
type: String as PropType<'ltr' | 'rtl'>,
default: 'ltr'
},
prevButton: {
type: Object as PropType<Button & { class?: string }>,
default: () => config.default.prevButton as Button & { class?: string }
@@ -124,12 +128,16 @@ export default defineComponent({
itemWidth.value = entry?.target?.firstElementChild?.clientWidth || 0
})
const isRtl = computed(() => props.dir === 'rtl')
const currentPage = computed(() => {
if (!itemWidth.value) {
return 0
}
return Math.round(x.value / itemWidth.value) + 1
return isRtl.value
? Math.round(-x.value / itemWidth.value) + 1
: Math.round(x.value / itemWidth.value) + 1
})
const pages = computed(() => {
@@ -144,15 +152,15 @@ export default defineComponent({
const isLast = computed(() => currentPage.value === pages.value)
function onClickNext () {
x.value += itemWidth.value
x.value += isRtl.value ? -itemWidth.value : itemWidth.value
}
function onClickPrev () {
x.value -= itemWidth.value
x.value -= isRtl.value ? -itemWidth.value : itemWidth.value
}
function onClick (page: number) {
x.value = (page - 1) * itemWidth.value
x.value = (page - 1) * itemWidth.value * (isRtl.value ? -1 : 1)
}
expose({