@istmx/ui 0.1.9 → 0.1.11
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.
- package/LICENSE +21 -0
- package/README.md +64 -0
- package/bin/istmx.mjs +50 -11
- package/package.json +1 -1
- package/registry/ai-chat-block/ai-chat-block.tsx +460 -0
- package/registry/ai-chat-block/ai-chat-input.tsx +694 -0
- package/registry/ai-chat-block/attachment.tsx +466 -0
- package/registry/ai-chat-block/chat-code-block.tsx +50 -0
- package/registry/ai-chat-block/chat-composer.tsx +51 -0
- package/registry/ai-chat-block/chat-logo.tsx +18 -0
- package/registry/ai-chat-block/chat-message.tsx +161 -0
- package/registry/ai-chat-block/chat-sidebar.tsx +273 -0
- package/registry/ai-chat-block/chat-user-avatar.tsx +9 -0
- package/registry/ai-chat-block/code-highlighter.tsx +357 -0
- package/registry/ai-chat-block/default-attachments.tsx +42 -0
- package/registry/ai-chat-block/index.ts +30 -0
- package/registry/ai-chat-block/message-content.tsx +287 -0
- package/registry/ai-chat-block/message-list.tsx +93 -0
- package/registry/ai-chat-block/send-button.tsx +102 -0
- package/registry/ai-chat-block/types.ts +71 -0
- package/registry/ai-chat-block/utils.ts +6 -0
- package/registry/ai-chat-block/voice-button.tsx +66 -0
- package/registry/bento-grid.tsx +433 -0
- package/registry/scroll-parallax-hero.tsx +565 -0
- package/registry/streaming-text.tsx +278 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
4
|
+
import type { ReactNode } from 'react'
|
|
5
|
+
import { motion, useReducedMotion } from 'motion/react'
|
|
6
|
+
import { cn } from '@/lib/utils'
|
|
7
|
+
|
|
8
|
+
export type StreamingTextEffect = 'fade' | 'blur' | 'slide' | 'wave'
|
|
9
|
+
|
|
10
|
+
export type StreamingTextProps = {
|
|
11
|
+
/** The string to stream, as if the model is still writing it. */
|
|
12
|
+
text: string
|
|
13
|
+
/** Characters revealed per second. Defaults to 34. */
|
|
14
|
+
speed?: number
|
|
15
|
+
/** Delay in milliseconds before streaming starts. Defaults to 0. */
|
|
16
|
+
startDelay?: number
|
|
17
|
+
/** How each new character enters. Defaults to blur. */
|
|
18
|
+
effect?: StreamingTextEffect
|
|
19
|
+
/** Time in seconds for a single character to settle. Defaults to 0.34. */
|
|
20
|
+
glyphDuration?: number
|
|
21
|
+
/** Blur radius used by the blur effect. Defaults to 6. */
|
|
22
|
+
blurAmount?: number
|
|
23
|
+
/** Rise distance used by the slide, blur, and wave effects. Defaults to 6. */
|
|
24
|
+
slideDistance?: number
|
|
25
|
+
/** Show the blinking caret while streaming. Defaults to true. */
|
|
26
|
+
cursor?: boolean
|
|
27
|
+
/** Keep the caret blinking after the string finishes. Defaults to false. */
|
|
28
|
+
cursorAfterComplete?: boolean
|
|
29
|
+
/** Restart the stream after the string finishes. Defaults to false. */
|
|
30
|
+
loop?: boolean
|
|
31
|
+
/** Pause in milliseconds between loop cycles. Defaults to 1200. */
|
|
32
|
+
loopDelay?: number
|
|
33
|
+
/** Called once the full string has been revealed. */
|
|
34
|
+
onComplete?: () => void
|
|
35
|
+
className?: string
|
|
36
|
+
textClassName?: string
|
|
37
|
+
cursorClassName?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const WHITESPACE = /^\s$/u
|
|
41
|
+
|
|
42
|
+
function splitGlyphs(value: string): string[] {
|
|
43
|
+
if (typeof Intl !== 'undefined' && 'Segmenter' in Intl) {
|
|
44
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' })
|
|
45
|
+
return Array.from(segmenter.segment(value), (part) => part.segment)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return Array.from(value)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type GlyphGroup = {
|
|
52
|
+
start: number
|
|
53
|
+
space: boolean
|
|
54
|
+
glyphs: string[]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function groupGlyphs(glyphs: string[]): GlyphGroup[] {
|
|
58
|
+
const groups: GlyphGroup[] = []
|
|
59
|
+
|
|
60
|
+
glyphs.forEach((glyph, index) => {
|
|
61
|
+
const space = WHITESPACE.test(glyph)
|
|
62
|
+
const last = groups[groups.length - 1]
|
|
63
|
+
|
|
64
|
+
if (last && last.space === space) {
|
|
65
|
+
last.glyphs.push(glyph)
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
groups.push({ start: index, space, glyphs: [glyph] })
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
return groups
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function glyphStates(
|
|
76
|
+
effect: StreamingTextEffect,
|
|
77
|
+
blurAmount: number,
|
|
78
|
+
slideDistance: number,
|
|
79
|
+
) {
|
|
80
|
+
switch (effect) {
|
|
81
|
+
case 'fade':
|
|
82
|
+
return { initial: { opacity: 0 }, animate: { opacity: 1 } }
|
|
83
|
+
case 'slide':
|
|
84
|
+
return {
|
|
85
|
+
initial: { opacity: 0, y: slideDistance },
|
|
86
|
+
animate: { opacity: 1, y: 0 },
|
|
87
|
+
}
|
|
88
|
+
case 'wave':
|
|
89
|
+
return {
|
|
90
|
+
initial: { opacity: 0, y: slideDistance, scale: 0.94 },
|
|
91
|
+
animate: { opacity: 1, y: 0, scale: 1 },
|
|
92
|
+
}
|
|
93
|
+
case 'blur':
|
|
94
|
+
default:
|
|
95
|
+
return {
|
|
96
|
+
initial: {
|
|
97
|
+
opacity: 0,
|
|
98
|
+
y: slideDistance * 0.5,
|
|
99
|
+
filter: `blur(${blurAmount}px)`,
|
|
100
|
+
},
|
|
101
|
+
animate: { opacity: 1, y: 0, filter: 'blur(0px)' },
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function StreamingText({
|
|
107
|
+
text,
|
|
108
|
+
speed = 34,
|
|
109
|
+
startDelay = 0,
|
|
110
|
+
effect = 'blur',
|
|
111
|
+
glyphDuration = 0.34,
|
|
112
|
+
blurAmount = 6,
|
|
113
|
+
slideDistance = 6,
|
|
114
|
+
cursor = true,
|
|
115
|
+
cursorAfterComplete = false,
|
|
116
|
+
loop = false,
|
|
117
|
+
loopDelay = 1200,
|
|
118
|
+
onComplete,
|
|
119
|
+
className,
|
|
120
|
+
textClassName,
|
|
121
|
+
cursorClassName,
|
|
122
|
+
}: StreamingTextProps) {
|
|
123
|
+
const shouldReduceMotion = useReducedMotion()
|
|
124
|
+
const glyphs = useMemo(() => splitGlyphs(text), [text])
|
|
125
|
+
const [revealed, setRevealed] = useState(0)
|
|
126
|
+
const [cycle, setCycle] = useState(0)
|
|
127
|
+
const onCompleteRef = useRef(onComplete)
|
|
128
|
+
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
onCompleteRef.current = onComplete
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
let frame = 0
|
|
135
|
+
let timeout = 0
|
|
136
|
+
|
|
137
|
+
const cancel = () => {
|
|
138
|
+
window.cancelAnimationFrame(frame)
|
|
139
|
+
window.clearTimeout(timeout)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (shouldReduceMotion) {
|
|
143
|
+
queueMicrotask(() => {
|
|
144
|
+
setRevealed(glyphs.length)
|
|
145
|
+
})
|
|
146
|
+
return cancel
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
queueMicrotask(() => {
|
|
150
|
+
setRevealed(0)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
let startedAt: number | null = null
|
|
154
|
+
const perGlyph = 1000 / Math.max(1, speed)
|
|
155
|
+
const delay = Math.max(0, startDelay)
|
|
156
|
+
const pause = Math.max(0, loopDelay)
|
|
157
|
+
|
|
158
|
+
const step = (now: number) => {
|
|
159
|
+
if (startedAt === null) startedAt = now
|
|
160
|
+
|
|
161
|
+
const elapsed = now - startedAt - delay
|
|
162
|
+
|
|
163
|
+
if (elapsed < 0) {
|
|
164
|
+
frame = window.requestAnimationFrame(step)
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const count = Math.min(glyphs.length, Math.floor(elapsed / perGlyph) + 1)
|
|
169
|
+
setRevealed(count)
|
|
170
|
+
|
|
171
|
+
if (count < glyphs.length) {
|
|
172
|
+
frame = window.requestAnimationFrame(step)
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
onCompleteRef.current?.()
|
|
177
|
+
|
|
178
|
+
if (loop) {
|
|
179
|
+
timeout = window.setTimeout(() => {
|
|
180
|
+
setCycle((value) => value + 1)
|
|
181
|
+
}, pause)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
frame = window.requestAnimationFrame(step)
|
|
186
|
+
|
|
187
|
+
return cancel
|
|
188
|
+
}, [cycle, glyphs, loop, loopDelay, shouldReduceMotion, speed, startDelay])
|
|
189
|
+
|
|
190
|
+
const visibleGlyphs = useMemo(
|
|
191
|
+
() => glyphs.slice(0, Math.max(0, revealed)),
|
|
192
|
+
[glyphs, revealed],
|
|
193
|
+
)
|
|
194
|
+
const groups = useMemo(() => groupGlyphs(visibleGlyphs), [visibleGlyphs])
|
|
195
|
+
const streaming = revealed < glyphs.length
|
|
196
|
+
const showCursor = cursor && (streaming || cursorAfterComplete)
|
|
197
|
+
const states = glyphStates(effect, blurAmount, slideDistance)
|
|
198
|
+
|
|
199
|
+
const content: ReactNode[] = groups.map((group) => {
|
|
200
|
+
if (group.space) {
|
|
201
|
+
return (
|
|
202
|
+
<span key={`space-${group.start}`} className="whitespace-pre">
|
|
203
|
+
{group.glyphs.join('')}
|
|
204
|
+
</span>
|
|
205
|
+
)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return (
|
|
209
|
+
<span
|
|
210
|
+
key={`word-${group.start}`}
|
|
211
|
+
className="inline-block whitespace-nowrap"
|
|
212
|
+
>
|
|
213
|
+
{group.glyphs.map((glyph, offset) => {
|
|
214
|
+
const index = group.start + offset
|
|
215
|
+
|
|
216
|
+
return (
|
|
217
|
+
<motion.span
|
|
218
|
+
key={index}
|
|
219
|
+
initial={shouldReduceMotion ? false : states.initial}
|
|
220
|
+
animate={states.animate}
|
|
221
|
+
transition={
|
|
222
|
+
shouldReduceMotion
|
|
223
|
+
? { duration: 0 }
|
|
224
|
+
: effect === 'wave'
|
|
225
|
+
? { type: 'spring', stiffness: 360, damping: 24, mass: 0.6 }
|
|
226
|
+
: {
|
|
227
|
+
duration: Math.max(0.05, glyphDuration),
|
|
228
|
+
ease: 'easeOut',
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
className="inline-block"
|
|
232
|
+
>
|
|
233
|
+
{glyph}
|
|
234
|
+
</motion.span>
|
|
235
|
+
)
|
|
236
|
+
})}
|
|
237
|
+
</span>
|
|
238
|
+
)
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
return (
|
|
242
|
+
<span
|
|
243
|
+
role="status"
|
|
244
|
+
aria-live="polite"
|
|
245
|
+
aria-atomic="true"
|
|
246
|
+
data-state={streaming ? 'streaming' : 'complete'}
|
|
247
|
+
className={cn('relative inline', className)}
|
|
248
|
+
>
|
|
249
|
+
<span
|
|
250
|
+
aria-hidden="true"
|
|
251
|
+
className={cn('whitespace-pre-wrap', textClassName)}
|
|
252
|
+
>
|
|
253
|
+
{content}
|
|
254
|
+
{showCursor ? (
|
|
255
|
+
<motion.span
|
|
256
|
+
aria-hidden="true"
|
|
257
|
+
initial={{ opacity: 1 }}
|
|
258
|
+
animate={
|
|
259
|
+
shouldReduceMotion ? { opacity: 1 } : { opacity: [1, 0.15, 1] }
|
|
260
|
+
}
|
|
261
|
+
transition={
|
|
262
|
+
shouldReduceMotion
|
|
263
|
+
? { duration: 0 }
|
|
264
|
+
: { duration: 1, ease: 'easeInOut', repeat: Infinity }
|
|
265
|
+
}
|
|
266
|
+
className={cn(
|
|
267
|
+
'bg-foreground ml-0.5 inline-block h-[0.95em] w-[2px] translate-y-[0.1em] rounded-full align-middle',
|
|
268
|
+
cursorClassName,
|
|
269
|
+
)}
|
|
270
|
+
/>
|
|
271
|
+
) : null}
|
|
272
|
+
</span>
|
|
273
|
+
<span className="sr-only">{text}</span>
|
|
274
|
+
</span>
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export default StreamingText
|