Skip to content

Type Editor


Type Editor / @type-editor/commands / toggle-block-type / toggleBlockType

Function: toggleBlockType()

ts
function toggleBlockType(
  nodeType,
  unwrapNodeType,
  attrs?,
  allowUnwrap?,
): Command;

Defined in: toggle-block-type.ts:23

Creates a command that toggles between two block types.

If the selection is currently inside a node of the specified nodeType and unwrapping is allowed, it will convert the block to unwrapNodeType. Otherwise, it will convert the block to nodeType.

This is useful for implementing toggle buttons (e.g., toggling between a heading and a paragraph).

Parameters

ParameterTypeDefault valueDescription
nodeTypeNodeTypeundefinedThe node type to toggle to when not already in this type.
unwrapNodeTypeNodeTypeundefinedThe node type to revert to when already inside nodeType.
attrsReadonly<Record<string, any>>nullOptional attributes to apply to the new block node.
allowUnwrapbooleantrueWhether to allow toggling back to unwrapNodeType. Defaults to true.

Returns

Command

A command function that toggles the block type when executed.