← All Articles
React, Next.js, TiptapSep 22, 2024◷ 7 min read

How to Integrate Tiptap with React and Next.js

How to Integrate Tiptap with React and Next.js

In this tutorial, we will guide you through integrating Tiptap, a rich text editor built on ProseMirror, with React and Next.js. We’ll start by creating a basic text editor using Tiptap’s Starter Kit, and then expand it with tools like the Floating Menu and Bubble Menu for formatting.

Part 1: Building a Basic Editor with Tiptap

Prerequisites

  • Basic knowledge of React and Next.js.
  • A Next.js project set up.

Step 1: Setting Up the Project

If you don’t have a Next.js project yet, create one by running:

bash
npx create-next-app@latest tiptap-nextjs
cd tiptap-nextjs

Start the development server:

bash
npm run dev

Step 2: Installing Basic Tiptap Dependencies

For the basic editor, you'll need the following Tiptap packages:

bash
npm install @tiptap/react @tiptap/pm @tiptap/starter-kit @tiptap/extension-placeholder
  • @tiptap/react: React bindings for Tiptap.
  • @tiptap/pm: A wrapper package for ProseMirror (important!!).
  • @tiptap/starter-kit: A set of basic editor functionality like bold, italic, bullet points, etc.
  • @tiptap/extension-placeholder: Adds a placeholder when the editor is empty.

Step 3: Creating the Basic Rich Text Editor Component

Now, create a simple editor with Tiptap using the starter kit and a placeholder extension.

tsx
1// components/RichTextEditor.tsx
2
3"use client";
4
5import { useEditor, EditorContent } from "@tiptap/react";
6import StarterKit from "@tiptap/starter-kit";
7import Placeholder from "@tiptap/extension-placeholder";
8import { useEffect } from "react";
9
10interface TiptapProps {
11  value: string;
12  placeholder?: string;
13  className?: string;
14  onChange: (value: string) => void;
15}
16
17const Tiptap: React.FC<TiptapProps> = ({ value, onChange, placeholder, className }) => {
18  const editor = useEditor({
19    extensions: [
20      StarterKit,
21      // Provides basic editor functionality
22      Placeholder.configure({ placeholder }), // Adds a placeholder when the editor is empty
23    ],
24    content: value,
25    editorProps: {
26      attributes: {
27        class: `prose prose-sm sm:prose lg:prose-lg xl:prose-2xl mx-auto focus:outline-none bg-slate-200/20 rounded-md px-4 min-h-20 ${className}`,
28      },
29    },
30    onUpdate: ({ editor }) => {
31      onChange(editor.getHTML());
32    },
33  });
34
35  useEffect(() => {
36    if (editor && value !== editor.getHTML()) {
37      editor.commands.setContent(value);
38    }
39  }, [value, editor]);
40
41  if (!editor) return null;
42
43  return <EditorContent editor={editor} />;
44};
45
46export default Tiptap;

Step 4: Styling the Basic Editor

Here’s the CSS to style the basic editor:

css
1/* styles.css */
2.tiptap {
3  :first-child {
4    margin-top: 0;
5  }
6
7  /* Basic headings and lists */
8  h1, h2, h3, h4, h5, h6 {
9    margin: 0.5rem 0;
10    font-weight: bold;
11  }
12
13  ul {
14    list-style-type: disc;
15    padding-left: 1rem;
16  }
17
18  ol {
19    list-style-type: decimal;
20    padding-left: 1rem;
21  }
22}

At this point, you’ve created a simple rich text editor using Tiptap’s Starter Kit with placeholder functionality.

But come on, we can’t stop there! A basic editor is like having pizza without toppings—it’s good, but it could be so much better. Let’s make it fun! 🎉 Time to give our editor some interactive spice and take things up a notch.


Part 2: Extending the Editor with Tools and Menus

Now that we’ve laid the foundation, let’s bring in the bells and whistles! We'll add a Floating Menu and a Bubble Menu because what’s an editor without some formatting flair? These tools will let users format their content with ease, giving them the power they never knew they needed.

Step 1: Install Additional Dependencies

Before we get into coding the menus, we need a few extra tools. Let’s install React Icons and a UI component library to help us build some cool interactive elements. For this example, we'll be using shadcn/ui for components like toggles, but you can use any UI library of your choice.

To install React Icons, run:

bash
npm install react-icons

Next, set up shadcn/ui by following their official guide. This will help us easily create UI components such as Toggles for our formatting options.

Now with everything installed, we're ready to add some fun interaction!

Step 2: Adding the Floating Menu

To enhance our editor, we’ll configure it with extensions for headings, bullet lists, and ordered lists. These extensions will power the formatting options in our Floating Menu.

First, install the required extensions:

bash
npm install @tiptap/extension-heading @tiptap/extension-bullet-list @tiptap/extension-ordered-list @tiptap/extension-list-item

Now, update the

text
RichTextEditor
component to support these new features:

tsx
1// components/RichTextEditor.tsx
2
3"use client";
4
5import { useEditor, EditorContent } from "@tiptap/react";
6import StarterKit from "@tiptap/starter-kit";
7import Placeholder from "@tiptap/extension-placeholder";
8import Heading from "@tiptap/extension-heading";  // New extension for headings
9import BulletList from "@tiptap/extension-bullet-list";  // New extension for bullet lists
10import OrderedList from "@tiptap/extension-ordered-list";  // New extension for ordered lists
11import { useEffect } from "react";
12import FloatingMenuComponent from "./FloatingMenuComponent";  // Include the floating menu we created
13import BubbleMenuComponent from "./BubbleMenuComponent";  // ... (additional imports)

Step 3: Adding the Bubble Menu

The Bubble Menu floats near selected text and provides inline formatting options like bold, italic, and strikethrough.

Step 4: Adding Styling for Floating and Bubble Menus

Here’s some basic CSS for the floating and bubble menus:

css
1/* Floating menu styles */
2.floating-menu {
3  display: flex;
4  background: white;
5  border: 1px solid #ddd;
6  border-radius: 6px;
7  padding: 4px;
8  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
9}
10
11/* Bubble menu styles */
12.bubble-menu {
13  display: flex;
14  background: white;
15  border: 1px solid #ddd;
16  border-radius: 6px;
17  padding: 4px;
18  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
19}

Congratulations! You've successfully integrated a powerful Tiptap editor into your Next.js application. With the addition of floating and bubble menus, you've transformed a basic text editor into a dynamic content creation tool. Now, users can easily format their text, add headings, and create lists—all while enjoying a smooth and interactive experience. This setup not only enhances user engagement but also opens the door to more advanced features like collaborative editing or custom extensions in the future. If you’d like to dive deeper, check out the full code in my GitHub repository. Happy coding, and may your editor empower countless creators to share their stories!

Published on September 22, 2024