@rfkhusnutdinov/scrollbar-width-observer 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +59 -0
- package/dist/js/index.esm.js +122 -0
- package/dist/js/index.js +130 -0
- package/dist/js/index.min.js +1 -0
- package/package.json +69 -0
- package/src/js/index.js +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Rinat Khusnutdinov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# ScrollbarWidthObserver
|
|
2
|
+
|
|
3
|
+
A lightweight JavaScript plugin to monitor changes in the scrollbar width.
|
|
4
|
+
Automatically updates a CSS variable and dispatches a `scrollbarwidthchange` event on changes.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- Observes scrollbar width changes on window resize
|
|
9
|
+
- Updates a CSS variable (default `--scrollbar-width`) with the current width
|
|
10
|
+
- Dispatches a custom `scrollbarwidthchange` event
|
|
11
|
+
- Singleton pattern ensures only one observer instance
|
|
12
|
+
- Debounced resize handling to optimize performance
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
### Install from npm
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @rfkhusnutdinov/scrollbar-width-observer
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
import ScrollbarWidthObserver from "@rfkhusnutdinov/scrollbar-width-observer";
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Use ScrollbarWidthObserver from CDN
|
|
27
|
+
|
|
28
|
+
```html
|
|
29
|
+
<script src="https://cdn.jsdelivr.net/npm/@rfkhusnutdinov/scrollbar-width-observer@latest"></script>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
const observer = new ScrollbarWidthObserver({
|
|
36
|
+
cssVariableName: "--scrollbar-width", // optional, default is --scrollbar-width
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Listen for scrollbar width changes
|
|
40
|
+
window.addEventListener("scrollbarwidthchange", (event) => {
|
|
41
|
+
console.log("Scrollbar width changed to:", event.detail.scrollbarWidth);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Disconnect observer when no longer needed
|
|
45
|
+
observer.disconnect();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Notes
|
|
49
|
+
|
|
50
|
+
- The plugin calculates the scrollbar width using a temporary DOM element.
|
|
51
|
+
- CSS variable is applied to body for easy usage in styles.
|
|
52
|
+
- Singleton pattern ensures only one instance is active at a time.
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
| Method | Description |
|
|
57
|
+
| ----------------------------------------------- | ----------------------------------------------------------------- |
|
|
58
|
+
| new ScrollbarWidthObserver({ cssVariableName }) | Initializes the observer or returns the existing instance |
|
|
59
|
+
| disconnect() | Stops observing, removes CSS variable, and cleans up the instance |
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
function _assertClassBrand(e, t, n) {
|
|
2
|
+
if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n;
|
|
3
|
+
throw new TypeError("Private element is not present on this object");
|
|
4
|
+
}
|
|
5
|
+
function _checkPrivateRedeclaration(e, t) {
|
|
6
|
+
if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
7
|
+
}
|
|
8
|
+
function _classCallCheck(a, n) {
|
|
9
|
+
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
|
|
10
|
+
}
|
|
11
|
+
function _classPrivateFieldGet2(s, a) {
|
|
12
|
+
return s.get(_assertClassBrand(s, a));
|
|
13
|
+
}
|
|
14
|
+
function _classPrivateFieldInitSpec(e, t, a) {
|
|
15
|
+
_checkPrivateRedeclaration(e, t), t.set(e, a);
|
|
16
|
+
}
|
|
17
|
+
function _classPrivateFieldSet2(s, a, r) {
|
|
18
|
+
return s.set(_assertClassBrand(s, a), r), r;
|
|
19
|
+
}
|
|
20
|
+
function _classPrivateMethodInitSpec(e, a) {
|
|
21
|
+
_checkPrivateRedeclaration(e, a), a.add(e);
|
|
22
|
+
}
|
|
23
|
+
function _defineProperties(e, r) {
|
|
24
|
+
for (var t = 0; t < r.length; t++) {
|
|
25
|
+
var o = r[t];
|
|
26
|
+
o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function _createClass(e, r, t) {
|
|
30
|
+
return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
|
|
31
|
+
writable: false
|
|
32
|
+
}), e;
|
|
33
|
+
}
|
|
34
|
+
function _toPrimitive(t, r) {
|
|
35
|
+
if ("object" != typeof t || !t) return t;
|
|
36
|
+
var e = t[Symbol.toPrimitive];
|
|
37
|
+
if (void 0 !== e) {
|
|
38
|
+
var i = e.call(t, r);
|
|
39
|
+
if ("object" != typeof i) return i;
|
|
40
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
41
|
+
}
|
|
42
|
+
return (String )(t);
|
|
43
|
+
}
|
|
44
|
+
function _toPropertyKey(t) {
|
|
45
|
+
var i = _toPrimitive(t, "string");
|
|
46
|
+
return "symbol" == typeof i ? i : i + "";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
var _timer = /*#__PURE__*/new WeakMap();
|
|
50
|
+
var _debounceDelay = /*#__PURE__*/new WeakMap();
|
|
51
|
+
var _cssVariableName = /*#__PURE__*/new WeakMap();
|
|
52
|
+
var _onResize = /*#__PURE__*/new WeakMap();
|
|
53
|
+
var _ScrollbarWidthObserver_brand = /*#__PURE__*/new WeakSet();
|
|
54
|
+
var ScrollbarWidthObserver = /*#__PURE__*/function () {
|
|
55
|
+
function ScrollbarWidthObserver() {
|
|
56
|
+
var _this = this;
|
|
57
|
+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
58
|
+
_ref$cssVariableName = _ref.cssVariableName,
|
|
59
|
+
cssVariableName = _ref$cssVariableName === void 0 ? "--scrollbar-width" : _ref$cssVariableName;
|
|
60
|
+
_classCallCheck(this, ScrollbarWidthObserver);
|
|
61
|
+
_classPrivateMethodInitSpec(this, _ScrollbarWidthObserver_brand);
|
|
62
|
+
_classPrivateFieldInitSpec(this, _timer, null);
|
|
63
|
+
_classPrivateFieldInitSpec(this, _debounceDelay, 50);
|
|
64
|
+
_classPrivateFieldInitSpec(this, _cssVariableName, void 0);
|
|
65
|
+
_classPrivateFieldInitSpec(this, _onResize, function () {
|
|
66
|
+
clearTimeout(_classPrivateFieldGet2(_timer, _this));
|
|
67
|
+
_classPrivateFieldSet2(_timer, _this, setTimeout(function () {
|
|
68
|
+
var newWidth = _assertClassBrand(_ScrollbarWidthObserver_brand, _this, _getSystemScrollbarWidth).call(_this);
|
|
69
|
+
if (newWidth === _this.scrollbarWidth) return;
|
|
70
|
+
_assertClassBrand(_ScrollbarWidthObserver_brand, _this, _updateScrollbarWidth).call(_this, newWidth);
|
|
71
|
+
}, _classPrivateFieldGet2(_debounceDelay, _this)));
|
|
72
|
+
});
|
|
73
|
+
if (_instance._) {
|
|
74
|
+
return _instance._;
|
|
75
|
+
}
|
|
76
|
+
_classPrivateFieldSet2(_cssVariableName, this, cssVariableName);
|
|
77
|
+
this.scrollbarWidth = _assertClassBrand(_ScrollbarWidthObserver_brand, this, _getSystemScrollbarWidth).call(this);
|
|
78
|
+
_assertClassBrand(_ScrollbarWidthObserver_brand, this, _setScrollbarWidthVariable).call(this);
|
|
79
|
+
window.addEventListener("resize", _classPrivateFieldGet2(_onResize, this));
|
|
80
|
+
_instance._ = this;
|
|
81
|
+
}
|
|
82
|
+
return _createClass(ScrollbarWidthObserver, [{
|
|
83
|
+
key: "disconnect",
|
|
84
|
+
value: function disconnect() {
|
|
85
|
+
if (!_instance._) return;
|
|
86
|
+
clearTimeout(_classPrivateFieldGet2(_timer, this));
|
|
87
|
+
window.removeEventListener("resize", _classPrivateFieldGet2(_onResize, this));
|
|
88
|
+
document.body.style.removeProperty(_classPrivateFieldGet2(_cssVariableName, this));
|
|
89
|
+
this.scrollbarWidth = null;
|
|
90
|
+
_instance._ = null;
|
|
91
|
+
}
|
|
92
|
+
}]);
|
|
93
|
+
}();
|
|
94
|
+
function _updateScrollbarWidth(width) {
|
|
95
|
+
this.scrollbarWidth = width;
|
|
96
|
+
_assertClassBrand(_ScrollbarWidthObserver_brand, this, _setScrollbarWidthVariable).call(this);
|
|
97
|
+
window.dispatchEvent(new CustomEvent("scrollbarwidthchange", {
|
|
98
|
+
detail: {
|
|
99
|
+
scrollbarWidth: width
|
|
100
|
+
}
|
|
101
|
+
}));
|
|
102
|
+
}
|
|
103
|
+
function _getSystemScrollbarWidth() {
|
|
104
|
+
var div = document.createElement("div");
|
|
105
|
+
div.style.width = "100px";
|
|
106
|
+
div.style.height = "100px";
|
|
107
|
+
div.style.overflow = "scroll";
|
|
108
|
+
div.style.position = "absolute";
|
|
109
|
+
div.style.top = "-9999px";
|
|
110
|
+
document.body.appendChild(div);
|
|
111
|
+
var width = div.offsetWidth - div.clientWidth;
|
|
112
|
+
document.body.removeChild(div);
|
|
113
|
+
return width;
|
|
114
|
+
}
|
|
115
|
+
function _setScrollbarWidthVariable() {
|
|
116
|
+
document.body.style.setProperty(_classPrivateFieldGet2(_cssVariableName, this), "".concat(this.scrollbarWidth, "px"));
|
|
117
|
+
}
|
|
118
|
+
var _instance = {
|
|
119
|
+
_: void 0
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export { ScrollbarWidthObserver as default };
|
package/dist/js/index.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ScrollbarWidthObserver = factory());
|
|
5
|
+
})(this, (function () { 'use strict';
|
|
6
|
+
|
|
7
|
+
function _assertClassBrand(e, t, n) {
|
|
8
|
+
if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n;
|
|
9
|
+
throw new TypeError("Private element is not present on this object");
|
|
10
|
+
}
|
|
11
|
+
function _checkPrivateRedeclaration(e, t) {
|
|
12
|
+
if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
13
|
+
}
|
|
14
|
+
function _classCallCheck(a, n) {
|
|
15
|
+
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
|
|
16
|
+
}
|
|
17
|
+
function _classPrivateFieldGet2(s, a) {
|
|
18
|
+
return s.get(_assertClassBrand(s, a));
|
|
19
|
+
}
|
|
20
|
+
function _classPrivateFieldInitSpec(e, t, a) {
|
|
21
|
+
_checkPrivateRedeclaration(e, t), t.set(e, a);
|
|
22
|
+
}
|
|
23
|
+
function _classPrivateFieldSet2(s, a, r) {
|
|
24
|
+
return s.set(_assertClassBrand(s, a), r), r;
|
|
25
|
+
}
|
|
26
|
+
function _classPrivateMethodInitSpec(e, a) {
|
|
27
|
+
_checkPrivateRedeclaration(e, a), a.add(e);
|
|
28
|
+
}
|
|
29
|
+
function _defineProperties(e, r) {
|
|
30
|
+
for (var t = 0; t < r.length; t++) {
|
|
31
|
+
var o = r[t];
|
|
32
|
+
o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function _createClass(e, r, t) {
|
|
36
|
+
return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
|
|
37
|
+
writable: false
|
|
38
|
+
}), e;
|
|
39
|
+
}
|
|
40
|
+
function _toPrimitive(t, r) {
|
|
41
|
+
if ("object" != typeof t || !t) return t;
|
|
42
|
+
var e = t[Symbol.toPrimitive];
|
|
43
|
+
if (void 0 !== e) {
|
|
44
|
+
var i = e.call(t, r);
|
|
45
|
+
if ("object" != typeof i) return i;
|
|
46
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
47
|
+
}
|
|
48
|
+
return (String )(t);
|
|
49
|
+
}
|
|
50
|
+
function _toPropertyKey(t) {
|
|
51
|
+
var i = _toPrimitive(t, "string");
|
|
52
|
+
return "symbol" == typeof i ? i : i + "";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
var _timer = /*#__PURE__*/new WeakMap();
|
|
56
|
+
var _debounceDelay = /*#__PURE__*/new WeakMap();
|
|
57
|
+
var _cssVariableName = /*#__PURE__*/new WeakMap();
|
|
58
|
+
var _onResize = /*#__PURE__*/new WeakMap();
|
|
59
|
+
var _ScrollbarWidthObserver_brand = /*#__PURE__*/new WeakSet();
|
|
60
|
+
var ScrollbarWidthObserver = /*#__PURE__*/function () {
|
|
61
|
+
function ScrollbarWidthObserver() {
|
|
62
|
+
var _this = this;
|
|
63
|
+
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
64
|
+
_ref$cssVariableName = _ref.cssVariableName,
|
|
65
|
+
cssVariableName = _ref$cssVariableName === void 0 ? "--scrollbar-width" : _ref$cssVariableName;
|
|
66
|
+
_classCallCheck(this, ScrollbarWidthObserver);
|
|
67
|
+
_classPrivateMethodInitSpec(this, _ScrollbarWidthObserver_brand);
|
|
68
|
+
_classPrivateFieldInitSpec(this, _timer, null);
|
|
69
|
+
_classPrivateFieldInitSpec(this, _debounceDelay, 50);
|
|
70
|
+
_classPrivateFieldInitSpec(this, _cssVariableName, void 0);
|
|
71
|
+
_classPrivateFieldInitSpec(this, _onResize, function () {
|
|
72
|
+
clearTimeout(_classPrivateFieldGet2(_timer, _this));
|
|
73
|
+
_classPrivateFieldSet2(_timer, _this, setTimeout(function () {
|
|
74
|
+
var newWidth = _assertClassBrand(_ScrollbarWidthObserver_brand, _this, _getSystemScrollbarWidth).call(_this);
|
|
75
|
+
if (newWidth === _this.scrollbarWidth) return;
|
|
76
|
+
_assertClassBrand(_ScrollbarWidthObserver_brand, _this, _updateScrollbarWidth).call(_this, newWidth);
|
|
77
|
+
}, _classPrivateFieldGet2(_debounceDelay, _this)));
|
|
78
|
+
});
|
|
79
|
+
if (_instance._) {
|
|
80
|
+
return _instance._;
|
|
81
|
+
}
|
|
82
|
+
_classPrivateFieldSet2(_cssVariableName, this, cssVariableName);
|
|
83
|
+
this.scrollbarWidth = _assertClassBrand(_ScrollbarWidthObserver_brand, this, _getSystemScrollbarWidth).call(this);
|
|
84
|
+
_assertClassBrand(_ScrollbarWidthObserver_brand, this, _setScrollbarWidthVariable).call(this);
|
|
85
|
+
window.addEventListener("resize", _classPrivateFieldGet2(_onResize, this));
|
|
86
|
+
_instance._ = this;
|
|
87
|
+
}
|
|
88
|
+
return _createClass(ScrollbarWidthObserver, [{
|
|
89
|
+
key: "disconnect",
|
|
90
|
+
value: function disconnect() {
|
|
91
|
+
if (!_instance._) return;
|
|
92
|
+
clearTimeout(_classPrivateFieldGet2(_timer, this));
|
|
93
|
+
window.removeEventListener("resize", _classPrivateFieldGet2(_onResize, this));
|
|
94
|
+
document.body.style.removeProperty(_classPrivateFieldGet2(_cssVariableName, this));
|
|
95
|
+
this.scrollbarWidth = null;
|
|
96
|
+
_instance._ = null;
|
|
97
|
+
}
|
|
98
|
+
}]);
|
|
99
|
+
}();
|
|
100
|
+
function _updateScrollbarWidth(width) {
|
|
101
|
+
this.scrollbarWidth = width;
|
|
102
|
+
_assertClassBrand(_ScrollbarWidthObserver_brand, this, _setScrollbarWidthVariable).call(this);
|
|
103
|
+
window.dispatchEvent(new CustomEvent("scrollbarwidthchange", {
|
|
104
|
+
detail: {
|
|
105
|
+
scrollbarWidth: width
|
|
106
|
+
}
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
function _getSystemScrollbarWidth() {
|
|
110
|
+
var div = document.createElement("div");
|
|
111
|
+
div.style.width = "100px";
|
|
112
|
+
div.style.height = "100px";
|
|
113
|
+
div.style.overflow = "scroll";
|
|
114
|
+
div.style.position = "absolute";
|
|
115
|
+
div.style.top = "-9999px";
|
|
116
|
+
document.body.appendChild(div);
|
|
117
|
+
var width = div.offsetWidth - div.clientWidth;
|
|
118
|
+
document.body.removeChild(div);
|
|
119
|
+
return width;
|
|
120
|
+
}
|
|
121
|
+
function _setScrollbarWidthVariable() {
|
|
122
|
+
document.body.style.setProperty(_classPrivateFieldGet2(_cssVariableName, this), "".concat(this.scrollbarWidth, "px"));
|
|
123
|
+
}
|
|
124
|
+
var _instance = {
|
|
125
|
+
_: void 0
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return ScrollbarWidthObserver;
|
|
129
|
+
|
|
130
|
+
}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).ScrollbarWidthObserver=e()}(this,function(){"use strict";function t(t,e,i){if("function"==typeof t?t===e:t.has(e))return arguments.length<3?e:i;throw new TypeError("Private element is not present on this object")}function e(t,e){if(e.has(t))throw new TypeError("Cannot initialize the same private elements twice on an object")}function i(e,i){return e.get(t(e,i))}function n(t,i,n){e(t,i),i.set(t,n)}function o(e,i,n){return e.set(t(e,i),n),n}function r(t,e,i){return e&&function(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,l(n.key),n)}}(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t}function l(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var n=i.call(t,e);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t,"string");return"symbol"==typeof e?e:e+""}var s=new WeakMap,a=new WeakMap,c=new WeakMap,u=new WeakMap,h=new WeakSet,d=function(){return r(function r(){var l,d,b=this,m=(arguments.length>0&&void 0!==arguments[0]?arguments[0]:{}).cssVariableName,w=void 0===m?"--scrollbar-width":m;if(function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,r),e(l=this,d=h),d.add(l),n(this,s,null),n(this,a,50),n(this,c,void 0),n(this,u,function(){clearTimeout(i(s,b)),o(s,b,setTimeout(function(){var e=t(h,b,p).call(b);e!==b.scrollbarWidth&&t(h,b,f).call(b,e)},i(a,b)))}),y._)return y._;o(c,this,w),this.scrollbarWidth=t(h,this,p).call(this),t(h,this,v).call(this),window.addEventListener("resize",i(u,this)),y._=this},[{key:"disconnect",value:function(){y._&&(clearTimeout(i(s,this)),window.removeEventListener("resize",i(u,this)),document.body.style.removeProperty(i(c,this)),this.scrollbarWidth=null,y._=null)}}])}();function f(e){this.scrollbarWidth=e,t(h,this,v).call(this),window.dispatchEvent(new CustomEvent("scrollbarwidthchange",{detail:{scrollbarWidth:e}}))}function p(){var t=document.createElement("div");t.style.width="100px",t.style.height="100px",t.style.overflow="scroll",t.style.position="absolute",t.style.top="-9999px",document.body.appendChild(t);var e=t.offsetWidth-t.clientWidth;return document.body.removeChild(t),e}function v(){document.body.style.setProperty(i(c,this),"".concat(this.scrollbarWidth,"px"))}var y={_:void 0};return d});
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rfkhusnutdinov/scrollbar-width-observer",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "JavaScript plugin to monitor scrollbar width and update a CSS variable.",
|
|
5
|
+
"author": "Rinat Khusnutdinov <rfkhusnutdinov@yandex.ru>",
|
|
6
|
+
"homepage": "https://github.com/rfkhusnutdinov/scrollbar-width-observer#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/rfkhusnutdinov/scrollbar-width-observer/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/rfkhusnutdinov/scrollbar-width-observer.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"javascript",
|
|
16
|
+
"plugin",
|
|
17
|
+
"scrollbar",
|
|
18
|
+
"resize",
|
|
19
|
+
"observer",
|
|
20
|
+
"ui",
|
|
21
|
+
"scroll"
|
|
22
|
+
],
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"src"
|
|
26
|
+
],
|
|
27
|
+
"main": "./dust/js/index.js",
|
|
28
|
+
"module": "./dist/js/index.esm.js",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"babel": {
|
|
31
|
+
"presets": [
|
|
32
|
+
"@babel/preset-env"
|
|
33
|
+
]
|
|
34
|
+
},
|
|
35
|
+
"browserslist": [
|
|
36
|
+
">0.3%",
|
|
37
|
+
"defaults",
|
|
38
|
+
"fully supports es6-module"
|
|
39
|
+
],
|
|
40
|
+
"scripts": {
|
|
41
|
+
"lint:scripts": "eslint src/**/*.{js,mjs} --fix || true",
|
|
42
|
+
"lint:styles": "stylelint --fix ./src/**/*.{scss,css}",
|
|
43
|
+
"lint": "npm run lint:scripts && npm run lint:styles",
|
|
44
|
+
"build:styles": "node scripts/build-styles.mjs --prod",
|
|
45
|
+
"build:scripts": "node scripts/build-scripts.mjs --prod",
|
|
46
|
+
"build": "npm run build:styles && npm run build:scripts",
|
|
47
|
+
"watch": "node scripts/watch-files.mjs"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@babel/preset-env": "^7.26.7",
|
|
51
|
+
"@eslint/js": "^9.19.0",
|
|
52
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
53
|
+
"@rollup/plugin-commonjs": "^28.0.2",
|
|
54
|
+
"@rollup/plugin-node-resolve": "^16.0.0",
|
|
55
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
56
|
+
"autoprefixer": "^10.4.20",
|
|
57
|
+
"chokidar": "^4.0.3",
|
|
58
|
+
"cssnano": "^7.0.6",
|
|
59
|
+
"eslint": "^9.19.0",
|
|
60
|
+
"globals": "^15.14.0",
|
|
61
|
+
"postcss": "^8.5.1",
|
|
62
|
+
"prettier": "^3.4.2",
|
|
63
|
+
"rollup": "^4.34.0",
|
|
64
|
+
"sass": "^1.83.4",
|
|
65
|
+
"stylelint": "^16.14.1",
|
|
66
|
+
"stylelint-config-recess-order": "^6.0.0",
|
|
67
|
+
"stylelint-config-standard-scss": "^14.0.0"
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/js/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
class ScrollbarWidthObserver {
|
|
2
|
+
static #instance;
|
|
3
|
+
#timer = null;
|
|
4
|
+
#debounceDelay = 50;
|
|
5
|
+
#cssVariableName;
|
|
6
|
+
|
|
7
|
+
constructor({ cssVariableName = "--scrollbar-width" } = {}) {
|
|
8
|
+
if (ScrollbarWidthObserver.#instance) {
|
|
9
|
+
return ScrollbarWidthObserver.#instance;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
this.#cssVariableName = cssVariableName;
|
|
13
|
+
|
|
14
|
+
this.scrollbarWidth = this.#getSystemScrollbarWidth();
|
|
15
|
+
|
|
16
|
+
this.#setScrollbarWidthVariable();
|
|
17
|
+
window.addEventListener("resize", this.#onResize);
|
|
18
|
+
|
|
19
|
+
ScrollbarWidthObserver.#instance = this;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
#onResize = () => {
|
|
23
|
+
clearTimeout(this.#timer);
|
|
24
|
+
|
|
25
|
+
this.#timer = setTimeout(() => {
|
|
26
|
+
const newWidth = this.#getSystemScrollbarWidth();
|
|
27
|
+
if (newWidth === this.scrollbarWidth) return;
|
|
28
|
+
|
|
29
|
+
this.#updateScrollbarWidth(newWidth);
|
|
30
|
+
}, this.#debounceDelay);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
#updateScrollbarWidth(width) {
|
|
34
|
+
this.scrollbarWidth = width;
|
|
35
|
+
this.#setScrollbarWidthVariable();
|
|
36
|
+
|
|
37
|
+
window.dispatchEvent(
|
|
38
|
+
new CustomEvent("scrollbarwidthchange", {
|
|
39
|
+
detail: { scrollbarWidth: width },
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#getSystemScrollbarWidth() {
|
|
45
|
+
const div = document.createElement("div");
|
|
46
|
+
|
|
47
|
+
div.style.width = "100px";
|
|
48
|
+
div.style.height = "100px";
|
|
49
|
+
div.style.overflow = "scroll";
|
|
50
|
+
div.style.position = "absolute";
|
|
51
|
+
div.style.top = "-9999px";
|
|
52
|
+
|
|
53
|
+
document.body.appendChild(div);
|
|
54
|
+
|
|
55
|
+
const width = div.offsetWidth - div.clientWidth;
|
|
56
|
+
|
|
57
|
+
document.body.removeChild(div);
|
|
58
|
+
|
|
59
|
+
return width;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
#setScrollbarWidthVariable() {
|
|
63
|
+
document.body.style.setProperty(
|
|
64
|
+
this.#cssVariableName,
|
|
65
|
+
`${this.scrollbarWidth}px`,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
disconnect() {
|
|
70
|
+
if (!ScrollbarWidthObserver.#instance) return;
|
|
71
|
+
|
|
72
|
+
clearTimeout(this.#timer);
|
|
73
|
+
window.removeEventListener("resize", this.#onResize);
|
|
74
|
+
|
|
75
|
+
document.body.style.removeProperty(this.#cssVariableName);
|
|
76
|
+
|
|
77
|
+
this.scrollbarWidth = null;
|
|
78
|
+
ScrollbarWidthObserver.#instance = null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export default ScrollbarWidthObserver;
|