# Rich Text Field

The frame around a rich text editor: a label, a toolbar of 28 px formatting buttons over a divider, and the text with the field's edge and focus. The editor engine stays in the app (the CRM's Tiptap); paragraphs, lists and links inside take the field's type.

- Beta: ready to use; its API can still change in a minor release.
- Category: Form fields

```tsx
import { RichTextButton, RichTextField, RichTextSeparator } from "@leap-crm/design-system";
```

## Best practices

- Do: Use it when the reader needs the formatting, like a note with a list of next steps or an email body with links. Use `Textarea` for plain text.
- Do: Pass the editor as a function child and spread the props it receives onto the editable element, so `label` names the editor for screen readers.
- Do: Give every `RichTextButton` a `label`, and a `shortcut` where one exists, like `mod+b` for bold. The label is its name and its tooltip.
- Do: Separate groups of buttons with `RichTextSeparator`, like text styles apart from lists.
- Don't: Set `invalid` with nothing else. It only turns the edge red, so add text under the field that says what to do, like "Please enter a note".
- Don't: Put a separate `Label` above it. Use `label`, which is wired to the editor.

## Keyboard

| Keys | What they do |
| --- | --- |
| Tab | Lands on the toolbar once, and Tab again leaves it. |
| ArrowRight, ArrowLeft | Move between the toolbar's buttons. |
| Enter, Space | Press the focused toolbar button. |

## Related

- [Textarea](./textarea.md)
- [Toolbar](./toolbar.md)
- [Note composer](./note-composer.md)

## Examples

What the examples import:

```tsx
import { Bold, Italic, Link, List, ListOrdered } from "lucide-react";
import { useState } from "react";
import { RichTextButton, RichTextField, RichTextSeparator } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
function Formatting() {
  const [on, setOn] = useState<Record<string, boolean>>({ list: true });
  const toggle = (key: string) =>
    setOn(current => ({ ...current, [key]: !current[key] }));
  return (
    <>
      <RichTextButton
        icon={<Bold />}
        label="Bold"
        shortcut="mod+b"
        pressed={on.bold ?? false}
        onClick={() => toggle("bold")}
      />
      <RichTextButton
        icon={<Italic />}
        label="Italic"
        shortcut="mod+i"
        pressed={on.italic ?? false}
        onClick={() => toggle("italic")}
      />
      <RichTextButton
        icon={<List />}
        label="Bulleted list"
        pressed={on.list ?? false}
        onClick={() => toggle("list")}
      />
      <RichTextButton
        icon={<ListOrdered />}
        label="Numbered list"
        pressed={on.numbered ?? false}
        onClick={() => toggle("numbered")}
      />
      <RichTextSeparator />
      <RichTextButton icon={<Link />} label="Link" shortcut="mod+k" />
    </>
  );
}

const sample = (
  <>
    <p>
      Prefers email. <a href="/rich-text-field">Settlement checklist</a> is in
      the matter folder.
    </p>
    <ul>
      <li>Call the vendor’s solicitor</li>
      <li>Send the costs agreement</li>
    </ul>
  </>
);
```

### With a toolbar

Click into the text for the focus edge. Tab reaches the toolbar once; arrow keys move along it. A pressed button shows the pressed fill. In the CRM the children are Tiptap's EditorContent, given the editor props.

```tsx
<div className="w-md">
  <RichTextField label="Notes" toolbar={<Formatting />}>
    {editor => (
      <div {...editor} contentEditable suppressContentEditableWarning>
        {sample}
      </div>
    )}
  </RichTextField>
</div>
```

### Invalid

```tsx
<div className="w-md">
  <RichTextField label="Notes" invalid toolbar={<Formatting />}>
    {editor => (
      <div {...editor} contentEditable suppressContentEditableWarning>
        <p>Too short.</p>
      </div>
    )}
  </RichTextField>
</div>
```

## Props

### RichTextField

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `ReactNode` |  | Names the editor. |
| `toolbar` | `ReactNode` |  | The formatting buttons: RichTextButton and RichTextSeparator. |
| `toolbarLabel` | `string` | `"Formatting"` | Names the toolbar for screen readers. |
| `invalid` | `boolean` | `false` | Draws the field invalid. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
| `children` (required) | `ReactNode \| ((props: EditorProps) => ReactNode)` |  | The editor. A function receives the props that name it. |

### RichTextButton

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `icon` (required) | `ReactNode` |  | The 14 px icon. |

### RichTextSeparator

No props of its own.
