UNPKG

2.7 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = getScrollBarSize;
7exports.getTargetScrollBarSize = getTargetScrollBarSize;
8var _dynamicCSS = require("./Dom/dynamicCSS");
9/* eslint-disable no-param-reassign */
10
11var cached;
12function 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 // Create Style
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 // Clone Style if needed
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 // Set Webkit style
35 var webkitScrollbarStyle = getComputedStyle(ele, '::-webkit-scrollbar');
36 var width = parseInt(webkitScrollbarStyle.width, 10);
37 var height = parseInt(webkitScrollbarStyle.height, 10);
38
39 // Try wrap to handle CSP case
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 // Can't wrap, just log error
46 console.error(e);
47
48 // Get from style directly
49 fallbackWidth = width;
50 fallbackHeight = height;
51 }
52 }
53 document.body.appendChild(measureEle);
54
55 // Measure. Get fallback style if provided
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 // Clean up
60 document.body.removeChild(measureEle);
61 (0, _dynamicCSS.removeCSS)(randomId);
62 return {
63 width: scrollWidth,
64 height: scrollHeight
65 };
66}
67function 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}
76function 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