Pattern Components Intermediate
Prompt Input
A chat composer works best as a textarea that grows with its content, sends on Enter, adds a newline on Shift+Enter, ignores Enter during IME composition, and becomes a stop button while the model is replying.
The problem
If you drop a single-line <input> into your chat UI, three things can go wrong right away. The user can’t write a second paragraph without the text scrolling out of view. They might press Enter to add a line break and accidentally send a half-written message. And when they’re typing Japanese or Chinese, pressing Enter to confirm the character they’ve picked sends the prompt instead.
The pattern
The composer is built on a <textarea> so that multi-line input works, and it grows to fit its content up to a maximum height, then scrolls. Enter sends the message, and Shift+Enter inserts a newline. Some apps do it the other way around, with Enter for a newline and Ctrl+Enter or Cmd+Enter to send, and either works as long as you pick one convention and stick with it. While an Input Method Editor (IME) composition is active, the composer has to ignore Enter, and you can check for that with event.isComposing or event.keyCode === 229. Otherwise, users writing in many languages might send half-typed messages.
Problem
Then the model starts replying and the send button stays enabled, so the user can send a second prompt while the first reply is still streaming in.
A prompt composer looks like just a box and a button, but users interact with it more than anything else in a chat UI, so small problems add up. Most of what makes it pleasant or frustrating to use is how it handles the keyboard and keeps track of its own state, and those are the parts that are easiest to skip.
Solution
There’s one button, and what it does depends on whether a response is being generated. While nothing is generating, it’s a send button that stays disabled until the field has text, and while a response is streaming, it becomes a Stop Generation button. Sending a new prompt is disabled during generation, but the stop button is always enabled. Whitespace is trimmed before sending, and empty or whitespace-only prompts are rejected. Sending clears the field and resets its height, so the next message starts on one line.
Example
The first example is the composer itself in each framework, with auto-grow and keyboard handling that’s safe for IME input. The others show the send and stop button, then two ways to let the browser do more of the work: CSS-only auto-grow and a form that works without JavaScript.
The Composer
function PromptInput({ onSend, busy }) {
const [value, setValue] = useState('');
const ref = useRef(null);
const grow = (el) => {
el.style.height = 'auto';
el.style.height = Math.min(el.scrollHeight, 200) + 'px';
};
const submit = () => {
const text = value.trim();
if (!text || busy) return;
onSend(text);
setValue('');
ref.current.style.height = 'auto';
};
const onKeyDown = (e) => {
// Enter sends; Shift+Enter is a newline; ignore Enter mid-IME-composition
// (keyCode 229 catches Safari, which reports isComposing as false on the confirming Enter)
const composing = e.nativeEvent.isComposing || e.keyCode === 229;
if (e.key === 'Enter' && !e.shiftKey && !composing) {
e.preventDefault();
submit();
}
};
return (
<textarea
ref={ref}
rows={1}
value={value}
placeholder="Message the assistant…"
onChange={(e) => { setValue(e.target.value); grow(e.target); }}
onKeyDown={onKeyDown}
/>
);
} <script setup>
import { ref } from 'vue';
const props = defineProps({ busy: Boolean });
const emit = defineEmits(['send']);
const value = ref('');
const el = ref(null);
function grow() {
el.value.style.height = 'auto';
el.value.style.height = Math.min(el.value.scrollHeight, 200) + 'px';
}
function submit() {
const text = value.value.trim();
if (!text || props.busy) return;
emit('send', text);
value.value = '';
el.value.style.height = 'auto';
}
function onKeyDown(e) {
const composing = e.isComposing || e.keyCode === 229;
if (e.key === 'Enter' && !e.shiftKey && !composing) {
e.preventDefault();
submit();
}
}
</script>
<template>
<textarea
ref="el"
rows="1"
v-model="value"
placeholder="Message the assistant…"
@input="grow"
@keydown="onKeyDown"
/>
</template> <script>
let { busy = false, onsend } = $props();
let value = $state('');
let el;
function grow() {
el.style.height = 'auto';
el.style.height = Math.min(el.scrollHeight, 200) + 'px';
}
function submit() {
const text = value.trim();
if (!text || busy) return;
onsend?.(text);
value = '';
el.style.height = 'auto';
}
function onKeyDown(e) {
const composing = e.isComposing || e.keyCode === 229;
if (e.key === 'Enter' && !e.shiftKey && !composing) {
e.preventDefault();
submit();
}
}
</script>
<textarea
bind:this={el}
bind:value
rows="1"
placeholder="Message the assistant…"
oninput={grow}
onkeydown={onKeyDown}
></textarea> class PromptInput extends HTMLElement {
connectedCallback() {
this.innerHTML = `<textarea rows="1" placeholder="Message the assistant…"></textarea>`;
const el = this.querySelector('textarea');
el.addEventListener('input', () => {
el.style.height = 'auto';
el.style.height = Math.min(el.scrollHeight, 200) + 'px';
});
el.addEventListener('keydown', (e) => {
const composing = e.isComposing || e.keyCode === 229;
if (e.key === 'Enter' && !e.shiftKey && !composing) {
e.preventDefault();
const text = el.value.trim();
if (!text || this.hasAttribute('busy')) return;
this.dispatchEvent(new CustomEvent('send', { detail: text }));
el.value = '';
el.style.height = 'auto';
}
});
}
}
customElements.define('prompt-input', PromptInput); Send or Stop, Never Both
The component only renders one of the two buttons at a time, so the user can’t click Send while a reply is streaming.
function Composer({ busy, canSend, onSend, onStop }) {
return (
<form className="composer" onSubmit={(e) => { e.preventDefault(); onSend(); }}>
<textarea /* …as above… */ />
{busy ? (
<button type="button" onClick={onStop} aria-label="Stop generating">
<StopIcon /> Stop
</button>
) : (
<button type="submit" disabled={!canSend} aria-label="Send message">
<SendIcon /> Send
</button>
)}
</form>
);
}
CSS-Only Auto-Grow
In newer browsers, CSS can size the textarea, so you don’t need JavaScript to set its height.
.composer textarea {
field-sizing: content; /* grows with content where supported */
min-height: 2.5rem;
max-height: 12rem; /* then it scrolls */
resize: none;
overflow-y: auto;
}
Working Without JavaScript
If you wrap the composer in a real <form> that posts to an endpoint, users can still send a message before your JavaScript has loaded, and the page can switch to streaming once it has.
<form action="/chat" method="post" class="composer">
<textarea name="prompt" rows="1" required placeholder="Message the assistant…"></textarea>
<button type="submit">Send</button>
</form>
Benefits
- Multi-line prompts work, so users can paste code or write structured questions without the text scrolling out of view.
- Sending on Enter and adding a newline on Shift+Enter matches what users are used to from other chat apps.
- Because the composer ignores Enter during IME composition, users typing in Japanese, Chinese, Korean, and other languages don’t send messages by accident.
- Replacing the send button with a stop button during generation shows users that a reply is still coming, and they can’t send a new prompt until it’s finished or stopped.
- Auto-grow keeps the whole draft visible while the user types.
- With a
<form>wrapper, the composer still works before JavaScript has loaded.
Tradeoffs
- Keyboard handling has a lot of edge cases, such as IME composition and mobile keyboards that send their own Enter, and you need to test on real devices to get them all right.
- Sending on Enter surprises users who expect Enter to add a newline. The convention is common now but not universal, so the composer should show users how to add a newline.
- The JavaScript version of auto-grow forces a reflow on every keystroke. CSS
field-sizingavoids that, but the last of the major browsers only added it in mid-2026, when it became Baseline newly available, so older browsers still need the JavaScript version. - On small screens, the growing textarea and the on-screen keyboard compete for vertical space, so the textarea needs a sensible maximum height.
- The button’s state has to stay in sync with whether a response is actually still streaming, or users might see a stop button for a response that’s already finished.
Summary
Users interact with the prompt composer more than any other control in an AI interface, so they notice small problems with it quickly. It needs an auto-growing textarea, Enter to send with Shift+Enter for newlines, key handling that’s safe for IME input, whitespace trimming, and an action button that becomes a stop button while the model replies. If you wrap it in a real form, users can still send a message before JavaScript has loaded.
See also
- Message Thread — Components
- Stop Generation — Resilience
- Streaming Response — Resilience
- textarea - MDN
- KeyboardEvent.isComposing - MDN
- field-sizing - MDN