Custom Vue Scrollbar
custom-vue-scrollbar is a lightweight (4.5KB gzipped) and high-performance Vue 3 component designed to provide customizable scrollbars while maintaining a native-like scrolling experience. As of version 0.0.8, it is in early development, with an irregular release cadence. It aims to offer advanced features like floating scrollbars (which fix at the bottom of the screen if the container exceeds visible area), toggleable native/simulated scroll modes for diverse use cases (e.g., synchronizing with outer elements), and automatic scrollbar hiding with configurable delay options. A key differentiator is its emphasis on minimal intrusion, few APIs, and direct access to the native scroll element, distinguishing it from libraries that significantly alter native scrolling behavior or introduce complex abstractions like `better-scroll`. It supports TypeScript and prioritizes performance, outperforming older alternatives by optimizing rendering and minimizing JavaScript-level calculations.
Common errors
-
Component 'CustomScrollbar' is not defined.
cause The CustomScrollbar component has not been globally registered or locally imported and registered within the current Vue component.fixFor global registration, add `app.component(CustomScrollbar.name, CustomScrollbar);` in your `main.ts` file. For local usage, import it and add it to the `components` option of your Vue component. -
Scrollbars are not visible or scrolling does not work as expected.
cause The `<custom-scrollbar>` component itself or its content does not have sufficient dimensions (width and height) to create an overflow condition, or the necessary CSS stylesheet is not loaded.fixEnsure the `<custom-scrollbar>` component has fixed `width` and `height` styles applied (e.g., `:style="{ width: '500px', height: '300px' }"`) and verify `import 'custom-vue-scrollbar/dist/style.css';` is present in your entry file. -
TypeError: Cannot read properties of undefined (reading 'scrollEl')
cause Attempting to access `scroll.value.scrollEl` before the component is mounted or if the ref is not properly assigned or resolved.fixEnsure the component is mounted and the ref (`scrollRef`) is correctly bound to the `<custom-scrollbar>` instance in the template. Always add a null/undefined check before accessing properties, e.g., `if (scrollRef.value?.scrollEl) { ... }`.
Warnings
- breaking As of version 0.0.8, this package is in early development. Significant API changes, including breaking alterations to props, events, or component structure, are highly probable before a stable `1.0.0` release. Users should anticipate and prepare for such changes.
- gotcha The `custom-scrollbar` component requires explicit `width` and `height` styles to be set on itself to establish a scrollable container. Without these, the component may not display scrollbars or function correctly, similar to native scroll elements.
- gotcha This library provides custom scrollbars but often expects interaction with the underlying native scroll element via `ref.scrollEl` for programmatic scrolling or obtaining scroll positions. Developers used to fully abstracted scroll APIs might find this approach different.
Install
-
npm install custom-vue-scrollbar -
yarn add custom-vue-scrollbar -
pnpm add custom-vue-scrollbar
Imports
- CustomScrollbar
const CustomScrollbar = require('custom-vue-scrollbar');import CustomScrollbar from 'custom-vue-scrollbar';
- Styling
@import '~custom-vue-scrollbar/dist/style.css';
import 'custom-vue-scrollbar/dist/style.css';
- Global Component Registration
import CustomScrollbar from 'custom-vue-scrollbar'; app.component(CustomScrollbar.name, CustomScrollbar);
Quickstart
import { createApp, ref } from 'vue';
import CustomScrollbar from 'custom-vue-scrollbar';
import 'custom-vue-scrollbar/dist/style.css';
const app = createApp({
template: `
<custom-scrollbar ref="scrollRef" :style="{ width: '500px', height: '300px', border: '1px solid #ccc', margin: '20px' }">
<div :style="{ width: '1000px', height: '600px', background: 'linear-gradient(to bottom right, #f0f8ff, #add8e6)' }">
<p>Some great content that extends beyond the scroll area, allowing the custom scrollbar to demonstrate its functionality.</p>
<p>Click the button below to scroll to the top smoothly.</p>
<button @click="handleClickScrollToTop">Scroll to Top</button>
</div>
</custom-scrollbar>
`,
setup() {
const scrollRef = ref<{ scrollEl: HTMLDivElement }>();
const handleClickScrollToTop = () => {
if (scrollRef.value?.scrollEl) {
scrollRef.value.scrollEl.scrollTo({ top: 0, behavior: 'smooth' });
}
};
return { scrollRef, handleClickScrollToTop };
}
});
app.component(CustomScrollbar.name, CustomScrollbar);
app.mount('#app');