-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomAutoScrollContainer.vue
More file actions
193 lines (164 loc) · 5.24 KB
/
Copy pathCustomAutoScrollContainer.vue
File metadata and controls
193 lines (164 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<template>
<CustomScrollbar
ref="containerRef"
class="auto-scroll-container mask-y"
:wrapperStyle = "wrapperStyle"
:contentStyle = "contentStyle"
:autoHide = "scrollBarAutoHide"
@scroll="handleScroll"
>
<slot />
</CustomScrollbar>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted, nextTick, computed } from 'vue'
import CustomScrollbar from 'custom-vue-scrollbar';
import 'custom-vue-scrollbar/dist/style.css';
const props = withDefaults(defineProps<{
enabled?: boolean
threshold?: number
behavior?: ScrollBehavior
wrapperStyle?: Record<string, string>
contentStyle?: Record<string, string>
scrollBarAutoHide?: boolean
debugMode?: boolean
scrollToBottomOnMount?: boolean
}>(), {
enabled: true,
threshold: 50,
behavior: 'instant',
scrollBarAutoHide: true,
debugMode: false,
scrollToBottomOnMount: true
})
const containerRef = ref<HTMLDivElement | null>(null)
const isUserScrolledUp = ref(false)
const scrollElement = ref<HTMLElement | null>(null)
let lastScrollTop = 0
let lastScrollHeight = 0
let observer: MutationObserver | null = null
const INITIAL_SCROLL_DEBOUNCE = 200
let initialScrollDone = false
let initialScrollTimer: ReturnType<typeof setTimeout> | null = null
function scheduleInitialScrollEnd(): void {
if (initialScrollTimer) clearTimeout(initialScrollTimer)
initialScrollTimer = setTimeout(() => {
initialScrollDone = true
initialScrollTimer = null
}, INITIAL_SCROLL_DEBOUNCE)
}
const scrollParams = ref({
scrollTop: 0,
scrollHeight: 0,
clientHeight: 0
});
defineExpose({
scrollToBottom: () => scrollToBottom(true),
isUserScrolledUp: () => isUserScrolledUp.value,
container: containerRef,
handleScroll,
scrollParams
})
onMounted(() => {
if (!containerRef.value) return
scrollElement.value = containerRef.value.scrollEl
lastScrollTop = containerRef.value.scrollEl.scrollTop
lastScrollHeight = containerRef.value.scrollEl.scrollHeight
observer = new MutationObserver(() => {
nextTick(() => {
if (!containerRef.value?.scrollEl) return
if (!hasScrollbar()) {
isUserScrolledUp.value = false
}
lastScrollHeight = containerRef.value.scrollEl.scrollHeight
scrollParams.value.scrollTop = containerRef.value.scrollEl.scrollTop
scrollParams.value.scrollHeight = containerRef.value.scrollEl.scrollHeight
scrollParams.value.clientHeight = containerRef.value.scrollEl.clientHeight
if (props.enabled && !isUserScrolledUp.value) {
scrollToBottom()
} else if (props.scrollToBottomOnMount && !initialScrollDone) {
// Delayed content is still settling in the initial scroll phase: keep
// pinning to the bottom and restart the quiet-period debounce.
scrollToBottom(true)
scheduleInitialScrollEnd()
}
})
})
observer.observe(containerRef.value?.scrollEl, {
childList: true,
subtree: true,
characterData: true
})
if (props.scrollToBottomOnMount) {
nextTick(() => scrollToBottom(true))
scheduleInitialScrollEnd()
}
})
onUnmounted(() => {
observer?.disconnect()
if (initialScrollTimer) clearTimeout(initialScrollTimer)
})
function isNearBottom(customThreshold?: number): boolean {
const container = containerRef.value?.scrollEl
if (!container) return true
const { scrollTop, scrollHeight, clientHeight } = container
scrollParams.value.scrollTop = scrollTop
scrollParams.value.scrollHeight = scrollHeight
scrollParams.value.clientHeight = clientHeight
const threshold = customThreshold ?? props.threshold
return scrollHeight - scrollTop - clientHeight <= threshold
}
function scrollToBottom(force = false): void {
if (props.debugMode) console.log('scrollToBottom called with force:', force)
const container = containerRef.value?.scrollEl
if (!container) return
if (isUserScrolledUp.value && !force) return
if (props.debugMode) console.log('Scrolling to bottom...:', container.scrollHeight)
container.scrollTo({
top: container.scrollHeight,
behavior: props.behavior
})
}
function hasScrollbar(): boolean {
const container = containerRef.value?.scrollEl
if (!container) return false
return container.scrollHeight > container.clientHeight
}
function handleScroll(detectScrollDown = true, customThreshold?: number): void {
const container = containerRef.value.scrollEl
if (!container) return
const { scrollTop, scrollHeight, clientHeight } = container
if (scrollHeight <= clientHeight) {
isUserScrolledUp.value = false
lastScrollTop = 0
lastScrollHeight = scrollHeight
return
}
if (isNearBottom(customThreshold)) {
isUserScrolledUp.value = false
} else {
const isScrollingUp = scrollTop < lastScrollTop
const isScrollingDown = detectScrollDown ? scrollTop > lastScrollTop : false
const isContentUnchanged = scrollHeight === lastScrollHeight
if ((isScrollingUp || isScrollingDown) && isContentUnchanged) {
isUserScrolledUp.value = true
}
}
lastScrollTop = scrollTop
lastScrollHeight = scrollHeight
}
</script>
<style>
.mask-y {
mask-image: linear-gradient(
to bottom,
transparent,
black 1.25rem,
black calc(100% - 1.25rem),
transparent
);
}
.scrollbar__thumb {
border-radius: 0.25rem;
}
</style>