Skip to content

Commit 9d1e632

Browse files
authored
Merge pull request #67 from Simon-He95/bugfix/mobile-scrollbar
fix: chat container in mobile viewport
2 parents ec6ddb7 + a39c13d commit 9d1e632

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

playground/src/App.vue

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import MarkdownRender from '../../src/components/NodeRenderer'
44
import { streamContent } from './const/markdown'
55
// 每隔 10 毫秒输出一部分内容
66
const content = ref<string>('')
7+
8+
const VIEWPORT_VH_VAR = '--app-viewport-vh'
9+
10+
const viewportCleanupFns: Array<() => void> = []
11+
12+
function updateViewportHeight() {
13+
if (typeof window === 'undefined')
14+
return
15+
16+
const viewport = window.visualViewport
17+
const height = viewport?.height ?? window.innerHeight
18+
const unit = height / 100
19+
document.documentElement.style.setProperty(VIEWPORT_VH_VAR, `${unit}px`)
20+
}
721
// To avoid flashing sequences like ":::" during streaming (which later
822
// become an AdmonitionNode), we look ahead when encountering ":" and
923
// defer appending consecutive colons until a non-colon character is seen.
@@ -264,6 +278,23 @@ function handleKeyDown(e: KeyboardEvent) {
264278
}
265279
266280
onMounted(() => {
281+
updateViewportHeight()
282+
283+
if (typeof window !== 'undefined') {
284+
const resizeHandler = () => updateViewportHeight()
285+
window.addEventListener('resize', resizeHandler)
286+
window.addEventListener('orientationchange', resizeHandler)
287+
viewportCleanupFns.push(() => window.removeEventListener('resize', resizeHandler))
288+
viewportCleanupFns.push(() => window.removeEventListener('orientationchange', resizeHandler))
289+
290+
if (window.visualViewport) {
291+
window.visualViewport.addEventListener('resize', resizeHandler)
292+
window.visualViewport.addEventListener('scroll', resizeHandler)
293+
viewportCleanupFns.push(() => window.visualViewport?.removeEventListener('resize', resizeHandler))
294+
viewportCleanupFns.push(() => window.visualViewport?.removeEventListener('scroll', resizeHandler))
295+
}
296+
}
297+
267298
// Initialize lastScrollTop and attach extra listeners
268299
if (messagesContainer.value) {
269300
lastScrollTop.value = messagesContainer.value.scrollTop
@@ -284,6 +315,9 @@ onUnmounted(() => {
284315
messagesContainer.value.removeEventListener('pointerdown', handlePointerDown)
285316
document.removeEventListener('keydown', handleKeyDown)
286317
}
318+
319+
viewportCleanupFns.forEach(fn => fn())
320+
viewportCleanupFns.length = 0
287321
})
288322
289323
watch(content, () => {
@@ -340,7 +374,7 @@ watch(content, () => {
340374
</script>
341375

342376
<template>
343-
<div class="h-screen flex items-center justify-center p-4 app-container bg-gray-50 dark:bg-gray-900">
377+
<div class="flex items-center justify-center p-4 app-container bg-gray-50 dark:bg-gray-900">
344378
<!-- 设置按钮和面板 -->
345379
<div class="fixed top-4 right-4 z-10">
346380
<!-- 设置按钮 -->
@@ -485,7 +519,7 @@ watch(content, () => {
485519
</div>
486520

487521
<!-- Chatbot-style container -->
488-
<div class="chatbot-container max-w-5xl w-full h-[85vh] bg-white dark:bg-gray-800 rounded-2xl shadow-2xl dark:shadow-gray-900/50 flex flex-col overflow-hidden border border-gray-200 dark:border-gray-700">
522+
<div class="chatbot-container max-w-5xl w-full bg-white dark:bg-gray-800 rounded-2xl shadow-2xl dark:shadow-gray-900/50 flex flex-col overflow-hidden border border-gray-200 dark:border-gray-700">
489523
<!-- Header -->
490524
<div class="chatbot-header px-6 py-4 border-b border-gray-200 dark:border-gray-700 bg-gradient-to-r from-blue-50 to-purple-50 dark:from-gray-800 dark:to-gray-800">
491525
<div class="flex items-center justify-between gap-3">
@@ -539,12 +573,21 @@ watch(content, () => {
539573
</template>
540574

541575
<style scoped>
576+
:global(:root) {
577+
--app-viewport-vh: 1vh;
578+
}
579+
542580
.app-container {
543581
transition: background-color 0.3s ease;
582+
min-height: calc(var(--app-viewport-vh, 1vh) * 100);
583+
overflow: hidden;
544584
}
545585
546586
.chatbot-container {
547587
transition: all 0.3s ease;
588+
overscroll-behavior: contain;
589+
height: calc(var(--app-viewport-vh, 1vh) * 100 - 2rem);
590+
max-height: calc(var(--app-viewport-vh, 1vh) * 100 - 2rem);
548591
}
549592
550593
.github-star-btn:active {
@@ -553,6 +596,7 @@ watch(content, () => {
553596
554597
.chatbot-messages {
555598
scroll-behavior: smooth;
599+
overscroll-behavior: contain;
556600
}
557601
558602
.chatbot-messages::-webkit-scrollbar {

0 commit comments

Comments
 (0)