How to Customize Context Menu in React DOCX Editor
27 Aug 202623 minutes to read
How to Customize Context Menu in the DOCX Editor
React DOCX Editor (Document Editor) allows you to add custom options to the context menu. Use the addCustomMenu() method to add a custom option, and handle the selection with the customContextMenuSelect event.
Add Custom Option
The following code shows how to add a custom option to the context menu.
import { createRoot } from 'react-dom/client';
import './index.css';
import * as React from 'react';
import { MenuItemModel } from '@syncfusion/ej2-navigations';
import {
DocumentEditorContainerComponent,
Toolbar,
} from '@syncfusion/ej2-react-documenteditor';
DocumentEditorContainerComponent.Inject(Toolbar);
let container = DocumentEditorContainerComponent;
function App() {
function onCreate() {
// creating Custom Options
let menuItems = [
{
text: 'Search In Google',
id: 'search_in_google',
iconCss: 'e-icons e-de-ctnr-find',
},
];
// adding Custom Options
container.documentEditor.contextMenu.addCustomMenu(menuItems, false);
// custom Options Select Event
container.documentEditor.customContextMenuSelect = (args) => {
// custom Options Functionality
let id = container.documentEditor.element.id;
switch (args.id) {
case id + 'search_in_google':
// To get the selected content as plain text
let searchContent = container.documentEditor.selection.text;
if (
!container.documentEditor.selection.isEmpty &&
/\S/.test(searchContent)
) {
window.open('http://google.com/search?q=' + searchContent);
}
break;
}
};
}
return (
<div>
<DocumentEditorContainerComponent
id="container"
ref={(scope) => {
container = scope;
}}
height={'590px'}
serviceUrl="https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/"
enableToolbar={true}
created={onCreate}
/>
</div>
);
}
export default App;
createRoot(document.getElementById('sample')).render(<App />);The Web API hosted link
https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/utilized in the DOCX Editor’s serviceUrl property is intended solely for demonstration and evaluation purposes. For production deployment, please host your own web service with your required server configurations. You can refer and reuse the GitHub Web Service example or Docker image for hosting your own web service and use for the serviceUrl property.
Customize Custom Option in Context Menu
The DOCX Editor allows you to customize an added custom option and also to hide or show default context menu items.
Hide Default Context Menu Items
Using the addCustomMenu() method, you can replace the default context menu by setting the second parameter to true.
The following code shows how to replace the default context menu and add a custom option in the context menu.
import { createRoot } from 'react-dom/client';
import './index.css';
import * as React from 'react';
import { MenuItemModel } from '@syncfusion/ej2-navigations';
import {
DocumentEditorContainerComponent,
Toolbar,
} from '@syncfusion/ej2-react-documenteditor';
DocumentEditorContainerComponent.Inject(Toolbar);
let container = DocumentEditorContainerComponent;
function App() {
function onCreate() {
// creating Custom Options
let menuItems = [
{
text: 'Search In Google',
id: 'search_in_google',
iconCss: 'e-icons e-de-ctnr-find',
},
];
// adding Custom Options
container.documentEditor.contextMenu.addCustomMenu(menuItems, true);
}
return (
<div>
<DocumentEditorContainerComponent
id="container"
ref={(scope) => {
container = scope;
}}
height={'590px'}
serviceUrl="https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/"
enableToolbar={true}
created={onCreate}
/>
</div>
);
}
export default App;
createRoot(document.getElementById('sample')).render(<App />);The Web API hosted link
https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/utilized in the DOCX Editor’s serviceUrl property is intended solely for demonstration and evaluation purposes. For production deployment, please host your own web service with your required server configurations. You can refer and reuse the GitHub Web Service example or Docker image for hosting your own web service and use for the serviceUrl property.
Customize Added Context Menu Items
The following code shows how to show or hide an added custom option in the context menu using the customContextMenuBeforeOpen event.
import { createRoot } from 'react-dom/client';
import './index.css';
import * as React from 'react';
import { MenuItemModel } from '@syncfusion/ej2-navigations';
import {
DocumentEditorContainerComponent,
Toolbar,
} from '@syncfusion/ej2-react-documenteditor';
DocumentEditorContainerComponent.Inject(Toolbar);
let container = DocumentEditorContainerComponent;
function App() {
function onCreate() {
// creating Custom Options
let menuItems = [
{
text: 'Search In Google',
id: 'search_in_google',
iconCss: 'e-icons e-de-ctnr-find',
},
];
// adding Custom Options
container.documentEditor.contextMenu.addCustomMenu(menuItems, false);
// custom Options Select Event
container.documentEditor.customContextMenuSelect = (args) => {
// custom Options Functionality
let id = container.documentEditor.element.id;
switch (args.id) {
case id + 'search_in_google':
let searchContent = container.documentEditor.selection.text;
if (!container.documentEditor.selection.isEmpty && /\S/.test(searchContent)) {
window.open('http://google.com/search?q=' + searchContent);
}
break;
}
};
// custom options hide/show functionality
container.documentEditor.customContextMenuBeforeOpen = (args) => {
let search = document.getElementById(args.ids[0]);
search.style.display = 'none';
let searchContent = container.documentEditor.selection.text;
if (!container.documentEditor.selection.isEmpty && /\S/.test(searchContent)) {
search.style.display = 'block';
}
};
}
return (
<div>
<DocumentEditorContainerComponent
id="container"
ref={(scope) => {
container = scope;
}}
height={'590px'}
serviceUrl="https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/"
enableToolbar={true}
created={onCreate}
/>
</div>
);
}
export default App;
createRoot(document.getElementById('sample')).render(<App />);The Web Service link
https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/used in the serviceUrl property is intended solely for demonstration and evaluation purposes. For production deployment, please host your own Web Service with your required server configurations. You can refer and reuse the GitHub Web Service example or Docker image for hosting your own Web Service and use it for the serviceUrl property.
The following is the output of the custom context menu with customization.
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import { DocumentEditorContainerComponent, Toolbar, } from '@syncfusion/ej2-react-documenteditor';
DocumentEditorContainerComponent.Inject(Toolbar);
function App() {
let container;
React.useEffect(() => {
onCreate();
}, []);
function onCreate() {
// creating Custom Options
let menuItems = [
{
text: 'Search In Google',
id: 'search_in_google',
iconCss: 'e-icons e-de-ctnr-find',
},
];
// adding Custom Options
container.documentEditor.contextMenu.addCustomMenu(menuItems, false);
// custom Options Select Event
container.documentEditor.customContextMenuSelect = (args) => {
// custom Options Functionality
let id = container.documentEditor.element.id;
switch (args.id) {
case id + 'search_in_google':
let searchContent = container.documentEditor.selection.text;
if (!container.documentEditor.selection.isEmpty && /\S/.test(searchContent)) {
window.open('http://google.com/search?q=' + searchContent);
}
break;
}
};
// custom options hide/show functionality
container.documentEditor.customContextMenuBeforeOpen = (args) => {
let search = document.getElementById(args.ids[0]);
search.style.display = 'none';
let searchContent = container.documentEditor.selection.text;
if (!container.documentEditor.selection.isEmpty && /\S/.test(searchContent)) {
search.style.display = 'block';
}
};
}
return (<DocumentEditorContainerComponent id="container" ref={(scope) => {
container = scope;
}} height={'590px'} serviceUrl="https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/" enableToolbar={true} created={onCreate}/>);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));import * as ReactDOM from 'react-dom';
import * as React from 'react';
import {
DocumentEditorContainerComponent,
Toolbar,
} from '@syncfusion/ej2-react-documenteditor';
import { MenuItemModel } from '@syncfusion/ej2-navigations';
DocumentEditorContainerComponent.Inject(Toolbar);
function App() {
let container: DocumentEditorContainerComponent;
React.useEffect(() => {
onCreate()
}, []);
function onCreate() {
// creating Custom Options
let menuItems: MenuItemModel[] = [
{
text: 'Search In Google',
id: 'search_in_google',
iconCss: 'e-icons e-de-ctnr-find',
},
];
// adding Custom Options
container.documentEditor.contextMenu.addCustomMenu(menuItems, false);
// custom Options Select Event
container.documentEditor.customContextMenuSelect = (args: any): void => {
// custom Options Functionality
let id: string = container.documentEditor.element.id;
switch (args.id) {
case id + 'search_in_google':
let searchContent: string = container.documentEditor.selection.text;
if (!container.documentEditor.selection.isEmpty && /\S/.test(searchContent)) {
window.open('http://google.com/search?q=' + searchContent);
}
break;
}
};
// custom options hide/show functionality
container.documentEditor.customContextMenuBeforeOpen = (args: any): void => {
let search: any = document.getElementById(args.ids[0]);
search.style.display = 'none';
let searchContent: string = container.documentEditor.selection.text;
if (!container.documentEditor.selection.isEmpty && /\S/.test(searchContent)) {
search.style.display = 'block';
}
};
}
return (
<DocumentEditorContainerComponent
id="container"
ref={(scope) => {
container = scope;
}}
height={'590px'}
serviceUrl="https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/"
enableToolbar={true}
created={onCreate}
/>
);
} export default App
ReactDOM.render(<App />, document.getElementById('sample'));<!DOCTYPE html>
<html lang="en">
<head>
<title>Syncfusion React Button</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 for React Components" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-react-documenteditor/styles/fabric.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-documenteditor/styles/fabric.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-buttons/styles/fabric.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-base/styles/fabric.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-dropdowns/styles/fabric.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-inputs/styles/fabric.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-splitbuttons/styles/fabric.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-lists/styles/fabric.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-navigations/styles/fabric.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/34.2.2/ej2-popups/styles/fabric.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='sample'>
<div id='loader'>Loading....</div>
</div>
</body>
</html>The Web Service link
https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/used in the serviceUrl property is intended solely for demonstration and evaluation purposes. For production deployment, please host your own Web Service with your required server configurations. You can refer and reuse the GitHub Web Service example or Docker image for hosting your own Web Service and use it for the serviceUrl property.
Customize Context Menu with Sub-Menu Items
The DOCX Editor allows you to customize the context menu with sub-menu items. It can be achieved by using the addCustomMenu() method.
The following code shows how to add sub-items inside a custom context menu option in the Document Editor container.
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import {
DocumentEditorContainerComponent,
Toolbar,
} from '@syncfusion/ej2-react-documenteditor';
DocumentEditorContainerComponent.Inject(Toolbar);
function App() {
let container;
let menuItems = [
{
text: 'Form field',
id: 'form field',
iconCss: 'e-de-formfield e-icons',
items: [
{
text: 'Text form',
id: 'Text form',
iconCss: 'e-icons e-de-textform',
},
{
text: 'Check box',
id: 'Check box',
iconCss: 'e-icons e-de-checkbox-form',
},
{
text: 'Drop down',
id: 'Drop down',
iconCss: 'e-icons e-de-dropdownform',
},
],
},
];
function onCreated() {
// adding Custom Options
container.documentEditor.contextMenu.addCustomMenu(menuItems, false, true);
}
return (
<DocumentEditorContainerComponent
id="container"
ref={(scope) => {
container = scope;
}}
height={'590px'}
serviceUrl="https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/"
enableToolbar={true}
created={onCreated}
/>
);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));The Web Service link
https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/used in the serviceUrl property is intended solely for demonstration and evaluation purposes. For production deployment, please host your own Web Service with your required server configurations. You can refer and reuse the GitHub Web Service example or Docker image for hosting your own Web Service and use it for the serviceUrl property.
Online Demo
Explore how to customize the context menu in the React DOCX Editor for working with Word documents in this live demo here.