Blocks / Rich composer

Rich composer

A box to write: @ and # pickers, a slash hook, markdown keys, files, preview, a reply bar, your send key.

import { RichComposer, applyConversationFormat, findMentionTrigger, insertMention, parseMentions } from "@forgeron/ui";
/01 → Default
⌘↵ sends
    import { RichComposer, type MentionOption } from "@forgeron/ui";
    import { useState } from "react";
    
    const people: MentionOption[] = [
      { id: "lea", label: "Léa Martin", hint: "Team lead" },
      { id: "karim", label: "Karim Aziz", hint: "Controller" },
      { id: "sam", label: "Sam Roche", hint: "Director" },
    ];
    const records: MentionOption[] = [
      { id: "ops-40", label: "OPS-40", hint: "Webhook sent twice" },
      { id: "ops-52", label: "OPS-52", hint: "Export timeout" },
    ];
    const commands: MentionOption[] = [
      { id: "remind", label: "/remind", hint: "Remind me later", insert: "/remind " },
      { id: "shrug", label: "/shrug", hint: "Append a shrug", insert: "¯\\_(ツ)_/¯" },
    ];
    
    export default function Default() {
      const [sent, setSent] = useState<string[]>([]);
      return (
        <div style={{ width: "100%", maxWidth: 640 }}>
          <RichComposer
            placeholder="Type @ to mention, # for a record, / for a command"
            triggers={[
              { char: "@", label: "People", options: people },
              { char: "#", label: "Records", options: records },
              { char: "/", label: "Commands", options: commands, atLineStart: true },
            ]}
            onSend={({ text, mentions }) =>
              setSent((s) => [`${text}  [${mentions.length} mention(s)]`, ...s])
            }
          />
          <ul
            style={{
              margin: "12px 0 0",
              padding: 0,
              listStyle: "none",
              font: "12px var(--font-mono)",
              color: "var(--ink-muted)",
            }}
          >
            {sent.map((s, i) => (
              <li key={i}>{s}</li>
            ))}
          </ul>
        </div>
      );
    }
    /02 → Reply enter
    Replying to Léa MartinFound it: the webhook is sent twice.
    ↵ sends, ⇧↵ new line
    import { RichComposer } from "@forgeron/ui";
    import { useState } from "react";
    
    export default function ReplyEnter() {
      const [reply, setReply] = useState<{ id: string; author: string; text: string } | null>({
        id: "42",
        author: "Léa Martin",
        text: "Found it: the webhook is sent twice.",
      });
      return (
        <div style={{ width: "100%", maxWidth: 640 }}>
          <RichComposer
            sendOn="enter"
            replyTo={reply}
            onCancelReply={() => setReply(null)}
            onSend={() => setReply(null)}
            attachmentOptions={{ maxSize: 5_000_000, maxFiles: 3 }}
          />
        </div>
      );
    }
    /03 → Read only
    You can read this conversation but not write in it.
    ⌘↵ sends
    import { RichComposer } from "@forgeron/ui";
    
    export default function ReadOnly() {
      return (
        <div style={{ display: "grid", gap: 16, width: "100%", maxWidth: 640 }}>
          <RichComposer readOnly defaultValue="A draft kept for reading" onSend={() => {}} />
          <RichComposer disabled placeholder="Sending is paused" onSend={() => {}} />
        </div>
      );
    }
    API → Props
    PropTypeDefaultDescription
    value / defaultValue / onChangestring—The draft. Uncontrolled, it empties once sent.
    onSend({ text, attachments, mentions, replyToId }) => void—On the button and the send key. Mentions are written @[Label](id) in text.
    sendOn"mod-enter" | "enter""mod-enter"⌘↵ sends and Enter is a line; or Enter sends and ⇧↵ is a line.
    triggers{ char, label, options, atLineStart?, filter?, loading?, limit?, empty? }[]—The pickers: @ for people, # for records, / (atLineStart) for commands. An option is { id, label, hint?, avatar?, insert? }; insert replaces the token (a command's text).
    onTrigger({ char, query } | null) => void—The slash hook, and the way to load options from a server (set filter: false, loading, and pass the answer as options).
    replyTo / onCancelReply{ id, author, text } | null; () => void—A bar above the box; Escape cancels it.
    attachments / onAttachmentsChange / attachmentOptionsFile[]; (files) => void; false | { accept, maxSize, maxFiles, onReject }—Files go through FileInput behind the paperclip.
    toolbar / maxRows / label / placeholder / sendLabel / onTypingboolean; number; string; string; string; () => void—Bold (⌘B), italic (⌘I), code (⌘E), list (⌘⇧L), preview; grows to maxRows.
    disabled / readOnly / readOnlyTextboolean; boolean; ReactNode—Locked states.
    classNamestring—Extra classes.