RichText
The RichText component enables content editors to edit a multiline rich text.
It is similar to the Text component, but it allows you to select which rich text features editors can use, such as bold, italic, link, highlight, quote, code, headings (H1..H6), ordered list, unordered list, superscript, and subscript.
You can also customize the default render function for each style.
Required properties:
propName: the name of the propvalue: prop value. It is strictly required only for RSC, but we recommend always providing it for Server Components compatibility.renderBlock: the render function for this text (not strictly required, defaults to a simple<span>if omitted)allowedFeatures: the available rich-text features (none by default)
Usage example
Section titled “Usage example”// from 'react-bricks/rsc' for usage with server componentsimport { types, RichText, Link } from 'react-bricks/frontend'
interface MyBrickProps { description: types.TextValue}
const MyBrick: types.Brick<MyBrickProps> = ({ description }) => ( ... <RichText propName="description" value={description} renderBlock={({ children }) => ( <p className="text-lg text-gray-600">{children}</p> )} placeholder="Type a description..."
allowedFeatures={[ types.RichTextFeatures.Bold, types.RichTextFeatures.Italic, types.RichTextFeatures.Link, //types.RichTextFeatures.Highlight, //types.RichTextFeatures.Heading1, //types.RichTextFeatures.Heading2, //types.RichTextFeatures.Heading3, //types.RichTextFeatures.Heading4, //types.RichTextFeatures.Heading5, //types.RichTextFeatures.Heading6, //types.RichTextFeatures.Code, //types.RichTextFeatures.Quote, //types.RichTextFeatures.OrderedList, //types.RichTextFeatures.UnorderedList, //types.RichTextFeatures.Subscript, //types.RichTextFeatures.Superscript, ]}
// Override default <b> tag for Bold style renderBold={({ children }) => ( <b className="text-pink-500"> {children} </b> )}
// Override rendering for Links renderLink={({ children, href, target, rel }) => ( <Link href={href} target={target} rel={rel} className="text-sky-500 hover:text-sky-600 transition-colors" > {children} </Link> )} /> ...)
MyBrick.schema = { ... }
export default MyBrickMulti-line
Section titled “Multi-line”By default, the RichText component allows multiple lines of text. You can modify this behavior with these props:
multiline(default: true): if false, prevents the content editor from creating multiple lines with theEnterkeysoftLineBreak(default: true): if false, disables the soft line break (<br />)
Bind to Meta data or Custom fields
Section titled “Bind to Meta data or Custom fields”Typically, a RichText component is used to save a text value for the current block in the page.
You can also bind it to a page’s custom field or meta field by using customFieldName or metaFieldName instead of propName. This creates a two-way data binding between the visual editing component and the values in the sidebar controls of the “Page” tab. For custom fields, you can opt to save the value as plain text using the customFieldPlainText prop.
Properties
Section titled “Properties”Here’s the Typescript interface for the RichText component props:
// For every RichText usageinterface BaseRichTextProps { renderBlock?: (props: { children: React.ReactNode }) => JSX.Element placeholder?: string renderPlaceholder?: (props: { children: React.ReactNode }) => JSX.Element multiline?: boolean softLineBreak?: boolean allowedFeatures?: types.RichTextFeatures[] renderBold?: (props: { children: React.ReactNode }) => JSX.Element renderItalic?: (props: { children: React.ReactNode }) => JSX.Element renderHighlight?: (props: { children: React.ReactNode }) => JSX.Element renderCode?: (props: { children: React.ReactNode }) => JSX.Element rendersSub?: (props: { children: React.ReactNode }) => JSX.Element renderSup?: (props: { children: React.ReactNode }) => JSX.Element renderLink?: (props: { href: string, target?: string. rel?: string }) => JSX.Element renderUL?: (props: { children: React.ReactNode }) => JSX.Element renderOL?: (props: { children: React.ReactNode }) => JSX.Element renderLI?: (props: { children: React.ReactNode }) => JSX.Element renderH1?: (props: { children: React.ReactNode }) => JSX.Element renderH2?: (props: { children: React.ReactNode }) => JSX.Element renderH3?: (props: { children: React.ReactNode }) => JSX.Element renderH4?: (props: { children: React.ReactNode }) => JSX.Element renderH5?: (props: { children: React.ReactNode }) => JSX.Element renderH6?: (props: { children: React.ReactNode }) => JSX.Element renderQuote?: (props: { children: React.ReactNode }) => JSX.Element}
// Usage with propName (usual case)interface RichTextPropsWithPropName extends BaseRichTextProps { propName: string value?: types.TextValue metaFieldName?: never customFieldName?: never customFieldPlainText?: never}
interface RichTextPropsWithMetaFieldName extends BaseRichTextProps { propName?: never value?: never metaFieldName: 'title' | 'description' | 'language' customFieldName?: never customFieldPlainText?: never}
interface RichTextPropsWithCustomFieldName extends BaseRichTextProps { propName?: never value?: never metaFieldName?: never customFieldName: string customFieldPlainText?: boolean}
/** * Props for RichText component */type RichTextProps = | RichTextPropsWithPropName | RichTextPropsWithMetaFieldName | RichTextPropsWithCustomFieldNameProperties definition
Section titled “Properties definition”Usage with Server Components
Section titled “Usage with Server Components”When working with Server Components, you need to import from 'react-bricks/rsc':
import { types, RichText, Link } from 'react-bricks/rsc'Create custom plugins
Section titled “Create custom plugins”To enhance the RichText component’s default functionality, such as adding an emoji button, you can use the RichTextExt component. This component is designed to be extended with custom plugins.
With RichTextExt, you can develop sophisticated rich text features, including those requiring configuration popups with custom fields. For more information, see the Extensible RichText documentation.
