Icons and navigation in React Context menu component

29 Aug 20238 minutes to read

Icons

The ContextMenu item have an icon / image in it to provide visual representation of the action. To place the icon on a menu item, set the iconCss property to e-icons with the required icon CSS. By default, the icon is positioned to the left side of the menu item. In the following sample, the icons for Cut, Copy and Paste menu items are added using iconCss property.

import { enableRipple } from '@syncfusion/ej2-base';
import { ContextMenuComponent } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
function App() {
    let menuItems = [
        {
            iconCss: 'e-cm-icons e-cut',
            text: 'Cut'
        },
        {
            iconCss: 'e-icons e-copy',
            text: 'Copy'
        },
        {
            iconCss: 'e-cm-icons e-paste',
            text: 'Paste'
        }
    ];
    return (<div className="container">
            <div id='target'>Right click / Touch hold to open the ContextMenu</div>
            <ContextMenuComponent id='contextmenu' target='#target' items={menuItems}/>
          </div>);
}
export default App;
ReactDom.render(<App />, document.getElementById('element'));
import { enableRipple } from '@syncfusion/ej2-base';
import { ContextMenuComponent, MenuItemModel } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';

enableRipple(true);

function App() {
  let menuItems: MenuItemModel[] = [
    {
        iconCss: 'e-cm-icons e-cut',
        text: 'Cut'
    },
    {
        iconCss: 'e-icons e-copy',
        text: 'Copy'
    },
    {
        iconCss: 'e-cm-icons e-paste',
        text: 'Paste'
    }];

  return (
          <div className="container">
            <div id='target'>Right click / Touch hold to open the ContextMenu</div>
            <ContextMenuComponent id='contextmenu' target='#target' items={menuItems}/>
          </div>
      );
}
export default App;
ReactDom.render(<App />,document.getElementById('element'));

Navigation URL in ContextMenu is used for navigating to other web page when menu item is clicked. This can be achieved by providing link to the menu item using the url property.In the following sample, Navigation URL for Flipkart, Amazon, and Snapdeal menu items are added using the url property.

import { enableRipple } from '@syncfusion/ej2-base';
import { ContextMenuComponent } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
function App() {
    let menuItems = [
        {
            iconCss: 'e-cart-icon e-link',
            text: 'Flipkart',
            url: 'https://www.google.co.in/search?q=flipkart'
        },
        {
            iconCss: 'e-cart-icon e-link',
            text: 'Amazon',
            url: 'https://www.google.co.in/search?q=amazon'
        },
        {
            iconCss: 'e-cart-icon e-link',
            text: 'Snapdeal',
            url: 'https://www.google.co.in/search?q=snapdeal'
        }
    ];
    function itemBeforeEvent(args) {
        args.element.getElementsByTagName('a')[0].setAttribute('target', '_blank');
    }
    return (<div className="container">
            <div id='target'>Right click / Touch hold to open the ContextMenu</div>
            <ContextMenuComponent id='contextmenu' target='#target' items={menuItems} beforeItemRender={itemBeforeEvent}/>
          </div>);
}
export default App;
ReactDom.render(<App />, document.getElementById('element'));
import { enableRipple } from '@syncfusion/ej2-base';
import { ContextMenuComponent, MenuEventArgs, MenuItemModel } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';

enableRipple(true);

function App() {
  let menuItems: MenuItemModel[] = [
    {
        iconCss: 'e-cart-icon e-link',
        text: 'Flipkart',
        url: 'https://www.google.co.in/search?q=flipkart'
    },
    {
        iconCss: 'e-cart-icon e-link',
        text: 'Amazon',
        url: 'https://www.google.co.in/search?q=amazon'
    },
    {
        iconCss: 'e-cart-icon e-link',
        text: 'Snapdeal',
        url: 'https://www.google.co.in/search?q=snapdeal'
    }];

  function itemBeforeEvent(args: MenuEventArgs) {
      args.element.getElementsByTagName('a')[0].setAttribute('target', '_blank');
  }

  return (
          <div className="container">
            <div id='target'>Right click / Touch hold to open the ContextMenu</div>
            <ContextMenuComponent id='contextmenu' target='#target' items={menuItems}
            beforeItemRender={itemBeforeEvent}/>
          </div>
      );
  }
export default App;
ReactDom.render(<App />,document.getElementById('element'));

To open the links in new tab, set ‘target’ attribute with the value ‘_blank’ in the
beforeItemRender event.

See Also