kabulmark
Version:
A React-based rich text editor built as a wrapper over Meta's Lexical library.
21 lines (20 loc) • 1.37 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
import { LexicalErrorBoundary } from "@lexical/react/LexicalErrorBoundary";
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
import { $getRoot } from "lexical";
import { useEffect, useState } from "react";
export default function EditorContent({ placeholder, height }) {
const [editor] = useLexicalComposerContext();
const [isEmpty, setIsEmpty] = useState(true);
useEffect(() => {
const unregister = editor.registerUpdateListener(({ editorState }) => {
editorState.read(() => {
setIsEmpty($getRoot().getChildrenSize() === 0);
});
});
return unregister;
}, [editor]);
return (_jsxs("div", { className: "relative", children: [_jsx(RichTextPlugin, { contentEditable: _jsx(ContentEditable, { className: "editor-content font-editor text-editor-text", style: { height, overflowY: "auto" }, role: "textbox", "aria-multiline": "true", "aria-label": "Rich text editing area" }), placeholder: null, ErrorBoundary: LexicalErrorBoundary }), isEmpty && (_jsx("div", { className: "editor-placeholder absolute top-4 left-4", children: placeholder }))] }));
}