Open In App

React Suite Nav Responsive

Last Updated : 23 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. Nav component helps to provide a list of various forms of navigation menus, which can be landscape and portrait layouts.

Responsive Nav helps to create a dynamic Nav. We can increase/decrease the number of menu items visible on the screen using a slider or button.

Syntax:

<ResponsiveNav appearance="tabs">
    {items.map(item => (
        <ResponsiveNav.Item >
            {item}
        </ResponsiveNav.Item>
    ))}
</ResponsiveNav>

Creating React Application And Installing Module:

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

npx create-react-app foldername

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

cd foldername

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

npm install rsuite
npm install @rsuite/responsive-nav
npm install @mui/material @emotion/react @emotion/styled

Project Structure: It will look like the following.

 

Example 1: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.

In this example, we will learn about Responsive Nav using a slider.

Javascript




import ResponsiveNav from '@rsuite/responsive-nav';
import React, { useState } from 'react'
import Slider from '@mui/material/Slider';
import 'rsuite/dist/rsuite.min.css';
  
const items = [
    { label: 'Item A' },
    { label: 'Item B' },
    { label: 'Item C' },
    { label: 'Item D' },
    { label: 'Item E' },
    { label: 'Item F' },
    { label: 'Item G' },
    { label: 'Item H' },
    { label: 'Item I' },
    { label: 'Item J' },
    { label: 'Item K' },
    { label: 'Item L' },
    { label: 'Item M' },
    { label: 'Item N' }
];
  
export default function App() {
    const [width, setWidth] = React.useState(500);
    const func = (event) => {
  
        setWidth(event.target.value)
    }
    return (
        <div>
            <h1 style={{ color: 'green' }}>
                GeeksforGeeks</h1>
            <h3>React Suite Nav Responsive</h3>
            <div style={{ width, border: '1px solid #ddd'
                padding: 10 }}>
                <ResponsiveNav appearance="tabs">
                    {items.map(item => (
                        <ResponsiveNav.Item  >
                            {item.label}
                        </ResponsiveNav.Item>
                    ))}
                </ResponsiveNav>
            </div>
            <hr />
            <Slider
                style={{ width: 500 }}
                min={300}
                max={1000}
                progress
                step={50}
                value={width}
                onChange={func}
            />
        </div>
    );
}


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

 

Example 2: In this example, we use a button to handle the responsive nav.

Javascript




import ResponsiveNav from '@rsuite/responsive-nav';
import React, { useState } from 'react'
import Slider from '@mui/material/Slider';
import 'rsuite/dist/rsuite.min.css';
import Button from '@mui/material/Button';
const items = [
    { label: 'Item A' },
    { label: 'Item B' },
    { label: 'Item C' },
    { label: 'Item D' },
    { label: 'Item E' },
    { label: 'Item F' },
    { label: 'Item G' },
    { label: 'Item H' },
    { label: 'Item I' },
    { label: 'Item J' },
    { label: 'Item K' },
    { label: 'Item L' },
    { label: 'Item M' },
    { label: 'Item N' }
];
  
export default function App() {
    const [width, setWidth] = React.useState(500);
    const Increase = (event) => {
  
        setWidth(width + 100)
  
    }
    const Decrease = (event) => {
  
        setWidth(width - 100)
    }
    return (
        <div>
            <h1 style={{ color: 'green' }}>
                GeeksforGeeks</h1>
            <h3>React Suite Nav Responsive</h3>
            <div style={{ width, border: '1px solid #ddd'
                padding: 10 }}>
                <ResponsiveNav appearance="tabs">
                    {items.map(item => (
                        <ResponsiveNav.Item  >
                            {item.label}
                        </ResponsiveNav.Item>
                    ))}
                </ResponsiveNav>
            </div>
            <hr />
            <button onClick={Increase} style={{ 
                background: 'green', color: 'white'
                padding: '2%' }}>
                    Increase</button>  
            <button onClick={Decrease} style={{ 
                background: 'red', color: 'white'
                padding: '2%' }}>
                    Decrease</button>
        </div>
    );
}


Output:

 

Reference: https://rsuitejs.com/components/nav/#responsive



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads