AI Response Stream & Code Block
Terminal-like AI code stream card featuring branch revision navigation, instant copy with tactile haptics, and response rating morphs.
Preview and code
/*
* MIT License
*
* Copyright (c) 2026 Syed Subhan Uddin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// AI Response Stream & Code Block from amicro by Syed Subhan Uddin
// https://github.com/Subhan-code/Amicro--Micro-transitions-/blob/main/src/components/ai/AiResponseStream.tsx
// MIT License, Copyright (c) 2026 Syed Subhan Uddin. Full licence: https://github.com/Subhan-code/Amicro--Micro-transitions-/blob/main/LICENSE
// Styled with Tailwind CSS. Needs: motion, lucide-react
import React, { useState } from 'react';
import { motion, AnimatePresence } from 'motion/react';
import { Copy, Check, RotateCw, ThumbsUp, ThumbsDown, ChevronLeft, ChevronRight, Code2 } from 'lucide-react';
import { useWebHaptics } from './use-web-haptics';
export interface AiResponseStreamProps {
theme?: 'dark' | 'light';
title?: string;
codeSnippet?: string;
explanation?: string;
}
const DEFAULT_CODE = `import { motion } from "motion/react";
export function SpringCard() {
return (
<motion.div
whileHover={{ y: -4, scale: 1.02 }}
whileTap={{ scale: 0.98 }}
transition={{ type: "spring", stiffness: 400, damping: 28 }}
className="p-6 rounded-2xl bg-zinc-900 border border-white/10 shadow-xl"
>
<h3 className="text-white font-semibold">Fluid Motion Card</h3>
<p className="text-zinc-400 text-sm mt-1">Zero layout shifts, pure 60fps spring responsiveness.</p>
</motion.div>
);
}`;
export function AiResponseStream({
theme = 'dark',
title = "Generated Spring Component",
codeSnippet = DEFAULT_CODE,
explanation = "Here is the verified motion component utilizing cubic-spring damping physics. It compiles cleanly with Tailwind CSS v4 and React 19.",
}: AiResponseStreamProps) {
const [copied, setCopied] = useState(false);
const [isRotating, setIsRotating] = useState(false);
const [vote, setVote] = useState<'up' | 'down' | null>(null);
const [branchIndex, setBranchIndex] = useState(1);
const totalBranches = 3;
const { trigger: triggerHaptic } = useWebHaptics();
const isDark = theme === 'dark';
const handleCopy = () => {
triggerHaptic('success');
navigator.clipboard.writeText(codeSnippet);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
const handleRegenerate = () => {
triggerHaptic('medium');
setIsRotating(true);
setTimeout(() => setIsRotating(false), 800);
};
const handleVote = (type: 'up' | 'down') => {
triggerHaptic('light');
setVote(prev => prev === type ? null : type);
};
const handlePrevBranch = () => {
triggerHaptic('light');
setBranchIndex(prev => (prev > 1 ? prev - 1 : totalBranches));
};
const handleNextBranch = () => {
triggerHaptic('light');
setBranchIndex(prev => (prev < totalBranches ? prev + 1 : 1));
};
return (
<div className="w-full max-w-[800px] mx-auto select-none">
<div
className={`rounded-2xl border transition-all duration-200 overflow-hidden shadow-md ${
isDark
? 'bg-[#141417] border-white/[0.08] text-white shadow-black/40'
: 'bg-white border-neutral-200 text-black shadow-neutral-200/50'
}`}
>
{/* Explanation text */}
<div className="p-4 sm:p-5 pb-3">
<p className={`text-[13.5px] sm:text-[14px] leading-relaxed m-0 ${isDark ? 'text-neutral-300' : 'text-neutral-700'}`}>
{explanation}
</p>
</div>
{/* Code Snippet Box */}
<div className="px-4 sm:px-5 pb-4">
<div className={`rounded-xl border overflow-hidden ${
isDark ? 'bg-[#0d0d0f] border-white/[0.06]' : 'bg-neutral-50 border-neutral-200'
}`}>
{/* Code Header Bar */}
<div className={`flex items-center justify-between px-3.5 py-2 border-b text-xs ${
isDark ? 'border-white/[0.06] text-neutral-400 bg-white/[0.02]' : 'border-neutral-200 text-neutral-600 bg-neutral-100/50'
}`}>
<div className="flex items-center gap-2">
<Code2 className="w-3.5 h-3.5 text-neutral-500" />
<span className="font-mono text-[11px] font-medium">SpringCard.tsx</span>
</div>
<button
type="button"
onClick={handleCopy}
className={`flex items-center gap-1.5 px-2 py-1 rounded-md cursor-pointer border-0 transition-colors text-xs ${
isDark ? 'hover:bg-white/10 text-neutral-300' : 'hover:bg-neutral-200 text-neutral-700'
}`}
>
{copied ? <Check className="w-3 h-3 text-emerald-500" /> : <Copy className="w-3 h-3" />}
<span className="text-[11px]">{copied ? 'Copied' : 'Copy'}</span>
</button>
</div>
{/* Code Content */}
<pre className="p-3.5 sm:p-4 text-xs font-mono leading-relaxed overflow-x-auto m-0 text-neutral-300">
<code>{codeSnippet}</code>
</pre>
</div>
</div>
{/* Micro-Action Response Footer Bar */}
<div className={`flex items-center justify-between px-4 sm:px-5 py-2.5 border-t text-xs ${
isDark ? 'border-white/[0.06] bg-white/[0.01]' : 'border-neutral-100 bg-neutral-50/50'
}`}>
{/* Left: Branch Version Switcher (< 1 of 3 >) */}
<div className="flex items-center gap-1.5">
<button
type="button"
onClick={handlePrevBranch}
className={`p-1 rounded-md cursor-pointer border-0 transition-colors ${
isDark ? 'hover:bg-white/10 text-neutral-400' : 'hover:bg-neutral-200 text-neutral-600'
}`}
title="Previous variation"
>
<ChevronLeft className="w-3.5 h-3.5" />
</button>
<span className="font-mono text-[11px] text-neutral-500">
{branchIndex} / {totalBranches}
</span>
<button
type="button"
onClick={handleNextBranch}
className={`p-1 rounded-md cursor-pointer border-0 transition-colors ${
isDark ? 'hover:bg-white/10 text-neutral-400' : 'hover:bg-neutral-200 text-neutral-600'
}`}
title="Next variation"
>
<ChevronRight className="w-3.5 h-3.5" />
</button>
</div>
{/* Right: Actions (Copy, Regenerate, Upvote, Downvote) */}
<div className="flex items-center gap-1">
{/* Copy Button */}
<motion.button
type="button"
whileTap={{ scale: 0.9 }}
onClick={handleCopy}
className={`p-1.5 rounded-lg cursor-pointer border-0 transition-colors ${
isDark ? 'hover:bg-white/10 text-neutral-400 hover:text-white' : 'hover:bg-neutral-200 text-neutral-600'
}`}
title="Copy output"
>
{copied ? <Check className="w-3.5 h-3.5 text-emerald-500" /> : <Copy className="w-3.5 h-3.5" />}
</motion.button>
{/* Regenerate Button */}
<motion.button
type="button"
whileTap={{ scale: 0.9 }}
onClick={handleRegenerate}
className={`p-1.5 rounded-lg cursor-pointer border-0 transition-colors ${
isDark ? 'hover:bg-white/10 text-neutral-400 hover:text-white' : 'hover:bg-neutral-200 text-neutral-600'
}`}
title="Regenerate alternative"
>
<motion.div
animate={isRotating ? { rotate: 360 } : { rotate: 0 }}
transition={{ duration: 0.25, ease: 'easeInOut' }}
>
<RotateCw className="w-3.5 h-3.5" />
</motion.div>
</motion.button>
{/* Thumbs Up */}
<motion.button
type="button"
whileTap={{ scale: 0.88 }}
onClick={() => handleVote('up')}
className={`p-1.5 rounded-lg cursor-pointer border-0 transition-colors ${
vote === 'up'
? 'bg-emerald-500/20 text-emerald-400'
: isDark ? 'hover:bg-white/10 text-neutral-400 hover:text-white' : 'hover:bg-neutral-200 text-neutral-600'
}`}
title="Helpful output"
>
<ThumbsUp className="w-3.5 h-3.5" />
</motion.button>
{/* Thumbs Down */}
<motion.button
type="button"
whileTap={{ scale: 0.88 }}
onClick={() => handleVote('down')}
className={`p-1.5 rounded-lg cursor-pointer border-0 transition-colors ${
vote === 'down'
? 'bg-rose-500/20 text-rose-400'
: isDark ? 'hover:bg-white/10 text-neutral-400 hover:text-white' : 'hover:bg-neutral-200 text-neutral-600'
}`}
title="Not what I needed"
>
<ThumbsDown className="w-3.5 h-3.5" />
</motion.button>
</div>
</div>
</div>
</div>
);
}
/*
* MIT License
*
* Copyright (c) 2026 Syed Subhan Uddin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { useCallback, useState, useEffect } from 'react';
export type HapticsType = 'light' | 'medium' | 'heavy' | 'success' | 'warning' | 'error';
let globalAudioCtx: AudioContext | null = null;
let soundEnabled = true;
if (typeof window !== 'undefined') {
try {
const saved = localStorage.getItem('amicro_sound_enabled');
soundEnabled = saved !== null ? saved === 'true' : true;
} catch {
soundEnabled = true;
}
}
function getAudioContext(): AudioContext | null {
if (typeof window === 'undefined') return null;
try {
if (!globalAudioCtx) {
const AudioCtx = window.AudioContext || (window as unknown as { webkitAudioContext: typeof AudioContext }).webkitAudioContext;
if (AudioCtx) {
globalAudioCtx = new AudioCtx();
}
}
if (globalAudioCtx && globalAudioCtx.state === 'suspended') {
globalAudioCtx.resume();
}
return globalAudioCtx;
} catch {
return null;
}
}
function playSynthesizedHaptic(type: HapticsType) {
if (!soundEnabled) return;
const ctx = getAudioContext();
if (!ctx) return;
try {
const now = ctx.currentTime;
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.connect(gain);
gain.connect(ctx.destination);
if (type === 'success') {
// Pleasant subtle two-tone chime for copy actions
osc.type = 'sine';
osc.frequency.setValueAtTime(820, now);
osc.frequency.exponentialRampToValueAtTime(1240, now + 0.06);
gain.gain.setValueAtTime(0.035, now);
gain.gain.exponentialRampToValueAtTime(0.0001, now + 0.14);
osc.start(now);
osc.stop(now + 0.14);
} else if (type === 'error') {
osc.type = 'sawtooth';
osc.frequency.setValueAtTime(220, now);
gain.gain.setValueAtTime(0.04, now);
gain.gain.exponentialRampToValueAtTime(0.0001, now + 0.08);
osc.start(now);
osc.stop(now + 0.08);
} else {
// Soft mechanical tactile click
osc.type = 'triangle';
const freq = type === 'medium' ? 1400 : type === 'heavy' ? 1100 : 1750;
osc.frequency.setValueAtTime(freq, now);
osc.frequency.exponentialRampToValueAtTime(freq * 0.5, now + 0.02);
gain.gain.setValueAtTime(0.025, now);
gain.gain.exponentialRampToValueAtTime(0.0001, now + 0.025);
osc.start(now);
osc.stop(now + 0.025);
}
} catch {
// Non-blocking fallback
}
}
export function useWebHaptics() {
const [isSoundOn, setIsSoundOn] = useState(soundEnabled);
useEffect(() => {
setIsSoundOn(soundEnabled);
}, []);
const toggleSound = useCallback(() => {
soundEnabled = !soundEnabled;
setIsSoundOn(soundEnabled);
try {
localStorage.setItem('amicro_sound_enabled', String(soundEnabled));
} catch {}
if (soundEnabled) {
playSynthesizedHaptic('light');
}
}, []);
const trigger = useCallback((type: HapticsType = 'light') => {
// 1. Play synthesized tactile audio
playSynthesizedHaptic(type);
// 2. Trigger hardware vibration on mobile if supported
if (typeof window !== 'undefined' && window.navigator && window.navigator.vibrate) {
try {
switch (type) {
case 'light':
return window.navigator.vibrate(10);
case 'medium':
return window.navigator.vibrate(25);
case 'heavy':
return window.navigator.vibrate(50);
case 'success':
return window.navigator.vibrate([15, 60, 15]);
case 'warning':
return window.navigator.vibrate([30, 60, 30]);
case 'error':
return window.navigator.vibrate([60, 60, 60, 60, 60]);
default:
return window.navigator.vibrate(10);
}
} catch {
return false;
}
}
return true;
}, []);
return { trigger, isSoundOn, toggleSound };
}
Needs: motion
lucide-react
react tailwindcss
Install
npx shadcn@latest add https://ownlyplans.com/r/bhaskar/ai-response-stream.json
Adds the React files to components/ownlyplans/ in a project set up with shadcn and Tailwind CSS. Or copy them from the Code tab.
About
Terminal-like AI code stream card featuring branch revision navigation, instant copy with tactile haptics, and response rating morphs.
A React AI interface piece built with Motion and Tailwind CSS. Open the Preview tab to try it, then copy the code from the Code tab or install it with the shadcn command. Hand this page’s link to Claude or any AI tool and it can read the code straight from the page.
Credit: from the amicro library by Syed Subhan Uddin (https://github.com/Subhan-code/Amicro–Micro-transitions-), shared here under the MIT licence.
MIT License
Copyright (c) 2026 Syed Subhan Uddin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.