Message Bubble
呈现紧凑会话气泡,并可组合头像、元数据与操作控件。
适用场景
- 当界面需要以下行为时使用 Message Bubble:呈现紧凑会话气泡,并可组合头像、元数据与操作控件。
安装
选择安装方式,用法示例将同步更新导入路径。
$ bunx --bun shadcn-vue@2.8.2 add https://orvui.efu.me/r/message-bubble.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 导入项目样式入口。
示例
Animated Surfaces
surfaces.vue比较接收消息与发送消息使用的不同气泡表面。
packages/vue/src/agents/message-bubble/examples/surfaces.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: "welcome-question",
from: "user",
content: "What should we include in the first release?",
animateIn: false,
},
{
id: "welcome-answer",
from: "assistant",
content: "Start with the smallest workflow that still feels complete.",
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 =
"That message mounted once with a spring pop. Streaming updates only change its content, so the entrance does not replay.";
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"
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 a bubble…"
class="border-0 bg-muted shadow-none focus-within:border-transparent"
/>
</div>
</div>
</template>
packages/vue/src/agents/message-bubble/src/MessageBubble.vue
With Avatars
avatars.vue将头像选项、消息内容与发送者元数据组合使用。
packages/vue/src/agents/message-bubble/examples/avatars.vue
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from "vue";
import { AnimatePresence } from "motion-v";
import { Bot, RotateCcw, User } from "lucide-vue-next";
import { MessageBubble, MessageBubbleContent } from "@/components/orvui/agents/message-bubble";
import {
Message,
MessageAvatar,
MessageContent,
MessageFooter,
MessageGroup,
MessageHeader,
} from "@/components/orvui/agents/message";
import { StreamingResponse } from "@/components/orvui/agents/streaming-response";
const step = ref(0),
run = ref(0);
const replies = [
"Absolutely. I’ll keep it concise and lead with what changed.",
"Do you want the tone to feel more technical or more customer-facing?",
];
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 replay() {
cancel();
run.value++;
step.value = 0;
later(650, () => {
step.value = 1;
});
later(1550, () => {
step.value = 2;
});
}
onMounted(replay);
</script>
<template>
<div class="flex h-[410px] w-full max-w-xl flex-col justify-center px-3">
<MessageGroup :key="run" spacing="default"
><Message from="user"
><MessageAvatar class="bg-foreground text-background"
><User /></MessageAvatar
><MessageContent
><MessageHeader
><span class="font-medium text-foreground/70">You</span
><span>Now</span></MessageHeader
><MessageBubble variant="solid"
><MessageBubbleContent
>Can you turn these notes into a launch
update?</MessageBubbleContent
></MessageBubble
><MessageFooter>Delivered</MessageFooter></MessageContent
></Message
><AnimatePresence mode="popLayout"
><Message
v-for="(reply, index) in replies.slice(0, step)"
:key="index"
from="assistant"
><MessageAvatar :placeholder="index > 0"
><Bot v-if="index === 0" /></MessageAvatar
><MessageContent
><MessageHeader v-if="index === 0"
><span class="font-medium text-foreground/70">Assistant</span
><span>Just now</span></MessageHeader
><MessageBubble variant="soft"
><MessageBubbleContent
><StreamingResponse status="complete" :show-actions="false">{{
reply
}}</StreamingResponse></MessageBubbleContent
></MessageBubble
></MessageContent
></Message
></AnimatePresence
></MessageGroup
>
<div class="flex h-11 items-end justify-center">
<button
type="button"
@click="replay"
class="inline-flex h-8 items-center gap-1.5 rounded-full border border-border px-3 text-xs font-medium text-muted-foreground outline-none transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring"
>
<RotateCcw class="size-3.5" />Replay
</button>
</div>
</div>
</template>
packages/vue/src/agents/message-bubble/src/MessageBubble.vue
Show More
show-more.vue原地展开较长气泡,保持周围会话控件的位置。
packages/vue/src/agents/message-bubble/examples/show-more.vue
<script setup lang="ts">
import { Bot } from "lucide-vue-next";
import {
MessageBubble,
MessageBubbleCollapsible,
MessageBubbleContent,
} from "@/components/orvui/agents/message-bubble";
import {
Message,
MessageAvatar,
MessageContent,
MessageHeader,
} from "@/components/orvui/agents/message";
import { StreamingResponse } from "@/components/orvui/agents/streaming-response";
const paragraphs = [
"The release is ready for a focused rollout. The main conversation flow, keyboard navigation, error recovery, and reduced-motion behavior are all covered.",
"I would keep advanced workflow controls out of this version. They add configuration without improving the first-run experience, and the usage data from this release will give us a better basis for those decisions.",
"Before publishing, run the accessibility suite once more and verify the streaming behavior with a long response on a smaller viewport.",
];
</script>
<template>
<div class="flex h-[410px] w-full max-w-xl items-start px-3 pt-14">
<Message from="assistant"
><MessageAvatar><Bot /></MessageAvatar
><MessageContent
><MessageHeader
><span class="font-medium text-foreground/70">Assistant</span
><span>Summary</span></MessageHeader
><MessageBubble variant="soft" :animate-in="false"
><MessageBubbleContent class="max-w-[90%]"
><StreamingResponse status="complete" :show-actions="false"
><MessageBubbleCollapsible :collapsed-lines="4"
><p v-for="paragraph in paragraphs" :key="paragraph">
{{ paragraph }}
</p></MessageBubbleCollapsible
></StreamingResponse
></MessageBubbleContent
></MessageBubble
></MessageContent
></Message
>
</div>
</template>
packages/vue/src/agents/message-bubble/src/MessageBubble.vue
组合结构
使用公开部件 MessageBubble, MessageBubbleContent 组合 Message Bubble,并从该组件的公开入口导入。
Message
├── MessageAvatar
└── MessageContent
└── MessageBubble
└── MessageBubbleContent说明: Message 使用内容、附件、来源与操作部件组合会话条目。 Streaming Response 组合持续变化的助手回复,包括文字、附件、引用和操作。
工作方式
呈现紧凑会话气泡,并可组合头像、元数据与操作控件。
状态与事件
需要由表单、路由或父组件管理时,绑定下列状态并接收对应更新。v-model 是属性与 update 事件的简写;值的类型应与 API 一致。
| 组件 | Vue 绑定 | 更新事件 |
|---|---|---|
MessageBubbleCollapsible | v-model:openboolean | update:open |
可选受控属性未传入时,按组件文档的默认行为工作;不要用固定的 false 或空值代替未传入。异步请求和持久化在应用中处理,再把结果传回组件。
API 参考
Animated Surfaces
MessageBubble
variant?"ghost" | "outline" | "danger" | "solid" | "soft" | "tint""soft"align?"start" | "end"(parent.value === "user" ? "end" : "start")animateIn?booleanfalse插槽
default() => VNodeChild#defaultMessageBubbleContent
as?string | ComponentOptions<any, any, any, ComputedOptions, MethodOptions, any, any, any, string, {}, {}, string, {}, {}, {}, string, ComponentProvideOptions> | FunctionalComponent<any, {}, any, {}> | ComponentPublicInstanceConstructor<any, any, any, any, ComputedOptions, MethodOptions>"div"插槽
default() => VNodeChild#defaultWith Avatars
MessageBubble
variant?"ghost" | "outline" | "danger" | "solid" | "soft" | "tint""soft"align?"start" | "end"(parent.value === "user" ? "end" : "start")animateIn?booleanfalse插槽
default() => VNodeChild#defaultMessageBubbleContent
as?string | ComponentOptions<any, any, any, ComputedOptions, MethodOptions, any, any, any, string, {}, {}, string, {}, {}, {}, string, ComponentProvideOptions> | FunctionalComponent<any, {}, any, {}> | ComponentPublicInstanceConstructor<any, any, any, any, ComputedOptions, MethodOptions>"div"插槽
default() => VNodeChild#defaultShow More
MessageBubble
variant?"ghost" | "outline" | "danger" | "solid" | "soft" | "tint""soft"align?"start" | "end"(parent.value === "user" ? "end" : "start")animateIn?booleanfalse插槽
default() => VNodeChild#defaultMessageBubbleCollapsible
open?booleanundefineddefaultOpen?booleanfalsecollapsedLines?2 | 3 | 5 | 4 | 64moreLabel?boolean | null | string | number | void | VNode<RendererNode, RendererElement, { [key: string]: any; }> | VNodeArrayChildren | ComponentOptions<any, any, any, ComputedOptions, MethodOptions, any, any, any, string, {}, {}, string, {}, {}, {}, string, ComponentProvideOptions> | FunctionalComponent<any, {}, any, {}> | ComponentPublicInstanceConstructor<any, any, any, any, ComputedOptions, MethodOptions>"Show more"lessLabel?boolean | null | string | number | void | VNode<RendererNode, RendererElement, { [key: string]: any; }> | VNodeArrayChildren | ComponentOptions<any, any, any, ComputedOptions, MethodOptions, any, any, any, string, {}, {}, string, {}, {}, {}, string, ComponentProvideOptions> | FunctionalComponent<any, {}, any, {}> | ComponentPublicInstanceConstructor<any, any, any, any, ComputedOptions, MethodOptions>"Show less"contentClassName?string—triggerClassName?string—事件
update:open[open: boolean]@update:openopenChange[open: boolean]@openChange插槽
default() => VNodeChild#defaultMessageBubbleContent
as?string | ComponentOptions<any, any, any, ComputedOptions, MethodOptions, any, any, any, string, {}, {}, string, {}, {}, {}, string, ComponentProvideOptions> | FunctionalComponent<any, {}, any, {}> | ComponentPublicInstanceConstructor<any, any, any, any, ComputedOptions, MethodOptions>"div"插槽
default() => VNodeChild#default无障碍
为交互控件提供明确的无障碍名称。保留文档中的键盘路径、可见焦点与减少动态效果状态;自定义插槽应继续保留所替代部件的语义。
常见问题
- 如何配置 Message Bubble?
- 通过 variant、align、animateIn 配置 Message Bubble;API 表列出其类型与默认值。
- 如何连接到应用状态?
- 通过default 插槽提供内容,应用副作用由调用方管理。
- 可以在服务端渲染吗?
- 可以在 Vue SSR 应用中正常渲染。服务端与客户端的初始 props 应保持一致;浏览器测量与交互资源在挂载后启动。
更新于
来源与许可 ↗MIT · 0.1.1