Rich-text editor example

Controlled content without cursor jumps.

Echo a fresh JSON object on each edit while preserving the active selection. Apply a new document identity to start a separate history, and inspect escaped HTML output as text.

Complete React client module · MIT · Next.js and Vite

Interactive example

Controlled content without cursor jumps

Echo a fresh JSON object on each edit while preserving the active selection. Apply a new document identity to start a separate history, and inspect escaped HTML output as text.

Keyboard

  • Edit in the middle of a sentence; controlled JSON echoes keep the caret in place.
  • Use formatting or keyboard shortcuts, then Undo/Redo. Replace the document to begin a new history.
  • Select text and use Edit link; unsafe destinations are rejected without changing the document.

States covered

  • Controlled JSON
  • Selection-safe echoes
  • Undo and redo
  • Document identity
  • Safe link editing
  • Safe HTML serialization

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-rich-text-controlled-document.json
Complete source
React / TypeScript
/*!
MIT License

Copyright (c) 2026 Jaison

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import {useState} from 'react';
import {Button,Checkbox,RichTextEditor,richTextToHtml,type RichTextDocument,UIProvider} from '@bld8/ui';
const first:RichTextDocument={type:'doc',content:[{type:'paragraph',content:[{type:'text',text:'Keep the useful details together.'}]}]};
export default function ControlledRichText(){const [value,setValue]=useState(first),[identity,setIdentity]=useState(0),[readOnly,setReadOnly]=useState(false);return <UIProvider><div style={{display:'grid',gap:20,maxWidth:760}}>
  <RichTextEditor label="Controlled note" name="controlledNote" value={value} onValueChange={next=>setValue(JSON.parse(JSON.stringify(next)))} documentKey={String(identity)} readOnly={readOnly}/>
  <div style={{display:'flex',gap:12,flexWrap:'wrap'}}><Button variant="secondary" onClick={()=>{setValue({type:'doc',content:[{type:'paragraph',content:[{type:'text',text:'A separate review.'}]}]});setIdentity(value=>value+1);}}>Replace document</Button><Checkbox label="Read-only note" checked={readOnly} onCheckedChange={setReadOnly}/></div>
  <pre data-rich-html style={{whiteSpace:'pre-wrap',overflowWrap:'anywhere',maxWidth:'100%'}}>{richTextToHtml(value)}</pre>
</div></UIProvider>;}