@karta.io/simplebar 0.34.0-alpha.2 → 0.34.0-alpha.3

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