|
| 1 | +--- |
| 2 | +title: Suggestion |
| 3 | +description: |
| 4 | +icon: lucide:lightbulb |
| 5 | +--- |
| 6 | + |
| 7 | +The `Suggestion` component displays a horizontal row of clickable suggestions for user interaction. |
| 8 | + |
| 9 | +:::ComponentLoader{label="Preview" componentName="Suggestion"} |
| 10 | +::: |
| 11 | + |
| 12 | +## Install using CLI |
| 13 | + |
| 14 | +::tabs{variant="card"} |
| 15 | + ::div{label="ai-elements-vue"} |
| 16 | + ```sh |
| 17 | + npx ai-elements-vue@latest add suggestion |
| 18 | + ``` |
| 19 | + :: |
| 20 | + ::div{label="shadcn-vue"} |
| 21 | + |
| 22 | + ```sh |
| 23 | + npx shadcn-vue@latest add https://registry.ai-elements-vue.com/suggestion.json |
| 24 | + ``` |
| 25 | + :: |
| 26 | +:: |
| 27 | + |
| 28 | +## Install Manually |
| 29 | + |
| 30 | +Copy and paste the following code in the same folder. |
| 31 | + |
| 32 | +:::code-group |
| 33 | +```vue [Suggestions.vue] |
| 34 | +<script setup lang="ts"> |
| 35 | +import type { HTMLAttributes } from 'vue' |
| 36 | +import { ScrollArea, ScrollBar } from '@repo/shadcn-vue/components/ui/scroll-area' |
| 37 | +import { cn } from '@repo/shadcn-vue/lib/utils' |
| 38 | +
|
| 39 | +interface SuggestionsProps { |
| 40 | + class?: HTMLAttributes['class'] |
| 41 | +} |
| 42 | +
|
| 43 | +const props = defineProps<SuggestionsProps>() |
| 44 | +</script> |
| 45 | +
|
| 46 | +<template> |
| 47 | + <ScrollArea class="w-full overflow-x-auto whitespace-nowrap" v-bind="$attrs"> |
| 48 | + <div :class="cn('flex w-max flex-nowrap items-center gap-2', props.class)"> |
| 49 | + <slot /> |
| 50 | + </div> |
| 51 | + <ScrollBar class="hidden" orientation="horizontal" /> |
| 52 | + </ScrollArea> |
| 53 | +</template> |
| 54 | +``` |
| 55 | + |
| 56 | +```vue [Suggestions.vue] |
| 57 | +<script setup lang="ts"> |
| 58 | +import type { HTMLAttributes } from 'vue' |
| 59 | +import { Button } from '@repo/shadcn-vue/components/ui/button' |
| 60 | +import { cn } from '@repo/shadcn-vue/lib/utils' |
| 61 | +
|
| 62 | +interface SuggestionProps { |
| 63 | + suggestion: string |
| 64 | + class?: HTMLAttributes['class'] |
| 65 | + variant?: 'outline' | 'default' | 'destructive' | 'secondary' | 'ghost' | 'link' |
| 66 | + size?: 'default' | 'sm' | 'lg' | 'icon' |
| 67 | +} |
| 68 | +
|
| 69 | +const props = withDefaults(defineProps<SuggestionProps>(), { |
| 70 | + variant: 'outline', |
| 71 | + size: 'sm', |
| 72 | +}) |
| 73 | +
|
| 74 | +const emit = defineEmits<{ |
| 75 | + (e: 'click', suggestion: string): void |
| 76 | +}>() |
| 77 | +
|
| 78 | +function handleClick() { |
| 79 | + emit('click', props.suggestion) |
| 80 | +} |
| 81 | +</script> |
| 82 | +
|
| 83 | +<template> |
| 84 | + <Button |
| 85 | + :class="cn('cursor-pointer rounded-full px-4', props.class)" |
| 86 | + :size="props.size" |
| 87 | + type="button" |
| 88 | + :variant="props.variant" |
| 89 | + v-bind="$attrs" |
| 90 | + @click="handleClick" |
| 91 | + > |
| 92 | + <slot>{{ props.suggestion }}</slot> |
| 93 | + </Button> |
| 94 | +</template> |
| 95 | +``` |
| 96 | + |
| 97 | +```ts [index.ts] |
| 98 | +export { default as Suggestion } from './Suggestion.vue' |
| 99 | +export { default as Suggestions } from './Suggestions.vue' |
| 100 | +``` |
| 101 | +::: |
| 102 | + |
| 103 | +## Usage |
| 104 | + |
| 105 | +```ts |
| 106 | +import { Suggestion, Suggestions } from '@/components/ai-elements/suggestion' |
| 107 | +``` |
| 108 | + |
| 109 | +```vue |
| 110 | +<Suggestions> |
| 111 | + <Suggestion suggestion="What are the latest trends in AI?" /> |
| 112 | +</Suggestions> |
| 113 | +``` |
| 114 | + |
| 115 | +## Usage with AI SDK |
| 116 | + |
| 117 | +Build a simple input with suggestions users can click to send a message to the LLM. |
| 118 | + |
| 119 | +Add the following component to your frontend: |
| 120 | + |
| 121 | +```vue [pages/index.vue] |
| 122 | +<script setup lang="ts"> |
| 123 | +import { useChat } from '@ai-sdk/vue' |
| 124 | +import { ref } from 'vue' |
| 125 | +import { |
| 126 | + Conversation, |
| 127 | + ConversationContent, |
| 128 | + ConversationScrollButton, |
| 129 | +} from '@/components/ai-elements/conversation' |
| 130 | +import { Message, MessageContent } from '@/components/ai-elements/message' |
| 131 | +import { Response } from '@/components/ai-elements/response' |
| 132 | +
|
| 133 | +const input = ref('') |
| 134 | +const { sendMessage, status } = useChat() |
| 135 | +
|
| 136 | +function handleSubmit() { |
| 137 | + if (input.value.trim()) { |
| 138 | + sendMessage({ text: input.value }) |
| 139 | + setInput('') |
| 140 | + } |
| 141 | +} |
| 142 | +
|
| 143 | +function handleSuggestionClick(suggestion: string) { |
| 144 | + sendMessage({ text: suggestion }) |
| 145 | +} |
| 146 | +</script> |
| 147 | +
|
| 148 | +<template> |
| 149 | + <div class="max-w-4xl mx-auto p-6 relative size-full rounded-lg border h-[600px]"> |
| 150 | + <div class="flex flex-col h-full"> |
| 151 | + <div class="flex flex-col gap-4"> |
| 152 | + <Suggestions> |
| 153 | + <Suggestion |
| 154 | + v-for="suggestion in suggestions" |
| 155 | + :key="suggestion" |
| 156 | + :suggestion="suggestion" |
| 157 | + @click="handleSuggestionClick" |
| 158 | + /> |
| 159 | + </Suggestions> |
| 160 | +
|
| 161 | + <Input class="mt-4 w-full max-w-2xl mx-auto relative" @submit.prevent="handleSubmit"> |
| 162 | + <PromptInputTextarea |
| 163 | + v-model="input" |
| 164 | + placeholder="Say something..." |
| 165 | + class="pr-12" |
| 166 | + /> |
| 167 | + <PromptInputSubmit |
| 168 | + :status="status === 'streaming' ? 'streaming' : 'ready'" |
| 169 | + :disabled="!input.trim()" |
| 170 | + class="absolute bottom-1 right-1" |
| 171 | + /> |
| 172 | + </Input> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + </div> |
| 176 | +</template> |
| 177 | +``` |
| 178 | + |
| 179 | +## Features |
| 180 | + |
| 181 | +- Horizontal row of clickable suggestion buttons |
| 182 | +- Customizable styling with variant and size options |
| 183 | +- Flexible layout that wraps suggestions on smaller screens |
| 184 | +- click event that emits the selected suggestion string |
| 185 | +- Support for both individual suggestions and suggestion lists |
| 186 | +- Clean, modern styling with hover effects |
| 187 | +- Responsive design with mobile-friendly touch targets |
| 188 | +- TypeScript support with proper type definitions |
| 189 | + |
| 190 | +## Examples |
| 191 | + |
| 192 | +### Usage with AI Input |
| 193 | + |
| 194 | +:::ComponentLoader{label="Preview" componentName="SuggestionAiInput"} |
| 195 | +::: |
| 196 | + |
| 197 | +## Props |
| 198 | + |
| 199 | +### `<Suggestion />` |
| 200 | + |
| 201 | +:::field-group |
| 202 | + ::field{name="click" type="event: Event" defaultValue="''"} |
| 203 | + The suggestion string to display and emit on click. |
| 204 | + :: |
| 205 | +::: |
| 206 | + |
| 207 | +## Emits |
| 208 | + |
| 209 | +### `<Suggestion />` |
| 210 | + |
| 211 | +:::field-group |
| 212 | + ::field{name="click" type="string" defaultValue="''"} |
| 213 | + The click event that's emitted. |
| 214 | + :: |
| 215 | +::: |
0 commit comments