말풍선
말풍선 (Bubble)
채팅에서 사용하는 말풍선 컴포넌트예요. AI와의 대화 메시지를 자연스럽게 보여 주는 데 쓰여요.
출처: 문서
본문
언제 사용하나요?
주로 채팅 시나리오에서 사용해요.
예제
Bubble.List 예제
API
공통 props 참고: 공통 props
Bubble
| Attribute | 설명 | 타입 | 기본값 | 버전 |
|---|---|---|---|---|
| placement | 말풍선 위치 | start | end |
start |
- |
| loading | 로딩 상태 | boolean | - | - |
| loadingRender | 커스텀 로딩 콘텐츠 렌더러 | () => React.ReactNode | - | - |
| content | 말풍선 콘텐츠 | ContentType | - | - |
| contentRender | 커스텀 콘텐츠 렌더러 | (content: ContentType, info: InfoType ) => React.ReactNode | - | - |
| editable | 편집 가능 여부 | boolean | EditableBubbleOption | false |
2.0.0 |
| typing | 타이핑 애니메이션 효과 | boolean | BubbleAnimationOption | ((content: ContentType, info: InfoType) => boolean | BubbleAnimationOption) | false |
- |
| streaming | 스트리밍 모드 | boolean | false |
- |
| variant | 말풍선 스타일 변형(variant) | filled | outlined | shadow | borderless |
filled |
- |
| shape | 말풍선 모양 | default | round | corner |
default |
- |
| footerPlacement | 푸터 슬롯 위치 | outer-start | outer-end | inner-start | inner-end |
outer-start |
2.0.0 |
| header | 헤더 슬롯 | BubbleSlot | - | - |
| footer | 푸터 슬롯 | BubbleSlot | - | - |
| avatar | 아바타 슬롯 | BubbleSlot | - | - |
| extra | 추가 슬롯 | BubbleSlot | - | - |
| onTyping | 타이핑 애니메이션 콜백 | (rendererContent: string, currentContent: string) => void | - | 2.0.0 |
| onTypingComplete | 타이핑 애니메이션 완료 콜백 | (content: string) => void | - | - |
| onEditConfirm | 편집 확인 콜백 | (content: string) => void | - | 2.0.0 |
| onEditCancel | 편집 취소 콜백 | () => void | - | 2.0.0 |
ContentType
기본 타입
type ContentType = React.ReactNode | AnyObject | string | number;
커스텀 타입 사용법
type CustomContentType {
...
}
<Bubble<CustomContentType> {...props} />
BubbleSlot
type BubbleSlot<ContentType> =
| React.ReactNode
| ((content: ContentType, info: InfoType) => React.ReactNode);
EditableBubbleOption
interface EditableBubbleOption {
/**
* @description Whether editable
*/
editing?: boolean;
/**
* @description OK button
*/
okText?: React.ReactNode;
/**
* @description Cancel button
*/
cancelText?: React.ReactNode;
}
BubbleAnimationOption
interface BubbleAnimationOption {
/**
* @description Animation effect type, typewriter, fade-in
* @default 'fade-in'
*/
effect: 'typing' | 'fade-in';
/**
* @description Content step unit, array format for random interval
* @default 6
*/
step?: number | [number, number];
/**
* @description Animation trigger interval
* @default 100
*/
interval?: number;
/**
* @description Whether to keep the common prefix when restarting animation
* @default true
*/
keepPrefix?: boolean;
}
streaming
streaming은 현재 content가 스트리밍 입력 중인지 Bubble에 알려 줘요. 스트리밍 모드에서는 Bubble의 입력 애니메이션 켜짐 여부와 무관하게, streaming이 false가 되기 전까지는 현재 content가 모두 출력됐더라도 Bubble이 onTypingComplete 콜백을 트리거하지 않아요. streaming이 false가 되고 콘텐츠가 전부 출력됐을 때에만 Bubble이 onTypingComplete를 트리거해요. 이는 스트리밍의 불안정성 때문에 여러 번 트리거되는 것을 막고, 스트리밍 과정당 한 번만 트리거되도록 보장해 줘요.
이 예제에서 스트리밍 플래그를 강제로 끄는 모습을 시도해 볼 수 있어요:
- 입력 애니메이션을 켜고 느린 로딩을 수행하면, 스트리밍 속도가 애니메이션 속도를 따라가지 못해
onTypingComplete가 여러 번 트리거될 수 있어요. - 입력 애니메이션을 끄면, 스트리밍 입력마다
onTypingComplete가 트리거돼요.
Bubble.List
| Attribute | 설명 | 타입 | 기본값 | 버전 |
|---|---|---|---|---|
| items | 말풍선 데이터 목록이에요. key와 role이 필수예요. X SDK의 useXChat과 함께 사용하면 Bubble이 구성을 관리하도록 돕는 status를 전달할 수 있어요 |
(BubbleProps & { key: string | number, role: string , status: MessageStatus, extraInfo?: AnyObject })[] | - | - |
| autoScroll | 자동 스크롤 | boolean | true |
- |
| role | 역할 기본 구성 | RoleType | - | - |
MessageStatus
type MessageStatus = 'local' | 'loading' | 'updating' | 'success' | 'error' | 'abort';
InfoType
useXChat과 함께 사용할 때 key는 MessageId로 쓸 수 있고, extraInfo는 커스텀 파라미터로 쓸 수 있어요.
type InfoType = {
status?: MessageStatus;
key?: string | number;
extraInfo?: AnyObject;
};
RoleType
export type RoleProps = Pick<
BubbleProps<any>,
| 'typing'
| 'variant'
| 'shape'
| 'placement'
| 'rootClassName'
| 'classNames'
| 'className'
| 'styles'
| 'style'
| 'loading'
| 'loadingRender'
| 'contentRender'
| 'footerPlacement'
| 'header'
| 'footer'
| 'avatar'
| 'extra'
| 'editable'
| 'onTyping'
| 'onTypingComplete'
| 'onEditConfirm'
| 'onEditCancel'
>;
export type FuncRoleProps = (data: BubbleItemType) => RoleProps;
export type DividerRoleProps = Partial<DividerBubbleProps>;
export type FuncDividerRoleProps = (data: BubbleItemType) => DividerRoleProps;
export type RoleType = Partial<
'ai' | 'system' | 'user', RoleProps | FuncRoleProps>
> & { divider: DividerRoleProps | FuncDividerRoleProps } & Record<
string,
RoleProps | FuncRoleProps
>;
Bubble.List autoScroll 상단 정렬
Bubble.List의 자동 스크롤은 단순한 역순 정렬 방식이에요. 고정 높이의 Bubble.List에서는 메시지 내용이 높이를 채우지 못하면 내용이 하단 정렬돼요. Bubble.List에 고정 높이를 설정하지 말고, 부모 컨테이너에 고정 높이를 설정한 뒤 flex 레이아웃(display: flex과 flex-direction: column)을 쓰는 걸 권해요. 이렇게 하면 Bubble.List가 높이에 맞춰 늘어나고 내용이 적을 때 상단 정렬돼요. Bubble List 데모에서 보여 주는 방식이에요.
<div style={{ height: 600, display: 'flex', flexDirection: 'column' }}>
<Bubble.List items={items} autoScroll />
</div>
flex 레이아웃을 쓰고 싶지 않다면 Bubble.List에 max-height를 설정할 수 있어요. 내용이 적을 때 높이에 맞춰 늘어나고 상단 정렬돼요.
<Bubble.List items={items} autoScroll style={{ maxHeight: 600 }} />
Bubble.List role과 커스텀 Bubble
Bubble.List의 role과 items 속성 둘 다 말풍선을 구성할 수 있어요. 이때 role 구성은 기본값으로 쓰이고 생략할 수 있어요. item.role은 데이터 항목의 말풍선 역할을 지정하는 데 쓰이며, Bubble.List.role과 매칭돼요. items 자체에도 말풍선 속성을 구성할 수 있고, role 구성보다 우선순위가 높아요. 최종 말풍선 구성은 { ...role[item.role], ...item }이 돼요.
Bubble.List의 시맨틱 구성으로도 말풍선을 스타일링할 수 있지만, 우선순위가 가장 낮아 role이나 items에 의해 덮어 쓰인다는 점에 유의하세요.
최종 구성 우선순위는 items > role > Bubble.List.styles = Bubble.List.classNames예요.
특별한 참고 사항: role에는 ai, user, system, divider 네 가지 기본 필드를 제공해요. 이 중 system과 divider는 예약된 필드예요. item.role에 이 둘 중 하나가 지정되면 Bubble.List는 이 말풍선 데이터를 Bubble.System(role = 'system') 또는 **Bubble.Divider(role = 'divider')**로 렌더링해요.
따라서 system Bubble이나 divider Bubble의 렌더링을 커스터마이즈하고 싶다면 다른 이름을 사용해야 해요.
말풍선 렌더링을 커스터마이즈하는 방법은 이 예제의 참조 렌더링 방식을 참고하세요.
Bubble.System
공통 props 참고: 공통 props
| Attribute | 설명 | 타입 | 기본값 | 버전 |
|---|---|---|---|---|
| content | 말풍선 콘텐츠 | ContentType | - | - |
| variant | 말풍선 스타일 변형(variant) | filled | outlined | shadow | borderless |
shadow |
- |
| shape | 말풍선 모양 | default | round | corner |
default |
- |
Bubble.Divider
공통 props 참고: 공통 props
| Attribute | 설명 | 타입 | 기본값 | 버전 |
|---|---|---|---|---|
| content | 말풍선 콘텐츠, Divider.children과 같아요 | ContentType | - | - |
| dividerProps | Divider props | Divider | - | - |
Semantic DOM
Design Token
더 알아보기
메시지 입력창이 필요하다면 입력창(Sender) 문서를 함께 보세요. 채팅 데이터 흐름은 X SDK의 useXChat 문서에서 다뤄요.