# Code block {#code-block}

Source: [Add code blocks to a React editor · Yurich](https://leeyujoo.com/yurich/features/code-block/)

Package: [`yurich`](https://www.npmjs.com/package/yurich)

Package version: `0.1.2`

Give multiline code its own preformatted block.

## Try it {#try-it}

Put the caret in the sample paragraph and click **Code block**. The result is `<pre>editor.execute('toggleBold');</pre>`. A plain-text paste inside this block keeps its line breaks.

## Add it to your editor {#setup}

Enable `CodeBlock` and use `toggleCodeBlock` for standalone, preformatted code. The command creates a `<pre>` block.

```tsx
import { EditorContent, EditorProvider, CodeBlock, useEditor, useIsActive } from 'yurich';

export function CodeBlockEditor() {
  const editor = useEditor({
    extensions: [CodeBlock],
    initialValue: "<p>editor.execute('toggleBold');</p>",
  });
  const active = useIsActive(editor, 'toggleCodeBlock');

  return (
    <EditorProvider editor={editor}>
      <button
        type="button"
        aria-pressed={active}
        onMouseDown={event => event.preventDefault()}
        onClick={() => editor.execute('toggleCodeBlock')}
      >
        Code block
      </button>
      <EditorContent ariaLabel="Editor" />
    </EditorProvider>
  );
}
```

## Behavior to account for {#behavior}

Add any code theme or syntax highlighting in the application. For short expressions that belong within a sentence, use [inline code](https://leeyujoo.com/yurich/features/code/).

[Features](https://leeyujoo.com/yurich/features/)
