TypeCellOS / BlockNote

A React Rich Text Editor that's block-based (Notion style) and extensible. Built on top of Prosemirror and Tiptap.
https://www.blocknotejs.org/
Mozilla Public License 2.0
5.89k stars 381 forks source link

Having trouble with autofocus feature - TypeError: Cannot read properties of undefined (reading 'focus') #891

Open softmarshmallow opened 2 days ago

softmarshmallow commented 2 days ago

The editor is typed NonNullable by useCreateBlockNote, but It cannot be accessed (or at least not mounted) on initial hook call.

Screenshot 2024-06-30 at 8 19 46 PM
  const editor = useCreateBlockNote({
    schema: schema,
    initialContent: safevalue(defaultValue),
  });

  useEffect(() => {
    if (!editor) return;
    if (!editor.focus) return;
    editor.focus();
  }, [editor]);

Am I missing something here?

Below will work when called via click event

     <div
          className="prose dark:prose-invert mx-auto w-full"
          onClick={() => editor.focus()}
        >
          <ThemedRichTextEditorContent
            onKeyDown={(e) => {
              // this is required for preventing exit on enter pressed
              e.stopPropagation();
            }}
            editor={editor}
          />
        </div>
YousefED commented 1 day ago

This seems unexpected. Are you using the latest version of BlockNote? Could you perhaps share a Stackblitz reproducing this issue?