Skip to content

createReactBlockSpec weird behaviour for placeholder and cursor for custom blocks #1802

Open
@kanishk619

Description

@kanishk619

Description

When creating a custom block using createReactBlockSpec, I encountered several rendering issues that make it impractical for use in certain scenarios.

❌ Problems Encountered

  • Placeholders do not appear when the block is empty
  • Cursor disappears if the context text is entirely removed

✅ Workaround

Switching to createStronglyTypedTiptapNode resolves the issues.

💡 Example (Working)

import {
  createStronglyTypedTiptapNode,
  createBlockSpecFromStronglyTypedTiptapNode,
  propsToAttributes,
  createDefaultBlockDOMOutputSpec,
  defaultProps,
} from "@blocknote/core";

const sectionPropSchema = {
  ...defaultProps,
};

const SectionBlockContent = createStronglyTypedTiptapNode({
  name: "section",
  content: "inline*",
  group: "blockContent",

  addAttributes() {
    return propsToAttributes(sectionPropSchema);
  },

  parseHTML() {
    return [
      {
        tag: "div[data-content-type=section]",
        contentElement: ".bn-inline-content",
      },
      {
        tag: "section",
        getAttrs: (element) => {
          if (typeof element === "string" || !element.textContent?.trim()) {
            return false;
          }
          return {};
        },
        node: "section",
      },
    ];
  },

  renderHTML({ HTMLAttributes }) {
    return createDefaultBlockDOMOutputSpec(
      this.name,
      "section",
      {
        ...HTMLAttributes,
      }
    );
  },
});

export const Section = createBlockSpecFromStronglyTypedTiptapNode(
  SectionBlockContent,
  sectionPropSchema
);

Not sure if I am missing something with createReactBlockSpec

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions