Bundling TinyMCE in React
This guide shows how to bundle TinyMCE in a React application with the TinyMCE React component.
Prerequisites
This procedure requires Node.js (and NPM).
Procedure
-
Use Vite and React SWC to create a new React project named
tinymce-react-demo.# NPM 7+, extra double-dash is needed npm create vite@5 tinymce-react-demo -- --template react-swc -
Go to the project directory and install
@tinymce/tinymce-reactcd tinymce-react-demo && npm install @tinymce/tinymce-react -
Install the
tinymcepackage.npm install tinymce -
Add
BundledEditor.jsxand importEditorandtinymcecomponents as below.import { Editor } from '@tinymce/tinymce-react'; // Ensure to import tinymce first as other components expect // a global variable `tinymce` to exist import 'tinymce/tinymce'; // DOM model import 'tinymce/models/dom/model' // Theme import 'tinymce/themes/silver'; // Toolbar icons import 'tinymce/icons/default'; // Editor styles import 'tinymce/skins/ui/oxide/skin'; // Content styles, including inline UI like fake cursors import 'tinymce/skins/content/default/content'; import 'tinymce/skins/ui/oxide/content'; // Import plugins import 'tinymce/plugins/anchor'; import 'tinymce/plugins/advlist'; import 'tinymce/plugins/autolink'; import 'tinymce/plugins/charmap'; import 'tinymce/plugins/code'; import 'tinymce/plugins/media'; import 'tinymce/plugins/visualblocks'; import 'tinymce/plugins/fullscreen'; import 'tinymce/plugins/insertdatetime'; import 'tinymce/plugins/preview'; import 'tinymce/plugins/help'; // Include resources that a plugin lazy-loads at the run-time import 'tinymce/plugins/help/js/i18n/keynav/en'; import 'tinymce/plugins/image'; import 'tinymce/plugins/link'; import 'tinymce/plugins/lists'; import 'tinymce/plugins/searchreplace'; import 'tinymce/plugins/table'; import 'tinymce/plugins/wordcount'; export default function BundledEditor(props) { return ( <Editor licenseKey='gpl' {...props} /> ); } -
Add
BundledEditorcomponent with configuration intoApp.jsx.import { useRef } from 'react'; import BundledEditor from './BundledEditor' import './App.css'; export default function App() { const editorRef = useRef(null); const log = () => { if (editorRef.current) { console.log(editorRef.current.getContent()); } }; return ( <> <BundledEditor onInit={(_evt, editor) => editorRef.current = editor} initialValue='<p>This is the initial content of the editor.</p>' init={{ height: 500, menubar: false, plugins: [ 'advlist', 'autolink', 'lists', 'link', 'image', 'charmap', 'anchor', 'searchreplace', 'visualblocks', 'code', 'fullscreen', 'insertdatetime', 'media', 'table', 'preview', 'help', 'wordcount' ], toolbar: 'undo redo | blocks | ' + 'bold italic forecolor | alignleft aligncenter ' + 'alignright alignjustify | bullist numlist outdent indent | ' + 'removeformat | help', content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:14px }' }} /> <button onClick={log}>Log editor content</button> </> ); } -
Run the development server to test the application:
npm run dev
Other resources
-
For information on bundling, see: Introduction to bundling TinyMCE
-
For examples of the TinyMCE React integration, see: the tinymce-react storybook.
-
For guides on integrating TinyMCE premium plugins, see: Using premium plugins.
-
For information on customizing:
-
TinyMCE integration, see: TinyMCE React integration technical reference.
-
TinyMCE, see: Basic setup.
-
The React application, see: Getting Started with Vite or the React documentation.
-