Search code examples
reactjstypescriptmaterial-uiappbar

How do I center a title above my menu items in a material ui app bar using typescript react?


This is what my app bar looks like: my app bar

And this is what I'm going for (the title above the menu items): goal inspiration app bar

This is my code:

import * as React from 'react';
import AppBar from '@mui/material/AppBar';
import Box from '@mui/material/Box';
import Toolbar from '@mui/material/Toolbar';
import Typography from '@mui/material/Typography';
import Menu from '@mui/material/Menu';
import Container from '@mui/material/Container';
import Button from '@mui/material/Button';
import MenuItem from '@mui/material/MenuItem';
import {Link}  from "react-router-dom";

const pages = ['Home', 'About'];

function ResponsiveAppBar() {
  const [anchorElNav, setAnchorElNav] = React.useState<null | HTMLElement>(null);

  const handleOpenNavMenu = (event: React.MouseEvent<HTMLElement>) => {
    setAnchorElNav(event.currentTarget);
  };

  const handleCloseNavMenu = () => {
    setAnchorElNav(null);
  };

  return (
    <AppBar position="static" sx={{ background: '#FEC107', display: "flex", flexDirection: "column", justifyContent: "center"}}>
      <Container maxWidth="xl">
        <Toolbar disableGutters sx={{ alignItems: "flex-start", justifyContent: "center", height: "100%"}}>
          <Box sx={{ display: 'flex', justifyContent: 'center', flexGrow: 1 }}>
            <Typography variant="h6" noWrap>
              To Do List
            </Typography>
            <Menu
              id="menu-appbar"
              anchorEl={anchorElNav}
              anchorOrigin={{
                vertical: 'bottom',
                horizontal: 'left',
              }}
              keepMounted
              transformOrigin={{
                vertical: 'top',
                horizontal: 'left',
              }}
              open={Boolean(anchorElNav)}
              onClose={handleCloseNavMenu}
              sx={{
                display: { xs: 'block', md: 'none' },
              }}
            >
              {pages.map((page) => (
                <MenuItem key={page} onClick={handleCloseNavMenu}>
                  <Typography textAlign="center">
                    <Link to={`/${page}`} style={{ color: 'white' }}>{page}</Link>
                  </Typography>
                </MenuItem>
              ))}
            </Menu>
          </Box>
          <Box sx={{ flexGrow: 1, display: { xs: 'none', md: 'flex' } }}>
            {pages.map((page) => (
              <Button
                key={page}
                onClick={handleCloseNavMenu}
                sx={{ my: 2, color: 'white', display: 'block' }}
              >
                <Link to={`/${page}`} style={{ color: 'black', fontWeight: 'bold', textDecoration: 'none' }}>{page}</Link>
              </Button>
            ))}
          </Box>
        </Toolbar>
      </Container>
    </AppBar>
  );
}
export default ResponsiveAppBar;

I've tried almost everything I found available but nothing wants to work. Even tried chat gpt but it couldn't put the lines above and below each other.


Solution

  • In the Box component you are missing the flex-direction: column; and you must keep the align-items: center; CSS rule to achieve the desired output.

    Try like this:

    import * as React from 'react';
    import AppBar from '@mui/material/AppBar';
    import Box from '@mui/material/Box';
    import Toolbar from '@mui/material/Toolbar';
    import Typography from '@mui/material/Typography';
    import Menu from '@mui/material/Menu';
    import Container from '@mui/material/Container';
    import Button from '@mui/material/Button';
    import MenuItem from '@mui/material/MenuItem';
    import {Link}  from "react-router-dom";
    
    const pages = ['Home', 'About'];
    
    function ResponsiveAppBar() {
      const [anchorElNav, setAnchorElNav] = React.useState<null | HTMLElement>(null);
    
      const handleOpenNavMenu = (event: React.MouseEvent<HTMLElement>) => {
        setAnchorElNav(event.currentTarget);
      };
    
      const handleCloseNavMenu = () => {
        setAnchorElNav(null);
      };
    
      return (
        <AppBar position="static" sx={{ background: '#FEC107', display: "flex", flexDirection: "column", justifyContent: "center"}}>
          <Container maxWidth="xl">
            <Toolbar disableGutters sx={{ flexDirection: 'column', alignItems: "center", justifyContent: "center", height: "100%"}}>
              <Box sx={{ display: 'flex', justifyContent: 'center', flexGrow: 1 }}>
                <Typography variant="h6" noWrap>
                  To Do List
                </Typography>
                <Menu
                  id="menu-appbar"
                  anchorEl={anchorElNav}
                  anchorOrigin={{
                    vertical: 'bottom',
                    horizontal: 'left',
                  }}
                  keepMounted
                  transformOrigin={{
                    vertical: 'top',
                    horizontal: 'left',
                  }}
                  open={Boolean(anchorElNav)}
                  onClose={handleCloseNavMenu}
                  sx={{
                    display: { xs: 'block', md: 'none' },
                  }}
                >
                  {pages.map((page) => (
                    <MenuItem key={page} onClick={handleCloseNavMenu}>
                      <Typography textAlign="center">
                        <Link to={`/${page}`} style={{ color: 'white' }}>{page}</Link>
                      </Typography>
                    </MenuItem>
                  ))}
                </Menu>
              </Box>
              <Box sx={{ flexGrow: 1, display: { xs: 'none', md: 'flex' } }}>
                {pages.map((page) => (
                  <Button
                    key={page}
                    onClick={handleCloseNavMenu}
                    sx={{ my: 2, color: 'white', display: 'block' }}
                  >
                    <Link to={`/${page}`} style={{ color: 'black', fontWeight: 'bold', textDecoration: 'none' }}>{page}</Link>
                  </Button>
                ))}
              </Box>
            </Toolbar>
          </Container>
        </AppBar>
      );
    }
    
    export default ResponsiveAppBar;