Streaming Chat Response
An assistant reply that streams in word by word with a caret, a Stop button and a clear finished state.
AI prompt
Build a chat card 320x220px with a scrolling message area and a composer (text input and a 40px round send button). Pressing Enter or the button adds the user's message as a right-aligned dark bubble (fade and rise 6px in 180ms) and starts a simulated assistant reply as a left-aligned light bubble. Stream a prepared paragraph word by word every 45ms with a thin blinking caret at the end; set aria-busy true on the message list while streaming and, when finished, remove the caret and announce "Response complete" in a visually hidden live region. While streaming the send button becomes a Stop button (square icon, aria-label "Stop generating"); pressing it ends the stream and appends a small "Stopped" label. Disable sending while streaming. Auto-scroll to the bottom unless the user scrolled up. Under prefers-reduced-motion show the full answer after 300ms with no caret blink.About this pattern
Definition
A chat exchange where the answer appears progressively as it is generated, instead of after a long wait. A Stop control lets the user cancel, and the interface clearly shows when the answer is complete.
Purpose & UX principles
- Streaming makes waiting feel shorter and lets people start reading early.
- A Stop button gives control; the Send button turns into Stop while generating and back afterwards.
- Screen readers should not hear every token: the message region is marked busy while streaming and the full answer is announced once at the end.
Interaction states
- Idle, sending (user bubble appears), streaming (caret, Stop), stopped (note "Stopped"), complete.
Motion specification
- Words arrive every 45ms; the caret blinks at 1s intervals; new bubbles fade and rise 6px in 180ms.
- Reduced motion: the whole answer appears at once after a short pause and the caret does not blink.
Responsive behaviour
- The message list scrolls and keeps the latest text in view unless the user scrolled up on purpose.
- The composer is a single row with a 44px button on touch.
Code
<div class="chat"><div class="msgs" id="msgs" aria-live="off" role="log" aria-label="Conversation"></div>
<form class="comp" id="f"><input id="in" type="text" placeholder="Ask anything…" aria-label="Message" autocomplete="off"><button id="go" aria-label="Send">↑</button></form><span class="sr" id="sr" aria-live="polite"></span></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.chat{width:min(320px,100%);height:230px;display:flex;flex-direction:column;border:1px solid var(--line);border-radius:14px;background:var(--surface);overflow:hidden}
.msgs{flex:1;overflow:auto;padding:12px;display:flex;flex-direction:column;gap:8px}
.m{max-width:86%;padding:8px 12px;border-radius:14px;font-size:13px;line-height:1.5;animation:in .18s ease-out}
.m.u{align-self:flex-end;background:#171717;color:#fff;border-bottom-right-radius:4px}
.m.a{align-self:flex-start;background:var(--bg);border-bottom-left-radius:4px}
.m.a.live::after{content:"";display:inline-block;width:2px;height:1em;margin-left:2px;vertical-align:-2px;background:#171717;animation:bl 1s steps(2) infinite}
.m small{display:block;margin-top:4px;font-size:11px;color:var(--muted)}
@keyframes in{from{opacity:0;transform:translateY(6px)}}
@keyframes bl{50%{opacity:0}}
.comp{display:flex;gap:8px;padding:8px;border-top:1px solid var(--line)}
.comp input{flex:1;height:40px;padding:0 14px;border:1px solid var(--line);border-radius:99px;background:var(--surface);font:inherit;font-size:13px;outline:none}
.comp input:focus{border-color:var(--text)}
.comp button{width:40px;height:40px;border-radius:50%;border:0;background:var(--accent);color:var(--accent-ink);font:inherit;font-size:16px;cursor:pointer}
.comp button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
@media (prefers-reduced-motion:reduce){.m{animation:none}.m.a.live::after{animation:none}}const msgs=document.getElementById('msgs'),f=document.getElementById('f'),inp=document.getElementById('in'),go=document.getElementById('go'),sr=document.getElementById('sr');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
const ANSWER='Good interfaces answer quickly and stay honest about what is happening. Show progress as soon as you have it, let people stop what they started, and mark clearly when the work is finished.'.split(' ');
let timer=0,busy=false,bubble=null;
const stick=()=>{const near=msgs.scrollHeight-msgs.scrollTop-msgs.clientHeight<40;return()=>{if(near)msgs.scrollTop=msgs.scrollHeight;};};
function add(cls,txt){const d=document.createElement('div');d.className='m '+cls;d.textContent=txt;msgs.append(d);msgs.scrollTop=msgs.scrollHeight;return d;}
function setBusy(b){busy=b;msgs.setAttribute('aria-busy',String(b));go.textContent=b?'■':'↑';go.setAttribute('aria-label',b?'Stop generating':'Send');}
function finish(stopped){clearInterval(timer);bubble.classList.remove('live');if(stopped){const s=document.createElement('small');s.textContent='Stopped';bubble.append(s);}setBusy(false);sr.textContent=stopped?'Stopped':'Response complete';}
f.addEventListener('submit',e=>{e.preventDefault();if(busy){finish(true);return;}
const t=inp.value.trim()||'How should an interface handle waiting?';inp.value='';add('u',t);sr.textContent='';bubble=add('a live','');setBusy(true);
if(reduce){setTimeout(()=>{bubble.textContent=ANSWER.join(' ');finish(false);},300);return;}
let i=0;timer=setInterval(()=>{const k=stick();bubble.textContent=ANSWER.slice(0,++i).join(' ');k();if(i>=ANSWER.length)finish(false);},45);});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Streaming Chat Response — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.chat{width:min(320px,100%);height:230px;display:flex;flex-direction:column;border:1px solid var(--line);border-radius:14px;background:var(--surface);overflow:hidden}
.msgs{flex:1;overflow:auto;padding:12px;display:flex;flex-direction:column;gap:8px}
.m{max-width:86%;padding:8px 12px;border-radius:14px;font-size:13px;line-height:1.5;animation:in .18s ease-out}
.m.u{align-self:flex-end;background:#171717;color:#fff;border-bottom-right-radius:4px}
.m.a{align-self:flex-start;background:var(--bg);border-bottom-left-radius:4px}
.m.a.live::after{content:"";display:inline-block;width:2px;height:1em;margin-left:2px;vertical-align:-2px;background:#171717;animation:bl 1s steps(2) infinite}
.m small{display:block;margin-top:4px;font-size:11px;color:var(--muted)}
@keyframes in{from{opacity:0;transform:translateY(6px)}}
@keyframes bl{50%{opacity:0}}
.comp{display:flex;gap:8px;padding:8px;border-top:1px solid var(--line)}
.comp input{flex:1;height:40px;padding:0 14px;border:1px solid var(--line);border-radius:99px;background:var(--surface);font:inherit;font-size:13px;outline:none}
.comp input:focus{border-color:var(--text)}
.comp button{width:40px;height:40px;border-radius:50%;border:0;background:var(--accent);color:var(--accent-ink);font:inherit;font-size:16px;cursor:pointer}
.comp button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
@media (prefers-reduced-motion:reduce){.m{animation:none}.m.a.live::after{animation:none}}
</style></head>
<body><div class="stage">
<div class="chat"><div class="msgs" id="msgs" aria-live="off" role="log" aria-label="Conversation"></div>
<form class="comp" id="f"><input id="in" type="text" placeholder="Ask anything…" aria-label="Message" autocomplete="off"><button id="go" aria-label="Send">↑</button></form><span class="sr" id="sr" aria-live="polite"></span></div>
</div>
<script>
const msgs=document.getElementById('msgs'),f=document.getElementById('f'),inp=document.getElementById('in'),go=document.getElementById('go'),sr=document.getElementById('sr');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
const ANSWER='Good interfaces answer quickly and stay honest about what is happening. Show progress as soon as you have it, let people stop what they started, and mark clearly when the work is finished.'.split(' ');
let timer=0,busy=false,bubble=null;
const stick=()=>{const near=msgs.scrollHeight-msgs.scrollTop-msgs.clientHeight<40;return()=>{if(near)msgs.scrollTop=msgs.scrollHeight;};};
function add(cls,txt){const d=document.createElement('div');d.className='m '+cls;d.textContent=txt;msgs.append(d);msgs.scrollTop=msgs.scrollHeight;return d;}
function setBusy(b){busy=b;msgs.setAttribute('aria-busy',String(b));go.textContent=b?'■':'↑';go.setAttribute('aria-label',b?'Stop generating':'Send');}
function finish(stopped){clearInterval(timer);bubble.classList.remove('live');if(stopped){const s=document.createElement('small');s.textContent='Stopped';bubble.append(s);}setBusy(false);sr.textContent=stopped?'Stopped':'Response complete';}
f.addEventListener('submit',e=>{e.preventDefault();if(busy){finish(true);return;}
const t=inp.value.trim()||'How should an interface handle waiting?';inp.value='';add('u',t);sr.textContent='';bubble=add('a live','');setBusy(true);
if(reduce){setTimeout(()=>{bubble.textContent=ANSWER.join(' ');finish(false);},300);return;}
let i=0;timer=setInterval(()=>{const k=stick();bubble.textContent=ANSWER.slice(0,++i).join(' ');k();if(i>=ANSWER.length)finish(false);},45);});
</script>
</body></html>