What should the first release include?
Message Scroller
在新消息加入时跟随内容,同时允许读者离开并返回最新位置。
适用场景
- 当界面需要以下行为时使用 Message Scroller:在新消息加入时跟随内容,同时允许读者离开并返回最新位置。
安装
选择安装方式,用法示例将同步更新导入路径。
$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/message-scroller.json https://orvui.efu.me/r/message.json https://orvui.efu.me/r/prompt-input.json https://orvui.efu.me/r/streaming-response.json
首次使用时,将安装的 src/components/orvui/styles.css 导入项目样式入口。
示例
预览
packages/vue/src/agents/message-scroller/demo.vue
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from "vue";
import { Bot, User } from "lucide-vue-next";
import {
Message,
MessageAvatar,
MessageContent,
MessageFooter,
MessageGroup,
MessageHeader,
MessageTyping,
} from "@/components/orvui/agents/message";
import { MessageBubble, MessageBubbleContent } from "@/components/orvui/agents/message-bubble";
import { MessageScroller } from "@/components/orvui/agents/message-scroller";
import { PromptInput } from "@/components/orvui/agents/prompt-input";
import { StreamingResponse } from "@/components/orvui/agents/streaming-response";
interface ChatMessage {
id: string;
from: "user" | "assistant";
content: string;
animateIn: boolean;
streaming?: boolean;
}
const messages = ref<ChatMessage[]>([
{
id: "scope-question",
from: "user",
content: "What should the first release include?",
animateIn: false,
},
{
id: "scope-answer",
from: "assistant",
content: "Start with the smallest workflow that still feels complete.",
animateIn: false,
},
{
id: "states-question",
from: "user",
content: "Include streaming and recovery states too.",
animateIn: false,
},
{
id: "states-answer",
from: "assistant",
content: "Yes. Those states make the first version feel dependable.",
animateIn: false,
},
{
id: "evidence-question",
from: "user",
content: "How should we present tool results?",
animateIn: false,
},
{
id: "evidence-answer",
from: "assistant",
content: "Keep results close to the action that produced them.",
animateIn: false,
},
{
id: "approval-question",
from: "user",
content: "What about actions that need confirmation?",
animateIn: false,
},
{
id: "approval-answer",
from: "assistant",
content: "Pause the run, explain the impact, and ask before continuing.",
animateIn: false,
},
{
id: "summary-question",
from: "user",
content: "Can the transcript stay easy to navigate?",
animateIn: false,
},
{
id: "summary-answer",
from: "assistant",
content: "Use the rail to jump between turns without losing your place.",
animateIn: false,
},
]);
const phase = ref<"idle" | "waiting" | "typing">("idle");
const pending = computed(() => phase.value === "waiting"),
loading = computed(() => phase.value !== "idle");
let serial = 0,
reduced = false,
animation: number | undefined;
let epoch = 0;
const scheduledTasks = new Set<ReturnType<typeof setTimeout>>();
function cancel() {
epoch++;
scheduledTasks.forEach(clearTimeout);
scheduledTasks.clear();
}
function later(milliseconds: number, task: () => void) {
const version = epoch;
const handle = setTimeout(() => {
scheduledTasks.delete(handle);
if (version === epoch) task();
}, milliseconds);
scheduledTasks.add(handle);
}
onBeforeUnmount(cancel);
function stop() {
cancel();
if (animation !== undefined) cancelAnimationFrame(animation);
animation = undefined;
phase.value = "idle";
messages.value.forEach((message) => {
message.streaming = false;
});
}
function submit(prompt: string) {
if (loading.value) return;
const turn = serial++,
text =
"The viewport follows while you stay at the live edge. Scroll upward while this response streams and it will leave your reading position alone.";
messages.value.push({
id: `sent-user-${turn}`,
from: "user",
content: prompt,
animateIn: true,
});
phase.value = "waiting";
later(reduced ? 0 : 420, () => {
messages.value.push({
id: `sent-assistant-${turn}`,
from: "assistant",
content: "",
animateIn: true,
streaming: true,
});
const target = messages.value[messages.value.length - 1];
phase.value = "typing";
if (reduced) {
target.content = text;
target.streaming = false;
phase.value = "idle";
return;
}
const start = performance.now() + 140,
version = epoch;
const render = (now: number) => {
if (version !== epoch) return;
const count = Math.min(
text.length,
Math.floor(Math.max(0, now - start) * 0.096),
);
target.content = text.substring(0, count);
if (count < text.length) animation = requestAnimationFrame(render);
else {
animation = undefined;
target.streaming = false;
phase.value = "idle";
}
};
animation = requestAnimationFrame(render);
});
}
onMounted(() => {
reduced = matchMedia("(prefers-reduced-motion: reduce)").matches;
});
onBeforeUnmount(stop);
</script>
<template>
<div
class="flex h-[440px] w-full max-w-xl flex-col overflow-hidden rounded-2xl border border-border/70 bg-background"
>
<MessageScroller
:busy="loading"
navigation="rail"
class="min-h-0 flex-1"
viewport-class-name="px-3 py-4"
content-class-name="min-h-full"
><MessageGroup spacing="default"
><Message
v-for="message in messages"
:key="message.id"
:id="message.id"
:from="message.from"
:animate-in="message.from === 'user' && message.animateIn"
><MessageContent
><MessageBubble
:variant="message.from === 'user' ? 'solid' : 'soft'"
:animate-in="false"
><MessageBubbleContent
><StreamingResponse
v-if="message.from === 'assistant'"
:status="message.streaming ? 'streaming' : 'complete'"
:show-actions="false"
:announce="false"
><template v-if="message.content">{{
message.content
}}</template
><MessageTyping v-else /></StreamingResponse
><template v-else>{{
message.content
}}</template></MessageBubbleContent
></MessageBubble
></MessageContent
></Message
><Message v-if="pending" id="pending-assistant" from="assistant"
><MessageContent
><MessageTyping
label="Preparing response" /></MessageContent></Message></MessageGroup
></MessageScroller>
<div class="shrink-0 border-t border-border/60 p-2">
<PromptInput
:min-rows="1"
:max-rows="1"
:loading="loading"
@submit="submit"
@stop="stop"
placeholder="Send another message…"
class="border-0 bg-muted shadow-none focus-within:border-transparent"
/>
</div>
</div>
</template>
packages/vue/src/agents/message-scroller/src/MessageScroller.vue
组合结构
使用公开部件 MessageScroller 组合 Message Scroller,并从该组件的公开入口导入。
MessageScroller
└── MessageGroup
└── Message
└── MessageContent说明: Message 使用内容、附件、来源与操作部件组合会话条目。 Prompt Input 通过自适应高度文本框、附件与键盘操作编写并提交提示词。 Streaming Response 组合持续变化的助手回复,包括文字、附件、引用和操作。
工作方式
在新消息加入时跟随内容,同时允许读者离开并返回最新位置。
状态与事件
此示例组合没有声明成对的受控属性与 update 事件。使用已列出的属性、原生事件和插槽组合内容;不要为它添加未公开的 v-model。
API 参考
followOutput?booleantruefollowThreshold?number56smooth?booleantruelabel?string"Conversation"busy?booleanfalsenavigation?"rail"—navigationLabel?string"Message navigation"viewportClassName?string—contentClassName?string—railClassName?string—viewportRef?Ref<HTMLElement | null, HTMLElement | null> | (element: HTMLElement | null) => void—viewportProps?HTMLAttributes{}contentProps?HTMLAttributes—事件
followChange[following: boolean]@followChange无障碍
为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。
常见问题
- 如何配置 Message Scroller?
- 通过 followOutput、followThreshold、smooth、label、busy、navigation、navigationLabel、viewportClassName、contentClassName、railClassName、viewportRef、viewportProps、contentProps 配置 Message Scroller;API 表列出其类型与默认值。
- 如何连接到应用状态?
- 监听 followChange,将组件连接到应用状态。
- 可以在服务端渲染吗?
- 可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。
更新于
来源与许可 ↗MIT · 0.1.1