# Rich text editor

A text box with a toolbar, for writing that needs headings, lists and links.

```tsx
import { RichTextEditor } from '@halo-ds/react';
```

## When to use it
Use it when people write longer content that needs simple formatting, like a description, a comment or release notes. Editor toolbar holds the formatting buttons; Rich text editor puts it on top of a Content slot; Rich text editor field adds a label, a hint and a character count.

## Do
- Turn off groups people don't need, like Insert in a comment box.
- Support keyboard shortcuts, like Ctrl+B for bold.
- Show a count when there is a limit.
- Keep the toolbar visible while people scroll through long text.

## Don't
- Use it where plain text is enough; use a Textarea.
- Add every tool by default; it crowds the toolbar.
- Hide errors in a toast; show them under the field.
- Lose people's writing on an error; keep the text.

## Rich text editor or Textarea?
Both take long text. Pick by whether people need formatting.
- **Rich text editor**: The text is shown with its formatting, like a post or a description. People need headings, lists or links. Longer writing, where structure helps readers.
- **Textarea**: Plain text, like feedback, a bio or a short note. The text is shown as written, without styles. Simpler to use and to store.

## Props
### RichTextEditor props
| Prop | Type | What it does |
| --- | --- | --- |
| `defaultValue` | `string \| undefined` | Starting content as HTML. The editor keeps its own state after that. |
| `onChange` | `((html: string) => void) \| undefined` | Called with the new HTML after each change. |
| `placeholder` | `string \| undefined` |  |
| `invalid` | `boolean \| undefined` |  |
| `disabled` | `boolean \| undefined` |  |
| `size` | `"sm" \| "md" \| undefined` | Small for phones and narrow panels: tighter padding, and the toolbar scrolls sideways in one row. Text and buttons keep their size so they stay easy to read and tap. Figma: Size. |
| `tools` | `Partial<Record<"format" \| "textStyle" \| "blocks" \| "insert" \| "history", boolean>> \| undefined` | Toolbar groups to show. Figma: Text style, Format, Blocks, Insert, History. |
| `minHeight` | `number \| undefined` | Minimum height of the writing area in px. |
| `id` | `string \| undefined` |  |
| `aria-label` | `string \| undefined` |  |
| `aria-labelledby` | `string \| undefined` |  |
| `aria-describedby` | `string \| undefined` |  |
| `aria-invalid` | `true \| undefined` |  |
| `className` | `string \| undefined` |  |
| `classNames` | `Partial<Record<RichTextEditorPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<RichTextEditorPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { RichTextEditor } from '@halo-ds/react';

export default function Example() {
  return (
    <div style={{ width: '100%', maxWidth: 560 }}>
      <RichTextEditor aria-label="Release notes" defaultValue="<p>This update makes <strong>sharing</strong> faster.</p>" />
    </div>
  );
}
```

### With a label and count
A label, a hint and a character limit, like other fields.
```tsx
import { RichTextEditorField } from '@halo-ds/react';

export default function Demo() {
  return (
    <div style={{ width: '100%', maxWidth: 560 }}>
      <RichTextEditorField label="Description" hint="Shown on the project page." maxLength={500} placeholder="Describe the project" />
    </div>
  );
}
```

### Basic tools only
Turn off tool groups you do not need.
```tsx
import { RichTextEditor } from '@halo-ds/react';

export default function Demo() {
  return (
    <div style={{ width: '100%', maxWidth: 560 }}>
      <RichTextEditor aria-label="Comment" tools={{ textStyle: false, blocks: false, insert: false }} minHeight={96} />
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/rich-text-editor
