@karta.io/simplebar 1.0.0-beta.5 → 1.0.0-beta.51

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.
@@ -1,735 +1,632 @@
1
- import throttle from 'lodash/throttle.js';
2
- import debounce from 'lodash/debounce.js';
3
- import memoize from 'lodash/memoize.js';
4
- import { ResizeObserver } from '@juggle/resize-observer';
5
-
6
- const canUseDOM = typeof window !== "undefined" && typeof window.document !== "undefined";
7
- const canUseDOM$1 = canUseDOM;
8
-
1
+ import throttle from "lodash/throttle.js";
2
+ import debounce from "lodash/debounce.js";
3
+ import memoize from "lodash/memoize.js";
4
+ import { ResizeObserver } from "@juggle/resize-observer";
5
+ //#region src/canUseDOM.js
6
+ var canUseDOM = typeof window !== "undefined" && typeof window.document !== "undefined";
7
+ //#endregion
8
+ //#region src/helpers.js
9
9
  function getElementWindow(element) {
10
- if (!element || !element.ownerDocument || !element.ownerDocument.defaultView) {
11
- return window;
12
- }
13
- return element.ownerDocument.defaultView;
10
+ if (!element || !element.ownerDocument || !element.ownerDocument.defaultView) return window;
11
+ return element.ownerDocument.defaultView;
14
12
  }
15
13
  function getElementDocument(element) {
16
- if (!element || !element.ownerDocument) {
17
- return document;
18
- }
19
- return element.ownerDocument;
20
- }
21
-
22
- let cachedScrollbarWidth = null;
23
- let cachedDevicePixelRatio = null;
24
- if (canUseDOM$1) {
25
- window.addEventListener("resize", () => {
26
- if (cachedDevicePixelRatio !== window.devicePixelRatio) {
27
- cachedDevicePixelRatio = window.devicePixelRatio;
28
- cachedScrollbarWidth = null;
29
- }
30
- });
14
+ if (!element || !element.ownerDocument) return document;
15
+ return element.ownerDocument;
31
16
  }
17
+ //#endregion
18
+ //#region src/scrollbar-width.js
19
+ var cachedScrollbarWidth = null;
20
+ var cachedDevicePixelRatio = null;
21
+ if (canUseDOM) window.addEventListener("resize", () => {
22
+ if (cachedDevicePixelRatio !== window.devicePixelRatio) {
23
+ cachedDevicePixelRatio = window.devicePixelRatio;
24
+ cachedScrollbarWidth = null;
25
+ }
26
+ });
32
27
  function scrollbarWidth(el) {
33
- if (cachedScrollbarWidth === null) {
34
- const document = getElementDocument(el);
35
- if (typeof document === "undefined") {
36
- cachedScrollbarWidth = 0;
37
- return cachedScrollbarWidth;
38
- }
39
- const body = document.body;
40
- const box = document.createElement("div");
41
- box.classList.add("simplebar-hide-scrollbar");
42
- body.appendChild(box);
43
- const width = box.getBoundingClientRect().right;
44
- body.removeChild(box);
45
- cachedScrollbarWidth = width;
46
- }
47
- return cachedScrollbarWidth;
28
+ if (cachedScrollbarWidth === null) {
29
+ const document = getElementDocument(el);
30
+ if (typeof document === "undefined") {
31
+ cachedScrollbarWidth = 0;
32
+ return cachedScrollbarWidth;
33
+ }
34
+ const body = document.body;
35
+ const box = document.createElement("div");
36
+ box.classList.add("simplebar-hide-scrollbar");
37
+ body.appendChild(box);
38
+ const width = box.getBoundingClientRect().right;
39
+ body.removeChild(box);
40
+ cachedScrollbarWidth = width;
41
+ }
42
+ return cachedScrollbarWidth;
48
43
  }
49
-
50
- var __defProp = Object.defineProperty;
51
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
52
- var __publicField = (obj, key, value) => {
53
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
54
- return value;
44
+ //#endregion
45
+ //#region src/simplebar.js
46
+ var SimpleBar = class SimpleBar {
47
+ constructor(element, options) {
48
+ this.el = element;
49
+ this.minScrollbarWidth = 20;
50
+ this.options = {
51
+ ...SimpleBar.defaultOptions,
52
+ ...options
53
+ };
54
+ this.classNames = {
55
+ ...SimpleBar.defaultOptions.classNames,
56
+ ...this.options.classNames
57
+ };
58
+ this.axis = {
59
+ x: {
60
+ scrollOffsetAttr: "scrollLeft",
61
+ sizeAttr: "width",
62
+ scrollSizeAttr: "scrollWidth",
63
+ offsetSizeAttr: "offsetWidth",
64
+ offsetAttr: "left",
65
+ overflowAttr: "overflowX",
66
+ dragOffset: 0,
67
+ isOverflowing: true,
68
+ isVisible: false,
69
+ forceVisible: false,
70
+ track: {},
71
+ scrollbar: {}
72
+ },
73
+ y: {
74
+ scrollOffsetAttr: "scrollTop",
75
+ sizeAttr: "height",
76
+ scrollSizeAttr: "scrollHeight",
77
+ offsetSizeAttr: "offsetHeight",
78
+ offsetAttr: "top",
79
+ overflowAttr: "overflowY",
80
+ dragOffset: 0,
81
+ isOverflowing: true,
82
+ isVisible: false,
83
+ forceVisible: false,
84
+ track: {},
85
+ scrollbar: {}
86
+ }
87
+ };
88
+ this.removePreventClickId = null;
89
+ if (SimpleBar.instances.has(this.el)) return;
90
+ this.recalculate = throttle(this.recalculate.bind(this), 64);
91
+ this.onMouseMove = throttle(this.onMouseMove.bind(this), 64);
92
+ this.hideScrollbars = debounce(this.hideScrollbars.bind(this), this.options.timeout);
93
+ this.onWindowResize = debounce(this.onWindowResize.bind(this), 64, { leading: true });
94
+ SimpleBar.getRtlHelpers = memoize(SimpleBar.getRtlHelpers);
95
+ this.init();
96
+ }
97
+ /**
98
+ * Static properties
99
+ */
100
+ /**
101
+ * Helper to fix browsers inconsistency on RTL:
102
+ * - Firefox inverts the scrollbar initial position
103
+ * - IE11 inverts both scrollbar position and scrolling offset
104
+ * Directly inspired by @KingSora's OverlayScrollbars https://github.com/KingSora/OverlayScrollbars/blob/master/js/OverlayScrollbars.js#L1634
105
+ */
106
+ static getRtlHelpers() {
107
+ const dummyDiv = document.createElement("div");
108
+ dummyDiv.innerHTML = "<div class=\"hs-dummy-scrollbar-size\"><div style=\"height: 200%; width: 200%; margin: 10px 0;\"></div></div>";
109
+ const scrollbarDummyEl = dummyDiv.firstElementChild;
110
+ document.body.appendChild(scrollbarDummyEl);
111
+ const dummyContainerChild = scrollbarDummyEl.firstElementChild;
112
+ scrollbarDummyEl.scrollLeft = 0;
113
+ const dummyContainerOffset = SimpleBar.getOffset(scrollbarDummyEl);
114
+ const dummyContainerChildOffset = SimpleBar.getOffset(dummyContainerChild);
115
+ scrollbarDummyEl.scrollLeft = 999;
116
+ const dummyContainerScrollOffsetAfterScroll = SimpleBar.getOffset(dummyContainerChild);
117
+ return {
118
+ isRtlScrollingInverted: dummyContainerOffset.left !== dummyContainerChildOffset.left && dummyContainerChildOffset.left - dummyContainerScrollOffsetAfterScroll.left !== 0,
119
+ isRtlScrollbarInverted: dummyContainerOffset.left !== dummyContainerChildOffset.left
120
+ };
121
+ }
122
+ static defaultOptions = {
123
+ autoHide: true,
124
+ forceVisible: false,
125
+ clickOnTrack: true,
126
+ clickOnTrackSpeed: 40,
127
+ classNames: {
128
+ contentEl: "simplebar-content",
129
+ contentWrapper: "simplebar-content-wrapper",
130
+ offset: "simplebar-offset",
131
+ mask: "simplebar-mask",
132
+ wrapper: "simplebar-wrapper",
133
+ placeholder: "simplebar-placeholder",
134
+ scrollbar: "simplebar-scrollbar",
135
+ track: "simplebar-track",
136
+ heightAutoObserverWrapperEl: "simplebar-height-auto-observer-wrapper",
137
+ heightAutoObserverEl: "simplebar-height-auto-observer",
138
+ visible: "simplebar-visible",
139
+ horizontal: "simplebar-horizontal",
140
+ vertical: "simplebar-vertical",
141
+ hover: "simplebar-hover",
142
+ dragging: "simplebar-dragging"
143
+ },
144
+ scrollbarMinSize: 25,
145
+ scrollbarMaxSize: 0,
146
+ timeout: 1e3
147
+ };
148
+ static getOffset(el) {
149
+ const rect = el.getBoundingClientRect();
150
+ const elDocument = getElementDocument(el);
151
+ const elWindow = getElementWindow(el);
152
+ return {
153
+ top: rect.top + (elWindow.pageYOffset || elDocument.documentElement.scrollTop),
154
+ left: rect.left + (elWindow.pageXOffset || elDocument.documentElement.scrollLeft)
155
+ };
156
+ }
157
+ static instances = /* @__PURE__ */ new WeakMap();
158
+ init() {
159
+ SimpleBar.instances.set(this.el, this);
160
+ if (canUseDOM) {
161
+ this.initDOM();
162
+ this.setAccessibilityAttributes();
163
+ this.scrollbarWidth = this.getScrollbarWidth();
164
+ this.recalculate();
165
+ this.initListeners();
166
+ }
167
+ }
168
+ initDOM() {
169
+ if (Array.prototype.filter.call(this.el.children, (child) => child.classList.contains(this.classNames.wrapper)).length) {
170
+ this.wrapperEl = this.el.querySelector(`.${this.classNames.wrapper}`);
171
+ this.contentWrapperEl = this.options.scrollableNode || this.el.querySelector(`.${this.classNames.contentWrapper}`);
172
+ this.contentEl = this.options.contentNode || this.el.querySelector(`.${this.classNames.contentEl}`);
173
+ this.offsetEl = this.el.querySelector(`.${this.classNames.offset}`);
174
+ this.maskEl = this.el.querySelector(`.${this.classNames.mask}`);
175
+ this.placeholderEl = this.findChild(this.wrapperEl, `.${this.classNames.placeholder}`);
176
+ this.heightAutoObserverWrapperEl = this.el.querySelector(`.${this.classNames.heightAutoObserverWrapperEl}`);
177
+ this.heightAutoObserverEl = this.el.querySelector(`.${this.classNames.heightAutoObserverEl}`);
178
+ this.axis.x.track.el = this.findChild(this.el, `.${this.classNames.track}.${this.classNames.horizontal}`);
179
+ this.axis.y.track.el = this.findChild(this.el, `.${this.classNames.track}.${this.classNames.vertical}`);
180
+ } else {
181
+ this.wrapperEl = document.createElement("div");
182
+ this.contentWrapperEl = document.createElement("div");
183
+ this.offsetEl = document.createElement("div");
184
+ this.maskEl = document.createElement("div");
185
+ this.contentEl = document.createElement("div");
186
+ this.placeholderEl = document.createElement("div");
187
+ this.heightAutoObserverWrapperEl = document.createElement("div");
188
+ this.heightAutoObserverEl = document.createElement("div");
189
+ this.wrapperEl.classList.add(this.classNames.wrapper);
190
+ this.contentWrapperEl.classList.add(this.classNames.contentWrapper);
191
+ this.offsetEl.classList.add(this.classNames.offset);
192
+ this.maskEl.classList.add(this.classNames.mask);
193
+ this.contentEl.classList.add(this.classNames.contentEl);
194
+ this.placeholderEl.classList.add(this.classNames.placeholder);
195
+ this.heightAutoObserverWrapperEl.classList.add(this.classNames.heightAutoObserverWrapperEl);
196
+ this.heightAutoObserverEl.classList.add(this.classNames.heightAutoObserverEl);
197
+ while (this.el.firstChild) this.contentEl.appendChild(this.el.firstChild);
198
+ this.contentWrapperEl.appendChild(this.contentEl);
199
+ this.offsetEl.appendChild(this.contentWrapperEl);
200
+ this.maskEl.appendChild(this.offsetEl);
201
+ this.heightAutoObserverWrapperEl.appendChild(this.heightAutoObserverEl);
202
+ this.wrapperEl.appendChild(this.heightAutoObserverWrapperEl);
203
+ this.wrapperEl.appendChild(this.maskEl);
204
+ this.wrapperEl.appendChild(this.placeholderEl);
205
+ this.el.appendChild(this.wrapperEl);
206
+ }
207
+ if (!this.axis.x.track.el || !this.axis.y.track.el) {
208
+ const track = document.createElement("div");
209
+ const scrollbar = document.createElement("div");
210
+ track.classList.add(this.classNames.track);
211
+ scrollbar.classList.add(this.classNames.scrollbar);
212
+ track.appendChild(scrollbar);
213
+ this.axis.x.track.el = track.cloneNode(true);
214
+ this.axis.x.track.el.classList.add(this.classNames.horizontal);
215
+ this.axis.y.track.el = track.cloneNode(true);
216
+ this.axis.y.track.el.classList.add(this.classNames.vertical);
217
+ this.el.appendChild(this.axis.x.track.el);
218
+ this.el.appendChild(this.axis.y.track.el);
219
+ }
220
+ this.axis.x.scrollbar.el = this.axis.x.track.el.querySelector(`.${this.classNames.scrollbar}`);
221
+ this.axis.y.scrollbar.el = this.axis.y.track.el.querySelector(`.${this.classNames.scrollbar}`);
222
+ if (!this.options.autoHide) {
223
+ this.axis.x.scrollbar.el.classList.add(this.classNames.visible);
224
+ this.axis.y.scrollbar.el.classList.add(this.classNames.visible);
225
+ }
226
+ this.el.setAttribute("data-simplebar", "init");
227
+ }
228
+ setAccessibilityAttributes() {
229
+ const ariaLabel = this.options.ariaLabel || "scrollable content";
230
+ this.contentWrapperEl.setAttribute("tabindex", "0");
231
+ this.contentWrapperEl.setAttribute("role", "region");
232
+ this.contentWrapperEl.setAttribute("aria-label", ariaLabel);
233
+ }
234
+ initListeners() {
235
+ const elWindow = getElementWindow(this.el);
236
+ if (this.options.autoHide) this.el.addEventListener("mouseenter", this.onMouseEnter);
237
+ [
238
+ "mousedown",
239
+ "click",
240
+ "dblclick"
241
+ ].forEach((e) => {
242
+ this.el.addEventListener(e, this.onPointerEvent, true);
243
+ });
244
+ [
245
+ "touchstart",
246
+ "touchend",
247
+ "touchmove"
248
+ ].forEach((e) => {
249
+ this.el.addEventListener(e, this.onPointerEvent, {
250
+ capture: true,
251
+ passive: true
252
+ });
253
+ });
254
+ this.el.addEventListener("mousemove", this.onMouseMove);
255
+ this.el.addEventListener("mouseleave", this.onMouseLeave);
256
+ this.contentWrapperEl.addEventListener("scroll", this.onScroll);
257
+ elWindow.addEventListener("resize", this.onWindowResize);
258
+ let resizeObserverStarted = false;
259
+ const resizeObserver = elWindow.ResizeObserver || ResizeObserver;
260
+ this.resizeObserver = new resizeObserver(() => {
261
+ if (!resizeObserverStarted) return;
262
+ this.recalculate();
263
+ });
264
+ this.resizeObserver.observe(this.el);
265
+ this.resizeObserver.observe(this.contentEl);
266
+ elWindow.requestAnimationFrame(() => {
267
+ resizeObserverStarted = true;
268
+ });
269
+ this.mutationObserver = new elWindow.MutationObserver(this.recalculate);
270
+ this.mutationObserver.observe(this.contentEl, {
271
+ childList: true,
272
+ subtree: true,
273
+ characterData: true
274
+ });
275
+ }
276
+ recalculate() {
277
+ const elWindow = getElementWindow(this.el);
278
+ this.elStyles = elWindow.getComputedStyle(this.el);
279
+ this.isRtl = this.elStyles.direction === "rtl";
280
+ const isHeightAuto = this.heightAutoObserverEl.offsetHeight <= 1;
281
+ const isWidthAuto = this.heightAutoObserverEl.offsetWidth <= 1;
282
+ const contentElOffsetWidth = this.contentEl.offsetWidth;
283
+ const contentWrapperElOffsetWidth = this.contentWrapperEl.offsetWidth;
284
+ const elOverflowX = this.elStyles.overflowX;
285
+ const elOverflowY = this.elStyles.overflowY;
286
+ this.contentEl.style.padding = `${this.elStyles.paddingTop} ${this.elStyles.paddingRight} ${this.elStyles.paddingBottom} ${this.elStyles.paddingLeft}`;
287
+ this.wrapperEl.style.margin = `-${this.elStyles.paddingTop} -${this.elStyles.paddingRight} -${this.elStyles.paddingBottom} -${this.elStyles.paddingLeft}`;
288
+ const contentElScrollHeight = this.contentEl.scrollHeight;
289
+ const contentElScrollWidth = this.contentEl.scrollWidth;
290
+ this.contentWrapperEl.style.height = isHeightAuto ? "auto" : "100%";
291
+ this.placeholderEl.style.width = isWidthAuto ? `${contentElOffsetWidth}px` : "auto";
292
+ this.placeholderEl.style.height = `${contentElScrollHeight}px`;
293
+ const contentWrapperElOffsetHeight = this.contentWrapperEl.offsetHeight;
294
+ this.axis.x.isOverflowing = contentElScrollWidth > contentElOffsetWidth;
295
+ this.axis.y.isOverflowing = contentElScrollHeight > contentWrapperElOffsetHeight;
296
+ this.axis.x.isOverflowing = elOverflowX === "hidden" ? false : this.axis.x.isOverflowing;
297
+ this.axis.y.isOverflowing = elOverflowY === "hidden" ? false : this.axis.y.isOverflowing;
298
+ this.axis.x.forceVisible = this.options.forceVisible === "x" || this.options.forceVisible === true;
299
+ this.axis.y.forceVisible = this.options.forceVisible === "y" || this.options.forceVisible === true;
300
+ this.hideNativeScrollbar();
301
+ let offsetForXScrollbar = this.axis.x.isOverflowing ? this.scrollbarWidth : 0;
302
+ let offsetForYScrollbar = this.axis.y.isOverflowing ? this.scrollbarWidth : 0;
303
+ this.axis.x.isOverflowing = this.axis.x.isOverflowing && contentElScrollWidth > contentWrapperElOffsetWidth - offsetForYScrollbar;
304
+ this.axis.y.isOverflowing = this.axis.y.isOverflowing && contentElScrollHeight > contentWrapperElOffsetHeight - offsetForXScrollbar;
305
+ this.axis.x.scrollbar.size = this.getScrollbarSize("x");
306
+ this.axis.y.scrollbar.size = this.getScrollbarSize("y");
307
+ this.axis.x.scrollbar.el.style.width = `${this.axis.x.scrollbar.size}px`;
308
+ this.axis.y.scrollbar.el.style.height = `${this.axis.y.scrollbar.size}px`;
309
+ this.positionScrollbar("x");
310
+ this.positionScrollbar("y");
311
+ this.toggleTrackVisibility("x");
312
+ this.toggleTrackVisibility("y");
313
+ }
314
+ /**
315
+ * Calculate scrollbar size
316
+ */
317
+ getScrollbarSize(axis = "y") {
318
+ if (!this.axis[axis].isOverflowing) return 0;
319
+ const contentSize = this.contentEl[this.axis[axis].scrollSizeAttr];
320
+ const trackSize = this.axis[axis].track.el[this.axis[axis].offsetSizeAttr];
321
+ let scrollbarSize;
322
+ let scrollbarRatio = trackSize / contentSize;
323
+ scrollbarSize = Math.max(~~(scrollbarRatio * trackSize), this.options.scrollbarMinSize);
324
+ if (this.options.scrollbarMaxSize) scrollbarSize = Math.min(scrollbarSize, this.options.scrollbarMaxSize);
325
+ return scrollbarSize;
326
+ }
327
+ positionScrollbar(axis = "y") {
328
+ if (!this.axis[axis].isOverflowing) return;
329
+ const contentSize = this.contentWrapperEl[this.axis[axis].scrollSizeAttr];
330
+ const trackSize = this.axis[axis].track.el[this.axis[axis].offsetSizeAttr];
331
+ const hostSize = parseInt(this.elStyles[this.axis[axis].sizeAttr], 10);
332
+ const scrollbar = this.axis[axis].scrollbar;
333
+ let scrollOffset = this.contentWrapperEl[this.axis[axis].scrollOffsetAttr];
334
+ scrollOffset = axis === "x" && this.isRtl && SimpleBar.getRtlHelpers().isRtlScrollingInverted ? -scrollOffset : scrollOffset;
335
+ let scrollPourcent = scrollOffset / (contentSize - hostSize);
336
+ let handleOffset = ~~((trackSize - scrollbar.size) * scrollPourcent);
337
+ handleOffset = axis === "x" && this.isRtl && SimpleBar.getRtlHelpers().isRtlScrollbarInverted ? handleOffset + (trackSize - scrollbar.size) : handleOffset;
338
+ scrollbar.el.style.transform = axis === "x" ? `translate3d(${handleOffset}px, 0, 0)` : `translate3d(0, ${handleOffset}px, 0)`;
339
+ }
340
+ toggleTrackVisibility(axis = "y") {
341
+ const track = this.axis[axis].track.el;
342
+ const scrollbar = this.axis[axis].scrollbar.el;
343
+ if (this.axis[axis].isOverflowing || this.axis[axis].forceVisible) {
344
+ track.style.visibility = "visible";
345
+ this.contentWrapperEl.style[this.axis[axis].overflowAttr] = "scroll";
346
+ } else {
347
+ track.style.visibility = "hidden";
348
+ this.contentWrapperEl.style[this.axis[axis].overflowAttr] = "hidden";
349
+ }
350
+ if (this.axis[axis].isOverflowing) scrollbar.style.display = "block";
351
+ else scrollbar.style.display = "none";
352
+ }
353
+ hideNativeScrollbar() {
354
+ this.offsetEl.style[this.isRtl ? "left" : "right"] = this.axis.y.isOverflowing || this.axis.y.forceVisible ? `-${this.scrollbarWidth}px` : 0;
355
+ this.offsetEl.style.bottom = this.axis.x.isOverflowing || this.axis.x.forceVisible ? `-${this.scrollbarWidth}px` : 0;
356
+ }
357
+ /**
358
+ * On scroll event handling
359
+ */
360
+ onScroll = () => {
361
+ const elWindow = getElementWindow(this.el);
362
+ if (!this.scrollXTicking) {
363
+ elWindow.requestAnimationFrame(this.scrollX);
364
+ this.scrollXTicking = true;
365
+ }
366
+ if (!this.scrollYTicking) {
367
+ elWindow.requestAnimationFrame(this.scrollY);
368
+ this.scrollYTicking = true;
369
+ }
370
+ };
371
+ scrollX = () => {
372
+ if (this.axis.x.isOverflowing) {
373
+ this.showScrollbar("x");
374
+ this.positionScrollbar("x");
375
+ }
376
+ this.scrollXTicking = false;
377
+ };
378
+ scrollY = () => {
379
+ if (this.axis.y.isOverflowing) {
380
+ this.showScrollbar("y");
381
+ this.positionScrollbar("y");
382
+ }
383
+ this.scrollYTicking = false;
384
+ };
385
+ onMouseEnter = () => {
386
+ this.showScrollbar("x");
387
+ this.showScrollbar("y");
388
+ };
389
+ onMouseMove = (e) => {
390
+ this.mouseX = e.clientX;
391
+ this.mouseY = e.clientY;
392
+ if (this.axis.x.isOverflowing || this.axis.x.forceVisible) this.onMouseMoveForAxis("x");
393
+ if (this.axis.y.isOverflowing || this.axis.y.forceVisible) this.onMouseMoveForAxis("y");
394
+ };
395
+ onMouseMoveForAxis(axis = "y") {
396
+ this.axis[axis].track.rect = this.axis[axis].track.el.getBoundingClientRect();
397
+ this.axis[axis].scrollbar.rect = this.axis[axis].scrollbar.el.getBoundingClientRect();
398
+ if (this.isWithinBounds(this.axis[axis].scrollbar.rect)) this.axis[axis].scrollbar.el.classList.add(this.classNames.hover);
399
+ else this.axis[axis].scrollbar.el.classList.remove(this.classNames.hover);
400
+ if (this.isWithinBounds(this.axis[axis].track.rect)) {
401
+ this.showScrollbar(axis);
402
+ this.axis[axis].track.el.classList.add(this.classNames.hover);
403
+ } else this.axis[axis].track.el.classList.remove(this.classNames.hover);
404
+ }
405
+ onMouseLeave = () => {
406
+ this.onMouseMove.cancel();
407
+ if (this.axis.x.isOverflowing || this.axis.x.forceVisible) this.onMouseLeaveForAxis("x");
408
+ if (this.axis.y.isOverflowing || this.axis.y.forceVisible) this.onMouseLeaveForAxis("y");
409
+ this.mouseX = -1;
410
+ this.mouseY = -1;
411
+ };
412
+ onMouseLeaveForAxis(axis = "y") {
413
+ this.axis[axis].track.el.classList.remove(this.classNames.hover);
414
+ this.axis[axis].scrollbar.el.classList.remove(this.classNames.hover);
415
+ }
416
+ onWindowResize = () => {
417
+ this.scrollbarWidth = this.getScrollbarWidth();
418
+ this.hideNativeScrollbar();
419
+ };
420
+ /**
421
+ * Show scrollbar
422
+ */
423
+ showScrollbar(axis = "y") {
424
+ let scrollbar = this.axis[axis].scrollbar.el;
425
+ if (!this.axis[axis].isVisible) {
426
+ scrollbar.classList.add(this.classNames.visible);
427
+ this.axis[axis].isVisible = true;
428
+ }
429
+ if (this.options.autoHide) this.hideScrollbars();
430
+ }
431
+ /**
432
+ * Hide Scrollbar
433
+ */
434
+ hideScrollbars = () => {
435
+ this.axis.x.track.rect = this.axis.x.track.el.getBoundingClientRect();
436
+ this.axis.y.track.rect = this.axis.y.track.el.getBoundingClientRect();
437
+ if (!this.isWithinBounds(this.axis.y.track.rect)) {
438
+ this.axis.y.scrollbar.el.classList.remove(this.classNames.visible);
439
+ this.axis.y.isVisible = false;
440
+ }
441
+ if (!this.isWithinBounds(this.axis.x.track.rect)) {
442
+ this.axis.x.scrollbar.el.classList.remove(this.classNames.visible);
443
+ this.axis.x.isVisible = false;
444
+ }
445
+ };
446
+ onPointerEvent = (e) => {
447
+ let isWithinTrackXBounds, isWithinTrackYBounds;
448
+ this.axis.x.track.rect = this.axis.x.track.el.getBoundingClientRect();
449
+ this.axis.y.track.rect = this.axis.y.track.el.getBoundingClientRect();
450
+ if (this.axis.x.isOverflowing || this.axis.x.forceVisible) isWithinTrackXBounds = this.isWithinBounds(this.axis.x.track.rect);
451
+ if (this.axis.y.isOverflowing || this.axis.y.forceVisible) isWithinTrackYBounds = this.isWithinBounds(this.axis.y.track.rect);
452
+ if (isWithinTrackXBounds || isWithinTrackYBounds) {
453
+ e.preventDefault();
454
+ e.stopPropagation();
455
+ if (e.type === "mousedown") {
456
+ if (isWithinTrackXBounds) {
457
+ this.axis.x.scrollbar.rect = this.axis.x.scrollbar.el.getBoundingClientRect();
458
+ if (this.isWithinBounds(this.axis.x.scrollbar.rect)) this.onDragStart(e, "x");
459
+ else this.onTrackClick(e, "x");
460
+ }
461
+ if (isWithinTrackYBounds) {
462
+ this.axis.y.scrollbar.rect = this.axis.y.scrollbar.el.getBoundingClientRect();
463
+ if (this.isWithinBounds(this.axis.y.scrollbar.rect)) this.onDragStart(e, "y");
464
+ else this.onTrackClick(e, "y");
465
+ }
466
+ }
467
+ }
468
+ };
469
+ /**
470
+ * on scrollbar handle drag movement starts
471
+ */
472
+ onDragStart(e, axis = "y") {
473
+ const elDocument = getElementDocument(this.el);
474
+ const elWindow = getElementWindow(this.el);
475
+ const scrollbar = this.axis[axis].scrollbar;
476
+ const eventOffset = axis === "y" ? e.pageY : e.pageX;
477
+ this.axis[axis].dragOffset = eventOffset - scrollbar.rect[this.axis[axis].offsetAttr];
478
+ this.draggedAxis = axis;
479
+ this.el.classList.add(this.classNames.dragging);
480
+ elDocument.addEventListener("mousemove", this.drag, true);
481
+ elDocument.addEventListener("mouseup", this.onEndDrag, true);
482
+ if (this.removePreventClickId === null) {
483
+ elDocument.addEventListener("click", this.preventClick, true);
484
+ elDocument.addEventListener("dblclick", this.preventClick, true);
485
+ } else {
486
+ elWindow.clearTimeout(this.removePreventClickId);
487
+ this.removePreventClickId = null;
488
+ }
489
+ }
490
+ /**
491
+ * Drag scrollbar handle
492
+ */
493
+ drag = (e) => {
494
+ let eventOffset;
495
+ const track = this.axis[this.draggedAxis].track;
496
+ const trackSize = track.rect[this.axis[this.draggedAxis].sizeAttr];
497
+ const scrollbar = this.axis[this.draggedAxis].scrollbar;
498
+ const contentSize = this.contentWrapperEl[this.axis[this.draggedAxis].scrollSizeAttr];
499
+ const hostSize = parseInt(this.elStyles[this.axis[this.draggedAxis].sizeAttr], 10);
500
+ e.preventDefault();
501
+ e.stopPropagation();
502
+ if (this.draggedAxis === "y") eventOffset = e.pageY;
503
+ else eventOffset = e.pageX;
504
+ let scrollPos = (eventOffset - track.rect[this.axis[this.draggedAxis].offsetAttr] - this.axis[this.draggedAxis].dragOffset) / (trackSize - scrollbar.size) * (contentSize - hostSize);
505
+ if (this.draggedAxis === "x") {
506
+ scrollPos = this.isRtl && SimpleBar.getRtlHelpers().isRtlScrollbarInverted ? scrollPos - (trackSize + scrollbar.size) : scrollPos;
507
+ scrollPos = this.isRtl && SimpleBar.getRtlHelpers().isRtlScrollingInverted ? -scrollPos : scrollPos;
508
+ }
509
+ this.contentWrapperEl[this.axis[this.draggedAxis].scrollOffsetAttr] = scrollPos;
510
+ };
511
+ /**
512
+ * End scroll handle drag
513
+ */
514
+ onEndDrag = (e) => {
515
+ const elDocument = getElementDocument(this.el);
516
+ const elWindow = getElementWindow(this.el);
517
+ e.preventDefault();
518
+ e.stopPropagation();
519
+ this.el.classList.remove(this.classNames.dragging);
520
+ elDocument.removeEventListener("mousemove", this.drag, true);
521
+ elDocument.removeEventListener("mouseup", this.onEndDrag, true);
522
+ this.removePreventClickId = elWindow.setTimeout(() => {
523
+ elDocument.removeEventListener("click", this.preventClick, true);
524
+ elDocument.removeEventListener("dblclick", this.preventClick, true);
525
+ this.removePreventClickId = null;
526
+ });
527
+ };
528
+ /**
529
+ * Handler to ignore click events during drag
530
+ */
531
+ preventClick = (e) => {
532
+ e.preventDefault();
533
+ e.stopPropagation();
534
+ };
535
+ onTrackClick(e, axis = "y") {
536
+ if (!this.options.clickOnTrack) return;
537
+ const elWindow = getElementWindow(this.el);
538
+ this.axis[axis].scrollbar.rect = this.axis[axis].scrollbar.el.getBoundingClientRect();
539
+ const scrollbarOffset = this.axis[axis].scrollbar.rect[this.axis[axis].offsetAttr];
540
+ const hostSize = parseInt(this.elStyles[this.axis[axis].sizeAttr], 10);
541
+ let scrolled = this.contentWrapperEl[this.axis[axis].scrollOffsetAttr];
542
+ const dir = (axis === "y" ? this.mouseY - scrollbarOffset : this.mouseX - scrollbarOffset) < 0 ? -1 : 1;
543
+ const scrollSize = dir === -1 ? scrolled - hostSize : scrolled + hostSize;
544
+ const scrollTo = () => {
545
+ if (dir === -1) {
546
+ if (scrolled > scrollSize) {
547
+ scrolled -= this.options.clickOnTrackSpeed;
548
+ this.contentWrapperEl.scrollTo({ [this.axis[axis].offsetAttr]: scrolled });
549
+ elWindow.requestAnimationFrame(scrollTo);
550
+ }
551
+ } else if (scrolled < scrollSize) {
552
+ scrolled += this.options.clickOnTrackSpeed;
553
+ this.contentWrapperEl.scrollTo({ [this.axis[axis].offsetAttr]: scrolled });
554
+ elWindow.requestAnimationFrame(scrollTo);
555
+ }
556
+ };
557
+ scrollTo();
558
+ }
559
+ /**
560
+ * Getter for content element
561
+ */
562
+ getContentElement() {
563
+ return this.contentEl;
564
+ }
565
+ /**
566
+ * Getter for original scrolling element
567
+ */
568
+ getScrollElement() {
569
+ return this.contentWrapperEl;
570
+ }
571
+ getScrollbarWidth() {
572
+ try {
573
+ if (getComputedStyle(this.contentWrapperEl, "::-webkit-scrollbar").display === "none" || "scrollbarWidth" in document.documentElement.style || "-ms-overflow-style" in document.documentElement.style) return 0;
574
+ else return scrollbarWidth(this.el);
575
+ } catch (e) {
576
+ return scrollbarWidth(this.el);
577
+ }
578
+ }
579
+ removeListeners() {
580
+ const elWindow = getElementWindow(this.el);
581
+ if (this.options.autoHide) this.el.removeEventListener("mouseenter", this.onMouseEnter);
582
+ [
583
+ "mousedown",
584
+ "click",
585
+ "dblclick"
586
+ ].forEach((e) => {
587
+ this.el.removeEventListener(e, this.onPointerEvent, true);
588
+ });
589
+ [
590
+ "touchstart",
591
+ "touchend",
592
+ "touchmove"
593
+ ].forEach((e) => {
594
+ this.el.removeEventListener(e, this.onPointerEvent, {
595
+ capture: true,
596
+ passive: true
597
+ });
598
+ });
599
+ this.el.removeEventListener("mousemove", this.onMouseMove);
600
+ this.el.removeEventListener("mouseleave", this.onMouseLeave);
601
+ if (this.contentWrapperEl) this.contentWrapperEl.removeEventListener("scroll", this.onScroll);
602
+ elWindow.removeEventListener("resize", this.onWindowResize);
603
+ if (this.mutationObserver) this.mutationObserver.disconnect();
604
+ if (this.resizeObserver) this.resizeObserver.disconnect();
605
+ this.recalculate.cancel();
606
+ this.onMouseMove.cancel();
607
+ this.hideScrollbars.cancel();
608
+ this.onWindowResize.cancel();
609
+ }
610
+ /**
611
+ * UnMount mutation observer and delete SimpleBar instance from DOM element
612
+ */
613
+ unMount() {
614
+ this.removeListeners();
615
+ SimpleBar.instances.delete(this.el);
616
+ }
617
+ /**
618
+ * Check if mouse is within bounds
619
+ */
620
+ isWithinBounds(bbox) {
621
+ return this.mouseX >= bbox.left && this.mouseX <= bbox.left + bbox.width && this.mouseY >= bbox.top && this.mouseY <= bbox.top + bbox.height;
622
+ }
623
+ /**
624
+ * Find element children matches query
625
+ */
626
+ findChild(el, query) {
627
+ const matches = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
628
+ return Array.prototype.filter.call(el.children, (child) => matches.call(child, query))[0];
629
+ }
55
630
  };
56
- const _SimpleBar = class _SimpleBar {
57
- constructor(element, options) {
58
- /**
59
- * On scroll event handling
60
- */
61
- __publicField(this, "onScroll", () => {
62
- const elWindow = getElementWindow(this.el);
63
- if (!this.scrollXTicking) {
64
- elWindow.requestAnimationFrame(this.scrollX);
65
- this.scrollXTicking = true;
66
- }
67
- if (!this.scrollYTicking) {
68
- elWindow.requestAnimationFrame(this.scrollY);
69
- this.scrollYTicking = true;
70
- }
71
- });
72
- __publicField(this, "scrollX", () => {
73
- if (this.axis.x.isOverflowing) {
74
- this.showScrollbar("x");
75
- this.positionScrollbar("x");
76
- }
77
- this.scrollXTicking = false;
78
- });
79
- __publicField(this, "scrollY", () => {
80
- if (this.axis.y.isOverflowing) {
81
- this.showScrollbar("y");
82
- this.positionScrollbar("y");
83
- }
84
- this.scrollYTicking = false;
85
- });
86
- __publicField(this, "onMouseEnter", () => {
87
- this.showScrollbar("x");
88
- this.showScrollbar("y");
89
- });
90
- __publicField(this, "onMouseMove", (e) => {
91
- this.mouseX = e.clientX;
92
- this.mouseY = e.clientY;
93
- if (this.axis.x.isOverflowing || this.axis.x.forceVisible) {
94
- this.onMouseMoveForAxis("x");
95
- }
96
- if (this.axis.y.isOverflowing || this.axis.y.forceVisible) {
97
- this.onMouseMoveForAxis("y");
98
- }
99
- });
100
- __publicField(this, "onMouseLeave", () => {
101
- this.onMouseMove.cancel();
102
- if (this.axis.x.isOverflowing || this.axis.x.forceVisible) {
103
- this.onMouseLeaveForAxis("x");
104
- }
105
- if (this.axis.y.isOverflowing || this.axis.y.forceVisible) {
106
- this.onMouseLeaveForAxis("y");
107
- }
108
- this.mouseX = -1;
109
- this.mouseY = -1;
110
- });
111
- __publicField(this, "onWindowResize", () => {
112
- this.scrollbarWidth = this.getScrollbarWidth();
113
- this.hideNativeScrollbar();
114
- });
115
- /**
116
- * Hide Scrollbar
117
- */
118
- __publicField(this, "hideScrollbars", () => {
119
- this.axis.x.track.rect = this.axis.x.track.el.getBoundingClientRect();
120
- this.axis.y.track.rect = this.axis.y.track.el.getBoundingClientRect();
121
- if (!this.isWithinBounds(this.axis.y.track.rect)) {
122
- this.axis.y.scrollbar.el.classList.remove(this.classNames.visible);
123
- this.axis.y.isVisible = false;
124
- }
125
- if (!this.isWithinBounds(this.axis.x.track.rect)) {
126
- this.axis.x.scrollbar.el.classList.remove(this.classNames.visible);
127
- this.axis.x.isVisible = false;
128
- }
129
- });
130
- __publicField(this, "onPointerEvent", (e) => {
131
- let isWithinTrackXBounds, isWithinTrackYBounds;
132
- this.axis.x.track.rect = this.axis.x.track.el.getBoundingClientRect();
133
- this.axis.y.track.rect = this.axis.y.track.el.getBoundingClientRect();
134
- if (this.axis.x.isOverflowing || this.axis.x.forceVisible) {
135
- isWithinTrackXBounds = this.isWithinBounds(this.axis.x.track.rect);
136
- }
137
- if (this.axis.y.isOverflowing || this.axis.y.forceVisible) {
138
- isWithinTrackYBounds = this.isWithinBounds(this.axis.y.track.rect);
139
- }
140
- if (isWithinTrackXBounds || isWithinTrackYBounds) {
141
- e.preventDefault();
142
- e.stopPropagation();
143
- if (e.type === "mousedown") {
144
- if (isWithinTrackXBounds) {
145
- this.axis.x.scrollbar.rect = this.axis.x.scrollbar.el.getBoundingClientRect();
146
- if (this.isWithinBounds(this.axis.x.scrollbar.rect)) {
147
- this.onDragStart(e, "x");
148
- } else {
149
- this.onTrackClick(e, "x");
150
- }
151
- }
152
- if (isWithinTrackYBounds) {
153
- this.axis.y.scrollbar.rect = this.axis.y.scrollbar.el.getBoundingClientRect();
154
- if (this.isWithinBounds(this.axis.y.scrollbar.rect)) {
155
- this.onDragStart(e, "y");
156
- } else {
157
- this.onTrackClick(e, "y");
158
- }
159
- }
160
- }
161
- }
162
- });
163
- /**
164
- * Drag scrollbar handle
165
- */
166
- __publicField(this, "drag", (e) => {
167
- let eventOffset;
168
- const track = this.axis[this.draggedAxis].track;
169
- const trackSize = track.rect[this.axis[this.draggedAxis].sizeAttr];
170
- const scrollbar = this.axis[this.draggedAxis].scrollbar;
171
- const contentSize = this.contentWrapperEl[this.axis[this.draggedAxis].scrollSizeAttr];
172
- const hostSize = parseInt(
173
- this.elStyles[this.axis[this.draggedAxis].sizeAttr],
174
- 10
175
- );
176
- e.preventDefault();
177
- e.stopPropagation();
178
- if (this.draggedAxis === "y") {
179
- eventOffset = e.pageY;
180
- } else {
181
- eventOffset = e.pageX;
182
- }
183
- let dragPos = eventOffset - track.rect[this.axis[this.draggedAxis].offsetAttr] - this.axis[this.draggedAxis].dragOffset;
184
- let dragPerc = dragPos / (trackSize - scrollbar.size);
185
- let scrollPos = dragPerc * (contentSize - hostSize);
186
- if (this.draggedAxis === "x") {
187
- scrollPos = this.isRtl && _SimpleBar.getRtlHelpers().isRtlScrollbarInverted ? scrollPos - (trackSize + scrollbar.size) : scrollPos;
188
- scrollPos = this.isRtl && _SimpleBar.getRtlHelpers().isRtlScrollingInverted ? -scrollPos : scrollPos;
189
- }
190
- this.contentWrapperEl[this.axis[this.draggedAxis].scrollOffsetAttr] = scrollPos;
191
- });
192
- /**
193
- * End scroll handle drag
194
- */
195
- __publicField(this, "onEndDrag", (e) => {
196
- const elDocument = getElementDocument(this.el);
197
- const elWindow = getElementWindow(this.el);
198
- e.preventDefault();
199
- e.stopPropagation();
200
- this.el.classList.remove(this.classNames.dragging);
201
- elDocument.removeEventListener("mousemove", this.drag, true);
202
- elDocument.removeEventListener("mouseup", this.onEndDrag, true);
203
- this.removePreventClickId = elWindow.setTimeout(() => {
204
- elDocument.removeEventListener("click", this.preventClick, true);
205
- elDocument.removeEventListener("dblclick", this.preventClick, true);
206
- this.removePreventClickId = null;
207
- });
208
- });
209
- /**
210
- * Handler to ignore click events during drag
211
- */
212
- __publicField(this, "preventClick", (e) => {
213
- e.preventDefault();
214
- e.stopPropagation();
215
- });
216
- this.el = element;
217
- this.minScrollbarWidth = 20;
218
- this.options = { ..._SimpleBar.defaultOptions, ...options };
219
- this.classNames = {
220
- ..._SimpleBar.defaultOptions.classNames,
221
- ...this.options.classNames
222
- };
223
- this.axis = {
224
- x: {
225
- scrollOffsetAttr: "scrollLeft",
226
- sizeAttr: "width",
227
- scrollSizeAttr: "scrollWidth",
228
- offsetSizeAttr: "offsetWidth",
229
- offsetAttr: "left",
230
- overflowAttr: "overflowX",
231
- dragOffset: 0,
232
- isOverflowing: true,
233
- isVisible: false,
234
- forceVisible: false,
235
- track: {},
236
- scrollbar: {}
237
- },
238
- y: {
239
- scrollOffsetAttr: "scrollTop",
240
- sizeAttr: "height",
241
- scrollSizeAttr: "scrollHeight",
242
- offsetSizeAttr: "offsetHeight",
243
- offsetAttr: "top",
244
- overflowAttr: "overflowY",
245
- dragOffset: 0,
246
- isOverflowing: true,
247
- isVisible: false,
248
- forceVisible: false,
249
- track: {},
250
- scrollbar: {}
251
- }
252
- };
253
- this.removePreventClickId = null;
254
- if (_SimpleBar.instances.has(this.el)) {
255
- return;
256
- }
257
- this.recalculate = throttle(this.recalculate.bind(this), 64);
258
- this.onMouseMove = throttle(this.onMouseMove.bind(this), 64);
259
- this.hideScrollbars = debounce(
260
- this.hideScrollbars.bind(this),
261
- this.options.timeout
262
- );
263
- this.onWindowResize = debounce(this.onWindowResize.bind(this), 64, {
264
- leading: true
265
- });
266
- _SimpleBar.getRtlHelpers = memoize(_SimpleBar.getRtlHelpers);
267
- this.init();
268
- }
269
- /**
270
- * Static properties
271
- */
272
- /**
273
- * Helper to fix browsers inconsistency on RTL:
274
- * - Firefox inverts the scrollbar initial position
275
- * - IE11 inverts both scrollbar position and scrolling offset
276
- * Directly inspired by @KingSora's OverlayScrollbars https://github.com/KingSora/OverlayScrollbars/blob/master/js/OverlayScrollbars.js#L1634
277
- */
278
- static getRtlHelpers() {
279
- const dummyDiv = document.createElement("div");
280
- dummyDiv.innerHTML = '<div class="hs-dummy-scrollbar-size"><div style="height: 200%; width: 200%; margin: 10px 0;"></div></div>';
281
- const scrollbarDummyEl = dummyDiv.firstElementChild;
282
- document.body.appendChild(scrollbarDummyEl);
283
- const dummyContainerChild = scrollbarDummyEl.firstElementChild;
284
- scrollbarDummyEl.scrollLeft = 0;
285
- const dummyContainerOffset = _SimpleBar.getOffset(scrollbarDummyEl);
286
- const dummyContainerChildOffset = _SimpleBar.getOffset(dummyContainerChild);
287
- scrollbarDummyEl.scrollLeft = 999;
288
- const dummyContainerScrollOffsetAfterScroll = _SimpleBar.getOffset(
289
- dummyContainerChild
290
- );
291
- return {
292
- // determines if the scrolling is responding with negative values
293
- isRtlScrollingInverted: dummyContainerOffset.left !== dummyContainerChildOffset.left && dummyContainerChildOffset.left - dummyContainerScrollOffsetAfterScroll.left !== 0,
294
- // determines if the origin scrollbar position is inverted or not (positioned on left or right)
295
- isRtlScrollbarInverted: dummyContainerOffset.left !== dummyContainerChildOffset.left
296
- };
297
- }
298
- static getOffset(el) {
299
- const rect = el.getBoundingClientRect();
300
- const elDocument = getElementDocument(el);
301
- const elWindow = getElementWindow(el);
302
- return {
303
- top: rect.top + (elWindow.pageYOffset || elDocument.documentElement.scrollTop),
304
- left: rect.left + (elWindow.pageXOffset || elDocument.documentElement.scrollLeft)
305
- };
306
- }
307
- init() {
308
- _SimpleBar.instances.set(this.el, this);
309
- if (canUseDOM$1) {
310
- this.initDOM();
311
- this.setAccessibilityAttributes();
312
- this.scrollbarWidth = this.getScrollbarWidth();
313
- this.recalculate();
314
- this.initListeners();
315
- }
316
- }
317
- initDOM() {
318
- if (Array.prototype.filter.call(
319
- this.el.children,
320
- (child) => child.classList.contains(this.classNames.wrapper)
321
- ).length) {
322
- this.wrapperEl = this.el.querySelector(`.${this.classNames.wrapper}`);
323
- this.contentWrapperEl = this.options.scrollableNode || this.el.querySelector(`.${this.classNames.contentWrapper}`);
324
- this.contentEl = this.options.contentNode || this.el.querySelector(`.${this.classNames.contentEl}`);
325
- this.offsetEl = this.el.querySelector(`.${this.classNames.offset}`);
326
- this.maskEl = this.el.querySelector(`.${this.classNames.mask}`);
327
- this.placeholderEl = this.findChild(
328
- this.wrapperEl,
329
- `.${this.classNames.placeholder}`
330
- );
331
- this.heightAutoObserverWrapperEl = this.el.querySelector(
332
- `.${this.classNames.heightAutoObserverWrapperEl}`
333
- );
334
- this.heightAutoObserverEl = this.el.querySelector(
335
- `.${this.classNames.heightAutoObserverEl}`
336
- );
337
- this.axis.x.track.el = this.findChild(
338
- this.el,
339
- `.${this.classNames.track}.${this.classNames.horizontal}`
340
- );
341
- this.axis.y.track.el = this.findChild(
342
- this.el,
343
- `.${this.classNames.track}.${this.classNames.vertical}`
344
- );
345
- } else {
346
- this.wrapperEl = document.createElement("div");
347
- this.contentWrapperEl = document.createElement("div");
348
- this.offsetEl = document.createElement("div");
349
- this.maskEl = document.createElement("div");
350
- this.contentEl = document.createElement("div");
351
- this.placeholderEl = document.createElement("div");
352
- this.heightAutoObserverWrapperEl = document.createElement("div");
353
- this.heightAutoObserverEl = document.createElement("div");
354
- this.wrapperEl.classList.add(this.classNames.wrapper);
355
- this.contentWrapperEl.classList.add(this.classNames.contentWrapper);
356
- this.offsetEl.classList.add(this.classNames.offset);
357
- this.maskEl.classList.add(this.classNames.mask);
358
- this.contentEl.classList.add(this.classNames.contentEl);
359
- this.placeholderEl.classList.add(this.classNames.placeholder);
360
- this.heightAutoObserverWrapperEl.classList.add(
361
- this.classNames.heightAutoObserverWrapperEl
362
- );
363
- this.heightAutoObserverEl.classList.add(
364
- this.classNames.heightAutoObserverEl
365
- );
366
- while (this.el.firstChild) {
367
- this.contentEl.appendChild(this.el.firstChild);
368
- }
369
- this.contentWrapperEl.appendChild(this.contentEl);
370
- this.offsetEl.appendChild(this.contentWrapperEl);
371
- this.maskEl.appendChild(this.offsetEl);
372
- this.heightAutoObserverWrapperEl.appendChild(this.heightAutoObserverEl);
373
- this.wrapperEl.appendChild(this.heightAutoObserverWrapperEl);
374
- this.wrapperEl.appendChild(this.maskEl);
375
- this.wrapperEl.appendChild(this.placeholderEl);
376
- this.el.appendChild(this.wrapperEl);
377
- }
378
- if (!this.axis.x.track.el || !this.axis.y.track.el) {
379
- const track = document.createElement("div");
380
- const scrollbar = document.createElement("div");
381
- track.classList.add(this.classNames.track);
382
- scrollbar.classList.add(this.classNames.scrollbar);
383
- track.appendChild(scrollbar);
384
- this.axis.x.track.el = track.cloneNode(true);
385
- this.axis.x.track.el.classList.add(this.classNames.horizontal);
386
- this.axis.y.track.el = track.cloneNode(true);
387
- this.axis.y.track.el.classList.add(this.classNames.vertical);
388
- this.el.appendChild(this.axis.x.track.el);
389
- this.el.appendChild(this.axis.y.track.el);
390
- }
391
- this.axis.x.scrollbar.el = this.axis.x.track.el.querySelector(
392
- `.${this.classNames.scrollbar}`
393
- );
394
- this.axis.y.scrollbar.el = this.axis.y.track.el.querySelector(
395
- `.${this.classNames.scrollbar}`
396
- );
397
- if (!this.options.autoHide) {
398
- this.axis.x.scrollbar.el.classList.add(this.classNames.visible);
399
- this.axis.y.scrollbar.el.classList.add(this.classNames.visible);
400
- }
401
- this.el.setAttribute("data-simplebar", "init");
402
- }
403
- setAccessibilityAttributes() {
404
- const ariaLabel = this.options.ariaLabel || "scrollable content";
405
- this.contentWrapperEl.setAttribute("tabindex", "0");
406
- this.contentWrapperEl.setAttribute("role", "region");
407
- this.contentWrapperEl.setAttribute("aria-label", ariaLabel);
408
- }
409
- initListeners() {
410
- const elWindow = getElementWindow(this.el);
411
- if (this.options.autoHide) {
412
- this.el.addEventListener("mouseenter", this.onMouseEnter);
413
- }
414
- ["mousedown", "click", "dblclick"].forEach((e) => {
415
- this.el.addEventListener(e, this.onPointerEvent, true);
416
- });
417
- ["touchstart", "touchend", "touchmove"].forEach((e) => {
418
- this.el.addEventListener(e, this.onPointerEvent, {
419
- capture: true,
420
- passive: true
421
- });
422
- });
423
- this.el.addEventListener("mousemove", this.onMouseMove);
424
- this.el.addEventListener("mouseleave", this.onMouseLeave);
425
- this.contentWrapperEl.addEventListener("scroll", this.onScroll);
426
- elWindow.addEventListener("resize", this.onWindowResize);
427
- let resizeObserverStarted = false;
428
- const resizeObserver = elWindow.ResizeObserver || ResizeObserver;
429
- this.resizeObserver = new resizeObserver(() => {
430
- if (!resizeObserverStarted)
431
- return;
432
- this.recalculate();
433
- });
434
- this.resizeObserver.observe(this.el);
435
- this.resizeObserver.observe(this.contentEl);
436
- elWindow.requestAnimationFrame(() => {
437
- resizeObserverStarted = true;
438
- });
439
- this.mutationObserver = new elWindow.MutationObserver(this.recalculate);
440
- this.mutationObserver.observe(this.contentEl, {
441
- childList: true,
442
- subtree: true,
443
- characterData: true
444
- });
445
- }
446
- recalculate() {
447
- const elWindow = getElementWindow(this.el);
448
- this.elStyles = elWindow.getComputedStyle(this.el);
449
- this.isRtl = this.elStyles.direction === "rtl";
450
- const isHeightAuto = this.heightAutoObserverEl.offsetHeight <= 1;
451
- const isWidthAuto = this.heightAutoObserverEl.offsetWidth <= 1;
452
- const contentElOffsetWidth = this.contentEl.offsetWidth;
453
- const contentWrapperElOffsetWidth = this.contentWrapperEl.offsetWidth;
454
- const elOverflowX = this.elStyles.overflowX;
455
- const elOverflowY = this.elStyles.overflowY;
456
- this.contentEl.style.padding = `${this.elStyles.paddingTop} ${this.elStyles.paddingRight} ${this.elStyles.paddingBottom} ${this.elStyles.paddingLeft}`;
457
- this.wrapperEl.style.margin = `-${this.elStyles.paddingTop} -${this.elStyles.paddingRight} -${this.elStyles.paddingBottom} -${this.elStyles.paddingLeft}`;
458
- const contentElScrollHeight = this.contentEl.scrollHeight;
459
- const contentElScrollWidth = this.contentEl.scrollWidth;
460
- this.contentWrapperEl.style.height = isHeightAuto ? "auto" : "100%";
461
- this.placeholderEl.style.width = isWidthAuto ? `${contentElOffsetWidth}px` : "auto";
462
- this.placeholderEl.style.height = `${contentElScrollHeight}px`;
463
- const contentWrapperElOffsetHeight = this.contentWrapperEl.offsetHeight;
464
- this.axis.x.isOverflowing = contentElScrollWidth > contentElOffsetWidth;
465
- this.axis.y.isOverflowing = contentElScrollHeight > contentWrapperElOffsetHeight;
466
- this.axis.x.isOverflowing = elOverflowX === "hidden" ? false : this.axis.x.isOverflowing;
467
- this.axis.y.isOverflowing = elOverflowY === "hidden" ? false : this.axis.y.isOverflowing;
468
- this.axis.x.forceVisible = this.options.forceVisible === "x" || this.options.forceVisible === true;
469
- this.axis.y.forceVisible = this.options.forceVisible === "y" || this.options.forceVisible === true;
470
- this.hideNativeScrollbar();
471
- let offsetForXScrollbar = this.axis.x.isOverflowing ? this.scrollbarWidth : 0;
472
- let offsetForYScrollbar = this.axis.y.isOverflowing ? this.scrollbarWidth : 0;
473
- this.axis.x.isOverflowing = this.axis.x.isOverflowing && contentElScrollWidth > contentWrapperElOffsetWidth - offsetForYScrollbar;
474
- this.axis.y.isOverflowing = this.axis.y.isOverflowing && contentElScrollHeight > contentWrapperElOffsetHeight - offsetForXScrollbar;
475
- this.axis.x.scrollbar.size = this.getScrollbarSize("x");
476
- this.axis.y.scrollbar.size = this.getScrollbarSize("y");
477
- this.axis.x.scrollbar.el.style.width = `${this.axis.x.scrollbar.size}px`;
478
- this.axis.y.scrollbar.el.style.height = `${this.axis.y.scrollbar.size}px`;
479
- this.positionScrollbar("x");
480
- this.positionScrollbar("y");
481
- this.toggleTrackVisibility("x");
482
- this.toggleTrackVisibility("y");
483
- }
484
- /**
485
- * Calculate scrollbar size
486
- */
487
- getScrollbarSize(axis = "y") {
488
- if (!this.axis[axis].isOverflowing) {
489
- return 0;
490
- }
491
- const contentSize = this.contentEl[this.axis[axis].scrollSizeAttr];
492
- const trackSize = this.axis[axis].track.el[this.axis[axis].offsetSizeAttr];
493
- let scrollbarSize;
494
- let scrollbarRatio = trackSize / contentSize;
495
- scrollbarSize = Math.max(
496
- ~~(scrollbarRatio * trackSize),
497
- this.options.scrollbarMinSize
498
- );
499
- if (this.options.scrollbarMaxSize) {
500
- scrollbarSize = Math.min(scrollbarSize, this.options.scrollbarMaxSize);
501
- }
502
- return scrollbarSize;
503
- }
504
- positionScrollbar(axis = "y") {
505
- if (!this.axis[axis].isOverflowing) {
506
- return;
507
- }
508
- const contentSize = this.contentWrapperEl[this.axis[axis].scrollSizeAttr];
509
- const trackSize = this.axis[axis].track.el[this.axis[axis].offsetSizeAttr];
510
- const hostSize = parseInt(this.elStyles[this.axis[axis].sizeAttr], 10);
511
- const scrollbar = this.axis[axis].scrollbar;
512
- let scrollOffset = this.contentWrapperEl[this.axis[axis].scrollOffsetAttr];
513
- scrollOffset = axis === "x" && this.isRtl && _SimpleBar.getRtlHelpers().isRtlScrollingInverted ? -scrollOffset : scrollOffset;
514
- let scrollPourcent = scrollOffset / (contentSize - hostSize);
515
- let handleOffset = ~~((trackSize - scrollbar.size) * scrollPourcent);
516
- handleOffset = axis === "x" && this.isRtl && _SimpleBar.getRtlHelpers().isRtlScrollbarInverted ? handleOffset + (trackSize - scrollbar.size) : handleOffset;
517
- scrollbar.el.style.transform = axis === "x" ? `translate3d(${handleOffset}px, 0, 0)` : `translate3d(0, ${handleOffset}px, 0)`;
518
- }
519
- toggleTrackVisibility(axis = "y") {
520
- const track = this.axis[axis].track.el;
521
- const scrollbar = this.axis[axis].scrollbar.el;
522
- if (this.axis[axis].isOverflowing || this.axis[axis].forceVisible) {
523
- track.style.visibility = "visible";
524
- this.contentWrapperEl.style[this.axis[axis].overflowAttr] = "scroll";
525
- } else {
526
- track.style.visibility = "hidden";
527
- this.contentWrapperEl.style[this.axis[axis].overflowAttr] = "hidden";
528
- }
529
- if (this.axis[axis].isOverflowing) {
530
- scrollbar.style.display = "block";
531
- } else {
532
- scrollbar.style.display = "none";
533
- }
534
- }
535
- hideNativeScrollbar() {
536
- this.offsetEl.style[this.isRtl ? "left" : "right"] = this.axis.y.isOverflowing || this.axis.y.forceVisible ? `-${this.scrollbarWidth}px` : 0;
537
- this.offsetEl.style.bottom = this.axis.x.isOverflowing || this.axis.x.forceVisible ? `-${this.scrollbarWidth}px` : 0;
538
- }
539
- onMouseMoveForAxis(axis = "y") {
540
- this.axis[axis].track.rect = this.axis[axis].track.el.getBoundingClientRect();
541
- this.axis[axis].scrollbar.rect = this.axis[axis].scrollbar.el.getBoundingClientRect();
542
- const isWithinScrollbarBoundsX = this.isWithinBounds(
543
- this.axis[axis].scrollbar.rect
544
- );
545
- if (isWithinScrollbarBoundsX) {
546
- this.axis[axis].scrollbar.el.classList.add(this.classNames.hover);
547
- } else {
548
- this.axis[axis].scrollbar.el.classList.remove(this.classNames.hover);
549
- }
550
- if (this.isWithinBounds(this.axis[axis].track.rect)) {
551
- this.showScrollbar(axis);
552
- this.axis[axis].track.el.classList.add(this.classNames.hover);
553
- } else {
554
- this.axis[axis].track.el.classList.remove(this.classNames.hover);
555
- }
556
- }
557
- onMouseLeaveForAxis(axis = "y") {
558
- this.axis[axis].track.el.classList.remove(this.classNames.hover);
559
- this.axis[axis].scrollbar.el.classList.remove(this.classNames.hover);
560
- }
561
- /**
562
- * Show scrollbar
563
- */
564
- showScrollbar(axis = "y") {
565
- let scrollbar = this.axis[axis].scrollbar.el;
566
- if (!this.axis[axis].isVisible) {
567
- scrollbar.classList.add(this.classNames.visible);
568
- this.axis[axis].isVisible = true;
569
- }
570
- if (this.options.autoHide) {
571
- this.hideScrollbars();
572
- }
573
- }
574
- /**
575
- * on scrollbar handle drag movement starts
576
- */
577
- onDragStart(e, axis = "y") {
578
- const elDocument = getElementDocument(this.el);
579
- const elWindow = getElementWindow(this.el);
580
- const scrollbar = this.axis[axis].scrollbar;
581
- const eventOffset = axis === "y" ? e.pageY : e.pageX;
582
- this.axis[axis].dragOffset = eventOffset - scrollbar.rect[this.axis[axis].offsetAttr];
583
- this.draggedAxis = axis;
584
- this.el.classList.add(this.classNames.dragging);
585
- elDocument.addEventListener("mousemove", this.drag, true);
586
- elDocument.addEventListener("mouseup", this.onEndDrag, true);
587
- if (this.removePreventClickId === null) {
588
- elDocument.addEventListener("click", this.preventClick, true);
589
- elDocument.addEventListener("dblclick", this.preventClick, true);
590
- } else {
591
- elWindow.clearTimeout(this.removePreventClickId);
592
- this.removePreventClickId = null;
593
- }
594
- }
595
- onTrackClick(e, axis = "y") {
596
- if (!this.options.clickOnTrack)
597
- return;
598
- const elWindow = getElementWindow(this.el);
599
- this.axis[axis].scrollbar.rect = this.axis[axis].scrollbar.el.getBoundingClientRect();
600
- const scrollbar = this.axis[axis].scrollbar;
601
- const scrollbarOffset = scrollbar.rect[this.axis[axis].offsetAttr];
602
- const hostSize = parseInt(this.elStyles[this.axis[axis].sizeAttr], 10);
603
- let scrolled = this.contentWrapperEl[this.axis[axis].scrollOffsetAttr];
604
- const t = axis === "y" ? this.mouseY - scrollbarOffset : this.mouseX - scrollbarOffset;
605
- const dir = t < 0 ? -1 : 1;
606
- const scrollSize = dir === -1 ? scrolled - hostSize : scrolled + hostSize;
607
- const scrollTo = () => {
608
- if (dir === -1) {
609
- if (scrolled > scrollSize) {
610
- scrolled -= this.options.clickOnTrackSpeed;
611
- this.contentWrapperEl.scrollTo({
612
- [this.axis[axis].offsetAttr]: scrolled
613
- });
614
- elWindow.requestAnimationFrame(scrollTo);
615
- }
616
- } else {
617
- if (scrolled < scrollSize) {
618
- scrolled += this.options.clickOnTrackSpeed;
619
- this.contentWrapperEl.scrollTo({
620
- [this.axis[axis].offsetAttr]: scrolled
621
- });
622
- elWindow.requestAnimationFrame(scrollTo);
623
- }
624
- }
625
- };
626
- scrollTo();
627
- }
628
- /**
629
- * Getter for content element
630
- */
631
- getContentElement() {
632
- return this.contentEl;
633
- }
634
- /**
635
- * Getter for original scrolling element
636
- */
637
- getScrollElement() {
638
- return this.contentWrapperEl;
639
- }
640
- getScrollbarWidth() {
641
- try {
642
- if (getComputedStyle(this.contentWrapperEl, "::-webkit-scrollbar").display === "none" || "scrollbarWidth" in document.documentElement.style || "-ms-overflow-style" in document.documentElement.style) {
643
- return 0;
644
- } else {
645
- return scrollbarWidth(this.el);
646
- }
647
- } catch (e) {
648
- return scrollbarWidth(this.el);
649
- }
650
- }
651
- removeListeners() {
652
- const elWindow = getElementWindow(this.el);
653
- if (this.options.autoHide) {
654
- this.el.removeEventListener("mouseenter", this.onMouseEnter);
655
- }
656
- ["mousedown", "click", "dblclick"].forEach((e) => {
657
- this.el.removeEventListener(e, this.onPointerEvent, true);
658
- });
659
- ["touchstart", "touchend", "touchmove"].forEach((e) => {
660
- this.el.removeEventListener(e, this.onPointerEvent, {
661
- capture: true,
662
- passive: true
663
- });
664
- });
665
- this.el.removeEventListener("mousemove", this.onMouseMove);
666
- this.el.removeEventListener("mouseleave", this.onMouseLeave);
667
- if (this.contentWrapperEl) {
668
- this.contentWrapperEl.removeEventListener("scroll", this.onScroll);
669
- }
670
- elWindow.removeEventListener("resize", this.onWindowResize);
671
- if (this.mutationObserver) {
672
- this.mutationObserver.disconnect();
673
- }
674
- if (this.resizeObserver) {
675
- this.resizeObserver.disconnect();
676
- }
677
- this.recalculate.cancel();
678
- this.onMouseMove.cancel();
679
- this.hideScrollbars.cancel();
680
- this.onWindowResize.cancel();
681
- }
682
- /**
683
- * UnMount mutation observer and delete SimpleBar instance from DOM element
684
- */
685
- unMount() {
686
- this.removeListeners();
687
- _SimpleBar.instances.delete(this.el);
688
- }
689
- /**
690
- * Check if mouse is within bounds
691
- */
692
- isWithinBounds(bbox) {
693
- return this.mouseX >= bbox.left && this.mouseX <= bbox.left + bbox.width && this.mouseY >= bbox.top && this.mouseY <= bbox.top + bbox.height;
694
- }
695
- /**
696
- * Find element children matches query
697
- */
698
- findChild(el, query) {
699
- const matches = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
700
- return Array.prototype.filter.call(
701
- el.children,
702
- (child) => matches.call(child, query)
703
- )[0];
704
- }
705
- };
706
- __publicField(_SimpleBar, "defaultOptions", {
707
- autoHide: true,
708
- forceVisible: false,
709
- clickOnTrack: true,
710
- clickOnTrackSpeed: 40,
711
- classNames: {
712
- contentEl: "simplebar-content",
713
- contentWrapper: "simplebar-content-wrapper",
714
- offset: "simplebar-offset",
715
- mask: "simplebar-mask",
716
- wrapper: "simplebar-wrapper",
717
- placeholder: "simplebar-placeholder",
718
- scrollbar: "simplebar-scrollbar",
719
- track: "simplebar-track",
720
- heightAutoObserverWrapperEl: "simplebar-height-auto-observer-wrapper",
721
- heightAutoObserverEl: "simplebar-height-auto-observer",
722
- visible: "simplebar-visible",
723
- horizontal: "simplebar-horizontal",
724
- vertical: "simplebar-vertical",
725
- hover: "simplebar-hover",
726
- dragging: "simplebar-dragging"
727
- },
728
- scrollbarMinSize: 25,
729
- scrollbarMaxSize: 0,
730
- timeout: 1e3
731
- });
732
- __publicField(_SimpleBar, "instances", /* @__PURE__ */ new WeakMap());
733
- let SimpleBar = _SimpleBar;
734
-
631
+ //#endregion
735
632
  export { SimpleBar as default };