PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / trunk
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts vtrunk
2.8.14 2.8.13 2.8.12 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.10 2.8.11 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 trunk 1.0.1 1.0.2 1.1.0 1.1.1 All 147 releases
content-blocks-builder / src / utils / dom / scrollbar.js

scrollbar.js in Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts trunk, at src/utils/dom/scrollbar.js

98 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Internal dependencies
3 */
4 import { exposeComponent } from "./utils";
5
6 /**
7 * Scrollbar helper
8 */
9
10 const PROPERTY_PADDING = "padding-right";
11 class ScrollBarHelper {
12 constructor() {
13 this._element = document.body;
14 }
15
16 // Public
17 getWidth() {
18 // https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth#usage_notes
19 const documentWidth = document.documentElement.clientWidth;
20 return Math.abs(window.innerWidth - documentWidth);
21 }
22
23 hide() {
24 const width = this.getWidth();
25 this._disableOverFlow();
26 // give padding to element to balance the hidden scrollbar width
27 this._setElementAttributes(
28 this._element,
29 PROPERTY_PADDING,
30 (calculatedValue) => calculatedValue + width
31 );
32 }
33
34 reset() {
35 this._resetElementAttributes("overflow");
36 this._resetElementAttributes(PROPERTY_PADDING);
37 }
38
39 isOverflowing() {
40 return this.getWidth() > 0;
41 }
42
43 // Private
44 _disableOverFlow() {
45 this._saveInitialAttribute(this._element, "overflow");
46 this._element.style.overflow = "hidden";
47 }
48
49 _setElementAttributes(selector, styleProperty, callback) {
50 const scrollbarWidth = this.getWidth();
51 const manipulationCallBack = (element) => {
52 if (window.innerWidth > element.clientWidth + scrollbarWidth) {
53 return;
54 }
55
56 this._saveInitialAttribute(element, styleProperty);
57 const calculatedValue = window
58 .getComputedStyle(element)
59 .getPropertyValue(styleProperty);
60 element.style.setProperty(
61 styleProperty,
62 `${callback(Number.parseFloat(calculatedValue))}px`
63 );
64 };
65
66 this._applyManipulationCallback(selector, manipulationCallBack);
67 }
68
69 _saveInitialAttribute(element, styleProperty) {
70 const actualValue = element.style.getPropertyValue(styleProperty);
71 if (actualValue) {
72 element.setAttribute(`data-${styleProperty}`, actualValue);
73 }
74 }
75
76 _resetElementAttributes(styleProperty) {
77 const value = this._element?.dataset
78 ? this._element.dataset[styleProperty]
79 : null;
80
81 this._element.removeAttribute(`data-${styleProperty}`);
82
83 if (value) {
84 this._element.style.setProperty(styleProperty, value);
85 } else {
86 this._element.style.removeProperty(styleProperty);
87 }
88 }
89
90 _applyManipulationCallback(selector, callBack) {
91 callBack(selector);
92 }
93 }
94
95 exposeComponent("ScrollBarHelper", ScrollBarHelper);
96
97 export { ScrollBarHelper };
98