rich-text@tolipovjs

API & Props

Full prop surface plus the imperative ref API.

Props

PropTypeDefaultDescription
valuestring""Controlled HTML content
onChange(html: string) => voidFires on sanitized content change
placeholderstring"Start typing..."Empty-state text
classNamestring""Extra class on root
styleReact.CSSPropertiesInline style on root
disabledbooleanfalseDisable + grey out
readOnlybooleanfalseView-only (no editing)
theme"light" | "dark" | "auto""light"Theme
toolbarToolbarPreset | ToolbarItem[]"all"Toolbar layout
customButtonsToolbarButtonConfig[]Append custom buttons
onImageUpload(file: File) => Promise<string>Async upload — return final URL
autoFocusbooleanfalseFocus on mount
maxLengthnumberHard cap on character count
minHeightstring"300px"CSS min-height for surface
textColorPresetsstring[]24 defaultsText color swatches
backgroundColorPresetsstring[]24 defaultsBG color swatches
showStatsbooleanfalseShow word/char count
allowHtmlModebooleantrueAllow Visual ↔ HTML toggle
onFocus() => voidFocus event
onBlur() => voidBlur event
onSelectionChange() => voidSelection change event

Imperative ref API

import { useRef } from "react"
import { RichTextEditor, type RichTextEditorHandle } from "@tolipovjs/rich-text"

function Editor() {
  const ref = useRef<RichTextEditorHandle>(null)
  return (
    <>
      <button onClick={() => ref.current?.focus()}>Focus</button>
      <button onClick={() => ref.current?.clear()}>Clear</button>
      <button onClick={() => console.log(ref.current?.getHTML())}>Log HTML</button>
      <RichTextEditor ref={ref} />
    </>
  )
}

Methods

  • focus() — focus editor surface
  • blur() — blur editor
  • clear() — empty contents
  • getHTML() — return current sanitized HTML
  • setHTML(html) — replace contents with new HTML
  • insertHTML(html) — insert HTML at the current selection
  • getText() — return plain text
  • execCommand(cmd, value?) — execute an underlying command
  • getStats() — return { characters, charactersNoSpaces, words }

Types

import type {
  RichTextEditorProps,
  RichTextEditorHandle,
  ToolbarItem,
  ToolbarPreset,
  BuiltInToolbarItem,
  ToolbarButtonConfig,
  ColorPickerProps,
  SanitizeOptions,
  EditorStats,
  Theme,
} from "@tolipovjs/rich-text"

Utilities

import {
  EditorCommands,
  HTMLSanitizer,
  HistoryStack,
  htmlToMarkdown,
  RichTextEditorContext,
  useRichTextEditor,
} from "@tolipovjs/rich-text"

const md = htmlToMarkdown("<h1>Hi</h1><p>It's <strong>me</strong></p>")
// "# Hi\n\nIt's **me**"

HTMLSanitizer.sanitize(dirty)                         // safe defaults
HTMLSanitizer.sanitize(dirty, { allowStyle: true })   // permit inline style