Skip to content

Commit

Permalink
add / createElements config option
Browse files Browse the repository at this point in the history
I did a react component to wrap gemini-scrollbar, however, becauses
gemini-scrollbar inyects HTMLElements at runtime modifying the nodes
structure, when the react-component did update, the contents will be all
messed up. This is because gemini-scrollbar was infering with React
(Virtual) DOM.

To fix this, I added a new config-option `createElements` (default =>
true), this option tells gemini-scrollbar if it needs to create and inyect
the elements at runtime, or if it should search for them on the `element`
container instead. Passing this option as false, will assume that you to have
added the required markup with the specific css-classes on them to the lib to
work.

Example markup:
```
<div class="something-scrollable">

  <!-- required markup -->
  <div class="gm-scrollbar -vertical">
    <div class="thumb"></div>
  </div>
  <div class="gm-scrollbar -horizontal">
    <div class="thumb"></div>
  </div>
  <div class="gm-scroll-view">
    All your content here.
  </div>
  <!-- required markup -->

</div>

<script>
new GeminiScrollbar({
  element : document.querySelector('.something-scrollable'),
  createElements : false
}).create();
</script>
```
  • Loading branch information
noeldelgado committed Mar 25, 2015
1 parent c1a7a89 commit 2bb73c8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gemini-scrollbar",
"main": "index.js",
"version": "0.0.2",
"version": "0.0.3",
"homepage": "https://github.com/noeldelgado/gemini-scrollbar",
"authors": [
"Noel Delgado <auhcsei@gmail.com>"
Expand Down
24 changes: 17 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
function GeminiScrollbar(config) {
this.element = null;
this.autoshow = false;
this.createElements = true;

Object.keys(config || {}).forEach(function (propertyName) {
this[propertyName] = config[propertyName];
Expand Down Expand Up @@ -77,13 +78,22 @@

this._document = document;
this._window = window;
this._viewElement = document.createElement('div');
this._scrollbarVerticalElement = document.createElement('div');
this._thumbVerticalElement = document.createElement('div');
this._scrollbarHorizontalElement = document.createElement('div');
this._thumbHorizontalElement = document.createElement('div');
while(this.element.childNodes.length > 0) {
this._viewElement.appendChild(this.element.childNodes[0]);

if (this.createElements === true) {
this._viewElement = document.createElement('div');
this._scrollbarVerticalElement = document.createElement('div');
this._thumbVerticalElement = document.createElement('div');
this._scrollbarHorizontalElement = document.createElement('div');
this._thumbHorizontalElement = document.createElement('div');
while(this.element.childNodes.length > 0) {
this._viewElement.appendChild(this.element.childNodes[0]);
}
} else {
this._viewElement = this.element.querySelector('.' + CLASSNAMES.view);
this._scrollbarVerticalElement =this.element.querySelector('.' + CLASSNAMES.verticalScrollbar.split(' ').join('.'));
this._thumbVerticalElement = this._scrollbarVerticalElement.querySelector('.' + CLASSNAMES.thumb);
this._scrollbarHorizontalElement = this.element.querySelector('.' + CLASSNAMES.horizontalScrollbar.split(' ').join('.'));
this._thumbHorizontalElement = this._scrollbarHorizontalElement.querySelector('.' + CLASSNAMES.thumb);
}

this.element.classList.add(CLASSNAMES.element);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gemini-scrollbar",
"version": "0.0.2",
"version": "0.0.3",
"description": "Custom scrollbars with native scrolling",
"license": "MIT",
"repository": "noeldelgado/gemini-scrollbar.git",
Expand Down

0 comments on commit 2bb73c8

Please sign in to comment.