OverlayScrollbars for Vue
OverlayScrollbars-Vue is the official Vue wrapper for the OverlayScrollbars library, providing highly customizable scrollbars that replace native browser scrollbars for consistent cross-browser styling and behavior. It integrates seamlessly into Vue 3 applications, offering both a component-based API (`OverlayScrollbarsComponent`) and a composable API for more granular control. Currently at version `0.5.9`, the package maintains an active release cadence, often aligning with updates to the underlying `OverlayScrollbars` core library, which is at `v2.15.0`. Recent core library updates have introduced features like configurable click-scroll behavior and performance optimizations such as a `sleep` function and enhanced debounce options. Its key differentiators include extensive customization options, performance optimizations like deferred initialization, and robust event handling, making it a powerful choice for enhancing UI/UX in Vue projects demanding bespoke scrollbar aesthetics and functionality.
Common errors
-
Module not found: Error: Can't resolve 'overlayscrollbars/overlayscrollbars.css' in '...' or similar CSS import error
cause The CSS file for OverlayScrollbars could not be found, likely due to an incorrect import path or module resolution issues.fixEnsure you have `overlayscrollbars` installed as a peer dependency (`npm install overlayscrollbars`) and try switching the import path between `'overlayscrollbars/overlayscrollbars.css'` and `'overlayscrollbars/styles/overlayscrollbars.css'`. -
Failed to resolve component: OverlayScrollbarsComponent If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.
cause The `OverlayScrollbarsComponent` was used in a Vue template but not correctly imported or registered with the component.fixEnsure `import { OverlayScrollbarsComponent } from 'overlayscrollbars-vue';` is present and the component is registered in your Vue component's `components` option: `components: { OverlayScrollbarsComponent }`. -
Uncaught ReferenceError: overlayscrollbars is not defined
cause The core `overlayscrollbars` library, a required peer dependency, is not installed or accessible.fixInstall the core library: `npm install overlayscrollbars`.
Warnings
- gotcha The `overlayscrollbars` and `vue` packages are peer dependencies and must be installed separately alongside `overlayscrollbars-vue`. Failure to install them will lead to runtime errors.
- gotcha The CSS import path can be ambiguous. The primary path is `overlayscrollbars/overlayscrollbars.css`, but some build systems might require `overlayscrollbars/styles/overlayscrollbars.css`.
- gotcha It is highly recommended to use the `defer` prop on the `OverlayScrollbarsComponent` to postpone initialization to a browser's idle period, improving initial page load performance.
- deprecated The `options.debounce` property in the core OverlayScrollbars library (which affects the `options` prop in this wrapper) was deprecated in v2.13.0 in favor of a new object syntax for fine-grained control over `mutations`, `resizes`, `events`, and `environmental changes`. While the old syntax is still supported, the new object syntax is preferred.
Install
-
npm install overlayscrollbars-vue -
yarn add overlayscrollbars-vue -
pnpm add overlayscrollbars-vue
Imports
- OverlayScrollbarsComponent
const OverlayScrollbarsComponent = require('overlayscrollbars-vue');import { OverlayScrollbarsComponent } from 'overlayscrollbars-vue'; - CSS
import 'overlayscrollbars/styles/overlayscrollbars.css';
import 'overlayscrollbars/overlayscrollbars.css';
Quickstart
import { defineComponent } from 'vue';
import { OverlayScrollbarsComponent } from 'overlayscrollbars-vue';
import 'overlayscrollbars/overlayscrollbars.css';
export default defineComponent({
components: { OverlayScrollbarsComponent },
data() {
return {
content: Array.from({ length: 50 }, (_, i) => `Item ${i + 1}`).join('<br>')
};
},
methods: {
onScroll(instance, event) {
console.log('Scroll event!', event, instance.state().scrollOffset);
},
onInitialized(instance) {
console.log('OverlayScrollbars initialized:', instance);
}
},
template: `
<div style="width: 300px; height: 200px; border: 1px solid #ccc;">
<h3>Scrollable Content</h3>
<OverlayScrollbarsComponent
element="div"
:options="{ scrollbars: { autoHide: 'scroll', autoHideDelay: 200 } }"
:events="{ scroll: onScroll, initialized: onInitialized }"
defer
style="height: 100%;"
>
<div v-html="content"></div>
</OverlayScrollbarsComponent>
</div>
`
});