How to scroll the ScrollArea to bottom #767
Answered
by
sadeghbarati
lowerbound123
asked this question in
Q&A
-
I was creating a web for chat and I want to keep the scrollArea viewport to the bottom so that my users can see the latest discussions. I tried to use scrollTop, but it seems to be useless. Here is my code. <script setup lang="ts">
const messageStore = useMessageStore()
const { globalMessage } = storeToRefs(messageStore)
const scrollAreaRef = ref(null)
onMounted(() => {
const scrollAreaEl = scrollAreaRef.value?.$el || scrollAreaRef.value
if (scrollAreaEl) {
scrollAreaEl.scrollTop = scrollAreaEl.scrollHeight
}
})
</script>
<template>
<div id="message-field" class="flex h-full flex-col">
<ScrollArea class="mx-2 mt-2 w-auto flex-1 rounded-lg border" ref="scrollAreaRef">
<div
v-for="message in globalMessage"
:key="message.message_id"
class="flex w-full flex-col p-3"
>
<div class="flex justify-between">
<Badge variant="secondary" class="my-auto w-fit whitespace-nowrap">{{
message.sender_name
}}</Badge>
<Badge variant="outline" class="my-auto w-fit whitespace-nowrap">
{{ message.sent_time }}
</Badge>
</div>
<span class="ml-6">{{ message.content }}</span>
</div>
</ScrollArea>
<ButtonInput
class="flex h-11 w-auto p-1.5"
input-class="h-full w-full"
button-class="h-full p-1.5 w-auto"
>
{{ $t('ph_submit') }}
</ButtonInput>
</div>
</template> |
Beta Was this translation helpful? Give feedback.
Answered by
sadeghbarati
Sep 14, 2024
Replies: 2 comments 1 reply
-
I found a solution in issues. const scrollToBottom = (behavior: 'auto' | 'smooth') => {
nextTick(() => {
if (scrollAreaRef.value) {
const viewport = scrollAreaRef.value.$el.querySelector('[data-radix-scroll-area-viewport]')
if (viewport) {
viewport.scrollTo({
top: viewport.scrollHeight,
behavior: behavior
})
}
}
})
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Check this issue and PR in |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
lowerbound123
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check this issue and PR in
radix-vue