Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including mui-rte with all npm packages installed. Try it out:

require("@material-ui/core/package.json"); // @material-ui/core is a peer dependency. require("@material-ui/icons/package.json"); // @material-ui/icons is a peer dependency. require("react/package.json"); // react is a peer dependency. require("react-dom/package.json"); // react-dom is a peer dependency. var muiRte = require("mui-rte")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

mui-rte v1.9.2

Material-UI Rich Text Editor and Viewer

mui-rte

The Material-UI Rich Text Editor and Viewer

mui-rte is a complete text editor and viewer for material-ui v3 and v4 based on draft-js and written in Typescript. It is ready to use out of the box yet supports user defined blocks, styles, callbacks, and decorators as well as toolbar and theme customization to enhance the editor to all needs.

Installation

npm install mui-rte --save

Install the peer dependencies: @material-ui/core, @material-ui/icons, react and react-dom.

Demo

Edit mui-rte basic

Usage

import MUIRichTextEditor from 'mui-rte'

ReactDOM.render(
    <MUIRichTextEditor label="Start typing..." />, 
    document.getElementById("root")
)

You can load default content as the following example. The value should be a stringified RawDraftContentState object:

import MUIRichTextEditor from 'mui-rte'

const data = getContentStateAsStringFromSomewhere()

ReactDOM.render(
    <MUIRichTextEditor 
        value={data}
        label="Start typing..." 
    />, 
    document.getElementById("root")
)

Examples

Check the examples directory for more.

Custom Controls

You can define your custom inline styles, blocks, atomic blocks and callback actions to the editor. Just select an icon from @material-ui/icons or create your own FunctionComponent and define your rules.

Adding a custom inline style

This sample adds a control to change the background color and font color of the typed or selected text:

import MUIRichTextEditor from 'mui-rte'
import InvertColorsIcon from '@material-ui/icons/InvertColors'

<MUIRichTextEditor 
    controls={["my-style"]}
    customControls={[
        {
            name: "my-style",
            icon: <InvertColorsIcon />,
            type: "inline",
            inlineStyle: {
                backgroundColor: "black",
                color: "white"
            }
        }
    ]}
/>

Adding a custom block

This sample adds a block to the editor based on a React Element:

import MUIRichTextEditor from 'mui-rte'
import TableChartIcon from '@material-ui/icons/TableChart'

const MyBlock = (props) => {
    return (
        <div style={{
            padding: 10,
            backgroundColor: "#ebebeb"
        }}>
            My Block content is:
            {props.children}
        </div>
    )
}

<MUIRichTextEditor 
    controls={["my-block"]}
    customControls={[
        {
            name: "my-block",
            icon: <TableChartIcon />,
            type: "block",
            blockWrapper: <MyBlock />
        }
    ]}
/>

Adding a custom atomic block

Check this sample that shows how to create a control to add a @material-ui/core Card component to the editor.

Adding a custom callback control

This sample adds a control that will trigger a custom callback function to clear the editor state:

import MUIRichTextEditor from 'mui-rte'
import DoneIcon from '@material-ui/icons/Done'
import { EditorState } from 'draft-js'

<MUIRichTextEditor 
    controls={["my-callback"]}
    customControls={[
        {
            name: "my-callback",
            icon: <DoneIcon />,
            type: "callback",
            onClick: (editorState, name, anchor) => {
                console.log(`Clicked ${name} control`)
                return EditorState.createEmpty()
            }
        }
    ]}
/>

Custom Decorators

You can define custom decorators to apply styles and/or functionality based on a provided regular expression.

Adding custom functionality with a decorator

To add some functionality when a user inputs a #hashtag use the following example. In this case, everytime the user inputs a word starting with a # character it will be automatically converted into a styled link:

import MUIRichTextEditor from 'mui-rte'

const MyHashTagDecorator = (props) => {
    const hashtagUrl = "http://myurl/" + props.decoratedText
    return (
        <a 
            href={hashtagUrl}
            style={{
                color: "green"
            }}
        >
            {props.children}
        </a>
    )
}

<MUIRichTextEditor 
    label="Type something here..."
    decorators={[
        {
            component: MyHashTagDecorator,
            regex: /\#[\w]+/g
        }
    ]}
/>

Inline toolbar

The editor includes an inline toolbar option which renders a pop-up inside the editor area when the user makes a selection. The inline toolbar supports user defined controls. Notice that only inline type controls will be rendered. The controls displayed on the main toolbar can be different from the ones in the inline toolbar. You can also hide the main toolbar and just enable the inline toolbar.

import MUIRichTextEditor from 'mui-rte'

<MUIRichTextEditor 
    label="Type something here..."
    inlineToolbar={true}
/>

Styling the editor

You can style the editor using the Material-UI theming feature. First create a theme with createMuiTheme and override classes such as root, container, editor, and editorContainer. Check the examples directory for more.

import { createMuiTheme, MuiThemeProvider } from '@material-ui/core/styles'
import MUIRichTextEditor from 'mui-rte'

const defaultTheme = createMuiTheme()

Object.assign(defaultTheme, {
    overrides: {
        MUIRichTextEditor: {
            root: {
                marginTop: 20,
                width: "80%"
            },
            editor: {
                borderBottom: "1px solid gray" 
            }
        }
    }
})

<MuiThemeProvider theme={defaultTheme}>
    <MUIRichTextEditor 
        label="Type something here..."
    />
</MuiThemeProvider>

API

<MUIRichTextEditor />

PropertyTypedescription
idstringoptionalBase Id name for the component HTML elements.
labelstringoptionalString to show when there is no content.
readOnlybooleanoptionalRead only mode. The toolbar is disabled by default.
valuestringoptionalDefault content to load. Should be a stringified Draft.Model.Encoding.RawDraftContentState object.
inheritFontSizebooleanoptionalInherit font size from parent. Useful for read only mode.
errorbooleanoptionalRenders the editor with an error style.
onSave(data:string) => voidoptionalFunction triggered when the save button is pressed. The data is a stringified Draft.Model.Encoding.RawDraftContentState object.
onChange(state: EditorState) => voidoptionalFunction triggered on any change in the editor (key input, delete, etc.). The state is a Draft.Model.ImmutableData.EditorState object.
controlsstring[]optionalList of controls to display in the main toolbar. If not provided, all controls will be rendered. Current available values are: "title", "bold", "italic", "underline", "strikethrough", "highlight", "undo", "redo", "link", "media", "numberList", "bulletList", "quote", "code", "clear", "save".
customControlsTCustomControl[]optionalDefines an array of user custom inline styles, blocks and callbacks. See more information in 'Custom Controls' below.
decoratorsTDecorator[]optionalDefines an array of user custom decorators. See more information in 'Custom Decorators'.
toolbarbooleanoptionalDefines if the main toolbar should be rendered.
inlineToolbarbooleanoptionalDefines if the inline toolbar should be rendered.
inlineToolbarControlsstring[]optionalList of controls to display in the inline toolbar. Available values are: "bold", "italic", "underline", "strikethrough", "highlight", "link", "clear", and user defined inline controls. If not provided and inlineToolbar is true the following inline styles will be displayed: bold, italic, underline and clear.


TCustomControl

PropertyTypedescription
idstringoptionalThe HTML id attribute for the control
namestringrequiredThe name of the custom control. For rendering the control this name should be added to the MUIRichTextEditor controls property.
iconJSX.ElementoptionalThe @material-ui/icons icon for the control. For "atomic" control type, the icon is not required. Check this for available icons.
componentReact.FunctionComponent<TToolbarComponentProps>optionalThe custom function component for the control. The icon has priority over the component, so if the icon is set the component will be ignored. For "atomic" control type, the component is not required.
typestringrequiredEither "inline", "block", "atomic" or "callback"
inlineStylestringoptionalThe React.CSSProperties object for styling the text when using a custom inline style.
blockWrapperReact.ReactElementoptionalThe custom React component used for rendering a custom block.
atomicComponentReact.FunctionComponentoptionalThe custom React FunctionComponent used for rendering a custom atomic block.
onClick`(editorState: EditorState, name: string, anchor: HTMLElementnull) => EditorStatevoid`


TToolbarComponentProps

|Property|Type|description| |---|---|---|---| |id|string|The id for the component.| |onMouseDown|(e: React.MouseEvent) => void|The mousedown handler.| |active|boolean|Defines if the block or inline type is active for the current editor selection.| |disabled|boolean|Sets if the toolbar is disabled.|


TDecorator

PropertyTypedescription
componentReact.FunctionComponentrequiredThe React component to use for rendering the decorator.
regexRegExprequiredThe regular expression to match a decorator.


Changelog

Check the release notes for the changelog.

Development

For development use:

$ npm run watch
$ npm run serve

Future plans

  • Add new features
  • Increase test coverage

Suggestions and issues

Please feel free to leave your comment on the Issues tab.

License

Licensed under MIT License.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free