채팅 팔레트
채팅 팔레트 (ChatPalette)
채팅 팔레트(ChatPalette)는 오버레이 안에 챗봇 인터페이스를 만들 수 있게 도와주는 채팅 컴포넌트예요. 스크롤 가능한 내용 영역에 ChatMessages를, 하단 고정 영역에 ChatPrompt를 배치해 모달·슬라이드오버·드로어 안에서 자연스러운 채팅 화면을 구성할 수 있게 해줘요.
출처: Nuxt UI
본문
오버레이 안에 챗봇 인터페이스를 만들기 위한 채팅 팔레트예요.
사용법 (Usage)
ChatPalette 컴포넌트는 구조화된 레이아웃 래퍼로, ChatMessages를 스크롤 가능한 내용 영역에, ChatPrompt를 하단 고정 섹션에 정리해 모달·슬라이드오버·드로어용 챗봇 인터페이스를 일관되게 만들어 줘요.
<template>
<UChatPalette>
<UChatMessages />
<template #prompt>
<UChatPrompt />
</template>
</UChatPalette>
</template>
예시 (Examples)
[!TIP] See: /docs/components/chat
Chat 개요 페이지에서 설치 방법, 서버 설정, 사용 예시를 확인해 보세요.
모달 안에서
Modal의 콘텐츠 안에 ChatPalette 컴포넌트를 사용할 수 있어요.
<script setup lang="ts">
import { isTextUIPart } from 'ai'
import type { UIMessage } from 'ai'
import { useChat } from '@ai-sdk/vue'
import { isPartStreaming } from '@nuxt/ui/utils/ai'
import { Markdown } from '@comark/vue'
import shiki from '@comark/vue/plugins/shiki'
import security from '@comark/vue/plugins/security'
const plugins = [shiki(), security({ blockedTags: ['script', 'style', 'iframe', 'object', 'embed', 'form'] })]
const initialMessages: UIMessage[] = [{
id: '1',
role: 'user',
parts: [{ type: 'text', text: 'What is Nuxt UI?' }]
}, {
id: '2',
role: 'assistant',
parts: [{ type: 'text', text: 'Nuxt UI is a Vue component library built on Reka UI, Tailwind CSS, and Tailwind Variants. It provides 125+ accessible components for building modern web apps.' }]
}]
const input = ref('')
const { messages, status, error, sendMessage } = useChat({
messages: initialMessages
})
function onSubmit() {
if (!input.value.trim()) return
sendMessage({ text: input.value })
input.value = ''
}
const ui = {
prose: {
p: { base: 'my-2 leading-6' },
li: { base: 'my-0.5 leading-6' },
ul: { base: 'my-2' },
ol: { base: 'my-2' },
h1: { base: 'text-xl my-2' },
h2: { base: 'text-lg my-2' },
h3: { base: 'text-base my-2' },
h4: { base: 'text-sm my-2' },
pre: { root: 'my-2' },
table: { root: 'my-2' },
hr: { base: 'my-2' }
}
}
</script>
<template>
<UModal open :ui="{ content: 'sm:max-w-3xl sm:h-[28rem]' }">
<template #content>
<UTheme :ui="ui">
<UChatPalette>
<UChatMessages
:messages="messages"
:status="status"
:user="{ side: 'left', variant: 'naked', avatar: { src: 'https://github.com/benjamincanac.png', loading: 'lazy' as const } }"
:assistant="{ icon: 'i-lucide-bot' }"
>
<template #content="{ message }">
<template v-for="(part, index) in message.parts" :key="`${message.id}-${part.type}-${index}`">
<template v-if="isTextUIPart(part)">
<Markdown
v-if="message.role === 'assistant'"
:value="part.text"
:streaming="isPartStreaming(part)"
:plugins="plugins"
class="*:first:mt-0 *:last:mb-0"
/>
<p v-else-if="message.role === 'user'" class="whitespace-pre-wrap leading-6">
{{ part.text }}
</p>
</template>
</template>
</template>
</UChatMessages>
<template #prompt>
<UChatPrompt
v-model="input"
icon="i-lucide-search"
variant="naked"
:error="error"
@submit="onSubmit"
/>
</template>
</UChatPalette>
</UTheme>
</template>
</UModal>
</template>
ContentSearch 안에서
ContentSearch의 콘텐츠 안에서 조건에 따라 ChatPalette 컴포넌트를 사용해, 사용자가 항목을 선택하면 챗봇 인터페이스를 보여줄 수 있어요.
<script setup lang="ts">
import { isTextUIPart } from 'ai'
import { useChat } from '@ai-sdk/vue'
import { isPartStreaming } from '@nuxt/ui/utils/ai'
import { Markdown } from '@comark/vue'
import shiki from '@comark/vue/plugins/shiki'
import security from '@comark/vue/plugins/security'
const plugins = [shiki(), security({ blockedTags: ['script', 'style', 'iframe', 'object', 'embed', 'form'] })]
const input = ref('')
const { messages, status, error, sendMessage, regenerate } = useChat()
const groups = computed(() => [{
id: 'ai',
ignoreFilter: true,
items: [{
label: searchTerm.value ? `Ask AI for “${searchTerm.value}”` : 'Ask AI',
icon: 'i-lucide-bot',
onSelect: (e: any) => {
e.preventDefault()
ai.value = true
if (searchTerm.value) {
messages.value = [...messages.value, {
id: '1',
role: 'user',
parts: [{ type: 'text', text: searchTerm.value }]
}]
regenerate()
}
}
}]
}])
const ai = ref(false)
const searchTerm = ref('')
function onSubmit() {
if (!input.value.trim()) return
sendMessage({ text: input.value })
input.value = ''
}
function onClose(e: Event) {
e.preventDefault()
ai.value = false
}
const ui = {
prose: {
p: { base: 'my-2 leading-6' },
li: { base: 'my-0.5 leading-6' },
ul: { base: 'my-2' },
ol: { base: 'my-2' },
h1: { base: 'text-xl my-2' },
h2: { base: 'text-lg my-2' },
h3: { base: 'text-base my-2' },
h4: { base: 'text-sm my-2' },
pre: { root: 'my-2' },
table: { root: 'my-2' },
hr: { base: 'my-2' }
}
}
</script>
<template>
<UContentSearch v-model:search-term="searchTerm" open :groups="groups">
<template v-if="ai" #content>
<UTheme :ui="ui">
<UChatPalette>
<UChatMessages
:messages="messages"
:status="status"
:user="{ side: 'left', variant: 'naked', avatar: { src: 'https://github.com/benjamincanac.png', loading: 'lazy' as const } }"
:assistant="{ icon: 'i-lucide-bot' }"
>
<template #content="{ message }">
<template v-for="(part, index) in message.parts" :key="`${message.id}-${part.type}-${index}`">
<template v-if="isTextUIPart(part)">
<Markdown
v-if="message.role === 'assistant'"
:value="part.text"
:streaming="isPartStreaming(part)"
:plugins="plugins"
class="*:first:mt-0 *:last:mb-0"
/>
<p v-else-if="message.role === 'user'" class="whitespace-pre-wrap leading-6">
{{ part.text }}
</p>
</template>
</template>
</template>
</UChatMessages>
<template #prompt>
<UChatPrompt
v-model="input"
icon="i-lucide-search"
variant="naked"
:error="error"
@submit="onSubmit"
@close="onClose"
/>
</template>
</UChatPalette>
</UTheme>
</template>
</UContentSearch>
</template>
API
Props
/**
* Props for the ChatPalette component
*/
interface ChatPaletteProps {
/**
* The element or component this component should render as.
* @default 'div'
*/
as?: any;
ui?: { root?: SlotClass; prompt?: SlotClass; close?: SlotClass; content?: SlotClass; } | undefined;
}
Slots
/**
* Slots for the ChatPalette component
*/
interface ChatPaletteSlots {
default(): any;
prompt(): any;
}
테마 (Theme)
export default defineAppConfig({
ui: {
chatPalette: {
slots: {
root: 'relative flex-1 flex flex-col min-h-0 min-w-0',
prompt: 'px-0 rounded-t-none border-t border-default',
close: '',
content: 'overflow-y-auto flex-1 flex flex-col py-3'
}
}
}
})