# コードブロック {#code-block}

Source: [React エディターにコードブロックを追加する · Yurich](https://leeyujoo.com/yurich/ja/features/code-block/)

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

Package version: `0.1.2`

複数行のコードを独立したブロックに表示します。

## 試してみる {#try-it}

段落内で **コードブロック** を押すと `<pre>editor.execute('toggleBold');</pre>` になります。

## エディターに追加する {#setup}

`CodeBlock` を有効にし、`toggleCodeBlock` で `<pre>` に変換します。

```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')}
      >
        コードブロック
      </button>
      <EditorContent ariaLabel="エディター" />
    </EditorProvider>
  );
}
```

## 動作の補足 {#behavior}

プレーンテキストを貼り付けても改行が保たれます。ハイライトはアプリ側で追加できます。

[機能](https://leeyujoo.com/yurich/ja/features/)
