Vue Tiny Slider
Vue Tiny Slider is a Vue.js wrapper component for the vanilla JavaScript `tiny-slider` library by ganlanyuan, designed to provide a lightweight and highly customizable carousel solution. The current stable version, 1.1.0, is built for Vue 3.x applications, while older Vue 2.x projects should use versions 0.1.x. The library differentiates itself by providing a direct and comprehensive exposure of `tiny-slider`'s extensive options as Vue props, along with access to the underlying slider instance's methods via template refs. This approach ensures maximum flexibility and control over the slider's behavior without abstracting away the core `tiny-slider` API. It ships with TypeScript types and includes specific guidance for handling Server-Side Rendering (SSR) environments, particularly with Nuxt 3, addressing potential DOM-related issues.
Common errors
-
ReferenceError: document is not defined
cause Attempting to render `vue-tiny-slider` in a Server-Side Rendering (SSR) environment without proper client-only hydration.fixFor SSR frameworks like Nuxt 3, ensure the component is wrapped within `<ClientOnly>` tags. If registering globally via a plugin, name the plugin file with a `.client.js` suffix. -
TypeError: Cannot read properties of undefined (reading 'slider')
cause Attempting to access the `slider` instance on the component's ref before the `vue-tiny-slider` component has fully mounted and initialized `tiny-slider`.fixEnsure you access `this.$refs.yourSliderName.slider` (Options API) or `yourSliderRef.value.slider` (Composition API) only after the component is mounted, typically within `onMounted` or a method triggered by user interaction, and add a null check for robustness.
Warnings
- breaking Major version incompatibility between Vue 2.x and Vue 3.x. Version `^1.0.0` of `vue-tiny-slider` is for Vue 3.x, while `^0.1.x` is for Vue 2.x.
- gotcha The component requires client-side rendering due to `tiny-slider` directly manipulating the DOM. Using it in Server-Side Rendering (SSR) frameworks like Nuxt 3 requires wrapping it in a `<ClientOnly>` component.
- gotcha The styling for `tiny-slider` is not bundled with `vue-tiny-slider` and must be imported separately to ensure the slider renders correctly.
Install
-
npm install vue-tiny-slider -
yarn add vue-tiny-slider -
pnpm add vue-tiny-slider
Imports
- VueTinySlider
import { VueTinySlider } from 'vue-tiny-slider'; // OR const VueTinySlider = require('vue-tiny-slider');import VueTinySlider from 'vue-tiny-slider';
- Tiny Slider SCSS
import 'tiny-slider/dist/tiny-slider.css';
@import 'tiny-slider/src/tiny-slider';
- TinySliderSettings (Type)
import type { TinySliderSettings } from 'vue-tiny-slider';
Quickstart
import { createApp, ref, onMounted } from 'vue';
import VueTinySlider from 'vue-tiny-slider';
import 'tiny-slider/src/tiny-slider.scss'; // Don't forget to import styles!
const App = {
components: { 'tiny-slider': VueTinySlider },
template: `
<div id="app">
<h1>My Awesome Carousel</h1>
<tiny-slider :mouse-drag="true" :loop="false" :items="2" :gutter="20" ref="mySlider">
<div>Slider item #1</div>
<div>Slider item #2</div>
<div>Slider item #3</div>
<div>Slider item #4</div>
<div>Slider item #5</div>
<div>Slider item #6</div>
</tiny-slider>
<button @click="goToNextSlide">Next Slide</button>
</div>
`,
setup() {
const mySlider = ref(null);
const goToNextSlide = () => {
if (mySlider.value && mySlider.value.slider) {
mySlider.value.slider.goTo('next');
} else {
console.warn('Slider instance not available yet.');
}
};
// Example of accessing slider info after mount
onMounted(() => {
if (mySlider.value && mySlider.value.slider) {
console.log('Slider initialized:', mySlider.value.slider.getInfo());
}
});
return { mySlider, goToNextSlide };
}
};
createApp(App).mount('#app');