Search results

Icons and Sub menu items in React Menu Bar component

15 Oct 2021 / 5 minutes to read

Icons

The menu item contains an icon/image in it to provide a visual representation of an action. To place the icon on a menu item, set the iconCss property with the required icon CSS. By default, the icon is positioned at the left of the menu item. In the following sample, the icons of File and Edit menu items and Open, Save, Cut, Copy,and Paste sub menu items are added using the iconCss property.

Source
Preview
index.tsx
index.html
index.css
index.jsx
Copied to clipboard
import { enableRipple } from '@syncfusion/ej2-base';
import { MenuComponent, MenuItemModel } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);

class App extends React.Component<{}, {}> {
    // Menu items definition
    public menuItems: MenuItemModel[] = [
        {
            iconCss: 'em-icons e-file',
            items: [
                { text: 'Open', iconCss: 'em-icons e-open' },
                { text: 'Save', iconCss: 'e-icons e-save' },
                { separator: true },
                { text: 'Exit' }
            ],
            text: 'File'
        },
        {
            iconCss: 'em-icons e-edit',
            items: [
                { text: 'Cut', iconCss: 'em-icons e-cut' },
                { text: 'Copy', iconCss: 'em-icons e-copy' },
                { text: 'Paste', iconCss: 'em-icons e-paste' }
            ],
            text: 'Edit'
        },
        {
            items: [
                { text: 'Toolbar' },
                { text: 'Sidebar' },
                { text: 'Full Screen' }
            ],
            text: 'View'
        },
        {
            items: [
                { text: 'Spelling & Grammar' },
                { text: 'Customize' },
                { text: 'Options' }
            ],
            text: 'Tools'
        },
        { text: 'Go' },
        { text: 'Help' }
    ];

    public render() {
        return (
            <MenuComponent items={this.menuItems}/>
        );
    }
}

ReactDom.render(<App />,document.getElementById('element'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React ContextMenu</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="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div id='element'>
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>
Copied to clipboard
#loader {
	color: #008cff;
	height: 40px;
	left: 45%;
	position: absolute;
	top: 45%;
	width: 30%;
}

body {
	margin-top: 100px;
	text-align: center;
}

/**
* ej2 Menu styles
*/
@font-face {
	font-family: 'e-menu';
	src:
	url(data:application/x-font-ttf;charset=utf-8;base64,AAEAAAAKAIAAAwAgT1MvMjvJSpgAAAEoAAAAVmNtYXBsm2feAAABpAAAAGxnbHlmmEcyrQAAAiQAAAWIaGVhZBJ0bwcAAADQAAAANmhoZWEHmQNyAAAArAAAACRobXR4I0AAAAAAAYAAAAAkbG9jYQaGB+4AAAIQAAAAFG1heHABGACaAAABCAAAACBuYW1lc0cOBgAAB6wAAAIlcG9zdJbKd4kAAAnUAAAAfQABAAADUv9qAFoEAAAA//4D6gABAAAAAAAAAAAAAAAAAAAACQABAAAAAQAAhka7o18PPPUACwPoAAAAANe2FRwAAAAA17YVHAAAAAAD6gPqAAAACAACAAAAAAAAAAEAAAAJAI4ABQAAAAAAAgAAAAoACgAAAP8AAAAAAAAAAQPrAZAABQAAAnoCvAAAAIwCegK8AAAB4AAxAQIAAAIABQMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUGZFZABA5anohgNS/2oAWgPqAJYAAAABAAAAAAAABAAAAAPoAAAD6AAAA+gAAAPoAAAD6AAAA+gAAAPoAAAD6AAAAAAAAgAAAAMAAAAUAAMAAQAAABQABABYAAAADgAIAAIABuWp5bPluefo6CLohv//AADlqeWy5bnn6Ogi6IX//wAAAAAAAAAAAAAAAAABAA4ADgAQABAAEAAQAAAABQAGAAcACAABAAIAAwAEAAAAAACsASoBRAGwAh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ABgAOAL0AAwABBAkACgBYAMsAAwABBAkACwAkASMgZS1pY29uc1JlZ3VsYXJlLWljb25zZS1pY29uc1ZlcnNpb24gMS4wZS1pY29uc0ZvbnQgZ2VuZXJhdGVkIHVzaW5nIFN5bmNmdXNpb24gTWV0cm8gU3R1ZGlvd3d3LnN5bmNmdXNpb24uY29tACAAZQAtAGkAYwBvAG4AcwBSAGUAZwB1AGwAYQByAGUALQBpAGMAbwBuAHMAZQAtAGkAYwBvAG4AcwBWAGUAcgBzAGkAbwBuACAAMQAuADAAZQAtAGkAYwBvAG4AcwBGAG8AbgB0ACAAZwBlAG4AZQByAGEAdABlAGQAIAB1AHMAaQBuAGcAIABTAHkAbgBjAGYAdQBzAGkAbwBuACAATQBlAHQAcgBvACAAUwB0AHUAZABpAG8AdwB3AHcALgBzAHkAbgBjAGYAdQBzAGkAbwBuAC4AYwBvAG0AAAAAAgAAAAAAAAAKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJAQIBAwEEAQUBBgEHAQgBCQEKAA1zaG9wcGluZy1jYXJ0B2VkaXQtMDUMZmlsZS1vcGVuLTAxDGZpbGUtdGV4dC0wMQNDdXQFUGFzdGUEQ29weQZTZWFyY2gAAAAAAA==) format('truetype');
	font-weight: normal;
	font-style: normal;
}

.em-icons {
	font-family: 'e-menu';
	font-style: normal;
	font-variant: normal;
	font-weight: normal;
	line-height: 1;
	text-transform: none;
}

.e-file::before {
	content: '\e886';
}

.e-edit::before {
	content: '\e822';
}

.e-cut::before {
	content: '\e5a9';
}

.e-copy::before {
	content: '\e5b3';
}

.e-paste::before {
	content: '\e5b2';
}

.e-open::before {
	content: '\e885';
}

.e-save::before {
	content: '\e98e';
}
Copied to clipboard
import { enableRipple } from '@syncfusion/ej2-base';
import { MenuComponent } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
class App extends React.Component {
    constructor() {
        super(...arguments);
        // Menu items definition
        this.menuItems = [
            {
                iconCss: 'em-icons e-file',
                items: [
                    { text: 'Open', iconCss: 'em-icons e-open' },
                    { text: 'Save', iconCss: 'e-icons e-save' },
                    { separator: true },
                    { text: 'Exit' }
                ],
                text: 'File'
            },
            {
                iconCss: 'em-icons e-edit',
                items: [
                    { text: 'Cut', iconCss: 'em-icons e-cut' },
                    { text: 'Copy', iconCss: 'em-icons e-copy' },
                    { text: 'Paste', iconCss: 'em-icons e-paste' }
                ],
                text: 'Edit'
            },
            {
                items: [
                    { text: 'Toolbar' },
                    { text: 'Sidebar' },
                    { text: 'Full Screen' }
                ],
                text: 'View'
            },
            {
                items: [
                    { text: 'Spelling & Grammar' },
                    { text: 'Customize' },
                    { text: 'Options' }
                ],
                text: 'Tools'
            },
            { text: 'Go' },
            { text: 'Help' }
        ];
    }
    render() {
        return (<MenuComponent items={this.menuItems}/>);
    }
}
ReactDom.render(<App />, document.getElementById('element'));

Navigation in Menu is used to navigate to the other web page when a menu item is clicked. It can be achieved by providing a link to the menu item using the url property. In the following sample, the Navigation URL is added to sub menu items using the url property.

Source
Preview
index.tsx
index.html
index.jsx
Copied to clipboard
import { enableRipple } from '@syncfusion/ej2-base';
import { MenuComponent, MenuEventArgs, MenuItemModel } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);

class App extends React.Component<{}, {}> {
    // Menu items definition
    public menuItems: MenuItemModel[] = [
        {
            items: [
                { text: 'Washing Machine', url: 'https://www.google.com/search?q=washing+machine' },
                { text: 'Air Conditioners', url: 'https://www.google.com/search?q=air+conditioners' }
            ],
            text: 'Appliances'
        },
        {
            items: [
                { text: 'Headphones', url: 'https://www.google.com/search?q=headphones' },
                { text: 'Memory Cards', url: 'https://www.google.com/search?q=memory+cards' },
                { text: 'Power Banks', url: 'https://www.google.com/search?q=power+banks' }
            ],
            text: 'Mobile'
        },
        {
            items: [
                { text: 'Televisions', url: 'https://www.google.com/search?q=televisions' },
                { text: 'Home Theatres', url: 'https://www.google.com/search?q=home+theatres' },
                { text: 'Gaming Laptops', url: 'https://www.google.com/search?q=gaming+laptops' }
            ],
            text: 'Entertainment'
        },
        { text: 'Fashion', url: 'https://www.google.com/search?q=fashion' },
        { text: 'Offers', url: 'https://www.google.com/search?q=offers' }
    ];

    public beforeItemRender(args: MenuEventArgs): void {
        if (args.item.url) {
            args.element.getElementsByTagName('a')[0].setAttribute('target', '_blank');
        }
    }

    public render() {
        return (
            <MenuComponent items={this.menuItems} beforeItemRender={this.beforeItemRender}/>
        );
    }
}

ReactDom.render(<App />,document.getElementById('element'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React ContextMenu</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="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div id='element'>
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>
Copied to clipboard
import { enableRipple } from '@syncfusion/ej2-base';
import { MenuComponent } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
class App extends React.Component {
    constructor() {
        super(...arguments);
        // Menu items definition
        this.menuItems = [
            {
                items: [
                    { text: 'Washing Machine', url: 'https://www.google.com/search?q=washing+machine' },
                    { text: 'Air Conditioners', url: 'https://www.google.com/search?q=air+conditioners' }
                ],
                text: 'Appliances'
            },
            {
                items: [
                    { text: 'Headphones', url: 'https://www.google.com/search?q=headphones' },
                    { text: 'Memory Cards', url: 'https://www.google.com/search?q=memory+cards' },
                    { text: 'Power Banks', url: 'https://www.google.com/search?q=power+banks' }
                ],
                text: 'Mobile'
            },
            {
                items: [
                    { text: 'Televisions', url: 'https://www.google.com/search?q=televisions' },
                    { text: 'Home Theatres', url: 'https://www.google.com/search?q=home+theatres' },
                    { text: 'Gaming Laptops', url: 'https://www.google.com/search?q=gaming+laptops' }
                ],
                text: 'Entertainment'
            },
            { text: 'Fashion', url: 'https://www.google.com/search?q=fashion' },
            { text: 'Offers', url: 'https://www.google.com/search?q=offers' }
        ];
    }
    beforeItemRender(args) {
        if (args.item.url) {
            args.element.getElementsByTagName('a')[0].setAttribute('target', '_blank');
        }
    }
    render() {
        return (<MenuComponent items={this.menuItems} beforeItemRender={this.beforeItemRender}/>);
    }
}
ReactDom.render(<App />, document.getElementById('element'));

Multilevel nesting

The Menu supports multiple level nesting, and it can be achieved by mapping the items property inside the parent menuItems. In the following sample, three-level nesting of menu has been provided.

Source
Preview
index.tsx
index.html
index.jsx
Copied to clipboard
import { enableRipple } from '@syncfusion/ej2-base';
import { MenuComponent, MenuItemModel } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);

class App extends React.Component<{}, {}> {
    // Menu items definition
    public menuItems: MenuItemModel[] = [
        {
            items: [
                {
                    items: [
                        {
                            items: [
                                { text: 'Trimmers' },
                                { text: 'Shavers' }
                            ],
                            text: 'Personal Care'
                        },
                        {
                            items: [
                                { text: 'Shirts' },
                                { text: 'Jackets' },
                                { text: 'Track Suits' }
                            ],
                            text: 'Clothing'
                        },
                        { text: 'Footwear' }
                    ],
                    text: 'Men Fashion'
                },
                {
                    items: [
                        {
                            items: [
                                { text: 'Kurtas' },
                                { text: 'Salwars' },
                                { text: 'Sarees' }
                            ],
                            text: 'Clothing'
                        },
                        {
                            items: [
                                { text: 'Nosepins' },
                                { text: 'Anklets' }
                            ],
                            text: 'Jewellery'
                        }
                    ],
                    text: 'Women Fashion'
                }
            ],
            text: 'Fashion'
        },
        {
            items: [
                {
                    items: [
                        { text: 'Fully Automatic' },
                        { text: 'Semi Automatic' }
                    ],
                    text: 'Washing Machine'
                },
                {
                    items: [
                        { text: 'Inverter ACs' },
                        { text: 'Split ACs' }
                    ],
                    text: 'Air Conditioners'
                }
            ],
            text: 'Home & Living'
        },
        { text: 'Accessories' },
        { text: 'Sports' },
        { text: 'Gaming' }
    ];

    public render() {
        return (
            <MenuComponent items={this.menuItems} />
        );
    }
}

ReactDom.render(<App />,document.getElementById('element'));
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion React ContextMenu</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="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
</head>

<body>
    <div id='element'>
        <div id='loader'>Loading....</div>
    </div>
</body>

</html>
Copied to clipboard
import { enableRipple } from '@syncfusion/ej2-base';
import { MenuComponent } from '@syncfusion/ej2-react-navigations';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
class App extends React.Component {
    constructor() {
        super(...arguments);
        // Menu items definition
        this.menuItems = [
            {
                items: [
                    {
                        items: [
                            {
                                items: [
                                    { text: 'Trimmers' },
                                    { text: 'Shavers' }
                                ],
                                text: 'Personal Care'
                            },
                            {
                                items: [
                                    { text: 'Shirts' },
                                    { text: 'Jackets' },
                                    { text: 'Track Suits' }
                                ],
                                text: 'Clothing'
                            },
                            { text: 'Footwear' }
                        ],
                        text: 'Men Fashion'
                    },
                    {
                        items: [
                            {
                                items: [
                                    { text: 'Kurtas' },
                                    { text: 'Salwars' },
                                    { text: 'Sarees' }
                                ],
                                text: 'Clothing'
                            },
                            {
                                items: [
                                    { text: 'Nosepins' },
                                    { text: 'Anklets' }
                                ],
                                text: 'Jewellery'
                            }
                        ],
                        text: 'Women Fashion'
                    }
                ],
                text: 'Fashion'
            },
            {
                items: [
                    {
                        items: [
                            { text: 'Fully Automatic' },
                            { text: 'Semi Automatic' }
                        ],
                        text: 'Washing Machine'
                    },
                    {
                        items: [
                            { text: 'Inverter ACs' },
                            { text: 'Split ACs' }
                        ],
                        text: 'Air Conditioners'
                    }
                ],
                text: 'Home & Living'
            },
            { text: 'Accessories' },
            { text: 'Sports' },
            { text: 'Gaming' }
        ];
    }
    render() {
        return (<MenuComponent items={this.menuItems}/>);
    }
}
ReactDom.render(<App />, document.getElementById('element'));

You can achieve multi level nesting with data source by mapping name of the child items to the children sub-property of fields property. Also, we can specify id property for menu items. For more information, refer to the data source binding section. To open sub menu items only on item click, showItemOnClick should be set as true.

The below table represents the MenuItem properties and it’s description.

Property Name Type Description
iconCss string Defines class/multiple classes separated by a space for the menu Item that is used to include an icon. Menu Item can include font icon and sprite image.
id string Specifies the id for menu item.
separator boolean Specifies separator between the menu items. Separator are either horizontal or vertical lines used to group menu items.
items MenuItemModel[] Specifies the sub menu items that is the array of MenuItem model/
text string Specifies text for menu item.
url string Specifies url for menu item that creates the anchor link to navigate to the url provided.

See Also