Skip to content

Rich message field

Pro

A message field that formats as you type, with markers in plain text: bold, italic, strike, code, links, lists, quotes and code blocks, with a formatting toolbar, and suggestions while typing (@mentions of people, #tags and emoji after :); plus FormattedText, which shows what it wrote. Not an M3 component (@windmorph/react-pro/rich-text, with Tiptap).

import { RichMessageField, FormattingToolbar, FormattedText, useRichMessageField } from '@windmorph/react-pro/rich-text';

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 message field that formats as you type, with markers in plain text: bold looks bold, code gets its box, and lists, quotes and code blocks are blocks. It is the MessageField frame (its look, rows, tools, Send, counter and tokens) with a rich editor inside, and it hands out plain text in the marker format (*bold*, _italic_, ~strike~, `code`, [words](https://…), > quote, - item, 1. item and ``` code blocks), which FormattedText shows. It is not an M3 component, and comes from @windmorph/react-pro/rich-text, which needs the @tiptap/* packages (an optional peer dependency, so apps that don't use it never install them).

Use a MessageField where formatting isn't wanted (chat, search-like inputs), and a full document editor for headings, tables or images in the text.

Behaviour

  • formatting: 'toggle' (an Aa tool shows and hides the formatting toolbar), 'always' (the toolbar stays, for a composer on larger windows) or 'none'. The toolbar is the field's toolbar row: formattingPlacement puts it above the text, between the text and the tools, or 'auto': between on phones, in thumb reach, and above from medium windows.
  • The buttons, and ⌘ or Ctrl + B, I, Shift+X and Shift+C, toggle formats without inserting anything visible, and show pressed where their format is on. Typing the markers turns them into formatting; ``` then Enter starts a code block. Link asks for an http or https address in a dialog, or takes a link off. The toolbar is one Tab stop: the arrows move between its buttons and wrap, Home and End reach the first and last, and a separator divides its groups. Its buttons stay at density 0, so the field keeps its height in a dense theme.
  • Enter sends, except in a list (the next item; an empty one ends the list) or a code block (a new line), and on touch screens, where it's a new line and Send sends; Shift+Enter starts a new line, and ⌘ or Ctrl + Enter always sends (submitOn as in MessageField).
  • value / onChange (or defaultValue) hold the text in the marker format; onSubmit gets it trimmed. Everything else is the frame's: tools (after Aa), banner, attachments, onFilePick, onFileAccept and onFileReject, maxLength (never cut; it counts the stored marker text, so markers and mentions count: *bold* is 6), expandOnFocus, docked and the rest. handleRef adds format, setLink and removeLink to focus, insertText and clear. Focusing the text (a press on the field or a tool, insertText) never scrolls the page; the app's focus() brings a field that is wholly off screen into view.
  • onFileAccept gets files pasted or dropped onto the field that match accept, before the editor sees them. 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).
  • 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, and onFilePick alone is enough to take files.
  • FormattedText shows the marker format as elements, never HTML from the text. Links are http and https only, open in a new tab and get rel="ugc nofollow noopener noreferrer"; links={false} shows them as words inside something pressable. Links to the app's own pages can be ordinary links: internalLinks(url) picks them (say, (url) => url.origin === SITE_ORIGIN, a constant from the app's config: it runs on the server too, where there is no location, and must answer the same there), and they open in the same tab, through the app's RouterProvider on the page's own origin (its navigate gets the link as written, a full https:// address), with no new-tab mark or words and internalLinkRel (none by default) in place of ugc nofollow. renderLink(link) renders a link yourself, for instance with your router's link component; return undefined to keep the default for that link. For a router that takes a path, render the link there and give it link.path: the path with one leading slash, then the search and hash. Never use the address's raw pathname: https://site.example//other.example/x has the pathname //other.example/x, which a browser or router reads as an address on another site, so a crafted link would send people there. An address whose path starts with // is never one of the app's own, whatever internalLinks says. parseMarkers, markersToDoc and docToMarkers read and convert the format.

When not to use

For plain messages use a Message field; for long documents with headings and tables, a full editor.

See also

  • Message field: 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).
  • Text field: Filled or outlined text input with a floating label, icons, affixes, counter, error icon and a text area.