|
| 1 | +<template> |
| 2 | + <div |
| 3 | + class="m-component m-component-slider-comment" |
| 4 | + style="overflow: hidden" |
| 5 | + > |
| 6 | + <div class="container"> |
| 7 | + <div class="m-component__grid"> |
| 8 | + <div class="m-component__column"> |
| 9 | + <section |
| 10 | + class="m-slider m-slider--visible-preview" |
| 11 | + aria-label="Slider mit Elementen" |
| 12 | + data-m-slider-splide="m-slider-comment" |
| 13 | + > |
| 14 | + <button |
| 15 | + v-if="showBackArrow" |
| 16 | + aria-label="Vorheriges Element" |
| 17 | + class="previous-button is-control" |
| 18 | + @click="prevSlide" |
| 19 | + > |
| 20 | + <svg class="icon"> |
| 21 | + <use xlink:href="#icon-arrow-left"></use> |
| 22 | + </svg> |
| 23 | + </button> |
| 24 | + <Splide |
| 25 | + :options="mucSliderOptions" |
| 26 | + aria-label="Dies ist ein Karussell mit rotierenden Elementen. Verwenden Sie |
| 27 | + die Pfeiltaste links und rechts oder die Buttons um zu navigieren." |
| 28 | + ref="splide" |
| 29 | + > |
| 30 | + <slot /> |
| 31 | + </Splide> |
| 32 | + <button |
| 33 | + v-if="showNextArrow" |
| 34 | + aria-label="Nächstes Element" |
| 35 | + class="next-button is-control" |
| 36 | + @click="nextSlide" |
| 37 | + > |
| 38 | + <svg class="icon"> |
| 39 | + <use xlink:href="#icon-arrow-right"></use> |
| 40 | + </svg> |
| 41 | + </button> |
| 42 | + </section> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + </div> |
| 46 | + </div> |
| 47 | +</template> |
| 48 | + |
| 49 | +<script setup lang="ts"> |
| 50 | +import { Splide } from "@splidejs/vue-splide"; |
| 51 | +import { computed, onMounted, ref, useTemplateRef } from "vue"; |
| 52 | +
|
| 53 | +import { mucSliderOptions } from "./MucSliderOptions"; |
| 54 | +
|
| 55 | +defineSlots<{ |
| 56 | + /** |
| 57 | + * MucSliderItems can be put into this slot. |
| 58 | + */ |
| 59 | + default(): any; |
| 60 | +}>(); |
| 61 | +
|
| 62 | +const emit = defineEmits<{ |
| 63 | + /** |
| 64 | + * Triggered when an item is clicked. |
| 65 | + * @param id of the clicked item |
| 66 | + */ |
| 67 | + changeSlide: [index: number]; |
| 68 | +}>(); |
| 69 | +
|
| 70 | +const splide = useTemplateRef("splide"); |
| 71 | +
|
| 72 | +/** |
| 73 | + * Index of the current silde |
| 74 | + */ |
| 75 | +const currentSlide = ref<number>(0); |
| 76 | +
|
| 77 | +/** |
| 78 | + * Length of the splide |
| 79 | + */ |
| 80 | +const splideLength = ref<number>(0); |
| 81 | +
|
| 82 | +/** |
| 83 | + * Set next slide |
| 84 | + */ |
| 85 | +const nextSlide = () => { |
| 86 | + if (splide.value) { |
| 87 | + splide.value.go(">"); |
| 88 | + } |
| 89 | +}; |
| 90 | +
|
| 91 | +/** |
| 92 | + * Set previous slide |
| 93 | + */ |
| 94 | +const prevSlide = () => { |
| 95 | + if (splide.value) { |
| 96 | + splide.value.go("<"); |
| 97 | + } |
| 98 | +}; |
| 99 | +
|
| 100 | +/** |
| 101 | + * Computed property set back button |
| 102 | + */ |
| 103 | +const showBackArrow = computed(() => currentSlide.value > 0); |
| 104 | +
|
| 105 | +/** |
| 106 | + * Computed property set next button |
| 107 | + */ |
| 108 | +const showNextArrow = computed( |
| 109 | + () => currentSlide.value < splideLength.value - 1 |
| 110 | +); |
| 111 | +
|
| 112 | +onMounted(() => { |
| 113 | + if (splide.value && splide.value.splide) { |
| 114 | + splideLength.value = splide.value.length; |
| 115 | + splide.value.splide.on("move", () => { |
| 116 | + if (splide.value) { |
| 117 | + currentSlide.value = splide.value.splide!.index; |
| 118 | + emit("changeSlide", splide.value.splide!.index); |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | +}); |
| 123 | +</script> |
| 124 | + |
| 125 | +<style scoped> |
| 126 | +.m-component-slider-comment { |
| 127 | + padding-right: 1.8rem; |
| 128 | + padding-left: 1.8rem; |
| 129 | +} |
| 130 | +</style> |
0 commit comments