---
title: "Add a button to toggle block-level nodes"
description: "Toggle through block-level nodes like code blocks with the code block button. More in the docs."
---

> This page is for Tiptap 2.x. [See the latest version of this page.](https://tiptap.dev/docs/ui-components/components/code-block-button.md)

# Add a button to toggle block-level nodes

Toggle through block-level nodes like code blocks with the code block button. More in the docs.

> **Upgrade to v3**
>
> To use UI components or templates you need to upgrade to Editor version 3.x. See the [upgrade
> guide](https://tiptap.dev/docs/resources/whats-new) for more details.

Toggle through block-level nodes like code blocks with the code block button.

> **Interactive demo:** [code block button](https://template.tiptap.dev/preview/tiptap-ui/code-block-button)

## Installation

You can add the component via Tiptap CLI (for Vite or Next.js)

```bash
npx @tiptap/cli add code-block-button
```

### Manual Integration

For frameworks other than Vite or Next.js, add the component manually from the [open-source repository](https://github.com/ueberdosis/tiptap-ui-components/tree/main/apps/web/src/components/tiptap-ui/code-block-button).

### Import styles

This component requires you to import our styles which were added to `styles/keyframe-animation.scss` and `styles/_variables.scss`.

## Usage

```tsx
import { EditorContent, EditorContext, useEditor } from '@tiptap/react'
import { StarterKit } from '@tiptap/starter-kit'
import { CodeBlockButton } from '@/components/tiptap-ui/code-block-button'

import '@/components/tiptap-node/code-block-node/code-block-node.scss'

export default function MyEditor() {
  const editor = useEditor({
    immediatelyRender: false,
    extensions: [StarterKit],
    content: `
        <pre><code>console.log('Hello, World!');</code></pre>
        `,
  })

  return (
    <EditorContext.Provider value={{ editor }}>
      <div className="tiptap-button-group" data-orientation="horizontal">
        <CodeBlockButton />
      </div>

      <EditorContent editor={editor} role="presentation" />
    </EditorContext.Provider>
  )
}
```

## Props

| Name                | Type           | Default | Description                                     |
| ------------------- | -------------- | ------- | ----------------------------------------------- |
| editor              | Editor \| null | null    | The Tiptap editor instance                      |
| hideWhenUnavailable | boolean        | false   | Whether the button should hide if not available |
| text                | string         | -       | Text to display alongside the icon              |

## Requirements

Used reference components

- `use-tiptap-editor` (hook)
- `tiptap-utils` (lib)
- `button` (primitive)
- `code-block-icon` (icon)
