Document boundaries and rejected controlled edits
Switch an uncontrolled document without carrying its defaults or history into another record. Rejected controlled edits restore the accepted JSON; unsupported content in a new document never reveals the previous document.
Keyboard
- Tab enters either editor. Rejected controlled edits keep the accepted text and named form value aligned.
- Switch a document identity to start fresh defaults and history; native reset uses the current document default.
States covered
- uncontrolled document identity
- fresh reset defaults
- controlled rejection
- invalid new document
- separate history
- native canonical JSON
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-rich-text-document-boundaries.jsonComplete source
/*!
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 {useId,useState} from 'react';
import {Button,Checkbox,RichTextEditor,type RichTextDocument,UIProvider} from '@bld8/ui';
const first:RichTextDocument={type:'doc',content:[{type:'paragraph',content:[{type:'text',text:'Private document A.'}]}]};
const second:RichTextDocument={type:'doc',content:[{type:'paragraph',content:[{type:'text',text:'Document B starts here.'}]}]};
export default function DocumentBoundaries(){const form=useId(),[record,setRecord]=useState('a'),[value,setValue]=useState(first),[identity,setIdentity]=useState('accepted'),[reject,setReject]=useState(true),[attempts,setAttempts]=useState(0);return <UIProvider><div style={{display:'grid',gap:24,maxWidth:760}}>
<RichTextEditor label="Uncontrolled record" name="record" form={form} documentKey={record} defaultValue={record==='a'?first:second}/>
<Button variant="secondary" onClick={()=>setRecord('b')}>Open second uncontrolled document</Button>
<RichTextEditor label="Controlled boundary note" name="controlled" form={form} documentKey={identity} value={value} onValueChange={next=>{setAttempts(count=>count+1);if(!reject)setValue(next);}}/>
<Checkbox label="Reject controlled edits" checked={reject} onCheckedChange={setReject}/>
<div style={{display:'flex',gap:12,flexWrap:'wrap'}}><Button variant="secondary" onClick={()=>{setValue({type:'doc',content:[{type:'image'}]} as unknown as RichTextDocument);setIdentity('unsupported');}}>Open unsupported controlled document</Button><Button variant="ghost" onClick={()=>{setValue(second);setIdentity('second');}}>Open second controlled document</Button></div>
<form id={form}><Button variant="secondary" type="reset">Reset boundary documents</Button></form><p data-rejected-attempts>{'Edit attempts: '+attempts}</p>
</div></UIProvider>;}