Open In App

React.js Blueprint Menu Components Submenus

BlueprintJS is a React-based UI toolkit for the web. This library is very optimized and popular for building complex data-dense interfaces for desktop applications. In this article, we will discuss React.js BluePrint Menu Component Item.

A Menu is a list of interactive items. A Submenu can be created by nesting one or more MenuItems inside another MenuItem. The submenus are javascript-only components and cannot be created using just CSS because they use the Popover component under the hood. Submenus open to the right of their parent but they adjust themselves if there is not enough on the right.



React.js BluePrint Menu Components Submenus Properties:

Since Submenu uses MenuItem Component so all the properties listed below can be used.



Syntax:

<Menu>
    <MenuItem text="...">
        <MenuItem text="..." />
        <MenuItem text="..." />
        <MenuItem text="..." />
    </MenuItem>
</Menu>

Creating React Application And Installing Modules:

Step 1: Create a React application using the following command:

npx create-react-app myApp

Step 2: After creating your project folder i.e. myApp, move to it using the following command:

cd myApp

Step 3: After creating the ReactJS application, Install the required modules using the following command:

npm install @blueprintjs/core @blueprintjs/icons

Project Structure: After following the above steps, the project structure will look as below:

 

Example 1: Now write down the following code in the App.js file. In this example, We created a submenu by nesting MenuItems inside MenuItem.




import React from 'react'
import { Menu, MenuItem }
    from '@blueprintjs/core';
import '@blueprintjs/core/lib/css/blueprint.css';
import 
    '@blueprintjs/icons/lib/css/blueprint-icons.css'
  
function App() {
  
    const divStyle = {
        display: 'block', width: 450,
        padding: 30
    };
    const menuStyle = {
        width: 150
    };
    return (
        <div style={divStyle}>
            <h2
                style={{ color: "green" }}>
                GeeksforGeeks
            </h2>
            <h3>
                React.js BluePrint Menu Component Submenu
            </h3>
  
            <Menu style={menuStyle}>
                <MenuItem text="Home" />
                <MenuItem text="Courses">
                    <MenuItem text="DSA Self Paced" />
                    <MenuItem text="GATE 2023" />
                    <MenuItem 
                        text="Full Stack Development" />
                </MenuItem>
                <MenuItem text="Practice" />
            </Menu>
        </div>
    );
}
  
export default App;

Step to run the App: Execute the command below to run the app and head over to http://localhost:3000/ to see the output

npm start

Output:

 

Example 2: This example shows the nesting of a submenu inside an item of a submenu. i.e two-level nesting of MenuItems.




import React from 'react'
import { Menu, MenuItem }
    from '@blueprintjs/core';
import '@blueprintjs/core/lib/css/blueprint.css';
import '@blueprintjs/icons/lib/css/blueprint-icons.css'
  
function App() {
  
    const divStyle = {
        display: 'block', width: 450,
        padding: 30
    };
    const menuStyle = {
        width: 150
    };
    return (
        <div style={divStyle}>
            <h2
                style={{ color: "green" }}>GeeksforGeeks
            </h2>
            <h3>
                React.js BluePrint Menu Component Submenu
            </h3>
  
            <Menu style={menuStyle}>
                <MenuItem text="Home" />
                <MenuItem text="Courses">
                    <MenuItem text="DSA Self Paced" />
                    <MenuItem text="GATE 2023">
                        <MenuItem
                            text="Operating System" />
                        <MenuItem
                            text="Computer Networks" />
                        <MenuItem text="DSA" />
                    </MenuItem>
                    <MenuItem
                        text="Full Stack Development" />
                </MenuItem>
                <MenuItem text="Practice" />
            </Menu>
        </div>
    );
}
  
export default App;

Output:

 

Reference: https://blueprintjs.com/docs/#core/components/menu.submenus


Article Tags :