Navbar React js 
Tags: javascript, react
Published: 2022-02-21 09:53:05

I made a navbar with Sass that is responsive and looks good on both desktop and mobile devices. This is what it looks like on a desktop screen:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2iref282hbvpactbi7p1.jpg)

And this is what it looks like on a small screen:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rl2ooit2hiiqkgczcz88.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rmvdseeufms4lhous2c7.jpg)

The navbar is in a component called Header because the nav tag is inside a header. The Header is in App.js:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cda4nzw1mbuogsdvm4i5.jpg)

The logo, useState, Link and header.scss are imported in Header.js:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/613mzkfwkv80yu75htx9.jpg)

I used the keyboard on the screen and on my phone because my phone is connected to a PC. I write code on my phone because it's way easier than typing on a keyboard. In this way you can turn on your computer in the morning and code with just a phone wherever you are. I think it's a good idea to unplug it at the end of the day. 
I try to avoid using divs but I needed them to style it. There's a div that's always on top and an unordered list for the hamburger menu inside the nav element:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9trl3hlrdgqgh2qbiuwl.jpg)

The header has its position set to sticky so that when you scroll down it stays at the same place. The logo, search bar and the button that opens the menu are in the class named "top-nav":

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wobpcmqu72kw1ampn1hj.jpg)

&#8811 is an arrow. HTML has so many symbols and emojis that are easy to use, you just have to copy their code. But I also used the "fa fa-bar" icon from Font awesome, which is probably not a good idea because it makes it slower. This is what the lighthouse report showed me: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iaymw1s9yoyyynvzjlpt.jpg)

The nav menu items are links in an unordered list. It's mobile first, so they're hidden by default:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/llkrlxg74snxr0udqivu.jpg)

I didn't want to use a div and spans for the hamburger menu because they're just basic containers and that's not good for screen readers. We should use buttons even if it's not very beautiful. And we should even avoid using icons and use descriptive words instead. Many different people will try to figure out what's going on and some might not have much experience. When I was a kid a lot of sites seemed to be very confusing. 
The function showHam toggles the class named "hidden" and the if-else block removes one icon and adds another one depending on what the user should see at this point. It uses the variable hamClosed to change the icon: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17hvclryx0aug54ix7nk.jpg)

The logo is avif, because I wanted to use a photo and they're tiny. 
These variables are placed in _variables.scss so I can change all the colors real quick:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqxscqkzfcigc53x8v7z.jpg)

And _variables.scss is imported in header.scss:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfzyqaimk6gyr44dpa69.jpg)

"nav-desc" has two direct children - a div for the things that will always stay on top (the logo, search bar and hamburger menu) and an unordered list with links for the menu. They stack on top of each other on small screens because "nav-desc" has a "display: flex" and "flex-direction" is set to "column". Here's the styling:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wr6tgclv6ukeyisjqsiz.jpg)

This is the css for the button, the logo and Link inside "top-nav":

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vdcv47b7wnoys3sefx4.jpg)

And also, the search bar, the submit button and the hover effect of the menu:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kougy9qcs4l4opejt4p.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a21cccu0funcuz78li0z.jpg)

On big screens the button that opens the hamburger menu has a display property set to "none". The "flex-direction" property of the hamburger menu is set to row so they're placed horizontally.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lawfcbc7t2bj8le4ga5u.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3ln7ojqt8walu9bxsy4.jpg)

Too much styling will make it slower. The users will want to see the content, not the styling, so I think it's a good idea to make a few elements a little different so that it looks good, without changing the others.