AppBar
is a component provided by Material UI that creates a responsive navigation bar at the top of a React application.
Key takeaways:
Material UI is an open-source library for React components based on Google’s Material Design.
We can use Material UI components to build a functional and visually appealing navigation bar.
We can use
AppBar
for a responsive React navigation bar andToolBar
for horizontal alignment of items.Add icons and text using
Typography
on the navbar, customizing with props likevariant
andsx
.We can also implement a settings option with
Avatar
,IconButton
, andMenu
, controlling visibility with the state.
When it comes to web design, a well-structured user interface is crucial for enhancing user experience. One essential feature in any web application development is a functional navigation bar. In this Answer, we’ll explore how to create a responsive navbar using the React framework and the Material UI component library.
Material UI is an open-source component library that implements Google’s Material Design providing beautifully styled and customizable React components. Among these, the AppBar
component is an excellent choice for creating a sleek and efficient navigation bar in our existing React project.
Material UI allows us to create a well-designed navigation bar that should be seamlessly integrated into the user experience so that users are not distracted while navigating. As Jared Spool said:
“Good design, when it’s done well, becomes invisible. It’s only when it’s done poorly that we notice it.”
We need a React application with Material UI to create a navigation bar.
Note: To get a better experience while viewing the navigation bar in the code widgets presented below, please click the link followed using Your app can be found at link:
.
AppBar
componentPlease click the “Run” button to view a simple and nonfunctional AppBar
component.
import React from 'react'; import AppBar from '@mui/material/AppBar'; import Toolbar from '@mui/material/Toolbar'; function App () { return ( <AppBar position="static"> <Toolbar> </Toolbar> </AppBar> ); } export default App;
Lines 2–3: We import AppBar
and ToolBar
components from @mui/material/AppBar
and @mui/material/ToolBar
, respectively.
Note:
ToolBar
is used to create a horizontal bar layout to align items such as buttons, text, and icons.
AppBar
is used to create responsive navbar at the top of the page.
Line 8: We create the AppBar
component with the position
position
prop helps us define how our React navigation bar should behave on scrolling.
Line 9: We create the ToolBar
component that helps us align the navigation bar’s contents in a horizontal layout. In the coming sections, we will define all functionalities and components in the ToolBar
.
AppBar
Please click the “Run” button to view the logo and text added in the AppBar
component.
import React from 'react'; import AppBar from '@mui/material/AppBar'; import Toolbar from '@mui/material/Toolbar'; import StoreIcon from '@mui/icons-material/Store'; import Typography from '@mui/material/Typography'; function App() { return ( <AppBar position="static"> <Toolbar> <StoreIcon></StoreIcon> <Typography variant="h5" noWrap component="a" href="/" sx={{ mr: 2, fontWeight: 200, fontFamily:'roboto', color:'white', letterSpacing: '.2rem', textDecoration: 'none', }} > Educative </Typography> </Toolbar> </AppBar> ); } export default App;
Line 4: We import StoreIcon
from @mui/icons-material/Store
.
Note: Material UI provides us with a collection of icons that we can get from this link.
Line 5: We import Typography
from @mui/material/Typography
.
Line 12: We create the StoreIcon
component, which will display a store icon.
Lines 13–26: We create the Typography
component to display text. We use the following props in it:
variant
: Defines the type of text to be displayed. The options are h1
, h2
, h3
, h4
, h5
, etc.
noWrap
: Defines that the text should not span to the next line.
component
: Defines that the component should be displayed as an anchor or link.
sx
: Defines the styles to be applied to the text.
Line 27: We write Educative
as the text to be displayed. We can customize the text according to our use case.
AppBar
Primarily, we use a navigation bar to create a link between pages. In this section, we will add pages to our navigation bar. To view the pages added, please click the “Run” button below.
import React from 'react'; import AppBar from '@mui/material/AppBar'; import Toolbar from '@mui/material/Toolbar'; import StoreIcon from '@mui/icons-material/Store'; import Typography from '@mui/material/Typography'; import { Box, Button } from '@mui/material'; function App() { const my_pages = [ 'Solutions' , 'Products', 'Pricing']; return ( <AppBar position="static"> <Toolbar> <StoreIcon></StoreIcon> <Typography variant="h5" noWrap component="a" href="/" sx={{ mr: 2, fontWeight: 200, fontFamily:'roboto', color:'white', letterSpacing: '.2rem', textDecoration: 'none', }} > Educative </Typography> <Box sx={{ flexGrow: 1 , display: 'flex'}}> {my_pages.map((page) => ( <Button key={my_pages} sx={{ my: 3, color: 'white', display: 'block' }} > {page} </Button> ))} </Box> </Toolbar> </AppBar> ); } export default App;
Line 6: We import the Box
and Button
components from @mui/material
.
Line 11: We create an array, my_pages
, which contains the name of pages to include in the navigation bar.
Lines 32–41: We create a Box
component containing a loop that iterates through the my_pages
array and renders a Button
component for each array item.
AppBar
We come across websites with settings options in the top-right corner of the page. In this section, we will see how we can add settings to our AppBar
component. To view the settings option, please click the “Run” button below.
import React, { useState } from 'react'; import AppBar from '@mui/material/AppBar'; import Toolbar from '@mui/material/Toolbar'; import StoreIcon from '@mui/icons-material/Store'; import Typography from '@mui/material/Typography'; import { Box, Button } from '@mui/material'; import Avatar from '@mui/material/Avatar'; import Tooltip from '@mui/material/Tooltip'; import IconButton from '@mui/material/IconButton'; import MenuItem from '@mui/material/MenuItem'; import Menu from '@mui/material/Menu'; function App() { const my_pages = [ 'Solutions' , 'Products', 'Pricing']; const my_settings = ['Profile', 'Account','Logout']; const [anchorElUser, setAnchorElUser] = useState(null); const handleOpenSettingsMenu = (event) => { setAnchorElUser(event.currentTarget); }; const handleCloseSettingsMenu = () => { setAnchorElUser(null); }; return ( <AppBar position="static"> <Toolbar> <StoreIcon></StoreIcon> <Typography variant="h5" noWrap component="a" href="/" sx={{ mr: 2, fontWeight: 200, fontFamily:'roboto', color:'white', letterSpacing: '.2rem', textDecoration: 'none', }} > Educative </Typography> <Box sx={{flexWrap:'wrap',flexGrow: 1, display:'flex' }}> {my_pages.map((page) => ( <Button key={my_pages} sx={{ my: 2, color: 'white', display: 'block' }} > {page} </Button> ))} </Box> <Box sx={{ flexGrow: 0 }}> <Tooltip title="Open my_settings"> <IconButton onClick={handleOpenSettingsMenu} sx={{ p: 0 }}> <Avatar alt="Remy Sharp" src="/static/images/avatar/2.jpg" /> </IconButton> </Tooltip> <Menu sx={{ mt: '55px' }} id="menu-appbar" anchorEl={anchorElUser} anchorOrigin={{ vertical: 'top', horizontal: 'right', }} open={Boolean(anchorElUser)} onClose={handleCloseSettingsMenu} > {my_settings.map((setting) => ( <MenuItem key={setting} onClick={handleCloseSettingsMenu}> <Typography textAlign="center">{setting}</Typography> </MenuItem> ))} </Menu> </Box> </Toolbar> </AppBar> ); } export default App
Lines 7–11: We import the required components to create the settings option.
Line 16: We create the settings array, my_settings
, which contains the setting items we want to add to our settings option.
Line 18: We create a useState
variable, anchorElUser
, that stores the state of the menu.
Lines 20–26: We define the handleOpenSettingsMenu
and the handleCloseSettinsMenu
that controls whether the setting items should be displayed on the screen.
Lines 59–82: We create the Box
component that contains the Avatar
component to be displayed on the screen. We define the onClick
function inside the IconButton
that executes the handlOpenSettingsMenu
function and toggles settings on the screen. The Menu
component contains a loop that iterates through the my_settings
array and renders the MenuItem
components.
In this way, we can create a navigation bar in our React application by leveraging the AppBar
component provided by Material UI.
Quiz!
Which option best describes the use of the sx
prop?
Modifies component behavior.
Sets the default color of the component.
Applies custom styles to the component.
None of them.
Haven’t found what you were looking for? Contact Us
Free Resources