@karta.io/simplebar 0.16.0
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/README.md +5 -0
- package/dist/simplebar.cjs +647 -0
- package/dist/simplebar.d.ts +50 -0
- package/dist/simplebar.mjs +641 -0
- package/package.json +42 -0
- package/src/helpers.js +46 -0
- package/src/index.js +109 -0
- package/src/scrollbar-width.js +40 -0
- package/src/simplebar.d.ts +51 -0
- package/src/simplebar.js +947 -0
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@karta.io/simplebar",
|
|
3
|
+
"version": "0.16.0",
|
|
4
|
+
"description": "Клон core части библиотеки simplebar",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"src",
|
|
8
|
+
"README.md"
|
|
9
|
+
],
|
|
10
|
+
"author": "Vitaly Blokhin <karambafe@gmail.com>",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"require": "./dist/simplebar.cjs",
|
|
14
|
+
"import": "./dist/simplebar.mjs"
|
|
15
|
+
},
|
|
16
|
+
"./*": "./*"
|
|
17
|
+
},
|
|
18
|
+
"main": "dist/simplebar.cjs",
|
|
19
|
+
"module": "dist/simplebar.mjs",
|
|
20
|
+
"types": "dist/simplebar.d.ts",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@juggle/resize-observer": "3.3.1",
|
|
24
|
+
"can-use-dom": "0.1.0",
|
|
25
|
+
"lodash-es": "4.17.21"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"unbuild": "0.7.4"
|
|
29
|
+
},
|
|
30
|
+
"lint-staged": {
|
|
31
|
+
"*.{js,jsx,json}": [
|
|
32
|
+
"prettier-eslint --write",
|
|
33
|
+
"git add"
|
|
34
|
+
]
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"dev": "rollup -c -w",
|
|
38
|
+
"build": "unbuild",
|
|
39
|
+
"precommit": "lint-staged",
|
|
40
|
+
"pub": "pnpm build && pnpm publish --no-git-checks"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/helpers.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Helper function to retrieve options from element attributes
|
|
2
|
+
export const getOptions = function(obj) {
|
|
3
|
+
const options = Array.prototype.reduce.call(
|
|
4
|
+
obj,
|
|
5
|
+
(acc, attribute) => {
|
|
6
|
+
const option = attribute.name.match(/data-simplebar-(.+)/);
|
|
7
|
+
if (option) {
|
|
8
|
+
const key = option[1].replace(/\W+(.)/g, (x, chr) => chr.toUpperCase());
|
|
9
|
+
switch (attribute.value) {
|
|
10
|
+
case 'true':
|
|
11
|
+
acc[key] = true;
|
|
12
|
+
break;
|
|
13
|
+
case 'false':
|
|
14
|
+
acc[key] = false;
|
|
15
|
+
break;
|
|
16
|
+
case undefined:
|
|
17
|
+
acc[key] = true;
|
|
18
|
+
break;
|
|
19
|
+
default:
|
|
20
|
+
acc[key] = attribute.value;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return acc;
|
|
24
|
+
},
|
|
25
|
+
{}
|
|
26
|
+
);
|
|
27
|
+
return options;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export function getElementWindow(element) {
|
|
31
|
+
if (
|
|
32
|
+
!element ||
|
|
33
|
+
!element.ownerDocument ||
|
|
34
|
+
!element.ownerDocument.defaultView
|
|
35
|
+
) {
|
|
36
|
+
return window;
|
|
37
|
+
}
|
|
38
|
+
return element.ownerDocument.defaultView;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getElementDocument(element) {
|
|
42
|
+
if (!element || !element.ownerDocument) {
|
|
43
|
+
return document;
|
|
44
|
+
}
|
|
45
|
+
return element.ownerDocument;
|
|
46
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import canUseDOM from 'can-use-dom';
|
|
2
|
+
|
|
3
|
+
import SimpleBar from './simplebar';
|
|
4
|
+
import { getOptions } from './helpers';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @todo переписать на typescript
|
|
8
|
+
*/
|
|
9
|
+
SimpleBar.initDOMLoadedElements = function() {
|
|
10
|
+
document.removeEventListener('DOMContentLoaded', this.initDOMLoadedElements);
|
|
11
|
+
window.removeEventListener('load', this.initDOMLoadedElements);
|
|
12
|
+
|
|
13
|
+
Array.prototype.forEach.call(
|
|
14
|
+
document.querySelectorAll('[data-simplebar]'),
|
|
15
|
+
el => {
|
|
16
|
+
if (
|
|
17
|
+
el.getAttribute('data-simplebar') !== 'init' &&
|
|
18
|
+
!SimpleBar.instances.has(el)
|
|
19
|
+
)
|
|
20
|
+
new SimpleBar(el, getOptions(el.attributes));
|
|
21
|
+
}
|
|
22
|
+
);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
SimpleBar.removeObserver = function() {
|
|
26
|
+
this.globalObserver.disconnect();
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
SimpleBar.initHtmlApi = function() {
|
|
30
|
+
this.initDOMLoadedElements = this.initDOMLoadedElements.bind(this);
|
|
31
|
+
|
|
32
|
+
// MutationObserver is IE11+
|
|
33
|
+
if (typeof MutationObserver !== 'undefined') {
|
|
34
|
+
// Mutation observer to observe dynamically added elements
|
|
35
|
+
this.globalObserver = new MutationObserver(SimpleBar.handleMutations);
|
|
36
|
+
|
|
37
|
+
this.globalObserver.observe(document, { childList: true, subtree: true });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Taken from jQuery `ready` function
|
|
41
|
+
// Instantiate elements already present on the page
|
|
42
|
+
if (
|
|
43
|
+
document.readyState === 'complete' ||
|
|
44
|
+
(document.readyState !== 'loading' && !document.documentElement.doScroll)
|
|
45
|
+
) {
|
|
46
|
+
// Handle it asynchronously to allow scripts the opportunity to delay init
|
|
47
|
+
window.setTimeout(this.initDOMLoadedElements);
|
|
48
|
+
} else {
|
|
49
|
+
document.addEventListener('DOMContentLoaded', this.initDOMLoadedElements);
|
|
50
|
+
window.addEventListener('load', this.initDOMLoadedElements);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
SimpleBar.handleMutations = mutations => {
|
|
55
|
+
mutations.forEach(mutation => {
|
|
56
|
+
Array.prototype.forEach.call(mutation.addedNodes, addedNode => {
|
|
57
|
+
if (addedNode.nodeType === 1) {
|
|
58
|
+
if (addedNode.hasAttribute('data-simplebar')) {
|
|
59
|
+
!SimpleBar.instances.has(addedNode) &&
|
|
60
|
+
document.documentElement.contains(addedNode) &&
|
|
61
|
+
new SimpleBar(addedNode, getOptions(addedNode.attributes));
|
|
62
|
+
} else {
|
|
63
|
+
Array.prototype.forEach.call(
|
|
64
|
+
addedNode.querySelectorAll('[data-simplebar]'),
|
|
65
|
+
function(el) {
|
|
66
|
+
if (
|
|
67
|
+
el.getAttribute('data-simplebar') !== 'init' &&
|
|
68
|
+
!SimpleBar.instances.has(el) &&
|
|
69
|
+
document.documentElement.contains(el)
|
|
70
|
+
)
|
|
71
|
+
new SimpleBar(el, getOptions(el.attributes));
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
Array.prototype.forEach.call(mutation.removedNodes, removedNode => {
|
|
79
|
+
if (removedNode.nodeType === 1) {
|
|
80
|
+
if (removedNode.getAttribute('data-simplebar') === 'init') {
|
|
81
|
+
SimpleBar.instances.has(removedNode) &&
|
|
82
|
+
!document.documentElement.contains(removedNode) &&
|
|
83
|
+
SimpleBar.instances.get(removedNode).unMount();
|
|
84
|
+
} else {
|
|
85
|
+
Array.prototype.forEach.call(
|
|
86
|
+
removedNode.querySelectorAll('[data-simplebar="init"]'),
|
|
87
|
+
el => {
|
|
88
|
+
SimpleBar.instances.has(el) &&
|
|
89
|
+
!document.documentElement.contains(el) &&
|
|
90
|
+
SimpleBar.instances.get(el).unMount();
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
SimpleBar.getOptions = getOptions;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* HTML API
|
|
103
|
+
* Called only in a browser env.
|
|
104
|
+
*/
|
|
105
|
+
if (canUseDOM) {
|
|
106
|
+
SimpleBar.initHtmlApi();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export default SimpleBar;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import canUseDOM from 'can-use-dom';
|
|
2
|
+
import { getElementDocument } from "./helpers";
|
|
3
|
+
|
|
4
|
+
let cachedScrollbarWidth = null;
|
|
5
|
+
let cachedDevicePixelRatio = null;
|
|
6
|
+
|
|
7
|
+
if (canUseDOM) {
|
|
8
|
+
window.addEventListener('resize', () => {
|
|
9
|
+
if (cachedDevicePixelRatio !== window.devicePixelRatio) {
|
|
10
|
+
cachedDevicePixelRatio = window.devicePixelRatio;
|
|
11
|
+
cachedScrollbarWidth = null;
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default function scrollbarWidth(el) {
|
|
17
|
+
if (cachedScrollbarWidth === null) {
|
|
18
|
+
|
|
19
|
+
const document = getElementDocument(el);
|
|
20
|
+
|
|
21
|
+
if (typeof document === 'undefined') {
|
|
22
|
+
cachedScrollbarWidth = 0;
|
|
23
|
+
return cachedScrollbarWidth;
|
|
24
|
+
}
|
|
25
|
+
const body = document.body;
|
|
26
|
+
const box = document.createElement('div');
|
|
27
|
+
|
|
28
|
+
box.classList.add('simplebar-hide-scrollbar');
|
|
29
|
+
|
|
30
|
+
body.appendChild(box);
|
|
31
|
+
|
|
32
|
+
const width = box.getBoundingClientRect().right;
|
|
33
|
+
|
|
34
|
+
body.removeChild(box);
|
|
35
|
+
|
|
36
|
+
cachedScrollbarWidth = width;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return cachedScrollbarWidth;
|
|
40
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export as namespace SimpleBar;
|
|
2
|
+
export = SimpleBar;
|
|
3
|
+
|
|
4
|
+
declare namespace SimpleBar {
|
|
5
|
+
interface KnownClassNamesOptions {
|
|
6
|
+
contentEl?: string;
|
|
7
|
+
contentWrapper?: string;
|
|
8
|
+
offset?: string;
|
|
9
|
+
mask?: string;
|
|
10
|
+
wrapper?: string;
|
|
11
|
+
placeholder?: string;
|
|
12
|
+
scrollbar?: string;
|
|
13
|
+
track?: string;
|
|
14
|
+
heightAutoObserverWrapperEl?: string;
|
|
15
|
+
heightAutoObserverEl?: string;
|
|
16
|
+
visible?: string;
|
|
17
|
+
horizontal?: string;
|
|
18
|
+
vertical?: string;
|
|
19
|
+
hover?: string;
|
|
20
|
+
dragging?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type ClassNamesOptions = KnownClassNamesOptions & {
|
|
24
|
+
[className: string]: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
interface Options {
|
|
28
|
+
autoHide?: boolean;
|
|
29
|
+
classNames?: ClassNamesOptions;
|
|
30
|
+
forceVisible?: boolean | 'x' | 'y';
|
|
31
|
+
direction?: 'rtl' | 'ltr';
|
|
32
|
+
timeout?: number;
|
|
33
|
+
clickOnTrack?: boolean;
|
|
34
|
+
scrollbarMinSize?: number;
|
|
35
|
+
scrollbarMaxSize?: number;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare class SimpleBar {
|
|
40
|
+
static removeObserver(): void;
|
|
41
|
+
static instances: Pick<WeakMap<HTMLElement, SimpleBar>, 'get' | 'has'>;
|
|
42
|
+
|
|
43
|
+
constructor(element: HTMLElement, options?: SimpleBar.Options);
|
|
44
|
+
|
|
45
|
+
recalculate(): void;
|
|
46
|
+
getScrollElement(): HTMLElement;
|
|
47
|
+
getContentElement(): HTMLElement;
|
|
48
|
+
unMount(): void;
|
|
49
|
+
|
|
50
|
+
el: HTMLElement;
|
|
51
|
+
}
|