Give me a NextJS and MUI layouts that has a drawer on the left with buttons for pages
- User
Give me a NextJS and MUI layouts that has a drawer on the left with buttons for pages
- Assistant
Give me a NextJS and MUI layouts that has a drawer on the left with buttons for pages
Sure! Below is an example of how you can create a Next.js layout with Material-UI (MUI) that includes a left drawer with buttons (links) to different pages. We'll use MUI's Drawer and List components to create the sidebar.
Let's go through each step.
First, create a new Next.js project if you haven't already:
npx create-next-app my-app
cd my-app
Install Material-UI (MUI) and its dependencies:
npm install @mui/material @emotion/react @emotion/styled
Create a Layout component that includes the AppBar and Drawer. You can place this in a components directory.
File: components/Layout.js
// components/Layout.js
import * as React from 'react';
import { useState } from 'react';
import { useRouter } from 'next/router';
import {
AppBar,
Toolbar,
CssBaseline,
Typography,
Drawer,
List,
ListItem,
ListItemIcon,
ListItemText,
IconButton,
Divider,
Box,
} from '@mui/material';
import {
Menu as MenuIcon,
Home as HomeIcon,
Info as InfoIcon,
ContactMail as ContactMailIcon,
} from '@mui/icons-material';
const drawerWidth = 240;
export default function Layout({ children }) {
const router = useRouter();
const [mobileOpen, setMobileOpen] = useState(false);
const handleDrawerToggle = () => {
setMobileOpen(!mobileOpen);
};
const menuItems = [
{ text: 'Home', icon: <HomeIcon />, path: '/' },
{ text: 'About', icon: <InfoIcon />, path: '/about' },
{ text: 'Contact', icon: <ContactMailIcon />, path: '/contact' },
];
const drawer = (
<div>
<Toolbar />
<Divider />
<List>
{menuItems.map((item) => (
<ListItem
button
key={item.text}
onClick={() => {
router.push(item.path);
}}
>
<ListItemIcon>{item.icon}</ListItemIcon>
<ListItemText primary={item.text} />
</ListItem>
))}
</List>
</div>
);
return (
<Box sx={{ display: 'flex' }}>
<CssBaseline />
{/* AppBar */}
<AppBar
position="fixed"
sx={{
width: { md: `calc(100% - ${drawerWidth}px)` },
ml: { md: `${drawerWidth}px` },
}}
>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
edge="start"
onClick={handleDrawerToggle}
sx={{ mr: 2, display: { md: 'none' } }}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap>
My App
</Typography>
</Toolbar>
</AppBar>
{/* Drawer */}
<Box
component="nav"
sx={{ width: { md: drawerWidth }, flexShrink: { md: 0 } }}
aria-label="mailbox folders"
>
{/* The implementation can be swapped with js to avoid SEO duplication of links. */}
<Drawer
variant="temporary"
open={mobileOpen}
onClose={handleDrawerToggle}
anchor="left"
ModalProps={{
keepMounted: true, // Better open performance on mobile.
}}
sx={{
display: { xs: 'block', md: 'none' },
'& .MuiDrawer-paper': { boxSizing: 'border-box', width: drawerWidth },
}}
>
{drawer}
</Drawer>
<Drawer
variant="permanent"
sx={{
display: { xs: 'none', md: 'block' },
'& .MuiDrawer-paper': { boxSizing: 'border-box', width: drawerWidth },
}}
open
>
{drawer}
</Drawer>
</Box>
{/* Main Content */}
<Box
component="main"
sx={{
flexGrow: 1,
p: 3,
width: { md: `calc(100% - ${drawerWidth}px)` },
}}
>
<Toolbar />
{children}
</Box>
</Box>
);
}
Explanation:
xs), the drawer is temporary and slides in/out.md), the drawer is permanent.router.push to navigate between pages when a menu item is clicked.You can wrap your pages with the Layout component.
Option 1: Using _app.js
If you want all pages to use the same layout, you can modify your pages/_app.js file.
File: pages/_app.js
// pages/_app.js
import * as React from 'react';
import Head from 'next/head';
import Layout from '../components/Layout';
function MyApp({ Component, pageProps }) {
return (
<>
<Head>
<title>My App</title>
</Head>
<Layout>
<Component {...pageProps} />
</Layout>
</>
);
}
export default MyApp;
Option 2: Per-page Layouts
Alternatively, if you only want some pages to use the layout, you can wrap those pages individually.
File: pages/index.js
// pages/index.js
import * as React from 'react';
import Typography from '@mui/material/Typography';
import Layout from '../components/Layout';
export default function Home() {
return (
<Layout>
<Typography paragraph>
Welcome to the Home Page!
</Typography>
</Layout>
);
}
Create the about and contact pages referenced in the menu.
File: pages/about.js
// pages/about.js
import * as React from 'react';
import Typography from '@mui/material/Typography';
import Layout from '../components/Layout';
export default function About() {
return (
<Layout>
<Typography paragraph>
This is the About Page.
</Typography>
</Layout>
);
}
File: pages/contact.js
// pages/contact.js
import * as React from 'react';
import Typography from '@mui/material/Typography';
import Layout from '../components/Layout';
export default function Contact() {
return (
<Layout>
<Typography paragraph>
This is the Contact Page.
</Typography>
</Layout>
);
}
Start the development server:
npm run dev
Open http://localhost:3000 in your browser to see the app.
sx prop or by creating custom themes.@mui/icons-material.my-app/
├── components/
│ └── Layout.js
├── pages/
│ ├── _app.js
│ ├── index.js
│ ├── about.js
│ └── contact.js
├── package.json
└── ...
You've now set up a Next.js application with MUI that includes a responsive drawer on the left containing buttons to navigate between pages. This structure provides a solid foundation for building out a more complex application with consistent navigation.
If you have any questions or need further assistance, feel free to ask!
Not yet included in a published release.