In Vue, you can abstract Smooothy logic into a composable. Use onMounted to instantiate the Core class using a ref to the DOM element and add the instance.update method to the gsap.ticker. Use onUnmounted to remove the ticker and call slider.value.destroy() to prevent memory leaks.
Example configuration for an infinite slider:
const { sliderElement, slider } = useSmooothy({
infinite: true,
})
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from "vue"
import Core, { CoreConfig } from "smooothy"
import gsap from "gsap"
/** composable */
function useSmooothy(config: Partial<CoreConfig> = {}) {
const sliderElement = ref<HTMLElement | null>(null)
const slider = ref<Core | null>(null)
onMounted(() => {
if (sliderElement.value) {
const instance = new Core(sliderElement.value, config)
gsap.ticker.add(instance.update.bind(instance))
slider.value = instance
}
})
onUnmounted(() => {
if (slider.value) {
gsap.ticker.remove(slider.value.update.bind(slider.value))
slider.value.destroy()
}
})
return {
sliderElement,
slider,
}
}
const slides = Array.from({ length: 10 }, (_, i) => i)
const { sliderElement, slider } = useSmooothy({
infinite: true,
})
</script>
<template>
<div ref="sliderElement" class="...">
<div v-for="(slide, i) in slides" :key="i">
<!-- slide content -->
</div>
</div>
</template>