Skip to content

Message field

Pro

A 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) to maxRows (6), then scrolls inside the field. value / onChange (or defaultValue) 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. onSubmit gets the trimmed text. sendable overrides "any text": a photo alone can be sent, or Send can wait for an upload.
  • The field doesn't clear itself: clear value once the send succeeds, or set clearOnSubmit. While pending, the send button shows the loading indicator and ignores presses; a send that ends while invalid shakes the field once beside errorMessage.
  • emptyAction takes the send button's place while there is no text, such as a microphone. send={false} hides the send button, and sendVariant="standard" shows it as the icon alone, in primary.
  • onFileAccept gets files pasted or dropped onto the field that match accept, and the field shows its drag-over state. accept lists 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, after onFileAccept and never with an empty list, each with its reason (type, off accept), 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 when accept leaves 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). Without onFileAccept or onFilePick, 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: accepted and rejected (with their reasons), each in the order the files came; accepted is empty when every file was refused. It runs first, then onFileAccept and onFileReject, 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 on MessageFieldFrame.
  • maxLength never 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 from counterThreshold (90%), or always with counter; nearing, reaching and passing the limit are each announced once.
  • banner is a row above the text ("Replying to Sam"), with a close button when onBannerClose is set; attachments is a row of content above the text, such as MediaUploadGrid with layout="row". onEscape handles Escape, for example to cancel the reply.
  • tools holds 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. With expandOnFocus, 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 with useMessageField(): insertText puts text at the caret (over the selection), and the app gets the same through handleRef. The closed pill hides its tools when they'd leave the text too narrow, or always with toolsWhenIdle="hidden"; give MessageFieldTrigger the same icons as tools so the pill matches.
  • toolbar is 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). docked squares 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 leading and trailing take the field's size. An avatar as tall as them (32px at sm, 40px at md and lg) sits concentric in the pill. Slot buttons sit 48px apart (16px beside xs buttons, 8px beside sm), 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 density on a provider or ThemeScope never squeezes their touch targets together.
  • Focusing the text never scrolls the page: a press on the field, Send, the banner's close and insertText put the caret back without moving it. The app's own focus() (through handleRef) brings a field that is wholly off screen into view with a short, smooth scroll.

Suggestions while typing

  • mentions, tags and emojiSuggestions suggest 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 , #tag or 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. (RichMessageField puts people and tags in as tokens.)
  • mentions.search(query, { signal }) and tags.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; signal is aborted once a newer query has replaced the search or the list has closed, so pass it on to fetch, and an answer that comes anyway is ignored. A search that takes a while shows a loading row, and one that fails shows nothing. maxItems caps the list (8), and classNames reaches its parts.
  • emojiSuggestions is a source built by createEmojiSuggestions() from @windmorph/react-pro/emoji-picker: build it once, at module level or in a useMemo. 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. So 10:30, a link's https:// and name@example.com never open a list. A name may hold single spaces (@Alex Ki), and the trigger nearest the caret wins (@Alex #de suggests 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 labels keys: mentionSuggestions, mentionLoading, mentionResults, tagSuggestions, tagLoading, tagResults, emojiSuggestions, emojiLoading and emojiResults, or once for the app under the MessageField key of a LabelsProvider. 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 invalid shakes 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).