--- layout: default title: "The RichTextInput Component" --- # `` `` lets users edit rich text in a WYSIWYG editor, and store the result as HTML. It is powered by [TipTap](https://www.tiptap.dev/). ## Usage Due to its size, `` is not bundled by default with react-admin. You must install it first, using npm: ```sh npm install ra-input-rich-text # or yarn add ra-input-rich-text ``` Use it as you would any react-admin inputs: ```jsx import { Edit, SimpleForm, TextInput } from 'react-admin'; import { RichTextInput } from 'ra-input-rich-text'; export const PostEdit = () => ( ); ``` ## Props | Prop | Required | Type | Default | Description | | ------ | -------- | -------- | ------- | ----------- | | `editorOptions` | Optional | `Object` | - | Options object to pass to the underlying TipTap editor. | | `toolbar` | Optional| `ReactNode` | - | The toolbar to use. If not set, the default toolbar is used. | `` also accepts the [common input props](./Inputs.md#common-input-props). ## `editorOptions` You might want to add more Tiptap extensions. The `` component accepts an `editorOptions` prop which is the [object passed to Tiptap Editor](https://www.tiptap.dev/guide/configuration). If you just want to **add** extensions, don't forget to include those needed by default for our implementation. Here's an example to add the [HorizontalRule node](https://www.tiptap.dev/api/nodes/horizontal-rule): ```jsx import { DefaultEditorOptions, RichTextInput, RichTextInputToolbar, LevelSelect, FormatButtons, AlignmentButtons, ListButtons, LinkButtons, QuoteButtons, ClearButtons, useTiptapEditor, } from 'ra-input-rich-text'; import HorizontalRule from '@tiptap/extension-horizontal-rule'; import Remove from '@mui/icons-material/Remove'; import { ToggleButton } from '@mui/material'; const MyRichTextInputToolbar = ({ size, ...props }) => { const editor = useTiptapEditor(); return ( editor.chain().focus().setHorizontalRule().run() } selected={editor && editor.isActive('horizontalRule')} > ); } const MyRichTextInput = ({ size, ...props }) => ( } label="Body" source="body" {...props} /> ); export const MyEditorOptions = { ...DefaultEditorOptions, extensions: [ ...DefaultEditorOptions.extensions, HorizontalRule, ], }; ``` ## `toolbar` The `` component has a `toolbar` prop that accepts a `ReactNode`. By default, it uses the `` component. You can leverage the `toolbar` prop to change the buttons size: ```jsx import { Edit, SimpleForm, TextInput } from 'react-admin'; import { RichTextInput, RichTextInputToolbar } from 'ra-input-rich-text'; export const PostEdit = () => ( } /> ); ``` Or to remove some prebuilt components like the ``: ```jsx import { RichTextInput, RichTextInputToolbar, LevelSelect, FormatButtons, ListButtons, LinkButtons, QuoteButtons, ClearButtons, } from 'ra-input-rich-text'; const MyRichTextInput = ({ size, ...props }) => ( } label="Body" source="body" {...props} /> ); ``` ## Calling The `editor` Object You may want to access the TipTap `editor` object to tweak extensions, input rules, etc. (see [the TipTap editor documentation](https://tiptap.dev/docs/editor/api/editor) for details). To do so, you can assign a `ref` in the `onCreate` function in the `editorOptions` prop of your `` component, as follows: {% raw %} ```tsx import React from 'react'; import { Edit, SaveButton, SimpleForm, TextInput, Toolbar } from 'react-admin'; import { DefaultEditorOptions, RichTextInput } from 'ra-input-rich-text'; import { Button } from 'ra-ui-materialui'; import { Editor } from '@tiptap/react'; export const PostEdit = () => { const editorRef = React.useRef(null); return ( } > { editorRef.current = editor; }, }} /> ); }; ``` {% endraw %} With this ref, you can now call the `editor` methods, for instance to set the `` content when the user clicks a button: {% raw %} ```jsx const MyToolbar = ({ editorRef }) => ( ); ``` {% endraw %} ## AI Writing Assistant Modern AI tools can be a great help for editors. React-admin proposes an AI-powered writing assistant for the `` component, called [``](./SmartRichTextInput.md): `` is a drop-in replacement for ``: ```jsx import { Edit, SimpleForm, TextInput } from 'react-admin'; import { SmartRichTextInput } from '@react-admin/ra-ai'; export const PostEdit = () => ( ); ``` `` is available as part of the [ra-ai](https://react-admin-ee.marmelab.com/documentation/ra-ai) enterprise package. ## Lazy Loading The `` component depends on TipTap, which in turns depends on ProseMirror. Together, these libraries represent about 120kB of minified JavaScript. If you don't use `` on all your forms, you can [lazy load](https://react.dev/reference/react/lazy#suspense-for-code-splitting) it to reduce the size of your bundle. To do so, replace the import: ```jsx import { RichTextInput } from 'ra-input-rich-text'; ``` with a dynamic import: ```jsx const RichTextInput = React.lazy(() => import('ra-input-rich-text').then(module => ({ default: module.RichTextInput, })) ); ``` Once compiled, your application will load the `` only when needed.