AI Prompt Composer
An auto-growing prompt box with suggestion chips, Enter to send, Shift+Enter for a new line and a character counter.
AI prompt
Create an AI prompt composer. A rounded 14 pixel box containing an auto-growing textarea (one to five lines) with placeholder "Ask anything…", a circular near-black Send button with an up arrow at the bottom right, and a counter "0 / 200" at the bottom left. Above the box show three suggestion chips (Summarize this, Explain simply, Write an email); clicking a chip puts its text in the box and focuses it. Enter sends, Shift+Enter inserts a new line, Send is disabled while the box is empty; when sent, show the text as a "You" message above and clear the box. The counter turns red above 180 characters and input stops at 200. Labelled textarea, Send has aria-label, sent messages announce through aria-live polite. Plain HTML, CSS and JavaScript with a prefers-reduced-motion fallback.About this pattern
Definition
A prompt composer is the text area where people write to an AI. Good ones grow as you type, offer starter suggestions, enable Send only when there is text and respect keyboard habits.
Purpose & UX principles
- Suggestion chips lower the blank-page barrier; clicking one fills the box instead of sending immediately so people can edit.
- Enter sends, Shift+Enter inserts a line break; show this hint where it is not obvious.
- Show a counter near the limit and keep the Send button's disabled state clear.
Interaction states
- Empty (Send disabled), typing (Send enabled), near limit (counter turns red), sent (message appears, box clears).
Motion specification
- Height grows smoothly as lines are added; Send button scales 0.94 on press for 100ms.
- Reduced motion: no height or press animation.
Responsive behaviour
- The box grows up to about five lines and then scrolls; on phones the Send button stays inside the box at 40px.
- Chips scroll horizontally instead of wrapping on narrow screens.
Code
<div class="bx"><div class="log" id="log" aria-live="polite"><p class="ph">Messages appear here.</p></div><div class="chips" id="chips"><button type="button">Summarize this</button><button type="button">Explain simply</button><button type="button">Write an email</button></div><div class="box"><label class="sr" for="ta">Message</label><textarea id="ta" rows="1" maxlength="200" placeholder="Ask anything…"></textarea><div class="ft"><span id="ct">0 / 200</span><button id="sd" type="button" aria-label="Send" disabled>↑</button></div></div></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}
.bx{width:min(320px,100%);display:grid;gap:8px}
.log{min-height:60px;max-height:60px;overflow:auto;display:grid;gap:5px;align-content:end}
.ph{font-size:12px;color:var(--muted);text-align:center}
.msg{justify-self:end;max-width:85%;padding:6px 12px;border-radius:14px 14px 4px 14px;background:var(--text);color:#fff;font-size:13px;white-space:pre-wrap;overflow-wrap:anywhere}
.chips{display:flex;gap:6px;overflow-x:auto;padding-bottom:2px}.chips button{flex:none;height:28px;padding:0 12px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12px;cursor:pointer}.chips button:hover{border-color:#bdbdb8}
.box{padding:10px 10px 8px 14px;border-radius:14px;background:var(--surface);border:1px solid var(--line)}.box:focus-within{outline:2px solid var(--accent);outline-offset:1px}
textarea{width:100%;border:0;outline:none;resize:none;background:none;font:inherit;font-size:14px;line-height:1.4;max-height:98px;display:block}
.ft{display:flex;justify-content:space-between;align-items:center;margin-top:4px}#ct{font-size:11px;color:var(--muted)}#ct.hot{color:var(--danger)}
#sd{width:32px;height:32px;border-radius:50%;border:0;background:var(--accent);color:var(--accent-ink);font-size:16px;font-weight:700;cursor:pointer;transition:transform .1s,opacity .15s}#sd:disabled{opacity:.25;cursor:default}#sd:active:not(:disabled){transform:scale(.94)}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){#sd{transition:none}}const $=id=>document.getElementById(id),ta=$('ta'),sd=$('sd'),ct=$('ct'),log=$('log');
function up(){ta.style.height='auto';ta.style.height=Math.min(ta.scrollHeight,98)+'px';sd.disabled=!ta.value.trim();ct.textContent=ta.value.length+' / 200';ct.className=ta.value.length>180?'hot':'';}
function send(){const t=ta.value.trim();if(!t)return;const ph=log.querySelector('.ph');if(ph)ph.remove();const m=document.createElement('div');m.className='msg';m.textContent=t;log.append(m);log.scrollTop=log.scrollHeight;ta.value='';up();ta.focus();}
ta.addEventListener('input',up);ta.addEventListener('keydown',e=>{if(e.key==='Enter'&&!e.shiftKey&&!e.isComposing){e.preventDefault();send();}});sd.addEventListener('click',send);
$('chips').addEventListener('click',e=>{const b=e.target.closest('button');if(b){ta.value=b.textContent+': ';up();ta.focus();}});up();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>AI Prompt Composer — 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}}
.bx{width:min(320px,100%);display:grid;gap:8px}
.log{min-height:60px;max-height:60px;overflow:auto;display:grid;gap:5px;align-content:end}
.ph{font-size:12px;color:var(--muted);text-align:center}
.msg{justify-self:end;max-width:85%;padding:6px 12px;border-radius:14px 14px 4px 14px;background:var(--text);color:#fff;font-size:13px;white-space:pre-wrap;overflow-wrap:anywhere}
.chips{display:flex;gap:6px;overflow-x:auto;padding-bottom:2px}.chips button{flex:none;height:28px;padding:0 12px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12px;cursor:pointer}.chips button:hover{border-color:#bdbdb8}
.box{padding:10px 10px 8px 14px;border-radius:14px;background:var(--surface);border:1px solid var(--line)}.box:focus-within{outline:2px solid var(--accent);outline-offset:1px}
textarea{width:100%;border:0;outline:none;resize:none;background:none;font:inherit;font-size:14px;line-height:1.4;max-height:98px;display:block}
.ft{display:flex;justify-content:space-between;align-items:center;margin-top:4px}#ct{font-size:11px;color:var(--muted)}#ct.hot{color:var(--danger)}
#sd{width:32px;height:32px;border-radius:50%;border:0;background:var(--accent);color:var(--accent-ink);font-size:16px;font-weight:700;cursor:pointer;transition:transform .1s,opacity .15s}#sd:disabled{opacity:.25;cursor:default}#sd:active:not(:disabled){transform:scale(.94)}
.sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0)}
button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){#sd{transition:none}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="log" id="log" aria-live="polite"><p class="ph">Messages appear here.</p></div><div class="chips" id="chips"><button type="button">Summarize this</button><button type="button">Explain simply</button><button type="button">Write an email</button></div><div class="box"><label class="sr" for="ta">Message</label><textarea id="ta" rows="1" maxlength="200" placeholder="Ask anything…"></textarea><div class="ft"><span id="ct">0 / 200</span><button id="sd" type="button" aria-label="Send" disabled>↑</button></div></div></div>
</div>
<script>
const $=id=>document.getElementById(id),ta=$('ta'),sd=$('sd'),ct=$('ct'),log=$('log');
function up(){ta.style.height='auto';ta.style.height=Math.min(ta.scrollHeight,98)+'px';sd.disabled=!ta.value.trim();ct.textContent=ta.value.length+' / 200';ct.className=ta.value.length>180?'hot':'';}
function send(){const t=ta.value.trim();if(!t)return;const ph=log.querySelector('.ph');if(ph)ph.remove();const m=document.createElement('div');m.className='msg';m.textContent=t;log.append(m);log.scrollTop=log.scrollHeight;ta.value='';up();ta.focus();}
ta.addEventListener('input',up);ta.addEventListener('keydown',e=>{if(e.key==='Enter'&&!e.shiftKey&&!e.isComposing){e.preventDefault();send();}});sd.addEventListener('click',send);
$('chips').addEventListener('click',e=>{const b=e.target.closest('button');if(b){ta.value=b.textContent+': ';up();ta.focus();}});up();
</script>
</body></html>