6.2.7 • Published 5 months ago

simplebar v6.2.7

Weekly downloads
1,065,392
License
MIT
Repository
github
Last release
5 months ago

SimpleBar npm package size-badge

SimpleBar is a plugin that tries to solve a long time problem: how to get custom scrollbars for your web-app while keeping a good user experience? SimpleBar does NOT implement a custom scroll behaviour. It keeps the native overflow: auto scroll and only replace the scrollbar visual appearance.

SimpleBar is meant to be as easy to use as possible and lightweight. If you want something more advanced I recommend KingSora 's Overlay Scrollbars.

Installation

- Via npm npm install simplebar resize-observer-polyfill --save

- Via Yarn yarn add simplebar resize-observer-polyfill

- Via <script> tag

<link
  rel="stylesheet"
  href="https://unpkg.com/simplebar@latest/dist/simplebar.css"
/>
<script src="https://unpkg.com/simplebar@latest/dist/simplebar.min.js"></script>
<!-- or -->
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/simplebar@latest/dist/simplebar.css"
/>
<script src="https://cdn.jsdelivr.net/npm/simplebar@latest/dist/simplebar.min.js"></script>

note: you should replace @latest to the latest version (ex @2.4.3), if you want to lock to a specific version. You can find the full list of modules available there.

Usage

Check out the React and Vue examples.

If you are using a module loader (like Webpack) you first need to load SimpleBar:

import 'simplebar'; // or "import SimpleBar from 'simplebar';" if you want to use it manually.
import 'simplebar/dist/simplebar.css';

// You will need a ResizeObserver polyfill for browsers that don't support it! (iOS Safari, Edge, ...)
import ResizeObserver from 'resize-observer-polyfill';
window.ResizeObserver = ResizeObserver;

You might also need other polyfills as SimpleBar comes with basic browser support only. You can use Babel @babel/preset-env to polyfill for you, see our examples package or check out polyfill.io.

Set data-simplebar on the element you want your custom scrollbar. You're done.

<div data-simplebar></div>

Don't forget to import both css and js in your project!

Noscript support

To make sure your elements are scrollable when JavaScript is disabled, it's important to include this snippet in your <head> to reset scrolling:

<noscript>
  <style>
    /**
    * Reinstate scrolling for non-JS clients
    */
    .simplebar-content-wrapper {
      scrollbar-width: auto;
      -ms-overflow-style: auto;
    }

    .simplebar-content-wrapper::-webkit-scrollbar,
    .simplebar-hide-scrollbar::-webkit-scrollbar {
      display: initial;
      width: initial;
      height: initial;
    }
  </style>
</noscript>

:warning: Warning!

SimpleBar is not intended to be used on the body element! I don't recommend wrapping your entire web page inside a custom scroll as it will often badly affect the user experience (slower scroll performance compared to the native body scroll, no native scroll behaviours like click on track, etc.). Do it at your own risk! SimpleBar is meant to improve the experience of internal web page scrolling; such as a chat box or a small scrolling area. Please read the caveats section first to be aware of the limitations!

Troubleshooting

If you are experiencing issues when setting up SimpleBar, it is most likely because your styles are clashing with SimpleBar ones. Make sure the element you are setting SimpleBar on does not override any SimpleBar css properties! We recommend to not style that element at all and use an inner element instead.

Sponsors

Thanks to BrowserStack for sponsoring open source projects and letting us test SimpleBar for free.


  1. Documentation
  2. Browsers support
  3. Demo
  4. How it works
  5. Caveats
  6. Changelog
  7. Credits

1. Documentation

Other usages

You can start SimpleBar manually if you need to:

new SimpleBar(document.getElementById('myElement'));

or

Array.prototype.forEach.call(
  document.querySelectorAll('.myElements'),
  (el) => new SimpleBar(el)
);

If you want to use jQuery:

new SimpleBar($('#myElement')[0]);

or

$('.myElements').each((el) => new SimpleBar(el));

Styling

The default styling is applied with CSS. There is no "built-in" way to style the scrollbar, you just need to override the default CSS.

Ex, to change the color of the scrollbar:

.simplebar-scrollbar::before {
  background-color: red;
}

Options

Options can be applied to the plugin during initialization:

new SimpleBar(document.getElementById('myElement'), {
  option1: value1,
  option2: value2,
});

or using data-attributes:

<div data-simplebar data-simplebar-auto-hide="false"></div>

Available options are:

autoHide

By default SimpleBar automatically hides the scrollbar if the user is not scrolling (it emulates Mac OSX Lion's scrollbar). You can make the scrollbar always visible by setting the autoHide option to false:

new SimpleBar(document.getElementById('myElement'), { autoHide: false });

Default value is true.

You can also control the animation via CSS as it's a simple CSS opacity transition.

scrollbarMinSize

Define the minimum scrollbar size in pixels.

Default value is 10.

classNames

It is possible to change the default class names that SimpleBar uses. To get your own styles to work refer to simplebar.css to get an idea how to setup your css.

  • content represents the wrapper for the content being scrolled.
  • scrollContent represents the container containing the elements being scrolled.
  • scrollbar defines the style of the scrollbar with which the user can interact to scroll the content.
  • track styles the area surrounding the scrollbar.
classNames: {
  // defaults
  content: 'simplebar-content',
  scrollContent: 'simplebar-scroll-content',
  scrollbar: 'simplebar-scrollbar',
  track: 'simplebar-track'
}

forceVisible

You can force the track to be visible (same behaviour as overflow: scroll) using the forceVisible option:

forceVisible: true|'x'|'y' (default to `false`)

By default, SimpleBar behave like overflow: auto.

direction (RTL support)

You can activate RTL support by passing the direction option:

direction: 'rtl' (default to `ltr`)

You will need both data-simplebar-direction='rtl' and a css rule with direction: rtl.

timeout (deprecated)

This option is deprecated. You can now achieve this in CSS:

.simplebar-scrollbar:before {
  transition-delay: 2s;
}

clickOnTrack

Controls the click on track behaviour.

Default to true.

scrollbarMinSize / scrollbarMaxSize

Controls the min and max size of the scrollbar in px.

Default for scrollbarMinSize is 25. Default for scrollbarMaxSize is 0 (no max size).

ariaLabel

Set custom aria-label attribute for users with screen reader.

The default value is scrollable content.

tabIndex

tabIndex to set for simplebar. Defaults to 0.

Apply scroll vertically only

Simply define in css overflow-x: hidden on your element.

Notifying the plugin of content changes

Note: you shouldn't need to use these functions as SimpleBar takes care of that automatically. This is for advanced usage only.

If later on you dynamically modify your content, for instance changing its height or width, or adding or removing content, you should recalculate the scrollbars like so:

const simpleBar = new SimpleBar(document.getElementById('myElement'));
simpleBar.recalculate();

Trigger programmatical scrolling

If you want to access to the original scroll element, you can retrieve it via a getter:

const simpleBar = new SimpleBar(document.getElementById('myElement'));
simpleBar.getScrollElement();

Subscribe to scroll event

You can subscribe to the scroll event, just like you do with native scrolling elements:

const simpleBar = new SimpleBar(document.getElementById('myElement'));
simpleBar.getScrollElement().addEventListener('scroll', function(...));

Add content dynamically

You can retrieve the element containing data like this:

const simpleBar = new SimpleBar(document.getElementById('myElement'));
simpleBar.getContentElement();

Disable Mutation Observer (core package only)

SimpleBar.removeObserver();

Retrieve SimpleBar instance from data-simplebar nodes

SimpleBar.instances.get(document.querySelector('[data-simplebar]']))

Non-JS fallback

SimpleBar hides the browser's default scrollbars, which obviously is undesirable if the user has JavaScript disabled. To restore the browser's scrollbars you can include the following noscript element in your document's head:

<noscript>
  <style>
    [data-simplebar] {
      overflow: auto;
    }
  </style>
</noscript>

2. Browsers support

SimpleBar has been tested on the following browsers: Chrome, Firefox, Safari, Edge, IE11.

Notice: IE10 doesn't support MutationObserver so you will still need to instantiate SimpleBar manually and call recalculate() as needed (or you can just use a polyfill for MutationObserver).

If you want to apply SimpleBar on an SVG element on IE11, you will need a polyfill for classList.

IE9 is not supported anymore (because we use translate3d to position the scrollbar) so please use SimpleBar v1 if you really need it.

3. Demo

https://grsmto.github.io/simplebar/

4. How it works

SimpleBar only does one thing: replace the browser's default scrollbars with a custom CSS-styled scrollbar without sacrificing performance. Unlike most other plugins, SimpleBar doesn't mimic scroll behaviour with Javascript, which typically causes jank and strange scrolling behaviour. You keep the awesomeness of native scrolling… with a custom scrollbar! Design your scrollbar how you like, with CSS, across all browsers.

For the most part SimpleBar uses the browser's native scrolling functionality, but replaces the conventional scrollbar with a custom CSS-styled scrollbar. The plugin listens for scroll events and redraws the custom scrollbar accordingly.

Key to this technique is hiding the native browser scrollbar. The scrollable element is made slightly wider/taller than its containing element, effectively hiding the scrollbar from view.

5. Caveats

  • SimpleBar can't be used on the <body>, <textarea>, <table> or <iframe> elements. If you are looking to support these, I suggest taking a look at OverLayScrollbars.
  • SimpleBar doesn't currently support overflow: visible. Which means any children of your scrolling div will be clipped (like with overflow: hidden).

Please take a look at this comparison table to see what SimpleBar does compare to others.

Community plugins

Ruby On Rails To include SimpleBar in the Ruby On Rails asset pipeline, use the simplebar-rails gem.

Ember.js To use SimpleBar with the Ember.js framework, use the ember-simplebars addon.

adminkit-propobeda-components@swarnavl/one-element-testabout-meepassbolt_extension@demivan/simplebar-vueplaylist-cover-makeressentialxdgiot_amis@williamyallop/cover-makerp2xinsig-ui-library@infinitebrahmanuniverse/nolb-simpleb@qrvey/qui-components@flipit/layoutvisionr-dev@everything-registry/sub-chunk-2767@vividcat/webcomponentssvelte-ui-componentssvelte-simplebarsveltekit-tailwind-componentstea-xyz-project-test-netwp-layout-componentwickes-csswickes-css2wtcher-js-teatea-project-test-netteammix-frame-element-vuetest-start-tempsvelponentsmidone-components@nikhilkhanke/react-mui-ui@musora/chatsora@musora/musora-uimidas_web_commonminimal-dashboard-react-componentslinkmatelaravel-scout-assetsinovabiz-template-frontendlowcoder-design1majestic-uimainboard-core-packagemainboard-core-testmainboard-core-test2mele-templatemixin-module-teamrvan2851mozo-sdk-uihlv-web-component-miahtmlpressimmerserinifokitaokitao-componentsmediafly-document-componentotsuka_design_system2022okam-componentsngx-intl-tel-input-sevenrepersonal-build-frontenderpesonamy-module-dark-thememyquant-fenuvo-reactnew-npm-testnet-teanpm-for-app-teareact-boilerplate-componentssiqthemeskewind-htmltwitch-twilight1ubold-laravelua-frontu-library@apl-common/components@applica-software-guru/react-admin@adminkit/core@alcercu/frontend-library@alfalab/core-components@alfalab/core-components-scrollbar@aiursoft/uistack@alicsystems/ui.components@azure/video-analyzer-widgets@capatech/mint@cling-se/widget@coffeekraken/scrollbar-webcomponent@bojit/svelte-components@deskpro/deskpro-uizephyrusvuesoravuerovue-simplebarwax-editor-core@festkit/festkit@fiftyone511/flashlight@greendistrict/sdk@fulcanellee/core-components-sandbox@genestack/ui@innomotics/brand-experience@lambda-platform/lambda-builder@leuven2030/ui@maejstro/sylius-megamenu-builder-plugin-frontend
6.2.7

5 months ago

6.2.6

7 months ago

6.1.0

2 years ago

6.1.1

2 years ago

6.2.5

2 years ago

6.2.4

2 years ago

6.2.1

2 years ago

6.2.0

2 years ago

6.2.3

2 years ago

6.2.2

2 years ago

6.0.0

2 years ago

6.0.0-beta.12

2 years ago

6.0.0-beta.11

2 years ago

5.3.9

2 years ago

5.3.8

3 years ago

5.3.7

3 years ago

5.3.6

3 years ago

5.3.5

3 years ago

5.3.4

3 years ago

5.3.3

4 years ago

5.3.2

4 years ago

6.0.0-beta.10

4 years ago

6.0.0-beta.9

4 years ago

6.0.0-beta.8

4 years ago

6.0.0-beta.7

4 years ago

5.3.0

4 years ago

6.0.0-beta.6

4 years ago

6.0.0-beta.5

4 years ago

6.0.0-beta.4

4 years ago

6.0.0-beta.3

4 years ago

6.0.0-beta.2

4 years ago

5.2.1

4 years ago

6.0.0-beta.1

5 years ago

5.2.0

5 years ago

5.1.0

5 years ago

6.0.0-beta.0

5 years ago

5.0.7

5 years ago

5.0.6

5 years ago

5.0.5-corejs2

5 years ago

5.0.5

5 years ago

5.0.4

5 years ago

5.0.3-corejs2

5 years ago

5.0.3

5 years ago

5.0.2-corejs2

5 years ago

5.0.2

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.3.0-alpha.1

5 years ago

4.2.3-corejs2

5 years ago

4.2.3

5 years ago

4.2.2

5 years ago

4.3.0-alpha.0

5 years ago

4.2.1

5 years ago

4.2.0

5 years ago

4.1.0

5 years ago

4.0.0

6 years ago

4.0.0-alpha.9

6 years ago

4.0.0-alpha.8

6 years ago

4.0.0-alpha.7

6 years ago

4.0.0-alpha.6

6 years ago

4.0.0-alpha.5

6 years ago

4.0.0-alpha.4

6 years ago

4.0.0-alpha.3

6 years ago

4.0.0-alpha.2

6 years ago

4.0.0-alpha.1

6 years ago

4.0.0-alpha.0

6 years ago

3.1.5

6 years ago

3.1.4

6 years ago

3.1.3

6 years ago

3.1.2

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.1.0-beta.5

6 years ago

3.1.0-beta.4

6 years ago

3.1.0-beta.3

6 years ago

3.1.0-beta.2

6 years ago

3.1.0-beta.1

6 years ago

3.0.0-beta.5

6 years ago

3.0.0-beta.4

6 years ago

3.0.0-beta.3

6 years ago

3.0.0-beta.2

6 years ago

3.0.0-beta.1

6 years ago

3.0.0-beta.0

6 years ago

2.6.1

7 years ago

2.6.0

7 years ago

2.5.1

7 years ago

2.5.0

7 years ago

2.4.4

7 years ago

2.4.3

7 years ago

2.4.2

7 years ago

2.4.1

8 years ago

2.4.0

8 years ago

2.3.2

8 years ago

2.3.1

8 years ago

2.3.0

8 years ago

2.2.2

8 years ago

2.2.1

8 years ago

2.2.0

8 years ago

2.1.0

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

2.0.0-beta.3

8 years ago

2.0.0-beta.2

8 years ago

2.0.0-beta.1

8 years ago

1.1.9

8 years ago