@karta.io/simplebar 0.16.0

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.
@@ -0,0 +1,947 @@
1
+ import { throttle, debounce, memoize } from 'lodash-es';
2
+ import { ResizeObserver } from '@juggle/resize-observer';
3
+ import canUseDOM from 'can-use-dom';
4
+ import scrollbarWidth from './scrollbar-width';
5
+ import { getElementWindow, getElementDocument } from './helpers';
6
+
7
+ export default class SimpleBar {
8
+ constructor(element, options) {
9
+ this.el = element;
10
+ this.minScrollbarWidth = 20;
11
+ this.options = { ...SimpleBar.defaultOptions, ...options };
12
+ this.classNames = {
13
+ ...SimpleBar.defaultOptions.classNames,
14
+ ...this.options.classNames
15
+ };
16
+ this.axis = {
17
+ x: {
18
+ scrollOffsetAttr: 'scrollLeft',
19
+ sizeAttr: 'width',
20
+ scrollSizeAttr: 'scrollWidth',
21
+ offsetSizeAttr: 'offsetWidth',
22
+ offsetAttr: 'left',
23
+ overflowAttr: 'overflowX',
24
+ dragOffset: 0,
25
+ isOverflowing: true,
26
+ isVisible: false,
27
+ forceVisible: false,
28
+ track: {},
29
+ scrollbar: {}
30
+ },
31
+ y: {
32
+ scrollOffsetAttr: 'scrollTop',
33
+ sizeAttr: 'height',
34
+ scrollSizeAttr: 'scrollHeight',
35
+ offsetSizeAttr: 'offsetHeight',
36
+ offsetAttr: 'top',
37
+ overflowAttr: 'overflowY',
38
+ dragOffset: 0,
39
+ isOverflowing: true,
40
+ isVisible: false,
41
+ forceVisible: false,
42
+ track: {},
43
+ scrollbar: {}
44
+ }
45
+ };
46
+ this.removePreventClickId = null;
47
+
48
+ // Don't re-instantiate over an existing one
49
+ if (SimpleBar.instances.has(this.el)) {
50
+ return;
51
+ }
52
+
53
+ this.recalculate = throttle(this.recalculate.bind(this), 64);
54
+ this.onMouseMove = throttle(this.onMouseMove.bind(this), 64);
55
+ this.hideScrollbars = debounce(
56
+ this.hideScrollbars.bind(this),
57
+ this.options.timeout
58
+ );
59
+ this.onWindowResize = debounce(this.onWindowResize.bind(this), 64, {
60
+ leading: true
61
+ });
62
+
63
+ SimpleBar.getRtlHelpers = memoize(SimpleBar.getRtlHelpers);
64
+
65
+ this.init();
66
+ }
67
+
68
+ /**
69
+ * Static properties
70
+ */
71
+
72
+ /**
73
+ * Helper to fix browsers inconsistency on RTL:
74
+ * - Firefox inverts the scrollbar initial position
75
+ * - IE11 inverts both scrollbar position and scrolling offset
76
+ * Directly inspired by @KingSora's OverlayScrollbars https://github.com/KingSora/OverlayScrollbars/blob/master/js/OverlayScrollbars.js#L1634
77
+ */
78
+ static getRtlHelpers() {
79
+ const dummyDiv = document.createElement('div');
80
+ dummyDiv.innerHTML =
81
+ '<div class="hs-dummy-scrollbar-size"><div style="height: 200%; width: 200%; margin: 10px 0;"></div></div>';
82
+ const scrollbarDummyEl = dummyDiv.firstElementChild;
83
+ document.body.appendChild(scrollbarDummyEl);
84
+ const dummyContainerChild = scrollbarDummyEl.firstElementChild;
85
+ scrollbarDummyEl.scrollLeft = 0;
86
+ const dummyContainerOffset = SimpleBar.getOffset(scrollbarDummyEl);
87
+ const dummyContainerChildOffset = SimpleBar.getOffset(dummyContainerChild);
88
+ scrollbarDummyEl.scrollLeft = 999;
89
+ const dummyContainerScrollOffsetAfterScroll = SimpleBar.getOffset(
90
+ dummyContainerChild
91
+ );
92
+
93
+ return {
94
+ // determines if the scrolling is responding with negative values
95
+ isRtlScrollingInverted:
96
+ dummyContainerOffset.left !== dummyContainerChildOffset.left &&
97
+ dummyContainerChildOffset.left -
98
+ dummyContainerScrollOffsetAfterScroll.left !==
99
+ 0,
100
+ // determines if the origin scrollbar position is inverted or not (positioned on left or right)
101
+ isRtlScrollbarInverted:
102
+ dummyContainerOffset.left !== dummyContainerChildOffset.left
103
+ };
104
+ }
105
+
106
+ static defaultOptions = {
107
+ autoHide: true,
108
+ forceVisible: false,
109
+ clickOnTrack: true,
110
+ clickOnTrackSpeed: 40,
111
+ classNames: {
112
+ contentEl: 'simplebar-content',
113
+ contentWrapper: 'simplebar-content-wrapper',
114
+ offset: 'simplebar-offset',
115
+ mask: 'simplebar-mask',
116
+ wrapper: 'simplebar-wrapper',
117
+ placeholder: 'simplebar-placeholder',
118
+ scrollbar: 'simplebar-scrollbar',
119
+ track: 'simplebar-track',
120
+ heightAutoObserverWrapperEl: 'simplebar-height-auto-observer-wrapper',
121
+ heightAutoObserverEl: 'simplebar-height-auto-observer',
122
+ visible: 'simplebar-visible',
123
+ horizontal: 'simplebar-horizontal',
124
+ vertical: 'simplebar-vertical',
125
+ hover: 'simplebar-hover',
126
+ dragging: 'simplebar-dragging'
127
+ },
128
+ scrollbarMinSize: 25,
129
+ scrollbarMaxSize: 0,
130
+ timeout: 1000
131
+ };
132
+
133
+ static getOffset(el) {
134
+ const rect = el.getBoundingClientRect();
135
+ const elDocument = getElementDocument(el);
136
+ const elWindow = getElementWindow(el);
137
+
138
+ return {
139
+ top:
140
+ rect.top +
141
+ (elWindow.pageYOffset || elDocument.documentElement.scrollTop),
142
+ left:
143
+ rect.left +
144
+ (elWindow.pageXOffset || elDocument.documentElement.scrollLeft)
145
+ };
146
+ }
147
+
148
+ static instances = new WeakMap();
149
+
150
+ init() {
151
+ // Save a reference to the instance, so we know this DOM node has already been instancied
152
+ SimpleBar.instances.set(this.el, this);
153
+
154
+ // We stop here on server-side
155
+ if (canUseDOM) {
156
+ this.initDOM();
157
+
158
+ this.setAccessibilityAttributes();
159
+
160
+ this.scrollbarWidth = this.getScrollbarWidth();
161
+
162
+ this.recalculate();
163
+
164
+ this.initListeners();
165
+ }
166
+ }
167
+
168
+ initDOM() {
169
+ // make sure this element doesn't have the elements yet
170
+ if (
171
+ Array.prototype.filter.call(this.el.children, child =>
172
+ child.classList.contains(this.classNames.wrapper)
173
+ ).length
174
+ ) {
175
+ // assume that element has his DOM already initiated
176
+ this.wrapperEl = this.el.querySelector(`.${this.classNames.wrapper}`);
177
+ this.contentWrapperEl =
178
+ this.options.scrollableNode ||
179
+ this.el.querySelector(`.${this.classNames.contentWrapper}`);
180
+ this.contentEl =
181
+ this.options.contentNode ||
182
+ this.el.querySelector(`.${this.classNames.contentEl}`);
183
+
184
+ this.offsetEl = this.el.querySelector(`.${this.classNames.offset}`);
185
+ this.maskEl = this.el.querySelector(`.${this.classNames.mask}`);
186
+
187
+ this.placeholderEl = this.findChild(
188
+ this.wrapperEl,
189
+ `.${this.classNames.placeholder}`
190
+ );
191
+ this.heightAutoObserverWrapperEl = this.el.querySelector(
192
+ `.${this.classNames.heightAutoObserverWrapperEl}`
193
+ );
194
+ this.heightAutoObserverEl = this.el.querySelector(
195
+ `.${this.classNames.heightAutoObserverEl}`
196
+ );
197
+ this.axis.x.track.el = this.findChild(
198
+ this.el,
199
+ `.${this.classNames.track}.${this.classNames.horizontal}`
200
+ );
201
+ this.axis.y.track.el = this.findChild(
202
+ this.el,
203
+ `.${this.classNames.track}.${this.classNames.vertical}`
204
+ );
205
+ } else {
206
+ // Prepare DOM
207
+ this.wrapperEl = document.createElement('div');
208
+ this.contentWrapperEl = document.createElement('div');
209
+ this.offsetEl = document.createElement('div');
210
+ this.maskEl = document.createElement('div');
211
+ this.contentEl = document.createElement('div');
212
+ this.placeholderEl = document.createElement('div');
213
+ this.heightAutoObserverWrapperEl = document.createElement('div');
214
+ this.heightAutoObserverEl = document.createElement('div');
215
+
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
+
229
+ while (this.el.firstChild) {
230
+ this.contentEl.appendChild(this.el.firstChild);
231
+ }
232
+
233
+ this.contentWrapperEl.appendChild(this.contentEl);
234
+ this.offsetEl.appendChild(this.contentWrapperEl);
235
+ this.maskEl.appendChild(this.offsetEl);
236
+ this.heightAutoObserverWrapperEl.appendChild(this.heightAutoObserverEl);
237
+ this.wrapperEl.appendChild(this.heightAutoObserverWrapperEl);
238
+ this.wrapperEl.appendChild(this.maskEl);
239
+ this.wrapperEl.appendChild(this.placeholderEl);
240
+ this.el.appendChild(this.wrapperEl);
241
+ }
242
+
243
+ if (!this.axis.x.track.el || !this.axis.y.track.el) {
244
+ const track = document.createElement('div');
245
+ const scrollbar = document.createElement('div');
246
+
247
+ track.classList.add(this.classNames.track);
248
+ scrollbar.classList.add(this.classNames.scrollbar);
249
+
250
+ track.appendChild(scrollbar);
251
+
252
+ this.axis.x.track.el = track.cloneNode(true);
253
+ this.axis.x.track.el.classList.add(this.classNames.horizontal);
254
+
255
+ this.axis.y.track.el = track.cloneNode(true);
256
+ this.axis.y.track.el.classList.add(this.classNames.vertical);
257
+
258
+ this.el.appendChild(this.axis.x.track.el);
259
+ this.el.appendChild(this.axis.y.track.el);
260
+ }
261
+
262
+ this.axis.x.scrollbar.el = this.axis.x.track.el.querySelector(
263
+ `.${this.classNames.scrollbar}`
264
+ );
265
+ this.axis.y.scrollbar.el = this.axis.y.track.el.querySelector(
266
+ `.${this.classNames.scrollbar}`
267
+ );
268
+
269
+ if (!this.options.autoHide) {
270
+ this.axis.x.scrollbar.el.classList.add(this.classNames.visible);
271
+ this.axis.y.scrollbar.el.classList.add(this.classNames.visible);
272
+ }
273
+
274
+ this.el.setAttribute('data-simplebar', 'init');
275
+ }
276
+
277
+ setAccessibilityAttributes() {
278
+ const ariaLabel = this.options.ariaLabel || 'scrollable content';
279
+
280
+ this.contentWrapperEl.setAttribute('tabindex', '0');
281
+ this.contentWrapperEl.setAttribute('role', 'region');
282
+ this.contentWrapperEl.setAttribute('aria-label', ariaLabel);
283
+ }
284
+
285
+ initListeners() {
286
+ const elWindow = getElementWindow(this.el);
287
+ // Event listeners
288
+ if (this.options.autoHide) {
289
+ this.el.addEventListener('mouseenter', this.onMouseEnter);
290
+ }
291
+
292
+ ['mousedown', 'click', 'dblclick'].forEach(e => {
293
+ this.el.addEventListener(e, this.onPointerEvent, true);
294
+ });
295
+
296
+ ['touchstart', 'touchend', 'touchmove'].forEach(e => {
297
+ this.el.addEventListener(e, this.onPointerEvent, {
298
+ capture: true,
299
+ passive: true
300
+ });
301
+ });
302
+
303
+ this.el.addEventListener('mousemove', this.onMouseMove);
304
+ this.el.addEventListener('mouseleave', this.onMouseLeave);
305
+
306
+ this.contentWrapperEl.addEventListener('scroll', this.onScroll);
307
+
308
+ // Browser zoom triggers a window resize
309
+ elWindow.addEventListener('resize', this.onWindowResize);
310
+
311
+ // Hack for https://github.com/WICG/ResizeObserver/issues/38
312
+ let resizeObserverStarted = false;
313
+ const resizeObserver = elWindow.ResizeObserver || ResizeObserver;
314
+ this.resizeObserver = new resizeObserver(() => {
315
+ if (!resizeObserverStarted) return;
316
+ this.recalculate();
317
+ });
318
+
319
+ this.resizeObserver.observe(this.el);
320
+ this.resizeObserver.observe(this.contentEl);
321
+
322
+ elWindow.requestAnimationFrame(() => {
323
+ resizeObserverStarted = true;
324
+ });
325
+
326
+ // This is required to detect horizontal scroll. Vertical scroll only needs the resizeObserver.
327
+ this.mutationObserver = new elWindow.MutationObserver(this.recalculate);
328
+
329
+ this.mutationObserver.observe(this.contentEl, {
330
+ childList: true,
331
+ subtree: true,
332
+ characterData: true
333
+ });
334
+ }
335
+
336
+ recalculate() {
337
+ const elWindow = getElementWindow(this.el);
338
+ this.elStyles = elWindow.getComputedStyle(this.el);
339
+ this.isRtl = this.elStyles.direction === 'rtl';
340
+
341
+ const isHeightAuto = this.heightAutoObserverEl.offsetHeight <= 1;
342
+ const isWidthAuto = this.heightAutoObserverEl.offsetWidth <= 1;
343
+ const contentElOffsetWidth = this.contentEl.offsetWidth;
344
+
345
+ const contentWrapperElOffsetWidth = this.contentWrapperEl.offsetWidth;
346
+
347
+ const elOverflowX = this.elStyles.overflowX;
348
+ const elOverflowY = this.elStyles.overflowY;
349
+
350
+ this.contentEl.style.padding = `${this.elStyles.paddingTop} ${this.elStyles.paddingRight} ${this.elStyles.paddingBottom} ${this.elStyles.paddingLeft}`;
351
+ this.wrapperEl.style.margin = `-${this.elStyles.paddingTop} -${this.elStyles.paddingRight} -${this.elStyles.paddingBottom} -${this.elStyles.paddingLeft}`;
352
+
353
+ const contentElScrollHeight = this.contentEl.scrollHeight;
354
+ const contentElScrollWidth = this.contentEl.scrollWidth;
355
+
356
+ this.contentWrapperEl.style.height = isHeightAuto ? 'auto' : '100%';
357
+
358
+ // Determine placeholder size
359
+ this.placeholderEl.style.width = isWidthAuto
360
+ ? `${contentElOffsetWidth}px`
361
+ : 'auto';
362
+ this.placeholderEl.style.height = `${contentElScrollHeight}px`;
363
+
364
+ const contentWrapperElOffsetHeight = this.contentWrapperEl.offsetHeight;
365
+
366
+ this.axis.x.isOverflowing = contentElScrollWidth > contentElOffsetWidth;
367
+ this.axis.y.isOverflowing =
368
+ contentElScrollHeight > contentWrapperElOffsetHeight;
369
+
370
+ // Set isOverflowing to false if user explicitely set hidden overflow
371
+ this.axis.x.isOverflowing =
372
+ elOverflowX === 'hidden' ? false : this.axis.x.isOverflowing;
373
+ this.axis.y.isOverflowing =
374
+ elOverflowY === 'hidden' ? false : this.axis.y.isOverflowing;
375
+
376
+ this.axis.x.forceVisible =
377
+ this.options.forceVisible === 'x' || this.options.forceVisible === true;
378
+ this.axis.y.forceVisible =
379
+ this.options.forceVisible === 'y' || this.options.forceVisible === true;
380
+
381
+ this.hideNativeScrollbar();
382
+
383
+ // Set isOverflowing to false if scrollbar is not necessary (content is shorter than offset)
384
+ let offsetForXScrollbar = this.axis.x.isOverflowing
385
+ ? this.scrollbarWidth
386
+ : 0;
387
+ let offsetForYScrollbar = this.axis.y.isOverflowing
388
+ ? this.scrollbarWidth
389
+ : 0;
390
+
391
+ this.axis.x.isOverflowing =
392
+ this.axis.x.isOverflowing &&
393
+ contentElScrollWidth > contentWrapperElOffsetWidth - offsetForYScrollbar;
394
+ this.axis.y.isOverflowing =
395
+ this.axis.y.isOverflowing &&
396
+ contentElScrollHeight >
397
+ contentWrapperElOffsetHeight - offsetForXScrollbar;
398
+
399
+ this.axis.x.scrollbar.size = this.getScrollbarSize('x');
400
+ this.axis.y.scrollbar.size = this.getScrollbarSize('y');
401
+
402
+ this.axis.x.scrollbar.el.style.width = `${this.axis.x.scrollbar.size}px`;
403
+ this.axis.y.scrollbar.el.style.height = `${this.axis.y.scrollbar.size}px`;
404
+
405
+ this.positionScrollbar('x');
406
+ this.positionScrollbar('y');
407
+
408
+ this.toggleTrackVisibility('x');
409
+ this.toggleTrackVisibility('y');
410
+ }
411
+
412
+ /**
413
+ * Calculate scrollbar size
414
+ */
415
+ getScrollbarSize(axis = 'y') {
416
+ if (!this.axis[axis].isOverflowing) {
417
+ return 0;
418
+ }
419
+
420
+ const contentSize = this.contentEl[this.axis[axis].scrollSizeAttr];
421
+ const trackSize = this.axis[axis].track.el[this.axis[axis].offsetSizeAttr];
422
+ let scrollbarSize;
423
+
424
+ let scrollbarRatio = trackSize / contentSize;
425
+
426
+ // Calculate new height/position of drag handle.
427
+ scrollbarSize = Math.max(
428
+ ~~(scrollbarRatio * trackSize),
429
+ this.options.scrollbarMinSize
430
+ );
431
+
432
+ if (this.options.scrollbarMaxSize) {
433
+ scrollbarSize = Math.min(scrollbarSize, this.options.scrollbarMaxSize);
434
+ }
435
+
436
+ return scrollbarSize;
437
+ }
438
+
439
+ positionScrollbar(axis = 'y') {
440
+ if (!this.axis[axis].isOverflowing) {
441
+ return;
442
+ }
443
+
444
+ const contentSize = this.contentWrapperEl[this.axis[axis].scrollSizeAttr];
445
+ const trackSize = this.axis[axis].track.el[this.axis[axis].offsetSizeAttr];
446
+ const hostSize = parseInt(this.elStyles[this.axis[axis].sizeAttr], 10);
447
+ const scrollbar = this.axis[axis].scrollbar;
448
+
449
+ let scrollOffset = this.contentWrapperEl[this.axis[axis].scrollOffsetAttr];
450
+ scrollOffset =
451
+ axis === 'x' &&
452
+ this.isRtl &&
453
+ SimpleBar.getRtlHelpers().isRtlScrollingInverted
454
+ ? -scrollOffset
455
+ : scrollOffset;
456
+ let scrollPourcent = scrollOffset / (contentSize - hostSize);
457
+
458
+ let handleOffset = ~~((trackSize - scrollbar.size) * scrollPourcent);
459
+ handleOffset =
460
+ axis === 'x' &&
461
+ this.isRtl &&
462
+ SimpleBar.getRtlHelpers().isRtlScrollbarInverted
463
+ ? handleOffset + (trackSize - scrollbar.size)
464
+ : handleOffset;
465
+
466
+ scrollbar.el.style.transform =
467
+ axis === 'x'
468
+ ? `translate3d(${handleOffset}px, 0, 0)`
469
+ : `translate3d(0, ${handleOffset}px, 0)`;
470
+ }
471
+
472
+ toggleTrackVisibility(axis = 'y') {
473
+ const track = this.axis[axis].track.el;
474
+ const scrollbar = this.axis[axis].scrollbar.el;
475
+
476
+ if (this.axis[axis].isOverflowing || this.axis[axis].forceVisible) {
477
+ track.style.visibility = 'visible';
478
+ this.contentWrapperEl.style[this.axis[axis].overflowAttr] = 'scroll';
479
+ } else {
480
+ track.style.visibility = 'hidden';
481
+ this.contentWrapperEl.style[this.axis[axis].overflowAttr] = 'hidden';
482
+ }
483
+
484
+ // Even if forceVisible is enabled, scrollbar itself should be hidden
485
+ if (this.axis[axis].isOverflowing) {
486
+ scrollbar.style.display = 'block';
487
+ } else {
488
+ scrollbar.style.display = 'none';
489
+ }
490
+ }
491
+
492
+ hideNativeScrollbar() {
493
+ this.offsetEl.style[this.isRtl ? 'left' : 'right'] =
494
+ this.axis.y.isOverflowing || this.axis.y.forceVisible
495
+ ? `-${this.scrollbarWidth}px`
496
+ : 0;
497
+ this.offsetEl.style.bottom =
498
+ this.axis.x.isOverflowing || this.axis.x.forceVisible
499
+ ? `-${this.scrollbarWidth}px`
500
+ : 0;
501
+ }
502
+
503
+ /**
504
+ * On scroll event handling
505
+ */
506
+ onScroll = () => {
507
+ const elWindow = getElementWindow(this.el);
508
+ if (!this.scrollXTicking) {
509
+ elWindow.requestAnimationFrame(this.scrollX);
510
+ this.scrollXTicking = true;
511
+ }
512
+
513
+ if (!this.scrollYTicking) {
514
+ elWindow.requestAnimationFrame(this.scrollY);
515
+ this.scrollYTicking = true;
516
+ }
517
+ };
518
+
519
+ scrollX = () => {
520
+ if (this.axis.x.isOverflowing) {
521
+ this.showScrollbar('x');
522
+ this.positionScrollbar('x');
523
+ }
524
+
525
+ this.scrollXTicking = false;
526
+ };
527
+
528
+ scrollY = () => {
529
+ if (this.axis.y.isOverflowing) {
530
+ this.showScrollbar('y');
531
+ this.positionScrollbar('y');
532
+ }
533
+
534
+ this.scrollYTicking = false;
535
+ };
536
+
537
+ onMouseEnter = () => {
538
+ this.showScrollbar('x');
539
+ this.showScrollbar('y');
540
+ };
541
+
542
+ onMouseMove = e => {
543
+ this.mouseX = e.clientX;
544
+ this.mouseY = e.clientY;
545
+
546
+ if (this.axis.x.isOverflowing || this.axis.x.forceVisible) {
547
+ this.onMouseMoveForAxis('x');
548
+ }
549
+
550
+ if (this.axis.y.isOverflowing || this.axis.y.forceVisible) {
551
+ this.onMouseMoveForAxis('y');
552
+ }
553
+ };
554
+
555
+ onMouseMoveForAxis(axis = 'y') {
556
+ this.axis[axis].track.rect = this.axis[
557
+ axis
558
+ ].track.el.getBoundingClientRect();
559
+ this.axis[axis].scrollbar.rect = this.axis[
560
+ axis
561
+ ].scrollbar.el.getBoundingClientRect();
562
+
563
+ const isWithinScrollbarBoundsX = this.isWithinBounds(
564
+ this.axis[axis].scrollbar.rect
565
+ );
566
+
567
+ if (isWithinScrollbarBoundsX) {
568
+ this.axis[axis].scrollbar.el.classList.add(this.classNames.hover);
569
+ } else {
570
+ this.axis[axis].scrollbar.el.classList.remove(this.classNames.hover);
571
+ }
572
+
573
+ if (this.isWithinBounds(this.axis[axis].track.rect)) {
574
+ this.showScrollbar(axis);
575
+ this.axis[axis].track.el.classList.add(this.classNames.hover);
576
+ } else {
577
+ this.axis[axis].track.el.classList.remove(this.classNames.hover);
578
+ }
579
+ }
580
+
581
+ onMouseLeave = () => {
582
+ this.onMouseMove.cancel();
583
+
584
+ if (this.axis.x.isOverflowing || this.axis.x.forceVisible) {
585
+ this.onMouseLeaveForAxis('x');
586
+ }
587
+
588
+ if (this.axis.y.isOverflowing || this.axis.y.forceVisible) {
589
+ this.onMouseLeaveForAxis('y');
590
+ }
591
+
592
+ this.mouseX = -1;
593
+ this.mouseY = -1;
594
+ };
595
+
596
+ onMouseLeaveForAxis(axis = 'y') {
597
+ this.axis[axis].track.el.classList.remove(this.classNames.hover);
598
+ this.axis[axis].scrollbar.el.classList.remove(this.classNames.hover);
599
+ }
600
+
601
+ onWindowResize = () => {
602
+ // Recalculate scrollbarWidth in case it's a zoom
603
+ this.scrollbarWidth = this.getScrollbarWidth();
604
+
605
+ this.hideNativeScrollbar();
606
+ };
607
+
608
+ /**
609
+ * Show scrollbar
610
+ */
611
+ showScrollbar(axis = 'y') {
612
+ let scrollbar = this.axis[axis].scrollbar.el;
613
+
614
+ if (!this.axis[axis].isVisible) {
615
+ scrollbar.classList.add(this.classNames.visible);
616
+ this.axis[axis].isVisible = true;
617
+ }
618
+
619
+ if (this.options.autoHide) {
620
+ this.hideScrollbars();
621
+ }
622
+ }
623
+
624
+ /**
625
+ * Hide Scrollbar
626
+ */
627
+ hideScrollbars = () => {
628
+ this.axis.x.track.rect = this.axis.x.track.el.getBoundingClientRect();
629
+ this.axis.y.track.rect = this.axis.y.track.el.getBoundingClientRect();
630
+
631
+ if (!this.isWithinBounds(this.axis.y.track.rect)) {
632
+ this.axis.y.scrollbar.el.classList.remove(this.classNames.visible);
633
+ this.axis.y.isVisible = false;
634
+ }
635
+
636
+ if (!this.isWithinBounds(this.axis.x.track.rect)) {
637
+ this.axis.x.scrollbar.el.classList.remove(this.classNames.visible);
638
+ this.axis.x.isVisible = false;
639
+ }
640
+ };
641
+
642
+ onPointerEvent = e => {
643
+ let isWithinTrackXBounds, isWithinTrackYBounds;
644
+
645
+ this.axis.x.track.rect = this.axis.x.track.el.getBoundingClientRect();
646
+ this.axis.y.track.rect = this.axis.y.track.el.getBoundingClientRect();
647
+
648
+ if (this.axis.x.isOverflowing || this.axis.x.forceVisible) {
649
+ isWithinTrackXBounds = this.isWithinBounds(this.axis.x.track.rect);
650
+ }
651
+
652
+ if (this.axis.y.isOverflowing || this.axis.y.forceVisible) {
653
+ isWithinTrackYBounds = this.isWithinBounds(this.axis.y.track.rect);
654
+ }
655
+
656
+ // If any pointer event is called on the scrollbar
657
+ if (isWithinTrackXBounds || isWithinTrackYBounds) {
658
+ // Preventing the event's default action stops text being
659
+ // selectable during the drag.
660
+ e.preventDefault();
661
+ // Prevent event leaking
662
+ e.stopPropagation();
663
+
664
+ if (e.type === 'mousedown') {
665
+ if (isWithinTrackXBounds) {
666
+ this.axis.x.scrollbar.rect = this.axis.x.scrollbar.el.getBoundingClientRect();
667
+
668
+ if (this.isWithinBounds(this.axis.x.scrollbar.rect)) {
669
+ this.onDragStart(e, 'x');
670
+ } else {
671
+ this.onTrackClick(e, 'x');
672
+ }
673
+ }
674
+
675
+ if (isWithinTrackYBounds) {
676
+ this.axis.y.scrollbar.rect = this.axis.y.scrollbar.el.getBoundingClientRect();
677
+
678
+ if (this.isWithinBounds(this.axis.y.scrollbar.rect)) {
679
+ this.onDragStart(e, 'y');
680
+ } else {
681
+ this.onTrackClick(e, 'y');
682
+ }
683
+ }
684
+ }
685
+ }
686
+ };
687
+
688
+ /**
689
+ * on scrollbar handle drag movement starts
690
+ */
691
+ onDragStart(e, axis = 'y') {
692
+ const elDocument = getElementDocument(this.el);
693
+ const elWindow = getElementWindow(this.el);
694
+ const scrollbar = this.axis[axis].scrollbar;
695
+
696
+ // Measure how far the user's mouse is from the top of the scrollbar drag handle.
697
+ const eventOffset = axis === 'y' ? e.pageY : e.pageX;
698
+ this.axis[axis].dragOffset =
699
+ eventOffset - scrollbar.rect[this.axis[axis].offsetAttr];
700
+ this.draggedAxis = axis;
701
+
702
+ this.el.classList.add(this.classNames.dragging);
703
+
704
+ elDocument.addEventListener('mousemove', this.drag, true);
705
+ elDocument.addEventListener('mouseup', this.onEndDrag, true);
706
+ if (this.removePreventClickId === null) {
707
+ elDocument.addEventListener('click', this.preventClick, true);
708
+ elDocument.addEventListener('dblclick', this.preventClick, true);
709
+ } else {
710
+ elWindow.clearTimeout(this.removePreventClickId);
711
+ this.removePreventClickId = null;
712
+ }
713
+ }
714
+
715
+ /**
716
+ * Drag scrollbar handle
717
+ */
718
+ drag = e => {
719
+ let eventOffset;
720
+ const track = this.axis[this.draggedAxis].track;
721
+ const trackSize = track.rect[this.axis[this.draggedAxis].sizeAttr];
722
+ const scrollbar = this.axis[this.draggedAxis].scrollbar;
723
+ const contentSize = this.contentWrapperEl[
724
+ this.axis[this.draggedAxis].scrollSizeAttr
725
+ ];
726
+ const hostSize = parseInt(
727
+ this.elStyles[this.axis[this.draggedAxis].sizeAttr],
728
+ 10
729
+ );
730
+
731
+ e.preventDefault();
732
+ e.stopPropagation();
733
+
734
+ if (this.draggedAxis === 'y') {
735
+ eventOffset = e.pageY;
736
+ } else {
737
+ eventOffset = e.pageX;
738
+ }
739
+
740
+ // Calculate how far the user's mouse is from the top/left of the scrollbar (minus the dragOffset).
741
+ let dragPos =
742
+ eventOffset -
743
+ track.rect[this.axis[this.draggedAxis].offsetAttr] -
744
+ this.axis[this.draggedAxis].dragOffset;
745
+ // Convert the mouse position into a percentage of the scrollbar height/width.
746
+ let dragPerc = dragPos / (trackSize - scrollbar.size);
747
+
748
+ // Scroll the content by the same percentage.
749
+ let scrollPos = dragPerc * (contentSize - hostSize);
750
+
751
+ // Fix browsers inconsistency on RTL
752
+ if (this.draggedAxis === 'x') {
753
+ scrollPos =
754
+ this.isRtl && SimpleBar.getRtlHelpers().isRtlScrollbarInverted
755
+ ? scrollPos - (trackSize + scrollbar.size)
756
+ : scrollPos;
757
+ scrollPos =
758
+ this.isRtl && SimpleBar.getRtlHelpers().isRtlScrollingInverted
759
+ ? -scrollPos
760
+ : scrollPos;
761
+ }
762
+
763
+ this.contentWrapperEl[
764
+ this.axis[this.draggedAxis].scrollOffsetAttr
765
+ ] = scrollPos;
766
+ };
767
+
768
+ /**
769
+ * End scroll handle drag
770
+ */
771
+ onEndDrag = e => {
772
+ const elDocument = getElementDocument(this.el);
773
+ const elWindow = getElementWindow(this.el);
774
+ e.preventDefault();
775
+ e.stopPropagation();
776
+
777
+ this.el.classList.remove(this.classNames.dragging);
778
+
779
+ elDocument.removeEventListener('mousemove', this.drag, true);
780
+ elDocument.removeEventListener('mouseup', this.onEndDrag, true);
781
+ this.removePreventClickId = elWindow.setTimeout(() => {
782
+ // Remove these asynchronously so we still suppress click events
783
+ // generated simultaneously with mouseup.
784
+ elDocument.removeEventListener('click', this.preventClick, true);
785
+ elDocument.removeEventListener('dblclick', this.preventClick, true);
786
+ this.removePreventClickId = null;
787
+ });
788
+ };
789
+
790
+ /**
791
+ * Handler to ignore click events during drag
792
+ */
793
+ preventClick = e => {
794
+ e.preventDefault();
795
+ e.stopPropagation();
796
+ };
797
+
798
+ onTrackClick(e, axis = 'y') {
799
+ if (!this.options.clickOnTrack) return;
800
+
801
+ const elWindow = getElementWindow(this.el);
802
+ this.axis[axis].scrollbar.rect = this.axis[
803
+ axis
804
+ ].scrollbar.el.getBoundingClientRect();
805
+ const scrollbar = this.axis[axis].scrollbar;
806
+ const scrollbarOffset = scrollbar.rect[this.axis[axis].offsetAttr];
807
+ const hostSize = parseInt(this.elStyles[this.axis[axis].sizeAttr], 10);
808
+ let scrolled = this.contentWrapperEl[this.axis[axis].scrollOffsetAttr];
809
+ const t =
810
+ axis === 'y'
811
+ ? this.mouseY - scrollbarOffset
812
+ : this.mouseX - scrollbarOffset;
813
+ const dir = t < 0 ? -1 : 1;
814
+ const scrollSize = dir === -1 ? scrolled - hostSize : scrolled + hostSize;
815
+
816
+ const scrollTo = () => {
817
+ if (dir === -1) {
818
+ if (scrolled > scrollSize) {
819
+ scrolled -= this.options.clickOnTrackSpeed;
820
+ this.contentWrapperEl.scrollTo({
821
+ [this.axis[axis].offsetAttr]: scrolled
822
+ });
823
+ elWindow.requestAnimationFrame(scrollTo);
824
+ }
825
+ } else {
826
+ if (scrolled < scrollSize) {
827
+ scrolled += this.options.clickOnTrackSpeed;
828
+ this.contentWrapperEl.scrollTo({
829
+ [this.axis[axis].offsetAttr]: scrolled
830
+ });
831
+ elWindow.requestAnimationFrame(scrollTo);
832
+ }
833
+ }
834
+ };
835
+
836
+ scrollTo();
837
+ }
838
+
839
+ /**
840
+ * Getter for content element
841
+ */
842
+ getContentElement() {
843
+ return this.contentEl;
844
+ }
845
+
846
+ /**
847
+ * Getter for original scrolling element
848
+ */
849
+ getScrollElement() {
850
+ return this.contentWrapperEl;
851
+ }
852
+
853
+ getScrollbarWidth() {
854
+ // Try/catch for FF 56 throwing on undefined computedStyles
855
+ try {
856
+ // Detect browsers supporting CSS scrollbar styling and do not calculate
857
+ if (
858
+ getComputedStyle(this.contentWrapperEl, '::-webkit-scrollbar')
859
+ .display === 'none' ||
860
+ 'scrollbarWidth' in document.documentElement.style ||
861
+ '-ms-overflow-style' in document.documentElement.style
862
+ ) {
863
+ return 0;
864
+ } else {
865
+ return scrollbarWidth(this.el);
866
+ }
867
+ } catch (e) {
868
+ return scrollbarWidth(this.el);
869
+ }
870
+ }
871
+
872
+ removeListeners() {
873
+ const elWindow = getElementWindow(this.el);
874
+ // Event listeners
875
+ if (this.options.autoHide) {
876
+ this.el.removeEventListener('mouseenter', this.onMouseEnter);
877
+ }
878
+
879
+ ['mousedown', 'click', 'dblclick'].forEach(e => {
880
+ this.el.removeEventListener(e, this.onPointerEvent, true);
881
+ });
882
+
883
+ ['touchstart', 'touchend', 'touchmove'].forEach(e => {
884
+ this.el.removeEventListener(e, this.onPointerEvent, {
885
+ capture: true,
886
+ passive: true
887
+ });
888
+ });
889
+
890
+ this.el.removeEventListener('mousemove', this.onMouseMove);
891
+ this.el.removeEventListener('mouseleave', this.onMouseLeave);
892
+
893
+ if (this.contentWrapperEl) {
894
+ this.contentWrapperEl.removeEventListener('scroll', this.onScroll);
895
+ }
896
+
897
+ elWindow.removeEventListener('resize', this.onWindowResize);
898
+
899
+ if (this.mutationObserver) {
900
+ this.mutationObserver.disconnect();
901
+ }
902
+
903
+ if (this.resizeObserver) {
904
+ this.resizeObserver.disconnect();
905
+ }
906
+
907
+ // Cancel all debounced functions
908
+ this.recalculate.cancel();
909
+ this.onMouseMove.cancel();
910
+ this.hideScrollbars.cancel();
911
+ this.onWindowResize.cancel();
912
+ }
913
+
914
+ /**
915
+ * UnMount mutation observer and delete SimpleBar instance from DOM element
916
+ */
917
+ unMount() {
918
+ this.removeListeners();
919
+ SimpleBar.instances.delete(this.el);
920
+ }
921
+
922
+ /**
923
+ * Check if mouse is within bounds
924
+ */
925
+ isWithinBounds(bbox) {
926
+ return (
927
+ this.mouseX >= bbox.left &&
928
+ this.mouseX <= bbox.left + bbox.width &&
929
+ this.mouseY >= bbox.top &&
930
+ this.mouseY <= bbox.top + bbox.height
931
+ );
932
+ }
933
+
934
+ /**
935
+ * Find element children matches query
936
+ */
937
+ findChild(el, query) {
938
+ const matches =
939
+ el.matches ||
940
+ el.webkitMatchesSelector ||
941
+ el.mozMatchesSelector ||
942
+ el.msMatchesSelector;
943
+ return Array.prototype.filter.call(el.children, child =>
944
+ matches.call(child, query)
945
+ )[0];
946
+ }
947
+ }