Skip to content

Commit

Permalink
Merge branch 'v1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Amphiluke committed Oct 2, 2021
2 parents 422e9a6 + 1516e8b commit 7f5e711
Show file tree
Hide file tree
Showing 12 changed files with 431 additions and 412 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 2018,
"sourceType": "module"
},
"rules": {
Expand Down Expand Up @@ -159,4 +159,4 @@
"error"
]
}
}
}
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ The module exports a single object `handyScroll` which provides the following me
* [`mount`](#mounting-the-widget) — initializes and “mounts” the widgets in the specified containers;
* [`mounted`](#checking-widget-existence) — checks if the widget is already mounted in the given container;
* [`update`](#updating-scrollbar) — updates the widget parameters and position;
* [`destroy`](#destroying-the-widget) — destroys the widgets mounted in the specified containers and removes all related event handlers.
* [`destroy`](#destroying-the-widget) — destroys the widgets mounted in the specified containers and removes all related event handlers;
* [`destroyDetached`](#destroying-detached-widgets) — destroys handy-scroll widget instances whose containers were removed from the document.

### Mounting the widget

Expand Down Expand Up @@ -69,7 +70,7 @@ console.log(handyScroll.mounted("#spacious-container")); // true

### Updating scrollbar

If you mount the widget in a container whose size and/or content may dynamically change, then you need a way to update the scrollbar each time the container’s sizes change. This can be done by invoking the method `handyScroll.update()` as in the example below.
If you mount the widget in a container whose size and/or content may dynamically change, you need a way to update the scrollbar each time the container’s sizes change. This can be done by invoking the method `handyScroll.update()` as in the example below.

```javascript
handyScroll.mount(".spacious-container");
Expand All @@ -89,6 +90,19 @@ handyScroll.destroy(".spacious-container");

The method expects a single argument, the target containers reference, which can be either an element, or a list of elements, or a selector.


### Destroying detached widgets

If your app completely re-renders a large portion of DOM where handy-scroll widgets were mounted, actual container references are lost, and therefore you cannot unmount the widgets and perform related cleanup using the `destroy` method. In this case, you may just call the `handyScroll.destroyDetached()` method, and the module will find all “zombie” instances and will destroy them for you.

```javascript
handyScroll.mount(".main-view .spacious-container");
// ... the app re-renders the main view ...
document.querySelector(".main-view").innerHTML = "...";
// destroy handy-scroll widgets whose containers are not in the document anymore
handyScroll.destroyDetached();
```

### Special cases

If you want to attach the widget to a container living in a positioned box (e.g. a modal popup with `position: fixed`) then you need to apply two special indicating class names in the markup. The module detects these indicating class names (they are prefixed with `handy-scroll-`) and switches to a special functioning mode.
Expand All @@ -115,10 +129,6 @@ The [handy-scroll.css](dist/handy-scroll.css) file provides some basic styles fo

You can make the widget more “unobtrusive” so that it will appear only when the mouse pointer hovers over the scrollable container. To do so just apply the class `handy-scroll-hoverable` to the desired scrollable container owning the widget.

## Integration with Angular

If you have problems with the widget integration into your Angular app, please consult [this instruction](doc/angular-integration.md) first.

## Live demos

Check out some usage demos [here](https://amphiluke.github.io/handy-scroll/).
28 changes: 23 additions & 5 deletions dist/handy-scroll.es6.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*!
handy-scroll v1.0.6
handy-scroll v1.1.0
https://amphiluke.github.io/handy-scroll/
(c) 2021 Amphiluke
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.handyScroll = factory());
}(this, (function () { 'use strict';
})(this, (function () { 'use strict';

let slice = Array.prototype.slice;

Expand Down Expand Up @@ -116,7 +116,12 @@ https://amphiluke.github.io/handy-scroll/
instance.skipSyncWidget = false;
},
focusin() {
setTimeout(() => instance.syncWidget(), 0);
setTimeout(() => {
// The widget might be destroyed before the timer is triggered (issue #14)
if (instance.widget) {
instance.syncWidget();
}
}, 0);
}
}
}
Expand Down Expand Up @@ -195,7 +200,7 @@ https://amphiluke.github.io/handy-scroll/
}
};

let instances = []; // if it were not for IE it would be better to use WeakMap (container -> instance)
let instances = []; // if it were not for IE, it would be better to use Map (container -> instance)

let handyScroll = {
/**
Expand Down Expand Up @@ -253,6 +258,19 @@ https://amphiluke.github.io/handy-scroll/
return false;
});
});
},

/**
* Destroy handyScroll widgets whose containers are not in the document anymore
*/
destroyDetached() {
instances = instances.filter(instance => {
if (!dom.body.contains(instance.container)) {
instance.destroy();
return false;
}
return true;
});
}
};

Expand All @@ -264,4 +282,4 @@ https://amphiluke.github.io/handy-scroll/

return handyScroll;

})));
}));
4 changes: 2 additions & 2 deletions dist/handy-scroll.es6.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 22 additions & 5 deletions dist/handy-scroll.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*!
handy-scroll v1.0.6
handy-scroll v1.1.0
https://amphiluke.github.io/handy-scroll/
(c) 2021 Amphiluke
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.handyScroll = factory());
}(this, (function () { 'use strict';
})(this, (function () { 'use strict';

var slice = Array.prototype.slice; // Precaution to avoid reference errors when imported for SSR (issue #13)

Expand Down Expand Up @@ -124,7 +124,10 @@ https://amphiluke.github.io/handy-scroll/
},
focusin: function focusin() {
setTimeout(function () {
return instance.syncWidget();
// The widget might be destroyed before the timer is triggered (issue #14)
if (instance.widget) {
instance.syncWidget();
}
}, 0);
}
}
Expand Down Expand Up @@ -216,7 +219,7 @@ https://amphiluke.github.io/handy-scroll/
}
};

var instances = []; // if it were not for IE it would be better to use WeakMap (container -> instance)
var instances = []; // if it were not for IE, it would be better to use Map (container -> instance)

var handyScroll = {
/**
Expand Down Expand Up @@ -279,6 +282,20 @@ https://amphiluke.github.io/handy-scroll/
return false;
});
});
},

/**
* Destroy handyScroll widgets whose containers are not in the document anymore
*/
destroyDetached: function destroyDetached() {
instances = instances.filter(function (instance) {
if (!dom.body.contains(instance.container)) {
instance.destroy();
return false;
}

return true;
});
}
};

Expand All @@ -290,4 +307,4 @@ https://amphiluke.github.io/handy-scroll/

return handyScroll;

})));
}));
Loading

0 comments on commit 7f5e711

Please sign in to comment.