shadcn.io is not affiliated with official shadcn/ui
Action Toast Notification Block for React
A messaging app's thread with a message toast that carries a Reply action: it never times out, Reply takes it away and puts the sender in the composer with focus, and Receive a message raises the next of three, up to three at once.
Installation
Overview
Let people answer a message without leaving what they're doing, with this action toast for React with Base UI. It's a conversation in Chatnook, a messaging app, drawn in greys inside a hairline frame with a plus mark on each corner: a top bar, a contact list and a thread of bubbles. Under the thread sits the real composer, an Input and a Send Button, and a new message arrives as a toast at the foot end, wearing the look of the catalog's Toast: an icon, the sender's name, the message, a Reply and a Dismiss. A toast with an action never times out. Reply takes the toast away, puts "Reply to" and the name in the composer and moves focus into it; Enter sends the reply into the thread (in this demo only) and Escape cancels. "Receive a message" raises the next of three typed messages as a real toast through the block's own toaster, at the end of the screen, three at most. It's built from Toast, Button, Input and Guides, with CSS motion only. For chat, comments, mentions and any message worth an answer.
FAQ
Why is a message already on the page, and what do I keep?
So you can see an action toast without waiting for one: on load the first message is drawn in place at the stage's foot end, using the catalog toast's own classes, and its Reply works. Receive a message raises a real toast and the drawn one stays gone until a reload. In your app keep the toaster and the raise call, and drop the drawn copy: the "in-place" state, DrawnToast and the ol in the frame.
How is the real toast with its action raised?
createToaster() (Base UI's createToastManager), renders <Toaster toaster={toaster} /> once, and raises a message with toaster.add({ id, type: "info", title, description, actionProps: { children: "Reply", onClick }, timeout: 0 }). A timeout of 0 keeps it up, and Base UI doesn't close the toast for its action, so the block does.The action is a button inside the toast, so Alt+T, Tab or a pointer reaches it; the block wraps all of it in raise(note, { act }).Why does Reply go to the composer and not open a field inside the toast?
A toast is a notice, and the toaster's toasts hold a title, a line, one action and a close button; an input inside one would move focus into a layer that comes and goes. Reply takes the toast away instead and hands the work to the composer on the page, where the field, its label and the thread belong. A custom toast can hold a field (Sonner's toast.custom, Ark's Toast parts in your own Toaster), at the price of owning its focus and timing.
What happens with more than three messages?
The toasts on screen are the newest three: when a fourth arrives the block takes the oldest away with its exit. The three typed messages come round again, each with its own id. Nothing is lost in your app, because the thread keeps every message; the toast is only the notice.
What does Send do?
Here it adds a bubble to the drawn thread, clears the composer and says in a polite status message that nothing was sent. An empty reply says "Write a reply first." and stays. In your app post the reply in send and add the message to the thread from the response. Only a toast that is up answers a Reply, once, because a toaster can deliver an action twice.
Is it accessible?
The real toast lives in a live region that speaks it once and never takes focus; because it carries an action it stays until it is answered or closed. Reply and Dismiss name the sender, the composer's name follows the reply ("Reply to Imre Dolan"), and focus moves to it when Reply is pressed, and to Receive a message when a reply is cancelled. The drawn copy is a list named "Notifications".
Related
- Pro
Undo Toast Notification
ToastsAn invoicing app's list with a delete on every row: the toast says how many are deleted with an Undo, a second delete merges into the same toast and starts its wait again, and Restore deleted on the page does the same as Undo.
- Pro
File Comments Notification
ConversationsReview comments grouped by file, each quoting the line it is about, with a Resolve toggle that moves a comment into a collapsed resolved group and back.
- Pro
Reply Composer Notification
ConversationsA reply notification with the quoted original (clamped, with Show more), the reply in full and a composer that sends a local reply into a mini thread, with Edit.
- Pro
The settings page of a form builder with its Save settings button: two success toasts are drawn on the page, and pressing Save raises a real toast through the block's own toaster, stacking up to three.
- Pro
Floating Notification Card
Floating cardsA visitor's message floating on a pricing page: a toggle group parks the card in any of four corners, Reply swaps its buttons for a one-line field, and Dismiss and Show again replay the slide-in.
- Pro
Toast Stack Notification
ToastsA post scheduler's publishing queue with an Add a toast button: the toasts overlap in a stack of three, open into a list on hover or focus, and the rest wait their turn.