1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = getScrollBarSize;
|
7 | exports.getTargetScrollBarSize = getTargetScrollBarSize;
|
8 | var _dynamicCSS = require("./Dom/dynamicCSS");
|
9 |
|
10 |
|
11 | var cached;
|
12 | function measureScrollbarSize(ele) {
|
13 | var randomId = "rc-scrollbar-measure-".concat(Math.random().toString(36).substring(7));
|
14 | var measureEle = document.createElement('div');
|
15 | measureEle.id = randomId;
|
16 |
|
17 |
|
18 | var measureStyle = measureEle.style;
|
19 | measureStyle.position = 'absolute';
|
20 | measureStyle.left = '0';
|
21 | measureStyle.top = '0';
|
22 | measureStyle.width = '100px';
|
23 | measureStyle.height = '100px';
|
24 | measureStyle.overflow = 'scroll';
|
25 |
|
26 |
|
27 | var fallbackWidth;
|
28 | var fallbackHeight;
|
29 | if (ele) {
|
30 | var targetStyle = getComputedStyle(ele);
|
31 | measureStyle.scrollbarColor = targetStyle.scrollbarColor;
|
32 | measureStyle.scrollbarWidth = targetStyle.scrollbarWidth;
|
33 |
|
34 |
|
35 | var webkitScrollbarStyle = getComputedStyle(ele, '::-webkit-scrollbar');
|
36 | var width = parseInt(webkitScrollbarStyle.width, 10);
|
37 | var height = parseInt(webkitScrollbarStyle.height, 10);
|
38 |
|
39 |
|
40 | try {
|
41 | var widthStyle = width ? "width: ".concat(webkitScrollbarStyle.width, ";") : '';
|
42 | var heightStyle = height ? "height: ".concat(webkitScrollbarStyle.height, ";") : '';
|
43 | (0, _dynamicCSS.updateCSS)("\n#".concat(randomId, "::-webkit-scrollbar {\n").concat(widthStyle, "\n").concat(heightStyle, "\n}"), randomId);
|
44 | } catch (e) {
|
45 |
|
46 | console.error(e);
|
47 |
|
48 |
|
49 | fallbackWidth = width;
|
50 | fallbackHeight = height;
|
51 | }
|
52 | }
|
53 | document.body.appendChild(measureEle);
|
54 |
|
55 |
|
56 | var scrollWidth = ele && fallbackWidth && !isNaN(fallbackWidth) ? fallbackWidth : measureEle.offsetWidth - measureEle.clientWidth;
|
57 | var scrollHeight = ele && fallbackHeight && !isNaN(fallbackHeight) ? fallbackHeight : measureEle.offsetHeight - measureEle.clientHeight;
|
58 |
|
59 |
|
60 | document.body.removeChild(measureEle);
|
61 | (0, _dynamicCSS.removeCSS)(randomId);
|
62 | return {
|
63 | width: scrollWidth,
|
64 | height: scrollHeight
|
65 | };
|
66 | }
|
67 | function getScrollBarSize(fresh) {
|
68 | if (typeof document === 'undefined') {
|
69 | return 0;
|
70 | }
|
71 | if (fresh || cached === undefined) {
|
72 | cached = measureScrollbarSize();
|
73 | }
|
74 | return cached.width;
|
75 | }
|
76 | function getTargetScrollBarSize(target) {
|
77 | if (typeof document === 'undefined' || !target || !(target instanceof Element)) {
|
78 | return {
|
79 | width: 0,
|
80 | height: 0
|
81 | };
|
82 | }
|
83 | return measureScrollbarSize(target);
|
84 | } |
\ | No newline at end of file |