Message field
ProA rounded field for writing something to send (a comment, a reply, a chat message) that grows from a pill into a box, with a send button, tools that open into their own row on focus, a reply banner, attachments and suggestions while typing (people after @, tags after #, emoji after :); plus its trigger. Not an M3 component (@windmorph/react-pro).
import { MessageField, MessageFieldFrame, MessageFieldTrigger, MessageFieldSend, useMessageField, useMessageFieldInput, useCoarsePointer } from '@windmorph/react-pro';Part of Windmorph Pro, paid components on the free core, not on sale yet. See the Pro guide for what it includes and to register interest.
Purpose
A rounded field for writing something to send: a comment, a reply, a chat message. It is a pill on one line, like the search bar, focused or not, and grows into a rounded box as the text runs over several lines. Enter sends, a send button grows in with the first character, and slots hold an avatar, icon buttons, a reply banner and attachments. It is not an M3 component, so it comes from @windmorph/react-pro. MessageFieldTrigger is the same look as one button ("What's on your mind?") that opens a bigger composer.
Use a TextField for form input that is submitted with the rest of a form (a bio, an address), and a SearchBar for search: it has search semantics, a back button and a suggestions view. Use a message field when what's written is sent on its own, straight away.
Behaviour
- The text grows from
minRows(1) tomaxRows(6), then scrolls inside the field.value/onChange(ordefaultValue) hold it. submitOn:'enter'(Enter sends, Shift+Enter is a new line; on touch screens Enter is a new line and Send sends, as most chat apps do on phones),'mod-enter'(Ctrl or ⌘+Enter sends) or'none'(only the button). Nothing is sent while an input method is composing (pinyin, kana or a dead-key accent), and a blank value is never sent.onSubmitgets the trimmed text.sendableoverrides "any text": a photo alone can be sent, or Send can wait for an upload.- The field doesn't clear itself: clear
valueonce the send succeeds, or setclearOnSubmit. Whilepending, the send button shows the loading indicator and ignores presses; a send that ends whileinvalidshakes the field once besideerrorMessage. emptyActiontakes the send button's place while there is no text, such as a microphone.send={false}hides the send button, andsendVariant="standard"shows it as the icon alone, in primary.onFileAcceptgets files pasted or dropped onto the field that matchaccept, and the field shows its drag-over state.acceptlists MIME types, wildcards (image/*) or extensions (.pdf): list the extensions beside the MIME types (['text/csv', '.csv']), since a file's type comes from the system and can differ from the one you expect.*or*/*takes any file, and an entry may hold several types, comma-separated.onFileReject(rejections)gets the files a paste or drop brought that weren't taken, afteronFileAcceptand never with an empty list, each with itsreason(type, offaccept), so you can say why a file didn't appear. A paste whose HTML holds words is a text paste whatever files it carries (copied cells or a document bring a picture of themselves), neither taken nor reported; a copied picture, whose HTML is the image alone, is taken, or reported whenacceptleaves it out. A paste that takes no file still pastes as usual, and reports its files unless it carries plain text (a copied file's name). WithoutonFileAcceptoronFilePick, files are ignored and text pastes as usual; a disabled or read-only field takes no files.onFilePick(pick)is called once for each paste or drop that brings files, with all of them:acceptedandrejected(with their reasons), each in the order the files came;acceptedis empty when every file was refused. It runs first, thenonFileAcceptandonFileReject, all in the same task, so you can add what fits and say once what was left out. A paste the field leaves as text calls nothing. It alone is enough to take files, here and onMessageFieldFrame.maxLengthnever cuts what's written: past it, the counter turns to the error colour, the field is invalid and Send waits, so a long paste loses no words. The counter ("1,840 / 2,000") shows fromcounterThreshold(90%), or always withcounter; nearing, reaching and passing the limit are each announced once.banneris a row above the text ("Replying to Sam"), with a close button whenonBannerCloseis set;attachmentsis a row of content above the text, such asMediaUploadGridwithlayout="row".onEscapehandles Escape, for example to cancel the reply.toolsholds icon buttons with their own actions (emoji, attach, GIF), at the end of the text row, in a toolbar: one Tab stop, and arrow keys between them. WithexpandOnFocus, the first focus opens the field, as a comment field under a post often does: the text takes the first row, and the tools and Send are on the second, with Send disabled until there's text. It stays open, so the page doesn't move each time focus leaves; it also starts open with text or a banner in it. A tool reaches the field withuseMessageField():insertTextputs text at the caret (over the selection), and the app gets the same throughhandleRef. The closed pill hides its tools when they'd leave the text too narrow, or always withtoolsWhenIdle="hidden"; giveMessageFieldTriggerthe same icons astoolsso the pill matches.toolbaris a row of controls inside the open field, such as formatting buttons: above the text, between the text and the tools (in thumb reach on phones), or by window size (toolbarPlacement).dockedsquares the corners on the field's spring, for a field that becomes its container's footer (a card's comment field);docked="when-open"docks it once it opens. Where it sits is the app's.- Icon buttons in
leadingandtrailingtake the field's size. An avatar as tall as them (32px atsm, 40px atmdandlg) sits concentric in the pill. Slot buttons sit 48px apart (16px besidexsbuttons, 8px besidesm), so their 48px touch targets never overlap. - The field keeps its height in a dense theme: its icon buttons stay at density 0 (the tools, Send, the banner's close and the slots), so a
densityon a provider orThemeScopenever squeezes their touch targets together. - Focusing the text never scrolls the page: a press on the field, Send, the banner's close and
insertTextput the caret back without moving it. The app's ownfocus()(throughhandleRef) brings a field that is wholly off screen into view with a short, smooth scroll.
Suggestions while typing
mentions,tagsandemojiSuggestionssuggest as you type:@and a name suggest people,#and a word suggest tags, and:and two letters (:sm) suggest emoji. Without them, each is just a character. Choosing one puts@Name,#tagor the emoji in place of what was typed, through the browser's own insert, so undo takes it out again; a tag typed and not chosen stays as typed. (RichMessageFieldputs people and tags in as tokens.)mentions.search(query, { signal })andtags.search(query, { signal })return the people or tags to suggest, best first: the app decides who and in what order. They run after a short pause in typing and may be async;signalis aborted once a newer query has replaced the search or the list has closed, so pass it on tofetch, and an answer that comes anyway is ignored. A search that takes a while shows a loading row, and one that fails shows nothing.maxItemscaps the list (8), andclassNamesreaches its parts.emojiSuggestionsis a source built bycreateEmojiSuggestions()from@windmorph/react-pro/emoji-picker: build it once, at module level or in auseMemo. A field without it never loads the emoji data; with it, the data loads from your own bundle on the first:. Its options (minChars,replaceOnColon,maxItems,emoticons, custom emoji and the picker's Recently used) are on the Emoji picker page.@and:start a suggestion only at the start of the text, after a space or after opening punctuation ((,"), and#only at the start or after a space, with a character after it. So10:30, a link'shttps://andname@example.comnever open a list. A name may hold single spaces (@Alex Ki), and the trigger nearest the caret wins (@Alex #desuggests tags).- A list opens only as the text changes, never as the caret moves back into a word. It opens above the field first, where a composer at the foot of a conversation has room, and below where there isn't. It moves with the field, so a scroll of the page doesn't close it, and pressing a suggestion keeps focus in the text.
- Focus stays in the text: Arrow Up and Arrow Down move through the list (wrapping), Enter or Tab puts the active suggestion in, and Escape dismisses the list until its query changes. Escape is the list's only while a list or its loading row shows; otherwise it goes on to the field's
onEscape. - The list's words are
labelskeys:mentionSuggestions,mentionLoading,mentionResults,tagSuggestions,tagLoading,tagResults,emojiSuggestions,emojiLoadingandemojiResults, or once for the app under theMessageFieldkey of aLabelsProvider. If the list's code can't load (offline, say), the field shows no list and keeps working.
Motion
- The field breathes as it grows: the pill's corners morph to extra-large (28px) as the text wraps or a banner or attachments open, and the height follows on the default spatial spring, so a new line slides the field open. Focus never changes the shape: one line stays a pill.
- The send button grows in from a small circle on the fast spatial spring and shrinks away with the last character. Pressing it squares its corners and springs back;
MessageFieldTrigger's press morph runs on the effects spring, with no bounce, as the core's non-toggle buttons. While sending, the send icon gives way to the loading indicator, which morphs through Material shapes. - Opening on focus puts the tools on their row at once and springs the field's height and corners, so the new row slides into view; Send grows in.
- The banner and attachments open and close on a spring with a fade, so the text never jumps; the counter fades in.
- A send that fails while
invalidshakes the field once, each step on the fast spatial spring. - The springs follow the motion scheme. Under reduced motion, shapes and heights change at once and nothing scales or shakes, but the fades stay on their effects timing: the rows, Send, its icon and the loading indicator still fade in and out.
When not to use
For form input sent with the rest of a form use a Text field; for search use a Search bar.
See also
- Text field: Filled or outlined text input with a floating label, icons, affixes, counter, error icon and a text area.
- Search: A search bar that opens a full-screen view on phones and a docked view on larger windows, and an app bar with search.
- Media upload grid: The photos someone is adding: upload progress and errors, remove and retry, drag to reorder (mouse, touch or keyboard), an add tile and file drop. Not an M3 component (@windmorph/react-pro).
- Emoji picker: A panel for choosing emoji, with search, a tab per category, recently used emoji and a skin tone; plus its trigger, which inserts at the caret of a message field and opens in a popover, a bottom sheet or docked in the keyboard’s place on phones; and createEmojiSuggestions, which suggests emoji after : in a message field. Not an M3 component (@windmorph/react-pro/emoji-picker, with emojibase-data).