material-ui 如何将所有抽屉设置在appbar下?

6tdlim6h  于 4个月前  发布在  其他
关注(0)|答案(4)|浏览(38)

https://mui.com/material-ui/api/drawer/

Kind of issue

Other

Issue description

import { Outlet } from "react-router-dom";
import { Box, Container, CssBaseline, Stack } from "@mui/material";
import AlertMsg from "../components/AlertMsg";
import HomeHeader from "./HomeHeader";
import LeftBar from "./LeftBar";
import RightBar from "./RightBar";
import MainFooter from "./MainFooter";
// kiem tra width
function HomeLayout() {
return (
<>
{/* <Box sx={{ display: "flex" }}>

<main>
      <LeftBar />

      <Outlet />

      <RightBar />
    </main>

    <MainFooter />
  </Box> */}

  <Stack sx={{ minHeight: "100vh" }}>
    <Box position="sticky">
      <HomeHeader />
    </Box>

    <AlertMsg />
    {/* react toastify */}

    <Container className="flex-row">
      {/* 3 thanh phan nay phai xep theo hang ngang */}
      <LeftBar />

      <Outlet />

      <RightBar />
    </Container>

    <Box sx={{ flexGrow: 1 }} />

    <MainFooter />
  </Stack>
</>

);
}
export default HomeLayout;

Context

import * as React from "react";
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 Menu from "@mui/material/Menu";
import MenuIcon from "@mui/icons-material/Menu"; //
import Container from "@mui/material/Container";
import Avatar from "@mui/material/Avatar";
import Button from "@mui/material/Button";
import Tooltip from "@mui/material/Tooltip";
import MenuItem from "@mui/material/MenuItem";
// import AdbIcon from "@mui/icons-material/Adb";
import { styled, alpha } from "@mui/material/styles";
import InputBase from "@mui/material/InputBase"; //
import SearchIcon from "@mui/icons-material/Search"; //
import { upperCase } from "lodash";
// https://mui.com/material-ui/react-app-bar/
const pages = ["Products", "Pricing", "Blog"];
const settings = ["Profile", "Account", "Dashboard", "Logout"];
function ResponsiveAppBar() {
const [anchorElNav, setAnchorElNav] = React.useState(null);
const [anchorElUser, setAnchorElUser] = React.useState(null);
const handleOpenNavMenu = (event) => {
setAnchorElNav(event.currentTarget);
};
const handleOpenUserMenu = (event) => {
setAnchorElUser(event.currentTarget);
};
const handleCloseNavMenu = () => {
setAnchorElNav(null);
};
const handleCloseUserMenu = () => {
setAnchorElUser(null);
};
// search
const Search = styled("div")(({ theme }) => ({
position: "relative",
borderRadius: theme.shape.borderRadius,
backgroundColor: alpha(theme.palette.common.white, 0.15),
"&:hover": {
backgroundColor: alpha(theme.palette.common.white, 0.25),
},
marginLeft: 0,
width: "100%",
[theme.breakpoints.up("sm")]: {
marginLeft: theme.spacing(1),
width: "auto",
},
}));
const SearchIconWrapper = styled("div")(({ theme }) => ({
padding: theme.spacing(0, 2),
height: "100%",
position: "absolute",
pointerEvents: "none",
display: "flex",
alignItems: "center",
justifyContent: "center",
}));
const StyledInputBase = styled(InputBase)(({ theme }) => ({
color: "inherit",
width: "100%",
"& .MuiInputBase-input": {
padding: theme.spacing(1, 1, 1, 0),
// vertical padding + font size from searchIcon
paddingLeft: calc(1em + ${theme.spacing(4)}) ,
transition: theme.transitions.create("width"),
[theme.breakpoints.up("sm")]: {
width: "12ch",
"&:focus": {
width: "20ch",
},
},
},
}));
//
return (
<AppBar
position="relative"
maxWidth="100%"
sx={{
backgroundColor: "#0A3161",
width: "1024px",
zIndex: (theme) => theme.zIndex.drawer + 1,
}}

{/* CENTER ? /}
{/
logo here */}
<IconButton sx={{ p: 0 }}>
<Typography
variant="h6"
noWrap
component="a"
href="#app-bar-with-responsive-menu"
sx={{
mr: 2,
display: { xs: "none", md: "flex" },
fontFamily: "monospace",
fontWeight: 700,
letterSpacing: ".3rem",
color: "inherit",
textDecoration: "none",
}}

MyCompany

{/* main menu ON THE RIGHT OF BAR*/}
      <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>

        <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" },
            upperCase,
          }}
        >
          {pages.map((page) => (
            <MenuItem key={page} onClick={handleCloseNavMenu}>
              <Typography textAlign="center uppercase" sx={upperCase}>
                {page}
              </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" }}
          >
            {page}
          </Button>
        ))}
      </Box>

      <Box sx={{ flexGrow: 0 }}>
        {/* thay avatar account vo */}
        <Tooltip title="Open settings">
          <IconButton onClick={handleOpenUserMenu} sx={{ p: 0 }}>
            <Avatar alt="" src="./logo.png" />
          </IconButton>
        </Tooltip>
        <Menu
          sx={{ mt: "45px" }}
          id="menu-appbar"
          anchorEl={anchorElUser}
          anchorOrigin={{
            vertical: "top",
            horizontal: "right",
          }}
          keepMounted
          transformOrigin={{
            vertical: "top",
            horizontal: "right",
          }}
          open={Boolean(anchorElUser)}
          onClose={handleCloseUserMenu}
        >
          {settings.map((setting) => (
            <MenuItem key={setting} onClick={handleCloseUserMenu}>
              <Typography textAlign="center">{setting}</Typography>
            </MenuItem>
          ))}
        </Menu>
      </Box>

      {/* search CAN PHAI CHO XUONG HANG*/}
      <Search>
        <SearchIconWrapper>
          <SearchIcon />
        </SearchIconWrapper>
        <StyledInputBase
          placeholder="Search"
          inputProps={{ "aria-label": "search" }}
        />
      </Search>
    </Toolbar>
  </Container>
</AppBar>

);
}
export default ResponsiveAppBar;

Search keywords: drawer, appbar

u59ebvdq

u59ebvdq1#

嘿,@thuphan27379;感谢你打开这个问题!你能在一个可复现的沙箱环境中提供上述所有代码吗?这样可以更好地进行故障排除。

qq24tv8q

qq24tv8q2#

我想要实现与https://mui.com/相同的布局,即在页面顶部有一个宽度占满屏幕的appbar。然后,左侧和右侧各有一个侧边栏,它们必须位于appbar下方。

我在问题中展示了我的代码,第一个(问题描述)是HomeLayout.js作为主布局,我想设置这些主要的appbar、leftbar、rightbar、outlet和footer。第二个(上下文)是HomeHeader.js作为主appbar。但是侧边栏仍然总是与appbar一起位于顶部。

你想看看我的项目GitHub仓库吗?

weylhg0b

weylhg0b3#

如果可能的话,可复用的代码沙盒会更好/更快!你能用你想要实现的大致布局概念制作一个吗?

vcudknz3

vcudknz34#

这是我的代码,请帮我,非常感谢。

相关问题