跳转到内容
Nacre
组件配色
EN

输入区 ChatComposerFrame

experimentalChatComposerFrame在画廊运行
ChatComposerFrame
在浏览器里运行真实组件。

会话输入区的外壳:圆角卡片、焦点描边,收起(一行)与展开(引用 / 附件、编辑器、工具栏)两种排布。不拥有草稿、发送、上传、快捷回复,这些由应用放进各个位置(参照 coss Composer 的框体)。

状态:experimental · 组件:ChatComposerFrame

import { ChatComposerFrame } from 'nacre/chat'

展开态:header 放引用条与待发附件,children 是编辑器,footer 是工具栏、发送提示与发送。编辑器聚焦时传 focused,描边换 ring 60% + 15% 光晕(常驻输入区比普通输入框轻一档)。

import { Text, View, signal } from 'nsh'
import { Input } from 'nsh/base'
import { ChatComposerFrame, ChatReplyReference } from 'nacre/chat'
import { IconButton, TEXT_SIZE, TOUCH } from 'nacre'
export function ChatComposerUsage() {
const focused = signal(false)
const text = signal('')
const quoting = signal(true)
return (
<View class="w-[520px] max-w-full h-[160px]">
<ChatComposerFrame
testID="demo-composer"
class="flex-1"
focused={focused()}
header={
<If when={quoting()}>
<ChatReplyReference
variant="composer"
class="mx-3 mt-2"
prefix="回复"
author="陈智源"
preview="那中签率大概多少?"
onDismiss={() => quoting.set(false)}
dismissLabel="取消引用"
/>
</If>
}
footer={
<>
<IconButton icon="image" label="图片" />
<IconButton icon="sparkles" label="写作助手" />
<View class="flex-1" />
<If when={!TOUCH}><Text class={`shrink-0 ${TEXT_SIZE.caption} text-muted-foreground mr-2`}>⏎ 发送</Text></If>
<IconButton testID="demo-composer-send" icon="send" variant="primary" size="md" label="发送" disabled={!text().trim()} onPress={() => text.set('')} />
</>
}
>
<Input
testID="demo-composer-input"
accessibilityLabel="消息内容"
value={text()}
onChange={(e) => text.set(e.value)}
onFocus={() => focused.set(true)}
onBlur={() => focused.set(false)}
placeholder="输入消息…"
class={`flex-1 px-3 pt-2 ${TEXT_SIZE.body} text-foreground placeholder:text-muted-foreground`}
/>
</ChatComposerFrame>
</View>
)
}

collapsed:一行,leading(选图 +)、编辑器、trailing(快捷键提示、发送)横排;header 与 footer 不显示。什么时候收起(失焦且为空 300ms 后)由应用决定。

手机:collapsed:一行,左侧选图(+)、编辑器、右侧发送横排;header 与 footer 不显示。

import { Text, View } from 'nsh'
import { ChatComposerFrame } from 'nacre/chat'
import { IconButton, TEXT_SIZE, TOUCH } from 'nacre'
export function ChatComposerCollapsed() {
return (
<View class="w-[520px] max-w-full h-12">
<ChatComposerFrame
testID="demo-composer-collapsed"
class="flex-1"
focused={false}
collapsed
leading={<IconButton icon="plus" variant="secondary" round label="图片" />}
trailing={
<View class="flex-row items-center gap-2">
<If when={!TOUCH}><Text class={`${TEXT_SIZE.caption} text-muted-foreground`}>⌘/ 快捷语</Text></If>
<IconButton icon="send" variant="primary" size="md" disabled label="发送" />
</View>
}
>
<Text class={`flex-1 min-w-0 truncate px-2 ${TEXT_SIZE.body} text-muted-foreground`}>回复 陈智源…</Text>
</ChatComposerFrame>
</View>
)
}
键 行为
Tab 在编辑器与框里的按钮之间移动;回车发送等快捷键由应用的编辑器处理
  • 外壳本身不进读屏树;编辑器、发送钮各自有读屏名。
  • disabled 只表现禁用外观,应用要同时禁用编辑器与发送。

该用

  • 点框里的空白处把焦点交给编辑器(onPress)
  • 引用条用 ChatReplyReference variant=“composer”
  • 窄时用 onFooterWidth 藏掉发送提示

不该用

  • 把草稿、发送逻辑放进外壳
  • 只靠 disabled 外观拦住发送
属性 类型 默认 说明
focused * boolean — 编辑器或框里的按钮有焦点:描边换 ring 60% + 15% 光晕(常驻输入区比普通输入框轻一档)
collapsed boolean — 收起态:一行(leading、编辑器、trailing 横排),没有 header 与 footer; 展开态:header、编辑器、footer 竖排
disabled boolean — 输入区禁用外观(secondary 底、无描边),见 ComposerFrame;编辑器与发送要由应用自己禁用
overlay JSX.Element — 叠在框上的浮层锚点(写作助手、快捷回复联想);放在最前,不占排版
header JSX.Element — 展开态编辑器上方:引用条(ChatReplyReference variant=“composer”)、待发附件
leading JSX.Element — 收起态编辑器前(选图 +)
trailing JSX.Element — 收起态编辑器后(快捷键提示、发送)
footer JSX.Element — 展开态底部一行(工具栏、发送提示、发送)
onFooterWidth (width: number) => void — 底部一行的宽(窄时应用可以藏掉发送提示)
onPress () => void — 点框里的空白处:应用把焦点交给编辑器
class string —
testID string —
children * JSX.Element — 编辑器

* 必填