@leitware/composables 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +387 -0
  3. package/dist/presets/brutalist.css +298 -0
  4. package/dist/presets/default.css +298 -0
  5. package/dist/presets/signal-pop.css +369 -0
  6. package/dist/styles.css +2 -0
  7. package/package.json +216 -0
  8. package/rules/composables.md +244 -0
  9. package/src/_internal/accordion.tsx +93 -0
  10. package/src/_internal/ai-elements/attachments.tsx +385 -0
  11. package/src/_internal/ai-elements/chain-of-thought.tsx +197 -0
  12. package/src/_internal/ai-elements/code-block.tsx +516 -0
  13. package/src/_internal/ai-elements/confirmation.tsx +150 -0
  14. package/src/_internal/ai-elements/conversation.tsx +148 -0
  15. package/src/_internal/ai-elements/inline-citation.tsx +248 -0
  16. package/src/_internal/ai-elements/message.tsx +292 -0
  17. package/src/_internal/ai-elements/plan.tsx +126 -0
  18. package/src/_internal/ai-elements/prompt-input.tsx +1285 -0
  19. package/src/_internal/ai-elements/reasoning.tsx +208 -0
  20. package/src/_internal/ai-elements/shimmer.tsx +72 -0
  21. package/src/_internal/ai-elements/sources.tsx +53 -0
  22. package/src/_internal/ai-elements/suggestion.tsx +48 -0
  23. package/src/_internal/ai-elements/task.tsx +64 -0
  24. package/src/_internal/ai-elements/tool.tsx +156 -0
  25. package/src/_internal/alert-dialog.tsx +159 -0
  26. package/src/_internal/alert.tsx +88 -0
  27. package/src/_internal/aspect-ratio.tsx +23 -0
  28. package/src/_internal/avatar.tsx +58 -0
  29. package/src/_internal/badge.tsx +60 -0
  30. package/src/_internal/breadcrumb.tsx +104 -0
  31. package/src/_internal/bubble.tsx +125 -0
  32. package/src/_internal/button-group.tsx +78 -0
  33. package/src/_internal/button.tsx +62 -0
  34. package/src/_internal/calendar.tsx +179 -0
  35. package/src/_internal/card.tsx +83 -0
  36. package/src/_internal/carousel.tsx +215 -0
  37. package/src/_internal/checkbox.tsx +26 -0
  38. package/src/_internal/code-block.tsx +53 -0
  39. package/src/_internal/collapsible.tsx +15 -0
  40. package/src/_internal/combobox.tsx +264 -0
  41. package/src/_internal/command.tsx +155 -0
  42. package/src/_internal/container.tsx +35 -0
  43. package/src/_internal/dialog.tsx +132 -0
  44. package/src/_internal/direction.tsx +12 -0
  45. package/src/_internal/divider.tsx +162 -0
  46. package/src/_internal/dropdown-menu.tsx +247 -0
  47. package/src/_internal/dropzone.tsx +481 -0
  48. package/src/_internal/empty.tsx +94 -0
  49. package/src/_internal/field.tsx +221 -0
  50. package/src/_internal/grid-overlay.tsx +48 -0
  51. package/src/_internal/grid.tsx +109 -0
  52. package/src/_internal/hover-card.tsx +47 -0
  53. package/src/_internal/icon.tsx +45 -0
  54. package/src/_internal/input-group.tsx +146 -0
  55. package/src/_internal/input.tsx +24 -0
  56. package/src/_internal/item.tsx +187 -0
  57. package/src/_internal/kbd.tsx +28 -0
  58. package/src/_internal/label.tsx +18 -0
  59. package/src/_internal/layout-bento.tsx +386 -0
  60. package/src/_internal/layout.tsx +174 -0
  61. package/src/_internal/marker.tsx +66 -0
  62. package/src/_internal/message.tsx +85 -0
  63. package/src/_internal/native-select.tsx +50 -0
  64. package/src/_internal/pagination.tsx +108 -0
  65. package/src/_internal/popover.tsx +83 -0
  66. package/src/_internal/progress.tsx +59 -0
  67. package/src/_internal/radio-group.tsx +36 -0
  68. package/src/_internal/resizable.tsx +44 -0
  69. package/src/_internal/scroll-area.tsx +48 -0
  70. package/src/_internal/select.tsx +187 -0
  71. package/src/_internal/separator.tsx +19 -0
  72. package/src/_internal/sheet.tsx +122 -0
  73. package/src/_internal/sidebar.tsx +687 -0
  74. package/src/_internal/skeleton.tsx +13 -0
  75. package/src/_internal/slider.tsx +60 -0
  76. package/src/_internal/sonner.tsx +51 -0
  77. package/src/_internal/spacer.tsx +42 -0
  78. package/src/_internal/spinner.tsx +16 -0
  79. package/src/_internal/stack.tsx +78 -0
  80. package/src/_internal/switch.tsx +31 -0
  81. package/src/_internal/table.tsx +84 -0
  82. package/src/_internal/tabs.tsx +74 -0
  83. package/src/_internal/textarea.tsx +19 -0
  84. package/src/_internal/toggle-group.tsx +99 -0
  85. package/src/_internal/toggle.tsx +42 -0
  86. package/src/_internal/tooltip.tsx +55 -0
  87. package/src/_internal/typography.tsx +129 -0
  88. package/src/hooks/use-mobile.ts +19 -0
  89. package/src/hooks/use-numeric-input.ts +119 -0
  90. package/src/index.ts +443 -0
  91. package/src/lib/numeric-input.ts +88 -0
  92. package/src/lib/utils.ts +39 -0
  93. package/src/opinionated/accordion.tsx +92 -0
  94. package/src/opinionated/ai/attachments.tsx +27 -0
  95. package/src/opinionated/ai/chain-of-thought.tsx +16 -0
  96. package/src/opinionated/ai/code-block.tsx +22 -0
  97. package/src/opinionated/ai/confirmation.tsx +16 -0
  98. package/src/opinionated/ai/conversation.tsx +13 -0
  99. package/src/opinionated/ai/index.ts +15 -0
  100. package/src/opinionated/ai/inline-citation.tsx +30 -0
  101. package/src/opinionated/ai/message.tsx +26 -0
  102. package/src/opinionated/ai/plan.tsx +18 -0
  103. package/src/opinionated/ai/prompt-input.tsx +29 -0
  104. package/src/opinionated/ai/reasoning.tsx +9 -0
  105. package/src/opinionated/ai/shimmer.tsx +4 -0
  106. package/src/opinionated/ai/sources.tsx +10 -0
  107. package/src/opinionated/ai/suggestion.tsx +6 -0
  108. package/src/opinionated/ai/task.tsx +12 -0
  109. package/src/opinionated/ai/tool.tsx +14 -0
  110. package/src/opinionated/alert-dialog.tsx +143 -0
  111. package/src/opinionated/alert.tsx +99 -0
  112. package/src/opinionated/aspect-ratio.tsx +12 -0
  113. package/src/opinionated/avatar.tsx +59 -0
  114. package/src/opinionated/badge.tsx +33 -0
  115. package/src/opinionated/breadcrumb.tsx +99 -0
  116. package/src/opinionated/bubble.tsx +9 -0
  117. package/src/opinionated/button.tsx +59 -0
  118. package/src/opinionated/calendar.tsx +20 -0
  119. package/src/opinionated/card.tsx +83 -0
  120. package/src/opinionated/carousel.tsx +99 -0
  121. package/src/opinionated/checkbox.tsx +11 -0
  122. package/src/opinionated/code-block.tsx +12 -0
  123. package/src/opinionated/collapsible.tsx +123 -0
  124. package/src/opinionated/combobox.tsx +123 -0
  125. package/src/opinionated/container.tsx +12 -0
  126. package/src/opinionated/dialog.tsx +124 -0
  127. package/src/opinionated/direction.tsx +6 -0
  128. package/src/opinionated/divider.tsx +13 -0
  129. package/src/opinionated/dropdown-menu.tsx +131 -0
  130. package/src/opinionated/dropzone.tsx +39 -0
  131. package/src/opinionated/empty.tsx +191 -0
  132. package/src/opinionated/field.tsx +26 -0
  133. package/src/opinionated/form-calendar-popover.tsx +138 -0
  134. package/src/opinionated/form-checkbox.tsx +107 -0
  135. package/src/opinionated/form-combobox.tsx +181 -0
  136. package/src/opinionated/form-date-of-birth.tsx +193 -0
  137. package/src/opinionated/form-dropzone.tsx +126 -0
  138. package/src/opinionated/form-input-group.tsx +184 -0
  139. package/src/opinionated/form-input.tsx +118 -0
  140. package/src/opinionated/form-multi-combobox.tsx +213 -0
  141. package/src/opinionated/form-multi-dropzone.tsx +141 -0
  142. package/src/opinionated/form-radiogroup.tsx +116 -0
  143. package/src/opinionated/form-select.tsx +176 -0
  144. package/src/opinionated/form-slider.tsx +100 -0
  145. package/src/opinionated/form-switch.tsx +117 -0
  146. package/src/opinionated/form-textarea.tsx +79 -0
  147. package/src/opinionated/grid-overlay.tsx +11 -0
  148. package/src/opinionated/grid.tsx +14 -0
  149. package/src/opinionated/hover-card.tsx +59 -0
  150. package/src/opinionated/icon.tsx +14 -0
  151. package/src/opinionated/input-group.tsx +20 -0
  152. package/src/opinionated/input.tsx +11 -0
  153. package/src/opinionated/item.tsx +182 -0
  154. package/src/opinionated/kbd.tsx +16 -0
  155. package/src/opinionated/label.tsx +11 -0
  156. package/src/opinionated/layout-bento.tsx +29 -0
  157. package/src/opinionated/layout.tsx +26 -0
  158. package/src/opinionated/list.tsx +94 -0
  159. package/src/opinionated/marker.tsx +7 -0
  160. package/src/opinionated/message.tsx +8 -0
  161. package/src/opinionated/native-select.tsx +51 -0
  162. package/src/opinionated/pagination.tsx +204 -0
  163. package/src/opinionated/popover.tsx +68 -0
  164. package/src/opinionated/progress.tsx +48 -0
  165. package/src/opinionated/radio-group.tsx +13 -0
  166. package/src/opinionated/resizable.tsx +2 -0
  167. package/src/opinionated/scroll-area.tsx +82 -0
  168. package/src/opinionated/select.tsx +101 -0
  169. package/src/opinionated/separator.tsx +11 -0
  170. package/src/opinionated/sheet.tsx +120 -0
  171. package/src/opinionated/sidebar.tsx +68 -0
  172. package/src/opinionated/skeleton.tsx +246 -0
  173. package/src/opinionated/slider.tsx +11 -0
  174. package/src/opinionated/sonner.tsx +15 -0
  175. package/src/opinionated/spacer.tsx +11 -0
  176. package/src/opinionated/stack.tsx +18 -0
  177. package/src/opinionated/switch.tsx +11 -0
  178. package/src/opinionated/table.tsx +11 -0
  179. package/src/opinionated/tabs.tsx +68 -0
  180. package/src/opinionated/textarea.tsx +11 -0
  181. package/src/opinionated/theme-injector.tsx +31 -0
  182. package/src/opinionated/toggle-group.tsx +63 -0
  183. package/src/opinionated/toggle.tsx +75 -0
  184. package/src/opinionated/token-config-panel/color-editor.tsx +85 -0
  185. package/src/opinionated/token-config-panel/helpers.ts +31 -0
  186. package/src/opinionated/token-config-panel/index.tsx +398 -0
  187. package/src/opinionated/token-config-panel/json-import-export.tsx +89 -0
  188. package/src/opinionated/token-config-panel/preset-manager.ts +31 -0
  189. package/src/opinionated/token-config-panel/presets-data.ts +23 -0
  190. package/src/opinionated/token-config-panel/spacing-editor.tsx +325 -0
  191. package/src/opinionated/token-config-panel/types.ts +39 -0
  192. package/src/opinionated/token-config-panel/typography-editor.tsx +145 -0
  193. package/src/opinionated/tooltip.tsx +62 -0
  194. package/src/opinionated/typography.tsx +7 -0
  195. package/src/scripts/lint-no-arbitrary.ts +155 -0
  196. package/src/styles/composable.css +68 -0
  197. package/src/styles/presets/brutalist.css +298 -0
  198. package/src/styles/presets/default.css +298 -0
  199. package/src/styles/presets/signal-pop.css +369 -0
  200. package/src/styles/presets-data/brutalist.ts +325 -0
  201. package/src/styles/presets-data/default.ts +315 -0
  202. package/src/styles/presets-data/index.ts +45 -0
  203. package/src/styles/presets-data/signal-pop.ts +393 -0
  204. package/src/styles/tokens/base.css +71 -0
  205. package/src/styles/tokens/components.css +150 -0
  206. package/src/styles/tokens/palette.css +795 -0
  207. package/src/styles/tokens/registry.ts +324 -0
  208. package/src/styles/tokens/semantic.css +266 -0
  209. package/src/styles/tokens/tailwind-theme.css +247 -0
@@ -0,0 +1,148 @@
1
+ 'use client'
2
+
3
+ import { Button } from '../button'
4
+ import { cn } from '../../lib/utils'
5
+ import type { UIMessage } from 'ai'
6
+ import { ArrowDownIcon, DownloadIcon } from 'lucide-react'
7
+ import type { ComponentProps } from 'react'
8
+ import { useCallback } from 'react'
9
+ import { StickToBottom, useStickToBottomContext } from 'use-stick-to-bottom'
10
+
11
+ export type ConversationProps = ComponentProps<typeof StickToBottom>
12
+
13
+ export const Conversation = ({ className, ...props }: ConversationProps) => (
14
+ <StickToBottom
15
+ className={cn('relative flex-1 overflow-y-hidden', className)}
16
+ initial="smooth"
17
+ resize="smooth"
18
+ role="log"
19
+ {...props}
20
+ />
21
+ )
22
+
23
+ export type ConversationContentProps = ComponentProps<typeof StickToBottom.Content>
24
+
25
+ export const ConversationContent = ({ className, ...props }: ConversationContentProps) => (
26
+ <StickToBottom.Content className={cn('flex flex-col gap-8 p-4', className)} {...props} />
27
+ )
28
+
29
+ export type ConversationEmptyStateProps = ComponentProps<'div'> & {
30
+ title?: string
31
+ description?: string
32
+ icon?: React.ReactNode
33
+ }
34
+
35
+ export const ConversationEmptyState = ({
36
+ className,
37
+ title = 'No messages yet',
38
+ description = 'Start a conversation to see messages here',
39
+ icon,
40
+ children,
41
+ ...props
42
+ }: ConversationEmptyStateProps) => (
43
+ <div
44
+ className={cn(
45
+ 'flex size-full flex-col items-center justify-center gap-3 p-8 text-center',
46
+ className,
47
+ )}
48
+ {...props}
49
+ >
50
+ {children ?? (
51
+ <>
52
+ {icon && <div className="text-muted-foreground">{icon}</div>}
53
+ <div className="space-y-1">
54
+ <h3 className="font-medium text-sm">{title}</h3>
55
+ {description && <p className="text-muted-foreground text-sm">{description}</p>}
56
+ </div>
57
+ </>
58
+ )}
59
+ </div>
60
+ )
61
+
62
+ export type ConversationScrollButtonProps = ComponentProps<typeof Button>
63
+
64
+ export const ConversationScrollButton = ({
65
+ className,
66
+ ...props
67
+ }: ConversationScrollButtonProps) => {
68
+ const { isAtBottom, scrollToBottom } = useStickToBottomContext()
69
+
70
+ const handleScrollToBottom = useCallback(() => {
71
+ scrollToBottom()
72
+ }, [scrollToBottom])
73
+
74
+ return (
75
+ !isAtBottom && (
76
+ <Button
77
+ className={cn(
78
+ 'absolute bottom-4 left-[50%] translate-x-[-50%] rounded-full dark:bg-page',
79
+ className,
80
+ )}
81
+ onClick={handleScrollToBottom}
82
+ size="icon"
83
+ type="button"
84
+ variant="outline"
85
+ {...props}
86
+ >
87
+ <ArrowDownIcon className="size-4" />
88
+ </Button>
89
+ )
90
+ )
91
+ }
92
+
93
+ const getMessageText = (message: UIMessage): string =>
94
+ message.parts
95
+ .filter((part) => part.type === 'text')
96
+ .map((part) => part.text)
97
+ .join('')
98
+
99
+ export type ConversationDownloadProps = Omit<ComponentProps<typeof Button>, 'onClick'> & {
100
+ messages: UIMessage[]
101
+ filename?: string
102
+ formatMessage?: (message: UIMessage, index: number) => string
103
+ }
104
+
105
+ const defaultFormatMessage = (message: UIMessage): string => {
106
+ const roleLabel = message.role.charAt(0).toUpperCase() + message.role.slice(1)
107
+ return `**${roleLabel}:** ${getMessageText(message)}`
108
+ }
109
+
110
+ export const messagesToMarkdown = (
111
+ messages: UIMessage[],
112
+ formatMessage: (message: UIMessage, index: number) => string = defaultFormatMessage,
113
+ ): string => messages.map((msg, i) => formatMessage(msg, i)).join('\n\n')
114
+
115
+ export const ConversationDownload = ({
116
+ messages,
117
+ filename = 'conversation.md',
118
+ formatMessage = defaultFormatMessage,
119
+ className,
120
+ children,
121
+ ...props
122
+ }: ConversationDownloadProps) => {
123
+ const handleDownload = useCallback(() => {
124
+ const markdown = messagesToMarkdown(messages, formatMessage)
125
+ const blob = new Blob([markdown], { type: 'text/markdown' })
126
+ const url = URL.createObjectURL(blob)
127
+ const link = document.createElement('a')
128
+ link.href = url
129
+ link.download = filename
130
+ document.body.append(link)
131
+ link.click()
132
+ link.remove()
133
+ URL.revokeObjectURL(url)
134
+ }, [messages, filename, formatMessage])
135
+
136
+ return (
137
+ <Button
138
+ className={cn('absolute top-4 right-4 rounded-full dark:bg-page', className)}
139
+ onClick={handleDownload}
140
+ size="icon"
141
+ type="button"
142
+ variant="outline"
143
+ {...props}
144
+ >
145
+ {children ?? <DownloadIcon className="size-4" />}
146
+ </Button>
147
+ )
148
+ }
@@ -0,0 +1,248 @@
1
+ 'use client'
2
+
3
+ import { Badge } from '../badge'
4
+ import type { CarouselApi } from '../carousel'
5
+ import { Carousel, CarouselContent, CarouselItem } from '../carousel'
6
+ import { HoverCard, HoverCardContent, HoverCardTrigger } from '../hover-card'
7
+ import { cn } from '../../lib/utils'
8
+ import { ArrowLeftIcon, ArrowRightIcon } from 'lucide-react'
9
+ import type { ComponentProps } from 'react'
10
+ import { createContext, useCallback, useContext, useEffect, useState } from 'react'
11
+
12
+ export type InlineCitationProps = ComponentProps<'span'>
13
+
14
+ export const InlineCitation = ({ className, ...props }: InlineCitationProps) => (
15
+ <span className={cn('group inline items-center gap-1', className)} {...props} />
16
+ )
17
+
18
+ export type InlineCitationTextProps = ComponentProps<'span'>
19
+
20
+ export const InlineCitationText = ({ className, ...props }: InlineCitationTextProps) => (
21
+ <span className={cn('transition-colors group-hover:bg-muted', className)} {...props} />
22
+ )
23
+
24
+ export type InlineCitationCardProps = ComponentProps<typeof HoverCard>
25
+
26
+ export const InlineCitationCard = (props: InlineCitationCardProps) => <HoverCard {...props} />
27
+
28
+ export type InlineCitationCardTriggerProps = ComponentProps<typeof Badge> & {
29
+ sources: string[]
30
+ }
31
+
32
+ export const InlineCitationCardTrigger = ({
33
+ sources,
34
+ className,
35
+ ...props
36
+ }: InlineCitationCardTriggerProps) => (
37
+ <HoverCardTrigger>
38
+ <Badge className={cn('ml-1 rounded-full', className)} variant="secondary" {...props}>
39
+ {sources[0] ? (
40
+ <>
41
+ {new URL(sources[0]).hostname} {sources.length > 1 && `+${sources.length - 1}`}
42
+ </>
43
+ ) : (
44
+ 'unknown'
45
+ )}
46
+ </Badge>
47
+ </HoverCardTrigger>
48
+ )
49
+
50
+ export type InlineCitationCardBodyProps = ComponentProps<'div'>
51
+
52
+ export const InlineCitationCardBody = ({ className, ...props }: InlineCitationCardBodyProps) => (
53
+ <HoverCardContent className={cn('relative w-80 p-0', className)} {...props} />
54
+ )
55
+
56
+ const CarouselApiContext = createContext<CarouselApi | undefined>(undefined)
57
+
58
+ const useCarouselApi = () => {
59
+ const context = useContext(CarouselApiContext)
60
+ return context
61
+ }
62
+
63
+ export type InlineCitationCarouselProps = ComponentProps<typeof Carousel>
64
+
65
+ export const InlineCitationCarousel = ({
66
+ className,
67
+ children,
68
+ ...props
69
+ }: InlineCitationCarouselProps) => {
70
+ const [api, setApi] = useState<CarouselApi>()
71
+
72
+ return (
73
+ <CarouselApiContext.Provider value={api}>
74
+ <Carousel className={cn('w-full', className)} setApi={setApi} {...props}>
75
+ {children}
76
+ </Carousel>
77
+ </CarouselApiContext.Provider>
78
+ )
79
+ }
80
+
81
+ export type InlineCitationCarouselContentProps = ComponentProps<'div'>
82
+
83
+ export const InlineCitationCarouselContent = (props: InlineCitationCarouselContentProps) => (
84
+ <CarouselContent {...props} />
85
+ )
86
+
87
+ export type InlineCitationCarouselItemProps = ComponentProps<'div'>
88
+
89
+ export const InlineCitationCarouselItem = ({
90
+ className,
91
+ ...props
92
+ }: InlineCitationCarouselItemProps) => (
93
+ <CarouselItem className={cn('w-full space-y-2 p-4 pl-8', className)} {...props} />
94
+ )
95
+
96
+ export type InlineCitationCarouselHeaderProps = ComponentProps<'div'>
97
+
98
+ export const InlineCitationCarouselHeader = ({
99
+ className,
100
+ ...props
101
+ }: InlineCitationCarouselHeaderProps) => (
102
+ <div
103
+ className={cn(
104
+ 'flex items-center justify-between gap-2 rounded-t-md bg-secondary p-2',
105
+ className,
106
+ )}
107
+ {...props}
108
+ />
109
+ )
110
+
111
+ export type InlineCitationCarouselIndexProps = ComponentProps<'div'>
112
+
113
+ export const InlineCitationCarouselIndex = ({
114
+ children,
115
+ className,
116
+ ...props
117
+ }: InlineCitationCarouselIndexProps) => {
118
+ const api = useCarouselApi()
119
+ const [current, setCurrent] = useState(0)
120
+ const [count, setCount] = useState(0)
121
+
122
+ const syncState = useCallback(() => {
123
+ if (!api) {
124
+ return
125
+ }
126
+ setCount(api.scrollSnapList().length)
127
+ setCurrent(api.selectedScrollSnap() + 1)
128
+ }, [api])
129
+
130
+ useEffect(() => {
131
+ if (!api) {
132
+ return
133
+ }
134
+
135
+ syncState()
136
+
137
+ api.on('select', syncState)
138
+
139
+ return () => {
140
+ api.off('select', syncState)
141
+ }
142
+ }, [api, syncState])
143
+
144
+ return (
145
+ <div
146
+ className={cn(
147
+ 'flex flex-1 items-center justify-end px-3 py-1 text-muted-foreground text-xs',
148
+ className,
149
+ )}
150
+ {...props}
151
+ >
152
+ {children ?? `${current}/${count}`}
153
+ </div>
154
+ )
155
+ }
156
+
157
+ export type InlineCitationCarouselPrevProps = ComponentProps<'button'>
158
+
159
+ export const InlineCitationCarouselPrev = ({
160
+ className,
161
+ ...props
162
+ }: InlineCitationCarouselPrevProps) => {
163
+ const api = useCarouselApi()
164
+
165
+ const handleClick = useCallback(() => {
166
+ if (api) {
167
+ api.scrollPrev()
168
+ }
169
+ }, [api])
170
+
171
+ return (
172
+ <button
173
+ aria-label="Previous"
174
+ className={cn('shrink-0', className)}
175
+ onClick={handleClick}
176
+ type="button"
177
+ {...props}
178
+ >
179
+ <ArrowLeftIcon className="size-4 text-muted-foreground" />
180
+ </button>
181
+ )
182
+ }
183
+
184
+ export type InlineCitationCarouselNextProps = ComponentProps<'button'>
185
+
186
+ export const InlineCitationCarouselNext = ({
187
+ className,
188
+ ...props
189
+ }: InlineCitationCarouselNextProps) => {
190
+ const api = useCarouselApi()
191
+
192
+ const handleClick = useCallback(() => {
193
+ if (api) {
194
+ api.scrollNext()
195
+ }
196
+ }, [api])
197
+
198
+ return (
199
+ <button
200
+ aria-label="Next"
201
+ className={cn('shrink-0', className)}
202
+ onClick={handleClick}
203
+ type="button"
204
+ {...props}
205
+ >
206
+ <ArrowRightIcon className="size-4 text-muted-foreground" />
207
+ </button>
208
+ )
209
+ }
210
+
211
+ export type InlineCitationSourceProps = ComponentProps<'div'> & {
212
+ title?: string
213
+ url?: string
214
+ description?: string
215
+ }
216
+
217
+ export const InlineCitationSource = ({
218
+ title,
219
+ url,
220
+ description,
221
+ className,
222
+ children,
223
+ ...props
224
+ }: InlineCitationSourceProps) => (
225
+ <div className={cn('space-y-1', className)} {...props}>
226
+ {title && <h4 className="truncate font-medium text-sm leading-tight">{title}</h4>}
227
+ {url && <p className="truncate break-all text-muted-foreground text-xs">{url}</p>}
228
+ {description && (
229
+ <p className="line-clamp-3 text-muted-foreground text-sm leading-relaxed">{description}</p>
230
+ )}
231
+ {children}
232
+ </div>
233
+ )
234
+
235
+ export type InlineCitationQuoteProps = ComponentProps<'blockquote'>
236
+
237
+ export const InlineCitationQuote = ({
238
+ children,
239
+ className,
240
+ ...props
241
+ }: InlineCitationQuoteProps) => (
242
+ <blockquote
243
+ className={cn('border-muted border-l-2 pl-3 text-muted-foreground text-sm italic', className)}
244
+ {...props}
245
+ >
246
+ {children}
247
+ </blockquote>
248
+ )
@@ -0,0 +1,292 @@
1
+ 'use client'
2
+
3
+ import { Button } from '../button'
4
+ import { ButtonGroup, ButtonGroupText } from '../button-group'
5
+ import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../tooltip'
6
+ import { cn } from '../../lib/utils'
7
+ import { cjk } from '@streamdown/cjk'
8
+ import { code } from '@streamdown/code'
9
+ import { math } from '@streamdown/math'
10
+ import { mermaid } from '@streamdown/mermaid'
11
+ import type { UIMessage } from 'ai'
12
+ import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react'
13
+ import type { ComponentProps, HTMLAttributes, ReactElement } from 'react'
14
+ import { createContext, memo, useCallback, useContext, useEffect, useMemo, useState } from 'react'
15
+ import { Streamdown } from 'streamdown'
16
+
17
+ export type MessageProps = HTMLAttributes<HTMLDivElement> & {
18
+ from: UIMessage['role']
19
+ }
20
+
21
+ export const Message = ({ className, from, ...props }: MessageProps) => (
22
+ <div
23
+ className={cn(
24
+ 'group flex w-full max-w-[95%] flex-col gap-2',
25
+ from === 'user' ? 'is-user ml-auto justify-end' : 'is-assistant',
26
+ className,
27
+ )}
28
+ {...props}
29
+ />
30
+ )
31
+
32
+ export type MessageContentProps = HTMLAttributes<HTMLDivElement>
33
+
34
+ export const MessageContent = ({ children, className, ...props }: MessageContentProps) => (
35
+ <div
36
+ className={cn(
37
+ 'is-user:dark flex w-fit min-w-0 max-w-full flex-col gap-2 overflow-hidden text-sm',
38
+ 'group-[.is-user]:ml-auto group-[.is-user]:rounded-lg group-[.is-user]:bg-secondary group-[.is-user]:px-4 group-[.is-user]:py-3 group-[.is-user]:text-foreground',
39
+ 'group-[.is-assistant]:text-foreground',
40
+ className,
41
+ )}
42
+ {...props}
43
+ >
44
+ {children}
45
+ </div>
46
+ )
47
+
48
+ export type MessageActionsProps = ComponentProps<'div'>
49
+
50
+ export const MessageActions = ({ className, children, ...props }: MessageActionsProps) => (
51
+ <div className={cn('flex items-center gap-1', className)} {...props}>
52
+ {children}
53
+ </div>
54
+ )
55
+
56
+ export type MessageActionProps = ComponentProps<typeof Button> & {
57
+ tooltip?: string
58
+ label?: string
59
+ }
60
+
61
+ export const MessageAction = ({
62
+ tooltip,
63
+ children,
64
+ label,
65
+ variant = 'ghost',
66
+ size = 'icon-sm',
67
+ ...props
68
+ }: MessageActionProps) => {
69
+ const button = (
70
+ <Button size={size} type="button" variant={variant} {...props}>
71
+ {children}
72
+ <span className="sr-only">{label || tooltip}</span>
73
+ </Button>
74
+ )
75
+
76
+ if (tooltip) {
77
+ return (
78
+ <TooltipProvider>
79
+ <Tooltip>
80
+ <TooltipTrigger>{button}</TooltipTrigger>
81
+ <TooltipContent>
82
+ <p>{tooltip}</p>
83
+ </TooltipContent>
84
+ </Tooltip>
85
+ </TooltipProvider>
86
+ )
87
+ }
88
+
89
+ return button
90
+ }
91
+
92
+ interface MessageBranchContextType {
93
+ currentBranch: number
94
+ totalBranches: number
95
+ goToPrevious: () => void
96
+ goToNext: () => void
97
+ branches: ReactElement[]
98
+ setBranches: (branches: ReactElement[]) => void
99
+ }
100
+
101
+ const MessageBranchContext = createContext<MessageBranchContextType | null>(null)
102
+
103
+ const useMessageBranch = () => {
104
+ const context = useContext(MessageBranchContext)
105
+
106
+ if (!context) {
107
+ throw new Error('MessageBranch components must be used within MessageBranch')
108
+ }
109
+
110
+ return context
111
+ }
112
+
113
+ export type MessageBranchProps = HTMLAttributes<HTMLDivElement> & {
114
+ defaultBranch?: number
115
+ onBranchChange?: (branchIndex: number) => void
116
+ }
117
+
118
+ export const MessageBranch = ({
119
+ defaultBranch = 0,
120
+ onBranchChange,
121
+ className,
122
+ ...props
123
+ }: MessageBranchProps) => {
124
+ const [currentBranch, setCurrentBranch] = useState(defaultBranch)
125
+ const [branches, setBranches] = useState<ReactElement[]>([])
126
+
127
+ const handleBranchChange = useCallback(
128
+ (newBranch: number) => {
129
+ setCurrentBranch(newBranch)
130
+ onBranchChange?.(newBranch)
131
+ },
132
+ [onBranchChange],
133
+ )
134
+
135
+ const goToPrevious = useCallback(() => {
136
+ const newBranch = currentBranch > 0 ? currentBranch - 1 : branches.length - 1
137
+ handleBranchChange(newBranch)
138
+ }, [currentBranch, branches.length, handleBranchChange])
139
+
140
+ const goToNext = useCallback(() => {
141
+ const newBranch = currentBranch < branches.length - 1 ? currentBranch + 1 : 0
142
+ handleBranchChange(newBranch)
143
+ }, [currentBranch, branches.length, handleBranchChange])
144
+
145
+ const contextValue = useMemo<MessageBranchContextType>(
146
+ () => ({
147
+ branches,
148
+ currentBranch,
149
+ goToNext,
150
+ goToPrevious,
151
+ setBranches,
152
+ totalBranches: branches.length,
153
+ }),
154
+ [branches, currentBranch, goToNext, goToPrevious],
155
+ )
156
+
157
+ return (
158
+ <MessageBranchContext.Provider value={contextValue}>
159
+ <div className={cn('grid w-full gap-2 [&>div]:pb-0', className)} {...props} />
160
+ </MessageBranchContext.Provider>
161
+ )
162
+ }
163
+
164
+ export type MessageBranchContentProps = HTMLAttributes<HTMLDivElement>
165
+
166
+ export const MessageBranchContent = ({ children, ...props }: MessageBranchContentProps) => {
167
+ const { currentBranch, setBranches, branches } = useMessageBranch()
168
+ const childrenArray = useMemo(() => (Array.isArray(children) ? children : [children]), [children])
169
+
170
+ // Use useEffect to update branches when they change
171
+ useEffect(() => {
172
+ if (branches.length !== childrenArray.length) {
173
+ setBranches(childrenArray)
174
+ }
175
+ }, [childrenArray, branches, setBranches])
176
+
177
+ return childrenArray.map((branch, index) => (
178
+ <div
179
+ className={cn(
180
+ 'grid gap-2 overflow-hidden [&>div]:pb-0',
181
+ index === currentBranch ? 'block' : 'hidden',
182
+ )}
183
+ key={branch.key}
184
+ {...props}
185
+ >
186
+ {branch}
187
+ </div>
188
+ ))
189
+ }
190
+
191
+ export type MessageBranchSelectorProps = ComponentProps<typeof ButtonGroup>
192
+
193
+ export const MessageBranchSelector = ({ className, ...props }: MessageBranchSelectorProps) => {
194
+ const { totalBranches } = useMessageBranch()
195
+
196
+ // Don't render if there's only one branch
197
+ if (totalBranches <= 1) {
198
+ return null
199
+ }
200
+
201
+ return (
202
+ <ButtonGroup
203
+ className={cn(
204
+ '[&>*:not(:first-child)]:rounded-l-md [&>*:not(:last-child)]:rounded-r-md',
205
+ className,
206
+ )}
207
+ orientation="horizontal"
208
+ {...props}
209
+ />
210
+ )
211
+ }
212
+
213
+ export type MessageBranchPreviousProps = ComponentProps<typeof Button>
214
+
215
+ export const MessageBranchPrevious = ({ children, ...props }: MessageBranchPreviousProps) => {
216
+ const { goToPrevious, totalBranches } = useMessageBranch()
217
+
218
+ return (
219
+ <Button
220
+ aria-label="Previous branch"
221
+ disabled={totalBranches <= 1}
222
+ onClick={goToPrevious}
223
+ size="icon-sm"
224
+ type="button"
225
+ variant="ghost"
226
+ {...props}
227
+ >
228
+ {children ?? <ChevronLeftIcon size={14} />}
229
+ </Button>
230
+ )
231
+ }
232
+
233
+ export type MessageBranchNextProps = ComponentProps<typeof Button>
234
+
235
+ export const MessageBranchNext = ({ children, ...props }: MessageBranchNextProps) => {
236
+ const { goToNext, totalBranches } = useMessageBranch()
237
+
238
+ return (
239
+ <Button
240
+ aria-label="Next branch"
241
+ disabled={totalBranches <= 1}
242
+ onClick={goToNext}
243
+ size="icon-sm"
244
+ type="button"
245
+ variant="ghost"
246
+ {...props}
247
+ >
248
+ {children ?? <ChevronRightIcon size={14} />}
249
+ </Button>
250
+ )
251
+ }
252
+
253
+ export type MessageBranchPageProps = HTMLAttributes<HTMLSpanElement>
254
+
255
+ export const MessageBranchPage = ({ className, ...props }: MessageBranchPageProps) => {
256
+ const { currentBranch, totalBranches } = useMessageBranch()
257
+
258
+ return (
259
+ <ButtonGroupText
260
+ className={cn('border-none bg-transparent text-muted-foreground shadow-none', className)}
261
+ {...props}
262
+ >
263
+ {currentBranch + 1} of {totalBranches}
264
+ </ButtonGroupText>
265
+ )
266
+ }
267
+
268
+ export type MessageResponseProps = ComponentProps<typeof Streamdown>
269
+
270
+ const streamdownPlugins = { cjk, code, math, mermaid }
271
+
272
+ export const MessageResponse = memo(
273
+ ({ className, ...props }: MessageResponseProps) => (
274
+ <Streamdown
275
+ className={cn('size-full [&>*:first-child]:mt-0 [&>*:last-child]:mb-0', className)}
276
+ plugins={streamdownPlugins}
277
+ {...props}
278
+ />
279
+ ),
280
+ (prevProps, nextProps) =>
281
+ prevProps.children === nextProps.children && nextProps.isAnimating === prevProps.isAnimating,
282
+ )
283
+
284
+ MessageResponse.displayName = 'MessageResponse'
285
+
286
+ export type MessageToolbarProps = ComponentProps<'div'>
287
+
288
+ export const MessageToolbar = ({ className, children, ...props }: MessageToolbarProps) => (
289
+ <div className={cn('mt-4 flex w-full items-center justify-between gap-4', className)} {...props}>
290
+ {children}
291
+ </div>
292
+ )