Reflex Logo

Intro

Gallery

Hosting

Learn

Components

Recipes

API Reference

Onboarding

Library

/

Forms

/

Editor

An HTML editor component based on Suneditor.

Editor content

import reflex as rx


class EditorState(rx.State):
    content: str = "<p>Editor content</p>"

    def handle_change(self, content: str):
        """Handle the editor value change."""
        self.content = content


def editor_example():
    return rx.vstack(
        rx.editor(
            set_contents=EditorState.content,
            on_change=EditorState.handle_change,
        ),
        rx.box(
            rx.html(EditorState.content),
            border="1px dashed #ccc",
            border_radius="4px",
            width="100%",
        ),
    )

The extended options and toolbar buttons can be customized by passing an instance of rx.EditorOptions for the set_options prop.

FieldDescription
default_tag Specifies default tag name of the editor. default: 'p' {String}
mode The mode of the editor ('classic', 'inline', 'balloon', 'balloon-always'). default: 'classic' {String}
rtl If true, the editor is set to RTL(Right To Left) mode. default: false {Boolean}
button_list List of buttons to use in the toolbar.

The button_list prop expects a list of lists, where each sublist contains the names of buttons forming a group on the toolbar. The character "/" can be used in place of a sublist to denote a line break in the toolbar.

Some valid button_list options are enumerated in rx.EditorButtonList, seen below.

class EditorButtonList(list, enum.Enum):
    BASIC = [
        ["font", "fontSize"],
        ["fontColor"],
        ["horizontalRule"],
        ["link", "image"],
    ]
    FORMATTING = [
        ["undo", "redo"],
        [
            "bold",
            "underline",
            "italic",
            "strike",
            "subscript",
            "superscript",
        ],
        ["removeFormat"],
        ["outdent", "indent"],
        ["fullScreen", "showBlocks", "codeView"],
        ["preview", "print"],
    ]
    COMPLEX = [
        ["undo", "redo"],
        ["font", "fontSize", "formatBlock"],
        [
            "bold",
            "underline",
            "italic",
            "strike",
            "subscript",
            "superscript",
        ],
        ["removeFormat"],
        "/",
        ["fontColor", "hiliteColor"],
        ["outdent", "indent"],
        ["align", "horizontalRule", "list", "table"],
        ["link", "image", "video"],
        ["fullScreen", "showBlocks", "codeView"],
        ["preview", "print"],
        ["save", "template"],
    ]

A custom list of toolbar buttons may also be specified using these names as seen in the following example.

Since this example uses the same state as above, the two editors contents are shared and can be modified by either one.

rx.editor(
    set_contents=EditorState.content,
    set_options=rx.EditorOptions(
        button_list=[
            ["font", "fontSize", "formatBlock"],
            ["fontColor", "hiliteColor"],
            [
                "bold",
                "underline",
                "italic",
                "strike",
                "subscript",
                "superscript",
            ],
            ["removeFormat"],
            "/",
            ["outdent", "indent"],
            ["align", "horizontalRule", "list", "table"],
            ["link"],
            ["fullScreen", "showBlocks", "codeView"],
            ["preview", "print"],
        ]
    ),
    on_change=EditorState.handle_change,
)

See the Suneditor README.md for more details on buttons and options.

A Rich Text Editor component based on SunEditor. Not every JS prop is listed here (some are not easily usable from python), refer to the library docs for a complete list.

PropTypeDefault ValueValues
lang
Union[Literal, dict]
name
str
default_value
str
width
str
height
str
placeholder
str
auto_focus
bool
set_options
Dict
set_all_plugins
bool
set_contents
str
append_contents
str
set_default_style
str
disable
bool
hide
bool
hide_toolbar
bool
disable_toolbar
bool
toggle_code_view
Annotated
toggle_full_screen
Annotated

Event Triggers

TriggerDescription
on_blur

Fired when the editor loses focus.

on_change

Fired when the editor content changes.

on_input

Fired when the something is inputted in the editor.

on_load

Fired when the editor is loaded.

on_resize_editor

Fired when the editor is resized.

on_copy

Fired when the editor content is copied.

on_cut

Fired when the editor content is cut.

on_paste

Fired when the editor content is pasted.

toggle_code_view

The toggle_code_view event handler is called when the user toggles code view. It receives a boolean whether code view is active.

toggle_full_screen

The toggle_full_screen event handler is called when the user toggles full screen. It receives a boolean whether full screen is active.

← DebounceForm →

Did you find this useful?

HomeGalleryChangelogIntroductionHosting