tiptap/docs/installation/react.md
svenadlung bdc51d12b5 Merge branch 'main' into develop
# Conflicts:
#	CHANGELOG.md
#	demos/CHANGELOG.md
#	demos/package-lock.json
#	demos/package.json
#	demos/src/Examples/CollaborativeEditing/React/index.jsx
#	lerna.json
#	package-lock.json
#	packages/core/CHANGELOG.md
#	packages/core/package.json
#	packages/extension-blockquote/CHANGELOG.md
#	packages/extension-blockquote/package.json
#	packages/extension-bold/CHANGELOG.md
#	packages/extension-bold/package.json
#	packages/extension-bubble-menu/CHANGELOG.md
#	packages/extension-bubble-menu/package.json
#	packages/extension-bullet-list/CHANGELOG.md
#	packages/extension-bullet-list/package.json
#	packages/extension-character-count/CHANGELOG.md
#	packages/extension-character-count/package.json
#	packages/extension-code-block-lowlight/CHANGELOG.md
#	packages/extension-code-block-lowlight/package.json
#	packages/extension-code-block/CHANGELOG.md
#	packages/extension-code-block/package.json
#	packages/extension-code/CHANGELOG.md
#	packages/extension-code/package.json
#	packages/extension-collaboration-cursor/CHANGELOG.md
#	packages/extension-collaboration-cursor/package.json
#	packages/extension-collaboration/CHANGELOG.md
#	packages/extension-collaboration/package.json
#	packages/extension-color/CHANGELOG.md
#	packages/extension-color/package.json
#	packages/extension-document/CHANGELOG.md
#	packages/extension-document/package.json
#	packages/extension-dropcursor/CHANGELOG.md
#	packages/extension-dropcursor/package.json
#	packages/extension-floating-menu/CHANGELOG.md
#	packages/extension-floating-menu/package.json
#	packages/extension-focus/CHANGELOG.md
#	packages/extension-focus/package.json
#	packages/extension-font-family/CHANGELOG.md
#	packages/extension-font-family/package.json
#	packages/extension-gapcursor/CHANGELOG.md
#	packages/extension-gapcursor/package.json
#	packages/extension-hard-break/CHANGELOG.md
#	packages/extension-hard-break/package.json
#	packages/extension-heading/CHANGELOG.md
#	packages/extension-heading/package.json
#	packages/extension-highlight/CHANGELOG.md
#	packages/extension-highlight/package.json
#	packages/extension-history/CHANGELOG.md
#	packages/extension-history/package.json
#	packages/extension-horizontal-rule/CHANGELOG.md
#	packages/extension-horizontal-rule/package.json
#	packages/extension-image/CHANGELOG.md
#	packages/extension-image/package.json
#	packages/extension-italic/CHANGELOG.md
#	packages/extension-italic/package.json
#	packages/extension-link/CHANGELOG.md
#	packages/extension-link/package.json
#	packages/extension-list-item/CHANGELOG.md
#	packages/extension-list-item/package.json
#	packages/extension-mention/CHANGELOG.md
#	packages/extension-mention/package.json
#	packages/extension-ordered-list/CHANGELOG.md
#	packages/extension-ordered-list/package.json
#	packages/extension-paragraph/CHANGELOG.md
#	packages/extension-paragraph/package.json
#	packages/extension-placeholder/CHANGELOG.md
#	packages/extension-placeholder/package.json
#	packages/extension-strike/CHANGELOG.md
#	packages/extension-strike/package.json
#	packages/extension-subscript/CHANGELOG.md
#	packages/extension-subscript/package.json
#	packages/extension-superscript/CHANGELOG.md
#	packages/extension-superscript/package.json
#	packages/extension-table-cell/CHANGELOG.md
#	packages/extension-table-cell/package.json
#	packages/extension-table-header/CHANGELOG.md
#	packages/extension-table-header/package.json
#	packages/extension-table-row/CHANGELOG.md
#	packages/extension-table-row/package.json
#	packages/extension-table/CHANGELOG.md
#	packages/extension-table/package.json
#	packages/extension-task-item/CHANGELOG.md
#	packages/extension-task-item/package.json
#	packages/extension-task-list/CHANGELOG.md
#	packages/extension-task-list/package.json
#	packages/extension-text-align/CHANGELOG.md
#	packages/extension-text-align/package.json
#	packages/extension-text-style/CHANGELOG.md
#	packages/extension-text-style/package.json
#	packages/extension-text/CHANGELOG.md
#	packages/extension-text/package.json
#	packages/extension-typography/CHANGELOG.md
#	packages/extension-typography/package.json
#	packages/extension-underline/CHANGELOG.md
#	packages/extension-underline/package.json
#	packages/extension-youtube/CHANGELOG.md
#	packages/extension-youtube/package.json
#	packages/html/CHANGELOG.md
#	packages/html/package.json
#	packages/pm/CHANGELOG.md
#	packages/pm/package.json
#	packages/react/CHANGELOG.md
#	packages/react/package.json
#	packages/starter-kit/CHANGELOG.md
#	packages/starter-kit/package.json
#	packages/suggestion/CHANGELOG.md
#	packages/suggestion/package.json
#	packages/vue-2/CHANGELOG.md
#	packages/vue-2/package.json
#	packages/vue-3/CHANGELOG.md
#	packages/vue-3/package.json
2023-07-17 13:03:44 +02:00

4.3 KiB
Raw Blame History

title tableOfContents
React WYSIWYG true

React

Introduction

The following guide describes how to integrate Tiptap with your React project. Were using Create React App here, but the workflow should be similar with other setups.

Create React App

Quickstart

If you just want to get up and running with Tiptap you can use the Tiptap Create React App template by @alb to create a new project with all the steps listed below completed already.

npx create-react-app my-tiptap-project --template tiptap

1. Create a project (optional)

Lets start with a fresh React project called my-tiptap-project. Create React App will set up everything we need.

# create a project with npm
npx create-react-app my-tiptap-project

# change directory
cd my-tiptap-project

2. Install the dependencies

Time to install the @tiptap/react package, @tiptap/pm (the ProseMirror library) and @tiptap/starter-kit, which includes the most popular extensions to get started quickly.

npm install @tiptap/react @tiptap/pm @tiptap/starter-kit

If you followed step 1 and 2, you can now start your project with npm run start, and open http://localhost:3000 in your browser.

3. Create a new component

To actually start using Tiptap we need to create a new component. Lets call it Tiptap and put the following example code in src/Tiptap.jsx.

// src/Tiptap.jsx
import { EditorProvider, FloatingMenu, BubbleMenu } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'

// define your extension array
const extensions = [
  StarterKit,
]

const content = '<p>Hello World!</p>'

const Tiptap = () => {
  return (
    <EditorProvider extensions={extensions} content={content}>
      <FloatingMenu>This is the floating menu</FloatingMenu>
      <BubbleMenu>This is the bubble menu</BubbleMenu>
    </EditorProvider>
  )
}

export default Tiptap

Important Note: You can always use the useEditor hook if you want to avoid using the Editor context.

// src/Tiptap.jsx
import { useEditor, EditorContent FloatingMenu, BubbleMenu } from '@tiptap/react'
import StarterKit from '@tiptap/starter-kit'

// define your extension array
const extensions = [
  StarterKit,
]

const content = '<p>Hello World!</p>'

const Tiptap = () => {
  const editor = useEditor({
    extensions,
    content,
  })

  return (
    <>
      <EditorContent editor={editor} />
      <FloatingMenu editor={editor}>This is the floating menu</FloatingMenu>
      <BubbleMenu editor={editor}>This is the bubble menu</BubbleMenu>
    </>
  )
}

export default Tiptap

4. Add it to your app

Finally, replace the content of src/App.js with our new Tiptap component.

import Tiptap from './Tiptap.jsx'

const App = () => {
  return (
    <div className="App">
      <Tiptap />
    </div>
  )
}

export default App

5. Consume the Editor context in child components

If you use the EditorProvider to setup your Tiptap editor, you can now easily access your editor instance from any child component using the useCurrentEditor hook.

import { useCurrentEditor } from '@tiptap/react'

const EditorJSONPreview = () => {
  const { editor } = useCurrentEditor()

  return (
    <pre>
      {JSON.stringify(editor.getJSON(), null, 2)}
    </pre>
  )
}

Important: This won't work if you use the useEditor hook to setup your editor.

You should now see a pretty barebones example of Tiptap in your browser.

6. Add before or after slots

Since the EditorContent component is rendered by the EditorProvider component, we now can't directly define where to render before or after content of our editor. For that we can use the slotBefore & slotAfter props on the EditorProvider component.

<EditorProvider
  extensions={extensions}
  content={content}
  slotBefore={<MyEditorToolbar />}
  slotAfter={<MyEditorFooter />}
/>

7. The complete setup (optional)

Ready to add more? Below is a demo that shows how you could set up a basic toolbar. Feel free to take it and start customizing it to your needs:

https://embed.tiptap.dev/preview/Examples/Default