import React, { forwardRef, useEffect, useImperativeHandle, useRef, } from 'react'; import type { CoreEditor } from '@kerebron/editor'; export interface EditorContentProps { /** The editor instance from useEditor */ editor: CoreEditor | null; /** Additional class name */ className?: string; /** Additional inline styles */ style?: React.CSSProperties; } export interface EditorContentRef { /** Get the container element */ getElement: () => HTMLDivElement | null; } /** * EditorContent renders the Kerebron editor. * Use with the useEditor hook. * * @example * ```tsx * const editor = useEditor({ editorKits: [StarterKit] }) * return * ``` */ export const EditorContent = forwardRef( function EditorContent( { editor, className = '', style }: EditorContentProps, ref: React.ForwardedRef, ) { const containerRef = useRef(null); useImperativeHandle(ref, () => ({ getElement: () => containerRef.current, })); useEffect(() => { const container = containerRef.current; if (!container || !editor) return; // Get the editor's DOM element and append it to our container const editorDom = editor.view?.dom as HTMLElement | undefined; if (editorDom && editorDom.parentElement !== container) { container.appendChild(editorDom); } return () => { // Don't remove on cleanup - editor.destroy() handles this }; }, [editor]); return (
); }, );