|
4 | 4 | ref="scrollContainer"
|
5 | 5 | :class="[$style['scroll-container'],{'no-scrollbar':hideScrollbar}]"
|
6 | 6 | :style="{ width: width?width:'auto', height }"
|
7 |
| - @scroll.passive="toggleShadow" |
| 7 | + @scroll.passive="throttled.toggleShadow" |
8 | 8 | >
|
9 | 9 | <slot />
|
10 | 10 | <span :class="[$style['shadow-top'], shadow.top && $style['is-active']]" :style="{
|
|
19 | 19 | </template>
|
20 | 20 |
|
21 | 21 | <script>
|
| 22 | +import throttle from 'lodash.throttle' |
22 | 23 | function newResizeObserver (callback) {
|
23 | 24 | // Skip this feature for browsers which
|
24 | 25 | // do not support ResizeObserver.
|
@@ -50,28 +51,31 @@ export default {
|
50 | 51 | bottom: false,
|
51 | 52 | left: false
|
52 | 53 | },
|
53 |
| - debounceTimeout: null, |
54 | 54 | scrollContainerObserver: null,
|
55 |
| - wrapObserver: null |
| 55 | + wrapObserver: null, |
| 56 | + throttled: {} |
56 | 57 | }
|
57 | 58 | },
|
58 | 59 | mounted () {
|
59 |
| - window.addEventListener('resize', this.calcDimensions) |
| 60 | + this.throttled.toggleShadow = throttle(this.toggleShadow, 100); |
| 61 | + this.throttled.calcDimensions = throttle(this.calcDimensions, 100); |
| 62 | +
|
| 63 | + window.addEventListener('resize', this.throttled.calcDimensions) |
60 | 64 |
|
61 | 65 | // Check if shadows are necessary after the element is resized.
|
62 |
| - const scrollContainerObserver = newResizeObserver(this.toggleShadow) |
| 66 | + const scrollContainerObserver = newResizeObserver(this.throttled.toggleShadow) |
63 | 67 | if (scrollContainerObserver) {
|
64 | 68 | scrollContainerObserver.observe(this.$refs.scrollContainer)
|
65 | 69 | }
|
66 | 70 |
|
67 | 71 | // Recalculate the container dimensions when the wrapper is resized.
|
68 |
| - this.wrapObserver = newResizeObserver(this.calcDimensions) |
| 72 | + this.wrapObserver = newResizeObserver(this.throttled.calcDimensions) |
69 | 73 | if (this.wrapObserver) {
|
70 | 74 | this.wrapObserver.observe(this.$el)
|
71 | 75 | }
|
72 | 76 | },
|
73 | 77 | unmounted () {
|
74 |
| - window.removeEventListener('resize', this.calcDimensions) |
| 78 | + window.removeEventListener('resize', this.throttled.calcDimensions) |
75 | 79 | // Cleanup when the component is unmounted.
|
76 | 80 | this.wrapObserver.disconnect()
|
77 | 81 | if (this.scrollContainerObserver) {
|
|
0 commit comments