Creating a Responsive Navbar in React/Next.js with Material-UI that hide on scroll

2023-11-09

Welcome to our step-by-step guide on creating a responsive navbar using React and Next.js, enriched with the elegance of Material-UI. A well-designed navbar is an essential part of any web application, providing users with easy access to different sections of your website. In this blog post, we will walk you through the process of building a responsive navbar that works seamlessly on both desktop and mobile devices.
Please note that:
This navigation bar is a very useful tool for web developers. It has three important features that make it different from other navigation bars. First, it offers a smooth scroll feature that hides the navbar when you scroll down and reveals it with style when you scroll up. Second, it’s super adaptable — it works seamlessly on big screens, small screens, and everything in between. Finally, it lets you showcase your brand with a logo and add a personal touch with your picture, making your website not only functional but also uniquely you. It’s the perfect professional touch for your web project.

Prerequisites
Before we dive into the code, make sure you have the following tools and libraries installed:
Material-UI: Material-UI is a popular React UI framework that provides a wide range of components to create modern, responsive designs. You can install it with the following command:
1
npm install @mui/material @mui/icons-material
Step 1: Importing Required Components and Libraries
We’ll begin by importing the necessary components and libraries that we’ll use to build our navbar. Here’s a list of the key components we’ll be working with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import React from "react";
import Link from "next/link"; // Import the Link component from Next.js
import AppBar from "@mui/material/AppBar";
import Box from "@mui/material/Box";
import Toolbar from "@mui/material/Toolbar";
import IconButton from "@mui/material/IconButton";
import Typography from "@mui/material/Typography";
import MenuIcon from "@mui/icons-material/Menu";
import Container from "@mui/material/Container";
import Avatar from "@mui/material/Avatar";
import Tooltip from "@mui/material/Tooltip";
import { Tabs, Tab, useMediaQuery, useTheme } from "@mui/material";
import InfoIcon from "@mui/icons-material/Info";
import ManageAccountsIcon from "@mui/icons-material/ManageAccounts";
import TimelineIcon from "@mui/icons-material/Timeline";
import AddReactionIcon from "@mui/icons-material/AddReaction";
import ContactMailIcon from "@mui/icons-material/ContactMail";
import {
List,
ListItemButton,
ListItemIcon,
ListItemText,
Menu,
} from "@mui/material";
import { Home } from "@mui/icons-material";
import Slide from "@mui/material/Slide"; // Import the Slide component from Material-UI
import useScrollTrigger from "@mui/material/useScrollTrigger"; // Import the useScrollTrigger hook
import CssBaseline from "@mui/material/CssBaseline";
import myProfilePic from "./sufi.webp";
import logo from "./logo.webp";
import Image from "next/image";
If you are building it on Nextjs, then don’t forget to import "use-client"
on the top of your code
Step 2: Creating a HideOnScroll Component
To achieve a smooth scrolling effect where the navbar disappears when scrolling down and reappears when scrolling up, we’ll create a custom HideOnScroll component. This component uses the Slide component from Material-UI.
1
2
3
4
5
6
7
8
9
10
function HideOnScroll(props) {
const { children } = props;
const trigger = useScrollTrigger();
return (
<Slide appear={false} direction="down" in={!trigger}>
{children}
</Slide>
);
}

Step 3: Building the Responsive Navbar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
function ResponsiveAppBar(props) {
const handleOpenNavMenu = (event) => {
setAnchorElNav(event.currentTarget);
};
const [anchorElNav, setAnchorElNav] = React.useState(null);
const handleCloseNavMenu = () => {
setAnchorElNav(null);
};
const theme = useTheme();
console.log(theme);
const isMatch = useMediaQuery(theme.breakpoints.down("md"));
console.log(isMatch);
return (
<React.Fragment>
<CssBaseline />
<HideOnScroll {...props}>
<AppBar
className="navbar1"
style={{
width: "93.5%",
right: "3.2%",
border: "2px solid white",
borderRadius: "30px",
background:
"linear-gradient( 90deg, rgba(78, 78, 246, 0.647) 0%, rgba(247, 90, 216, 0.696) 100% );",
}}
>
<Container maxWidth="xl">
<Toolbar disableGutters>
<Typography
variant="h6"
noWrap
component="a"
href="/"
sx={{
mr: 2,
display: { xs: "none", md: "flex" },
fontFamily: "monospace",
fontWeight: 700,
letterSpacing: ".3rem",
color: "inherit",
textDecoration: "none",
}}
className="cursorp"
>
<Avatar
sx={{ display: { xs: "none", md: "flex" }, mr: 1 }}
className="cursorp Tab8 animate__animated animate__backInLeft"
>
<Image
src={logo}
style={{ width: "100%", height: "auto" }}
alt="logo"
width={100}
height={100}
loading="lazy"
/>
</Avatar>
</Typography>
<Box sx={{ flexGrow: 1, display: { xs: "flex", md: "none" } }}>
<IconButton
size="large"
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick={handleOpenNavMenu}
color="inherit"
>
<MenuIcon />
</IconButton>
</Box>
<Typography
variant="h5"
noWrap
component="a"
href=""
sx={{
mr: 2,
display: { xs: "flex", md: "none" },
flexGrow: 1,
fontFamily: "monospace",
fontWeight: 700,
letterSpacing: ".3rem",
color: "inherit",
textDecoration: "none",
}}
>
{" "}
<Avatar
// alt="logo"
// src="https://res.cloudinary.com/dtvtphhsc/image/upload/fl_immutable_cache.preserve_transparency.progressive.sprite/v1693672396/logo_1_lk0neo.webp"
sx={{ display: { xs: "flex", md: "none" }, mr: 1 }}
>
<Image
src={logo}
style={{ width: "100%", height: "auto" }}
alt="logo"
width={100}
height={100}
loading="lazy"
/>
</Avatar>
</Typography>
<Box sx={{ flexGrow: 1, display: { xs: "none", md: "flex" } }}>
{isMatch ? (
<IconButton
size="large"
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick={handleOpenNavMenu}
color="inherit"
>
<MenuIcon />
</IconButton>
) : (
<>
<Tabs centered sx={{ margin: "auto" }}>
<Tab
value="one"
label={
<p>
<Home /> Home
</p>
}
onClick={() => {
window.location.href = "/";
}}
style={{
textDecoration: "none",
color: "white",
}}
className="Tab1 animate__animated animate__zoomIn"
/>
<Tab
label={
<Link
href="/about"
style={{
textDecoration: "none",
color: "white",
opacity: "1",
}}
>
<p>
{" "}
<InfoIcon /> About
</p>
</Link>
}
className="Tab2 animate__animated animate__zoomIn"
>
{" "}
</Tab>
<Tab
value="three"
label={
<p>
<Link
href="/skills"
style={{
textDecoration: "none",
color: "white",
}}
>
{" "}
<ManageAccountsIcon /> Skills
</Link>
</p>
}
className="Tab3 animate__animated animate__zoomIn"
/>
<Tab
value="three"
label={
<p>
<Link
href="/projects"
style={{
textDecoration: "none",
color: "white",
}}
>
<TimelineIcon /> Projects
</Link>
</p>
}
className="Tab3 animate__animated animate__zoomIn"
/>
<Tab
value="three"
label={
<p>
<Link
href="/blogs"
style={{
textDecoration: "none",
color: "white",
}}
>
{" "}
<AddReactionIcon /> Blogs
</Link>
</p>
}
className="Tab5 animate__animated animate__zoomIn"
/>
<Tab
value="three"
label={
<p>
<Link
href="/contact"
style={{
textDecoration: "none",
color: "white",
}}
>
<ContactMailIcon /> Contact
</Link>
</p>
}
className="Tab6 animate__animated animate__zoomIn"
/>
</Tabs>
</>
)}
</Box>
<Box sx={{ flexGrow: 0 }}>
<Tooltip>
<IconButton sx={{ p: 0 }}>
<Avatar className="Tab7 animate__animated animate__backInRight">
<Image
src={myProfilePic}
style={{ width: "100%", height: "auto" }}
alt="sufi"
width={100}
height={100}
loading="lazy"
/>
</Avatar>
</IconButton>
</Tooltip>
<Menu
id="menu-appbar-avatar"
anchorEl={anchorElNav}
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
keepMounted
transformOrigin={{
vertical: "top", // Adjust to match the new position
horizontal: "right",
}}
open={Boolean(anchorElNav)}
onClose={handleCloseNavMenu}
sx={{
display: { xs: "block", md: "none" },
}}
>
{/* <Drawer
open={isDrawerOpen}
onClose={() => setIsDrawerOpen(false)}
> */}
<List className="DrawerList">
<ListItemButton>
<ListItemIcon>
<Home />
</ListItemIcon>
<ListItemText
primary={"Home"}
onClick={() => {
window.location.href = "/";
}}
/>
</ListItemButton>
<ListItemButton>
<ListItemIcon>
<InfoIcon />
</ListItemIcon>
<Link
href="/about"
style={{ textDecoration: "none", color: "white" }}
>
<ListItemText
onClick={handleCloseNavMenu}
primary={"About"}
/>{" "}
</Link>
</ListItemButton>
<ListItemButton>
<ListItemIcon>
<ManageAccountsIcon />
</ListItemIcon>
<Link
href="/skills"
style={{ textDecoration: "none", color: "white" }}
>
<ListItemText
onClick={handleCloseNavMenu}
primary={"Skills"}
/>
</Link>
</ListItemButton>
<ListItemButton>
<ListItemIcon>
<TimelineIcon />
</ListItemIcon>
<Link
href="/projects"
style={{ textDecoration: "none", color: "white" }}
>
<ListItemText
onClick={handleCloseNavMenu}
primary={"Projects"}
/>
</Link>
</ListItemButton>
<ListItemButton>
<ListItemIcon>
<AddReactionIcon />
</ListItemIcon>
<Link
href="/blogs"
style={{ textDecoration: "none", color: "white" }}
>
<ListItemText
onClick={handleCloseNavMenu}
primary={"Blogs"}
/>
</Link>
</ListItemButton>
<ListItemButton>
<ListItemIcon>
<ContactMailIcon />
</ListItemIcon>
<Link
href="/contact"
style={{ textDecoration: "none", color: "white" }}
>
<ListItemText
onClick={handleCloseNavMenu}
primary={"Contact"}
/>
</Link>
</ListItemButton>
</List>
</Menu>
</Box>
</Toolbar>
</Container>
</AppBar>
</HideOnScroll>
</React.Fragment>
);
}
export default ResponsiveAppBar;

Putting It All Together: Complete Code for Your Responsive Navbar: Full Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
Explain
"use client";
import React from "react";
import Link from "next/link"; // Import the Link component from Next.js
import AppBar from "@mui/material/AppBar";
import Box from "@mui/material/Box";
import Toolbar from "@mui/material/Toolbar";
import IconButton from "@mui/material/IconButton";
import Typography from "@mui/material/Typography";
import MenuIcon from "@mui/icons-material/Menu";
import Container from "@mui/material/Container";
import Avatar from "@mui/material/Avatar";
import Tooltip from "@mui/material/Tooltip";
import { Tabs, Tab, useMediaQuery, useTheme } from "@mui/material";
import InfoIcon from "@mui/icons-material/Info";
import ManageAccountsIcon from "@mui/icons-material/ManageAccounts";
import TimelineIcon from "@mui/icons-material/Timeline";
import AddReactionIcon from "@mui/icons-material/AddReaction";
import ContactMailIcon from "@mui/icons-material/ContactMail";
import {
List,
ListItemButton,
ListItemIcon,
ListItemText,
Menu,
} from "@mui/material";
import { Home } from "@mui/icons-material";
import Slide from "@mui/material/Slide"; // Import the Slide component from Material-UI
import useScrollTrigger from "@mui/material/useScrollTrigger"; // Import the useScrollTrigger hook
import CssBaseline from "@mui/material/CssBaseline";
import myProfilePic from "./sufi.webp";
import logo from "./logo.webp";
import Image from "next/image";
function HideOnScroll(props) {
const { children } = props;
const trigger = useScrollTrigger();
return (
<Slide appear={false} direction="down" in={!trigger}>
{children}
</Slide>
);
}
function ResponsiveAppBar(props) {
const handleOpenNavMenu = (event) => {
setAnchorElNav(event.currentTarget);
};
const [anchorElNav, setAnchorElNav] = React.useState(null);
const handleCloseNavMenu = () => {
setAnchorElNav(null);
};
const theme = useTheme();
console.log(theme);
const isMatch = useMediaQuery(theme.breakpoints.down("md"));
console.log(isMatch);
return (
<React.Fragment>
<CssBaseline />
<HideOnScroll {...props}>
<AppBar
className="navbar1"
style={{
width: "93.5%",
right: "3.2%",
border: "2px solid white",
borderRadius: "30px",
background:
"linear-gradient( 90deg, rgba(78, 78, 246, 0.647) 0%, rgba(247, 90, 216, 0.696) 100% );",
}}
>
<Container maxWidth="xl">
<Toolbar disableGutters>
<Typography
variant="h6"
noWrap
component="a"
href="/"
sx={{
mr: 2,
display: { xs: "none", md: "flex" },
fontFamily: "monospace",
fontWeight: 700,
letterSpacing: ".3rem",
color: "inherit",
textDecoration: "none",
}}
className="cursorp"
>
<Avatar
sx={{ display: { xs: "none", md: "flex" }, mr: 1 }}
className="cursorp Tab8 animate__animated animate__backInLeft"
>
<Image
src={logo}
style={{ width: "100%", height: "auto" }}
alt="logo"
width={100}
height={100}
loading="lazy"
/>
</Avatar>
</Typography>
<Box sx={{ flexGrow: 1, display: { xs: "flex", md: "none" } }}>
<IconButton
size="large"
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick={handleOpenNavMenu}
color="inherit"
>
<MenuIcon />
</IconButton>
</Box>
<Typography
variant="h5"
noWrap
component="a"
href=""
sx={{
mr: 2,
display: { xs: "flex", md: "none" },
flexGrow: 1,
fontFamily: "monospace",
fontWeight: 700,
letterSpacing: ".3rem",
color: "inherit",
textDecoration: "none",
}}
>
{" "}
<Avatar
// alt="logo"
// src="https://res.cloudinary.com/dtvtphhsc/image/upload/fl_immutable_cache.preserve_transparency.progressive.sprite/v1693672396/logo_1_lk0neo.webp"
sx={{ display: { xs: "flex", md: "none" }, mr: 1 }}
>
<Image
src={logo}
style={{ width: "100%", height: "auto" }}
alt="logo"
width={100}
height={100}
loading="lazy"
/>
</Avatar>
</Typography>
<Box sx={{ flexGrow: 1, display: { xs: "none", md: "flex" } }}>
{isMatch ? (
<IconButton
size="large"
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick={handleOpenNavMenu}
color="inherit"
>
<MenuIcon />
</IconButton>
) : (
<>
<Tabs centered sx={{ margin: "auto" }}>
<Tab
value="one"
label={
<p>
<Home /> Home
</p>
}
onClick={() => {
window.location.href = "/";
}}
style={{
textDecoration: "none",
color: "white",
}}
className="Tab1 animate__animated animate__zoomIn"
/>
<Tab
label={
<Link
href="/about"
style={{
textDecoration: "none",
color: "white",
opacity: "1",
}}
>
<p>
{" "}
<InfoIcon /> About
</p>
</Link>
}
className="Tab2 animate__animated animate__zoomIn"
>
{" "}
</Tab>
<Tab
value="three"
label={
<p>
<Link
href="/skills"
style={{
textDecoration: "none",
color: "white",
}}
>
{" "}
<ManageAccountsIcon /> Skills
</Link>
</p>
}
className="Tab3 animate__animated animate__zoomIn"
/>
<Tab
value="three"
label={
<p>
<Link
href="/projects"
style={{
textDecoration: "none",
color: "white",
}}
>
<TimelineIcon /> Projects
</Link>
</p>
}
className="Tab3 animate__animated animate__zoomIn"
/>
<Tab
value="three"
label={
<p>
<Link
href="/blogs"
style={{
textDecoration: "none",
color: "white",
}}
>
{" "}
<AddReactionIcon /> Blogs
</Link>
</p>
}
className="Tab5 animate__animated animate__zoomIn"
/>
<Tab
value="three"
label={
<p>
<Link
href="/contact"
style={{
textDecoration: "none",
color: "white",
}}
>
<ContactMailIcon /> Contact
</Link>
</p>
}
className="Tab6 animate__animated animate__zoomIn"
/>
</Tabs>
</>
)}
</Box>
<Box sx={{ flexGrow: 0 }}>
<Tooltip>
<IconButton sx={{ p: 0 }}>
<Avatar className="Tab7 animate__animated animate__backInRight">
<Image
src={myProfilePic}
style={{ width: "100%", height: "auto" }}
alt="sufi"
width={100}
height={100}
loading="lazy"
/>
</Avatar>
</IconButton>
</Tooltip>
<Menu
id="menu-appbar-avatar"
anchorEl={anchorElNav}
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
keepMounted
transformOrigin={{
vertical: "top", // Adjust to match the new position
horizontal: "right",
}}
open={Boolean(anchorElNav)}
onClose={handleCloseNavMenu}
sx={{
display: { xs: "block", md: "none" },
}}
>
{/* <Drawer
open={isDrawerOpen}
onClose={() => setIsDrawerOpen(false)}
> */}
<List className="DrawerList">
<ListItemButton>
<ListItemIcon>
<Home />
</ListItemIcon>
<ListItemText
primary={"Home"}
onClick={() => {
window.location.href = "/";
}}
/>
</ListItemButton>
<ListItemButton>
<ListItemIcon>
<InfoIcon />
</ListItemIcon>
<Link
href="/about"
style={{ textDecoration: "none", color: "white" }}
>
<ListItemText
onClick={handleCloseNavMenu}
primary={"About"}
/>{" "}
</Link>
</ListItemButton>
<ListItemButton>
<ListItemIcon>
<ManageAccountsIcon />
</ListItemIcon>
<Link
href="/skills"
style={{ textDecoration: "none", color: "white" }}
>
<ListItemText
onClick={handleCloseNavMenu}
primary={"Skills"}
/>
</Link>
</ListItemButton>
<ListItemButton>
<ListItemIcon>
<TimelineIcon />
</ListItemIcon>
<Link
href="/projects"
style={{ textDecoration: "none", color: "white" }}
>
<ListItemText
onClick={handleCloseNavMenu}
primary={"Projects"}
/>
</Link>
</ListItemButton>
<ListItemButton>
<ListItemIcon>
<AddReactionIcon />
</ListItemIcon>
<Link
href="/blogs"
style={{ textDecoration: "none", color: "white" }}
>
<ListItemText
onClick={handleCloseNavMenu}
primary={"Blogs"}
/>
</Link>
</ListItemButton>
<ListItemButton>
<ListItemIcon>
<ContactMailIcon />
</ListItemIcon>
<Link
href="/contact"
style={{ textDecoration: "none", color: "white" }}
>
<ListItemText
onClick={handleCloseNavMenu}
primary={"Contact"}
/>
</Link>
</ListItemButton>
</List>
</Menu>
</Box>
</Toolbar>
</Container>
</AppBar>
</HideOnScroll>
</React.Fragment>
);
}
export default ResponsiveAppBar;
Conclusion
With the ResponsiveAppBar component in place, you now have a responsive navbar for your React or Next.js project that's both functional and visually appealing. Feel free to enhance and customize it further to meet your project's specific needs.
In our next blog post, we’ll explore how to connect these navigation tabs to different sections of your website. Stay tuned for more exciting web development tutorials!
Please Follow if u find it useful! Happy coding! 🚀
my website: https://sufianmustafa.com/