Patterns / Header composer
Start something new from the header in one sentence
Some apps start their main thing from a sentence: a project, a draft, a run. A + in the header lets a person do that from any page, without a new-item form. It opens a small panel holding one line to write in and a Start button.
The + is a ghost icon Button named for what it starts ("New project"), the trigger of a Popover. The Popover holds a Composer with layout="inline": one line that grows to six as the person writes, with Start at its end. sendLabel names the button, and sendShape="round" draws it round. Until there is something written, Start says "Write a message first" instead of sending nothing.
Enter sends and Shift+Enter breaks a line. onSubmit hands your app the text: create the thing, close the panel and go to it. Keep the draft in your own state, outside the Popover, so Escape or a click outside closes the panel without losing what the person wrote.
The components
- ComposerFreeThe one line to write in, growing to `maxRows`, with Start at its end; `note` can carry a line in your words, such as an error.
- PopoverFreeThe panel the + opens, named for what it starts; given the box as `initialFocus` it puts focus there, and it returns focus to the + when it closes.
- ButtonFreeThe ghost icon + that opens it.
See it working
The code
The + in the header: a Popover with an inline Composer that starts a project and goes to it.
"use client"
import * as React from "react"
import { useRouter } from "next/navigation"
import { Plus } from "lucide-react"
import { Button } from "@/components/candor/ui/button"
import { Popover, PopoverContent, PopoverTrigger } from "@/components/candor/ui/popover"
import { Composer } from "@/components/candor/composer"
import { startProject } from "@/lib/projects" // your own
export function NewProjectButton() {
const router = useRouter()
const [panelOpen, setPanelOpen] = React.useState(false)
// The draft lives here, not in the popover, so Escape or a click outside closes it without losing a word.
const [brief, setBrief] = React.useState("")
const [starting, setStarting] = React.useState(false)
const [didntStart, setDidntStart] = React.useState(false)
const box = React.useRef<HTMLTextAreaElement>(null)
async function start(text: string) {
setStarting(true)
setDidntStart(false)
try {
const project = await startProject(text)
setBrief("")
setPanelOpen(false)
router.push(`/projects/${project.id}`)
} catch {
// The draft stays, so the person can send it again.
setDidntStart(true)
} finally {
setStarting(false)
}
}
return (
<Popover open={panelOpen} onOpenChange={setPanelOpen}>
<PopoverTrigger render={<Button variant="ghost" size="icon" aria-label="New project"><Plus /></Button>} />
<PopoverContent align="end" aria-label="New project" initialFocus={box} className="w-[28rem] max-w-[calc(100vw-2rem)] p-2">
<Composer
layout="inline"
textareaRef={box}
value={brief}
onValueChange={setBrief}
onSubmit={start}
state={starting ? "sending" : "ready"}
label="Describe the project"
placeholder="Describe it in one sentence"
sendLabel="Start"
sendShape="round"
note={didntStart ? <span role="alert">That didn't start. Your words are still here to send again.</span> : undefined}
/>
</PopoverContent>
</Popover>
)
}
Install
Each component brings in what it depends on. These are free (MIT).
Good to know
- Pass the Composer's
textareaRefto PopoverContent'sinitialFocus, so typing can start as soon as the panel opens. - Leave suggestion chips out of the panel; it is for starting what the person already has in mind. PromptSuggestions is the item for chips, on a page with room for them.
- If starting fails, keep the draft and say so in
notewithrole="alert", so the person can send it again without writing it twice.