Prompt box
A message box for an assistant: it grows as you type, has a research switch and a model picker, and its send button turns into a stop button while an answer is on its way.
Options
Code
import { useRef, useState } from "react";
import "./prompt-box.css";
const SPARK = "M8 1c.4 0 .7.2.8.6l1 3.2a2 2 0 0 0 1.4 1.4l3.2 1a.8.8 0 0 1 0 1.6l-3.2 1a2 2 0 0 0-1.4 1.4l-1 3.2a.8.8 0 0 1-1.6 0l-1-3.2a2 2 0 0 0-1.4-1.4l-3.2-1a.8.8 0 0 1 0-1.6l3.2-1a2 2 0 0 0 1.4-1.4l1-3.2c.1-.4.4-.6.8-.6Z";
const PLUS = "M8 2.25a1 1 0 0 1 1 1V7h3.75a1 1 0 1 1 0 2H9v3.75a1 1 0 1 1-2 0V9H3.25a1 1 0 0 1 0-2H7V3.25a1 1 0 0 1 1-1Z";
const DOWN = "M4.2 5.7a1 1 0 0 1 1.4 0L8 8.1l2.4-2.4a1 1 0 1 1 1.4 1.4l-3.1 3.1a1 1 0 0 1-1.4 0L4.2 7.1a1 1 0 0 1 0-1.4Z";
const UP = "M8 2a1 1 0 0 1 .7.3l4 4a1 1 0 1 1-1.4 1.4L9 5.4V13a1 1 0 1 1-2 0V5.4L4.7 7.7a1 1 0 0 1-1.4-1.4l4-4A1 1 0 0 1 8 2Z";
function Icon({ d }) {
return (
<svg viewBox="0 0 16 16" aria-hidden="true">
<path fill="currentColor" d={d} />
</svg>
);
}
/* onSend gets the text and the chosen options, and can be async: the send button is
a stop button until it finishes. onStop is called if it is pressed meanwhile.
<PromptBox models={["Swift", "Balanced", "Deep"]} onSend={ask} onStop={cancel} /> */
export function PromptBox({ models = ["Swift", "Balanced", "Deep"], placeholder = "Ask anything", onSend, onStop, onAttach }) {
const [text, setText] = useState("");
const [research, setResearch] = useState(false);
const [model, setModel] = useState(models[Math.min(1, models.length - 1)]);
const [busy, setBusy] = useState(false);
const box = useRef(null);
function fit() {
if (CSS.supports("field-sizing", "content")) return;
box.current.style.height = "auto";
box.current.style.height = box.current.scrollHeight + "px";
}
async function submit(event) {
event.preventDefault();
if (busy) {
onStop?.();
return setBusy(false);
}
if (!text.trim()) return;
const message = text;
setText("");
setBusy(true);
requestAnimationFrame(fit);
try {
await onSend?.(message, { model, research });
} finally {
setBusy(false);
}
}
return (
<form className={"dh-prompt" + (busy ? " is-busy" : "")} onSubmit={submit}>
<textarea
ref={box}
rows={1}
placeholder={placeholder}
aria-label="Message"
value={text}
onChange={(event) => {
setText(event.target.value);
fit();
}}
onKeyDown={(event) => {
if (event.key === "Enter" && !event.shiftKey && !event.nativeEvent.isComposing) {
event.preventDefault();
event.currentTarget.form.requestSubmit();
}
}}
/>
<div className="dh-prompt-bar">
<button className="dh-prompt-tool" type="button" aria-label="Attach a file" onClick={onAttach}>
<Icon d={PLUS} />
</button>
<button className="dh-prompt-chip" type="button" aria-pressed={research} onClick={() => setResearch(!research)}>
<Icon d={SPARK} />
Deep research
</button>
<button
className="dh-prompt-model"
type="button"
aria-label={"Model: " + model + ". Press to change"}
onClick={() => setModel(models[(models.indexOf(model) + 1) % models.length])}
>
<span key={model} className="is-new">
{model}
</span>
<Icon d={DOWN} />
</button>
<button className="dh-prompt-send" type="submit" aria-label={busy ? "Stop" : "Send"} disabled={!busy && !text.trim()}>
<Icon d={UP} />
<i aria-hidden="true" />
</button>
</div>
</form>
);
}.dh-prompt {
--ink: var(--dh-fg, #171717);
--top: var(--dh-raised, #fff);
--well: var(--dh-well, #ededeb);
--soft: var(--dh-muted, #5c5c5c);
--faint: var(--dh-faint, #8a8a8a);
--hue: var(--dh-accent, #2563ff);
--rule: color-mix(in srgb, var(--ink) 9%, transparent);
--lift: 0 0 0 1px var(--rule), 0 1px 2px 0 rgb(0 0 0 / 0.1);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
display: grid;
gap: 0.6em;
width: min(100%, 36em);
margin: 0;
padding: 0.85em 0.7em 0.7em;
border-radius: 1.15em;
background: var(--top);
color: var(--ink);
font-size: 0.9375em;
font-weight: 500;
letter-spacing: -0.01em;
box-shadow: 0 0 0 1px var(--rule), 0 1px 2px 0 rgb(0 0 0 / 0.08), 0 0.5em 0.75em -0.5em rgb(0 0 0 / 0.14);
transition: box-shadow 200ms var(--ease);
}
.dh-prompt:focus-within {
box-shadow: 0 0 0 1px color-mix(in srgb, var(--ink) 22%, transparent), 0 1px 2px 0 rgb(0 0 0 / 0.08), 0 0.75em 1em -0.5em rgb(0 0 0 / 0.18);
}
.dh-prompt textarea {
width: 100%;
min-height: 1.5em;
max-height: 9em;
padding: 0 0.45em;
border: 0;
outline: 0;
background: none;
color: inherit;
font: inherit;
line-height: 1.5;
letter-spacing: inherit;
resize: none;
box-sizing: border-box;
field-sizing: content;
}
.dh-prompt textarea::placeholder {
color: var(--faint);
}
.dh-prompt-bar {
display: flex;
align-items: center;
gap: 0.4em;
}
.dh-prompt button {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.4em;
height: 2.3em;
padding: 0 0.8em;
border: 0;
border-radius: 999px;
background: var(--top);
color: var(--soft);
font: inherit;
font-size: 0.875em;
letter-spacing: inherit;
white-space: nowrap;
cursor: pointer;
box-shadow: var(--lift);
transition: scale 150ms var(--ease), background 150ms var(--ease), color 150ms var(--ease), box-shadow 150ms var(--ease), opacity 150ms var(--ease);
}
.dh-prompt button:active {
scale: 0.96;
}
.dh-prompt button:focus-visible {
outline: 2px solid var(--faint);
outline-offset: 2px;
}
.dh-prompt svg {
flex: none;
width: 1.05em;
height: 1.05em;
}
.dh-prompt .dh-prompt-tool,
.dh-prompt .dh-prompt-send {
width: 2.3em;
padding: 0;
}
.dh-prompt-chip[aria-pressed="true"] {
background: color-mix(in srgb, var(--hue) 10%, var(--top));
color: color-mix(in srgb, var(--hue) 82%, var(--ink));
box-shadow: 0 0 0 1px color-mix(in srgb, var(--hue) 28%, transparent), 0 1px 2px 0 color-mix(in srgb, var(--hue) 18%, transparent);
}
.dh-prompt .dh-prompt-model {
margin-left: auto;
padding: 0 0.5em 0 0.8em;
background: none;
box-shadow: none;
}
.dh-prompt-model span {
display: inline-block;
}
.dh-prompt-model span.is-new {
animation: dh-prompt-in 300ms var(--ease);
}
@keyframes dh-prompt-in {
from {
opacity: 0;
filter: blur(3px);
translate: 0 0.3em;
}
}
.dh-prompt .dh-prompt-send {
position: relative;
background: var(--ink);
color: var(--top);
box-shadow: inset 0 1px 0 0 rgb(255 255 255 / 0.16), 0 0 0 1px var(--ink), 0 1px 2px 0 rgb(0 0 0 / 0.3);
}
.dh-prompt .dh-prompt-send:disabled {
background: var(--well);
color: var(--faint);
box-shadow: inset 0 1px 2px 0 rgb(0 0 0 / 0.06);
cursor: default;
}
.dh-prompt-send :is(svg, i) {
grid-area: 1 / 1;
transition: opacity 200ms var(--ease), scale 200ms var(--ease), filter 200ms var(--ease);
}
.dh-prompt-send {
display: inline-grid !important;
}
.dh-prompt-send i {
width: 0.7em;
height: 0.7em;
border-radius: 0.15em;
background: currentColor;
}
.dh-prompt-send i,
.dh-prompt.is-busy .dh-prompt-send svg {
opacity: 0;
scale: 0.5;
filter: blur(3px);
}
.dh-prompt.is-busy .dh-prompt-send i {
opacity: 1;
scale: 1;
filter: none;
}
.dh-prompt.is-busy .dh-prompt-send::after {
content: "";
position: absolute;
inset: -3px;
border: 2px solid transparent;
border-top-color: var(--hue);
border-radius: 50%;
animation: dh-prompt-spin 800ms linear infinite;
}
@keyframes dh-prompt-spin {
to {
rotate: 1turn;
}
}
@media (hover: hover) {
.dh-prompt button:not(:disabled, .dh-prompt-send, [aria-pressed="true"]):hover {
background: color-mix(in srgb, var(--ink) 5%, var(--top));
color: var(--ink);
}
.dh-prompt .dh-prompt-send:not(:disabled):hover {
opacity: 0.88;
}
}
@media (pointer: coarse) {
.dh-prompt {
font-size: 1em;
}
.dh-prompt button {
height: 2.75em;
}
.dh-prompt .dh-prompt-tool,
.dh-prompt .dh-prompt-send {
width: 2.75em;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-prompt-model span.is-new,
.dh-prompt.is-busy .dh-prompt-send::after {
animation: none;
}
.dh-prompt-send :is(svg, i) {
transition: none;
}
}<form class="dh-prompt">
<textarea rows="1" placeholder="Ask anything" aria-label="Message"></textarea>
<div class="dh-prompt-bar">
<button class="dh-prompt-tool" type="button" aria-label="Attach a file"><svg viewBox="0 0 16 16" aria-hidden="true"><path fill="currentColor" d="M8 2.25a1 1 0 0 1 1 1V7h3.75a1 1 0 1 1 0 2H9v3.75a1 1 0 1 1-2 0V9H3.25a1 1 0 0 1 0-2H7V3.25a1 1 0 0 1 1-1Z" /></svg></button>
<button class="dh-prompt-chip" type="button" aria-pressed="false"><svg viewBox="0 0 16 16" aria-hidden="true"><path fill="currentColor" d="M8 1c.4 0 .7.2.8.6l1 3.2a2 2 0 0 0 1.4 1.4l3.2 1a.8.8 0 0 1 0 1.6l-3.2 1a2 2 0 0 0-1.4 1.4l-1 3.2a.8.8 0 0 1-1.6 0l-1-3.2a2 2 0 0 0-1.4-1.4l-3.2-1a.8.8 0 0 1 0-1.6l3.2-1a2 2 0 0 0 1.4-1.4l1-3.2c.1-.4.4-.6.8-.6Z" /></svg>Deep research</button>
<button class="dh-prompt-model" type="button" aria-label="Model: Balanced. Press to change"><span>Balanced</span><svg viewBox="0 0 16 16" aria-hidden="true"><path fill="currentColor" d="M4.2 5.7a1 1 0 0 1 1.4 0L8 8.1l2.4-2.4a1 1 0 1 1 1.4 1.4l-3.1 3.1a1 1 0 0 1-1.4 0L4.2 7.1a1 1 0 0 1 0-1.4Z" /></svg></button>
<button class="dh-prompt-send" type="submit" aria-label="Send" disabled>
<svg viewBox="0 0 16 16" aria-hidden="true"><path fill="currentColor" d="M8 2a1 1 0 0 1 .7.3l4 4a1 1 0 1 1-1.4 1.4L9 5.4V13a1 1 0 1 1-2 0V5.4L4.7 7.7a1 1 0 0 1-1.4-1.4l4-4A1 1 0 0 1 8 2Z" /></svg>
<i aria-hidden="true"></i>
</button>
</div>
</form>.dh-prompt {
--ink: var(--dh-fg, #171717);
--top: var(--dh-raised, #fff);
--well: var(--dh-well, #ededeb);
--soft: var(--dh-muted, #5c5c5c);
--faint: var(--dh-faint, #8a8a8a);
--hue: var(--dh-accent, #2563ff);
--rule: color-mix(in srgb, var(--ink) 9%, transparent);
--lift: 0 0 0 1px var(--rule), 0 1px 2px 0 rgb(0 0 0 / 0.1);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
display: grid;
gap: 0.6em;
width: min(100%, 36em);
margin: 0;
padding: 0.85em 0.7em 0.7em;
border-radius: 1.15em;
background: var(--top);
color: var(--ink);
font-size: 0.9375em;
font-weight: 500;
letter-spacing: -0.01em;
box-shadow: 0 0 0 1px var(--rule), 0 1px 2px 0 rgb(0 0 0 / 0.08), 0 0.5em 0.75em -0.5em rgb(0 0 0 / 0.14);
transition: box-shadow 200ms var(--ease);
}
.dh-prompt:focus-within {
box-shadow: 0 0 0 1px color-mix(in srgb, var(--ink) 22%, transparent), 0 1px 2px 0 rgb(0 0 0 / 0.08), 0 0.75em 1em -0.5em rgb(0 0 0 / 0.18);
}
.dh-prompt textarea {
width: 100%;
min-height: 1.5em;
max-height: 9em;
padding: 0 0.45em;
border: 0;
outline: 0;
background: none;
color: inherit;
font: inherit;
line-height: 1.5;
letter-spacing: inherit;
resize: none;
box-sizing: border-box;
field-sizing: content;
}
.dh-prompt textarea::placeholder {
color: var(--faint);
}
.dh-prompt-bar {
display: flex;
align-items: center;
gap: 0.4em;
}
.dh-prompt button {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.4em;
height: 2.3em;
padding: 0 0.8em;
border: 0;
border-radius: 999px;
background: var(--top);
color: var(--soft);
font: inherit;
font-size: 0.875em;
letter-spacing: inherit;
white-space: nowrap;
cursor: pointer;
box-shadow: var(--lift);
transition: scale 150ms var(--ease), background 150ms var(--ease), color 150ms var(--ease), box-shadow 150ms var(--ease), opacity 150ms var(--ease);
}
.dh-prompt button:active {
scale: 0.96;
}
.dh-prompt button:focus-visible {
outline: 2px solid var(--faint);
outline-offset: 2px;
}
.dh-prompt svg {
flex: none;
width: 1.05em;
height: 1.05em;
}
.dh-prompt .dh-prompt-tool,
.dh-prompt .dh-prompt-send {
width: 2.3em;
padding: 0;
}
.dh-prompt-chip[aria-pressed="true"] {
background: color-mix(in srgb, var(--hue) 10%, var(--top));
color: color-mix(in srgb, var(--hue) 82%, var(--ink));
box-shadow: 0 0 0 1px color-mix(in srgb, var(--hue) 28%, transparent), 0 1px 2px 0 color-mix(in srgb, var(--hue) 18%, transparent);
}
.dh-prompt .dh-prompt-model {
margin-left: auto;
padding: 0 0.5em 0 0.8em;
background: none;
box-shadow: none;
}
.dh-prompt-model span {
display: inline-block;
}
.dh-prompt-model span.is-new {
animation: dh-prompt-in 300ms var(--ease);
}
@keyframes dh-prompt-in {
from {
opacity: 0;
filter: blur(3px);
translate: 0 0.3em;
}
}
.dh-prompt .dh-prompt-send {
position: relative;
background: var(--ink);
color: var(--top);
box-shadow: inset 0 1px 0 0 rgb(255 255 255 / 0.16), 0 0 0 1px var(--ink), 0 1px 2px 0 rgb(0 0 0 / 0.3);
}
.dh-prompt .dh-prompt-send:disabled {
background: var(--well);
color: var(--faint);
box-shadow: inset 0 1px 2px 0 rgb(0 0 0 / 0.06);
cursor: default;
}
.dh-prompt-send :is(svg, i) {
grid-area: 1 / 1;
transition: opacity 200ms var(--ease), scale 200ms var(--ease), filter 200ms var(--ease);
}
.dh-prompt-send {
display: inline-grid !important;
}
.dh-prompt-send i {
width: 0.7em;
height: 0.7em;
border-radius: 0.15em;
background: currentColor;
}
.dh-prompt-send i,
.dh-prompt.is-busy .dh-prompt-send svg {
opacity: 0;
scale: 0.5;
filter: blur(3px);
}
.dh-prompt.is-busy .dh-prompt-send i {
opacity: 1;
scale: 1;
filter: none;
}
.dh-prompt.is-busy .dh-prompt-send::after {
content: "";
position: absolute;
inset: -3px;
border: 2px solid transparent;
border-top-color: var(--hue);
border-radius: 50%;
animation: dh-prompt-spin 800ms linear infinite;
}
@keyframes dh-prompt-spin {
to {
rotate: 1turn;
}
}
@media (hover: hover) {
.dh-prompt button:not(:disabled, .dh-prompt-send, [aria-pressed="true"]):hover {
background: color-mix(in srgb, var(--ink) 5%, var(--top));
color: var(--ink);
}
.dh-prompt .dh-prompt-send:not(:disabled):hover {
opacity: 0.88;
}
}
@media (pointer: coarse) {
.dh-prompt {
font-size: 1em;
}
.dh-prompt button {
height: 2.75em;
}
.dh-prompt .dh-prompt-tool,
.dh-prompt .dh-prompt-send {
width: 2.75em;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-prompt-model span.is-new,
.dh-prompt.is-busy .dh-prompt-send::after {
animation: none;
}
.dh-prompt-send :is(svg, i) {
transition: none;
}
}document.querySelectorAll(".dh-prompt").forEach((form) => {
const box = form.querySelector("textarea");
const send = form.querySelector(".dh-prompt-send");
const chip = form.querySelector(".dh-prompt-chip");
const model = form.querySelector(".dh-prompt-model");
const models = ["Swift", "Balanced", "Deep"];
let busy;
/* The box grows with what is typed. Browsers with field-sizing do this themselves. */
const fit = () => {
send.disabled = !box.value.trim() && !form.classList.contains("is-busy");
if (CSS.supports("field-sizing", "content")) return;
box.style.height = "auto";
box.style.height = box.scrollHeight + "px";
};
box.addEventListener("input", fit);
box.addEventListener("keydown", (event) => {
if (event.key === "Enter" && !event.shiftKey && !event.isComposing) {
event.preventDefault();
form.requestSubmit();
}
});
chip.addEventListener("click", () => chip.setAttribute("aria-pressed", String(chip.getAttribute("aria-pressed") !== "true")));
model.addEventListener("click", () => {
const name = model.querySelector("span");
const next = models[(models.indexOf(name.textContent) + 1) % models.length];
name.textContent = next;
model.setAttribute("aria-label", "Model: " + next + ". Press to change");
name.classList.remove("is-new");
void name.offsetWidth;
name.classList.add("is-new");
});
/* Sending turns the button into a stop button until the answer is done.
Here that is a short wait; swap in your own request. */
const done = () => {
clearTimeout(busy);
form.classList.remove("is-busy");
send.setAttribute("aria-label", "Send");
fit();
};
form.addEventListener("submit", (event) => {
event.preventDefault();
if (form.classList.contains("is-busy")) return done();
if (!box.value.trim()) return;
box.value = "";
form.classList.add("is-busy");
send.setAttribute("aria-label", "Stop");
fit();
busy = setTimeout(done, 2400);
});
});Prompt box is not in the Figma file yet.
Use the React or HTML code for now.