@karta.io/simplebar 1.0.0-beta.50 → 1.0.0-beta.52

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