SimpleBar for Vue
simplebar-vue is a Vue.js component wrapper for the highly customizable native JavaScript scrollbar library, SimpleBar. It enables developers to easily integrate custom scrollbars into their Vue applications, leveraging native browser scroll behavior for performance while offering extensive styling options. The current stable version of simplebar-vue is 2.4.2, which depends on SimpleBar core v6.x. The core SimpleBar library itself sees fairly regular updates, including major version bumps with breaking changes, which are then reflected in subsequent simplebar-vue releases. Its key differentiator lies in its approach to custom scrollbars: instead of recreating scroll behavior with JavaScript, it maintains the native scrolling experience, ensuring accessibility and performance, particularly for internal web page scrolling areas like chat boxes or modals, rather than the entire `body` element.
Common errors
-
Scrollbar is not visible or looks broken, or native scrollbar is still present.
cause Conflicting CSS styles, incorrect sizing of the parent or SimpleBar container, or not importing the necessary SimpleBar CSS stylesheet.fixEnsure `simplebar-vue/dist/simplebar.min.css` is imported. Verify that the parent container of `<simplebar>` has defined dimensions (e.g., `height` or `max-height`) to create an overflow condition. Also, avoid applying conflicting styles to the `<simplebar>` component itself; style its *inner content* instead. -
Vue component not rendering or 'simplebar is not defined' error in template.
cause The `simplebar` component was not properly registered in the Vue application's `components` option.fixMake sure to `import simplebar from 'simplebar-vue';` and add it to your Vue component's `components: { simplebar },` option before using it in the template. -
'TypeError: (0 , SimpleBar.default).getOptions is not a function' or similar when using simplebar-vue@2.0.0-beta.2.
cause This was a known bug in a specific beta version of `simplebar-vue` related to how the core SimpleBar library was being imported and exposed.fixUpgrade `simplebar-vue` to version `2.0.0-beta.3` or later to resolve this specific import/initialization issue. The current stable `2.4.2` should not have this problem.
Warnings
- gotcha SimpleBar is not intended for use on the `body` element. Applying it to the entire webpage can lead to reduced scroll performance and loss of native scroll behaviors. It is designed for internal page scrolling areas like chat boxes, modals, or specific content blocks.
- breaking Starting with core SimpleBar v6 (which simplebar-vue@2.4.2 depends on), the library no longer provides polyfills by default and was rewritten in TypeScript. If your target browsers do not natively support certain features, you might need to manually include polyfills (e.g., via Babel's `@babel/preset-env` or `polyfill.io`).
- breaking SimpleBar v6 dropped support for browsers that do not support scrollbar hiding via CSS. This resulted in the removal of several internal DOM elements (e.g., `.simplebar-height-auto-observer`, `.simplebar-mask`, `.simplebar-offset`). If your CSS or JavaScript directly interacted with these internal elements, your implementation might break.
- gotcha Custom styles applied directly to the element where `simplebar-vue` is instantiated can clash with SimpleBar's internal CSS, leading to unexpected rendering or scroll issues. SimpleBar injects its own styles to manage scrollbar appearance and behavior.
- breaking In core SimpleBar v4 and later, a new wrapper `.simplebar-content-wrapper` was introduced, and the method `getScrollElement()` no longer returned the content element. Developers needed to use `getContentElement()` instead. While `simplebar-vue` abstracts some DOM interaction, direct programmatic access to SimpleBar instances and their elements might be affected if not updated.
- breaking From SimpleBar v5.0.1, the library no longer mutates the attached DOM node for retrieving the SimpleBar instance. If you previously accessed `element.SimpleBar`, you now need to use `SimpleBar.instances.get(element)` after importing the core `simplebar` library.
Install
-
npm install simplebar-vue -
yarn add simplebar-vue -
pnpm add simplebar-vue
Imports
- simplebar
const simplebar = require('simplebar-vue');import simplebar from 'simplebar-vue';
- CSS Stylesheet
import 'simplebar/dist/simplebar.min.css';
import 'simplebar-vue/dist/simplebar.min.css';
- SimpleBar instance (advanced)
import SimpleBar from 'simplebar'; // ... later in Vue component method ... const simplebarInstance = SimpleBar.instances.get(this.$refs.mySimplebarRef.$el);
Quickstart
import { createApp } from 'vue';
import simplebar from 'simplebar-vue';
import 'simplebar-vue/dist/simplebar.min.css';
const app = createApp({
components: {
simplebar,
},
template: `
<div id="app-wrapper" style="height: 300px; width: 400px; border: 1px solid #eee;">
<h2>Content inside SimpleBar</h2>
<p>This is some content that will be scrollable using SimpleBar.</p>
<simplebar data-simplebar-auto-hide="false" style="max-height: 250px;">
<div style="height: 500px; padding: 20px;">
<p v-for="n in 20" :key="n">Scrollable content line {{ n }}.</p>
<p>More content to make it scroll.</p>
</div>
</simplebar>
<p>Content outside SimpleBar.</p>
</div>
`,
});
app.mount('#app-wrapper');