library_components/src/components/Ml/Slider/MlSlider.vue

143 lines
3.9 KiB
Vue
Raw Normal View History

2024-07-05 12:22:31 +00:00
<template>
<div v-show="showSwiper" class="relative" :class="rootClasses">
<swiper
loop-add-blank-slides
:class="sliderClasses"
:direction="direction"
:breakpoints="{
450: { slidesPerView: 1.2, spaceBetween: 20 },
500: { slidesPerView: 1.5, spaceBetween: 20 },
600: { slidesPerView: 1.5, spaceBetween: 20 },
770: { slidesPerView: 1, spaceBetween: 20 },
800: { slidesPerView: 1, spaceBetween: 20 },
1000: { slidesPerView: 1.2, spaceBetween: 20 },
1200: { slidesPerView: 1.5, spaceBetween: 20 },
1600: { slidesPerView: 2, spaceBetween: 40 },
2000: { slidesPerView: 2.5, spaceBetween: 40 },
}"
:space-between="50"
@swiper="onSwiper"
@slide-change="onSlideChange"
>
<swiper-slide
v-for="item in items"
:key="item.title"
class="hover:cursor-pointer"
>
<slot :item="item" />
</swiper-slide>
</swiper>
2024-07-08 12:32:30 +00:00
<div>
<div :class="counterClasses">
2024-07-08 12:47:58 +00:00
<div :class="progressClass" :style="`width: ${progerssBar}%`"></div>
2024-07-08 12:32:30 +00:00
<div class="flex justify-between my-4">
<button
:disabled="isPrevDisabled"
class="px-4 py-2 disabled:text-gray-400 cursor-pointer"
@click="
useRipple($event, true);
goPrev();
"
>
<ArrowLeftIcon class="size-5" />
</button>
<button
:disabled="isNextDisabled"
class="px-4 py-2 disabled:text-gray-400 cursor-pointer"
@click="
useRipple($event, true);
goNext();
"
>
<ArrowRightIcon class="size-5" />
</button>
</div>
</div>
</div>
2024-07-05 12:22:31 +00:00
</div>
</template>
<script setup lang="ts">
import { ref, computed, defineProps, withDefaults } from "vue";
import { Swiper, SwiperSlide } from "swiper/vue";
import type { Swiper as swiper } from "swiper/types";
import "swiper/css";
import { twMerge } from "tailwind-merge";
2024-07-08 12:47:58 +00:00
import { ArrowLeftIcon, ArrowRightIcon } from "#imports";
2024-07-05 12:22:31 +00:00
import type { PTAttribs, SliderProps } from "./types";
const showSwiper = ref(false);
const progerssBar = ref(0);
const prevDisabled = ref(true);
const nextDisabled = ref(true);
let swiperInstance: swiper;
const onSwiper = (swiper: swiper) => {
progerssBar.value = Math.ceil(
((swiper.realIndex + (swiper.params.slidesPerView as number)) /
swiper.el.querySelectorAll(".swiper-slide").length) *
100
);
swiperInstance = swiper;
showSwiper.value = true;
prevDisabled.value = !swiper.params.loop && swiper.realIndex == 0;
nextDisabled.value = swiperInstance.isEnd;
};
const onSlideChange = (swiper: swiper) => {
progerssBar.value = Math.ceil(
((swiper.realIndex + (swiper.params.slidesPerView as number)) /
swiper.slides.length) *
100
);
prevDisabled.value = !swiper.params.loop && swiper.realIndex == 0;
nextDisabled.value = swiper.isEnd;
};
const isPrevDisabled = computed(() => prevDisabled.value);
const isNextDisabled = computed(() => nextDisabled.value);
function goNext() {
swiperInstance.slideNext();
}
function goPrev() {
swiperInstance.slidePrev();
}
const props = withDefaults(defineProps<SliderProps>(), {
items: [],
});
const classes = ref({
root: {
class: ``,
},
slider: {
class: ``,
},
counter: {
2024-07-08 12:38:45 +00:00
class: ``,
2024-07-05 12:22:31 +00:00
},
2024-07-08 12:10:47 +00:00
progress: {
class: "",
},
} as unknown as PTAttribs);
2024-07-05 12:22:31 +00:00
const rootClasses = computed(() =>
twMerge(classes.value.root?.class, props.pt?.root?.class)
);
const sliderClasses = computed(() =>
twMerge(classes.value.slider?.class, props.pt?.slider?.class)
);
const counterClasses = computed(() =>
twMerge(classes.value.counter?.class, props.pt?.counter?.class)
);
2024-07-08 12:10:47 +00:00
const progressClass = computed(() =>
twMerge(classes.value.progress?.class, props.pt?.progress?.class)
);
2024-07-05 12:22:31 +00:00
const items = props.items;
</script>