Skip to main content
Version: 4.xx.xx
Swizzle Ready
Source Code

<ThemedLayout>

<ThemedLayoutV2> component that uses the <Drawer> from Material UI library to define the layout and structure of a web page. It includes customizable components for the header, sidebar, title, footer, and off-layout area, which can be replaced or customized as needed.

By using <ThemedLayoutV2>, developers can create a consistent look and feel across multiple pages or sections of a website, while also improving code maintainability and reusability. The customizable sections of <ThemedLayoutV2> include:

  • <ThemedHeaderV2>: displayed at the top of the page and can display the user's name and avatar.
  • <ThemedSiderV2>: displayed on the left side of the page and can display menu items.
  • <ThemedTitleV2V2>: displayed at the top of <ThemedSiderV2> and includes an icon and text.
  • <Footer>: displayed at the bottom of the page.
  • <OffLayoutArea>: rendered outside of the main layout component and can be placed anywhere on the page while still being part of the overall layout.

Footer and OffLayoutArea do not have any default components.

Usageโ€‹

localhost:3000/samples
import { Refine } from "@refinedev/core";

import { ThemedLayoutV2, RefineThemes, RefineSnackbarProvider } from "@refinedev/mui";
import { CssBaseline, GlobalStyles } from "@mui/material";
import { ThemeProvider } from "@mui/material/styles";

import routerProvider from "@refinedev/react-router-v6";
import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import dataProvider from "@refinedev/simple-rest";

import { MuiInferencer } from "@refinedev/inferencer/mui";

const API_URL = "https://api.fake-rest.refine.dev";

const App: React.FC = () => {
return (
<BrowserRouter>
<ThemeProvider theme={RefineThemes.Blue}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<RefineSnackbarProvider>
<Refine
dataProvider={dataProvider(API_URL)}
routerProvider={routerProvider}
resources={[
{
name: "samples",
list: "/samples",
},
]}
>
<Routes>
<Route
element={
<ThemedLayoutV2>
<Outlet />
</ThemedLayoutV2>
}
>
<Route path="/samples" element={<MuiInferencer />} />
</Route>
</Routes>
</Refine>
</RefineSnackbarProvider>
</ThemeProvider>
</BrowserRouter>
);
};
NOTE

<ThemedLayoutV2> is designed to be responsive. In the live-preview, it appears in tablet mode and toggle <Drawer>. On larger screens, it will use fixed open <Drawer>.

INFORMATION

Example of above showing how to use <ThemedLayoutV2> with React Router v6. You can see these examples for other routers:

โš ๏ธ Next.js examples are using <ThemedLayout> from @refinedev/antd package. But you can use <ThemedLayoutV2> from @refinedev/mui as same.

Propsโ€‹

Siderโ€‹

In <ThemedLayoutV2>, the sidebar section is rendered using the <ThemedSiderV2> component by default. This component is specifically designed to generate menu items based on the resources defined in <Refine> components, using the useMenu hook. However, if desired, it's possible to replace the default <ThemedSiderV2> component by passing a custom component to the Sider prop.

import { Refine } from "@refinedev/core";
import { ThemedLayoutV2 } from "@refinedev/mui";

import { CustomSider } from "./CustomSider";

const App: React.FC = () => {
return (
<Refine
// ...
>
<ThemedLayoutV2
Sider={() => <CustomSider />}
>
{/* ... */}
</ThemedLayoutV2>
</Refine>
);
};

Also, you can customize the default <ThemedSiderV2> component either by using its props or with the swizzle feature.

Here is an example of how to customize the default <ThemedSiderV2> component using the render and Title prop:

import { Refine } from "@refinedev/core";
import { ThemedLayoutV2, ThemedSiderV2 } from "@refinedev/mui";

import { CustomTitle } from "./CustomTitle";

const App: React.FC = () => {
return (
<Refine
// ...
>
<ThemedLayoutV2
Sider={() => (
<ThemedSiderV2
Title={({ collapsed }) => <CustomTitle collapsed={collapsed} />}
render={({ items, logout, collapsed }) => {
return (
<>
<div>My Custom Element</div>
{items}
{logout}
</>
);
}}
/>
)}
>
{/* ... */}
</ThemedLayoutV2>
</Refine>
);
};

Sider Propsโ€‹

PropTypeDescription
TitleReact.FCComponent to render at the top
renderSiderRenderFunctionFunction to render the menu items and other elements inside the <ThemedSider>
metaRecord<string,any>Meta data to use when creating routes for the menu items
activeItemDisabledbooleanWhether clicking on an active sider item should reload the page
type SiderRenderFunction = (props: {
items: JSX.Element[];
logout: React.ReactNode;
dashboard: React.ReactNode;
collapsed: boolean;
}) => React.ReactNode;

initialSiderCollapsedโ€‹

This prop is used to set the initial collapsed state of the <ThemedSiderV2> component. If it is true, It will be collapsed by default.

<ThemedLayoutV2
initialSiderCollapsed={true}
>
{/* ... */}
</ThemedLayoutV2>

In <ThemedLayoutV2>, the header section is rendered using the <ThemedHeaderV2> component by default. It uses useGetIdentity hook to display the user's name and avatar on the right side of the header. However, if desired, it's possible to replace the default <ThemedHeaderV2> component by passing a custom component to the Header prop.

Here is an example of how to replace the default <ThemedHeaderV2> component:

import { Refine } from "@refinedev/core";
import { ThemedLayoutV2 } from "@refinedev/mui";

import { CustomHeader } from "./CustomHeader";

const App: React.FC = () => {
return (
<Refine
// ...
>
<ThemedLayoutV2
Header={() => <CustomHeader />}
>
{/* ... */}
</ThemedLayoutV2>
</Refine>
);
};

You can also change the default sticky behavior of the <ThemedHeaderV2> component using the sticky prop, which is optional and defaults to true. An example of its usage is shown below:

import { Refine } from "@refinedev/core";
import {
ThemedLayoutV2,
ThemedHeaderV2,
} from "@refinedev/mui";

const App: React.FC = () => {
return (
<Refine
// ...
>
<ThemedLayoutV2
Header={() => <ThemedHeaderV2 sticky={false} />}
>
{/* ... */}
</ThemedLayoutV2>
</Refine>
);
};

Titleโ€‹

In <ThemedLayoutV2>, the title section is rendered using the <ThemedTitleV2V2> component by default. However, if desired, it's possible to replace the default <ThemedTitleV2V2> component by passing a custom component to the Title prop.

Here is an example of how to replace the default <ThemedTitleV2V2> component:

import { Refine } from "@refinedev/core";
import { ThemedLayoutV2, ThemedTitle } from "@refinedev/mui";

import { MyLargeIcon, MySmallIcon } from "./MyIcon";

const App: React.FC = () => {
return (
<Refine
// ...
>
<ThemedLayoutV2
Title={({ collapsed }) => (
<ThemedTitleV2V2
// collapsed is a boolean value that indicates whether the <Sidebar> is collapsed or not
collapsed={collapsed}
icon={collapsed ? <MySmallIcon /> : <MyLargeIcon />}
text="My Project"
/>
)}
>
{/* ... */}
</ThemedLayoutV2>
</Refine>
);
};

The footer section of the layout is displayed at the bottom of the page. refine doesn't provide a default footer component. However, you can pass a custom component to the Footer prop to display a footer section.

Here is an example of how to display a footer section:

localhost:3000/samples
import { Refine } from "@refinedev/core";

import { ThemedLayoutV2, RefineThemes } from "@refinedev/mui";
import { CssBaseline, GlobalStyles, Box } from "@mui/material";
import { ThemeProvider } from "@mui/material/styles";
import { MuiInferencer } from "@refinedev/inferencer/mui";

import routerProvider from "@refinedev/react-router-v6";
import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import dataProvider from "@refinedev/simple-rest";

import { authProvider } from "./authProvider";

const API_URL = "https://api.fake-rest.refine.dev";

const App: React.FC = () => {
return (
<BrowserRouter>
<ThemeProvider theme={RefineThemes.Blue}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(API_URL)}
authProvider={authProvider}
resources={[
{
name: "samples",
list: "/samples",
},
]}
>
<Routes>
<Route
element={
<ThemedLayoutV2
Footer={() => (
<Box
sx={{
display: "flex",
alignItems: "center",
justifyContent: "center",
width: "100%",
height: "64px",
backgroundColor: "primary.main",
}}
>
My Custom Footer
</Box>
)}
>
<Outlet />
</ThemedLayoutV2>
}
>
<Route path="samples">
<Route index element={<MuiInferencer />} />
</Route>
</Route>
</Routes>
</Refine>
</ThemeProvider>
</BrowserRouter>
);
};
import { Refine } from "@refinedev/core";
import { ThemedLayoutV2 } from "@refinedev/mui";
import { Box } from "@mui/material";

const App: React.FC = () => {
return (
<Refine
// ...
>
<ThemedLayoutV2
Footer={() => (
<Box
sx={{
display: "flex",
alignItems: "center",
justifyContent: "center",
width: "100%",
height: "64px",
backgroundColor: "primary.main",
}}
>
My Custom Footer
</Box>
)}
>
{/* ... */}
</ThemedLayoutV2>
</Refine>
);
};

OffLayoutAreaโ€‹

Used to component is rendered outside of the main layout component, allowing it to be placed anywhere on the page while still being part of the overall layout .refine doesn't provide a default off-layout area component. However, you can pass a custom component to the OffLayoutArea prop to display a custom off-layout area.

Here is an example of how to display a custom off-layout area:

localhost:3000/samples
import { Refine } from "@refinedev/core";

import { ThemedLayoutV2, RefineThemes } from "@refinedev/mui";
import { CssBaseline, GlobalStyles, Fab } from "@mui/material";
import { ThemeProvider } from "@mui/material/styles";
import { MuiInferencer } from "@refinedev/inferencer/mui";

import routerProvider from "@refinedev/react-router-v6";
import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import dataProvider from "@refinedev/simple-rest";

import { authProvider } from "./authProvider";

const API_URL = "https://api.fake-rest.refine.dev";

const App: React.FC = () => {
return (
<BrowserRouter>
<ThemeProvider theme={RefineThemes.Blue}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(API_URL)}
authProvider={authProvider}
resources={[
{
name: "samples",
list: "/samples",
},
]}
>
<Routes>
<Route
element={
<ThemedLayoutV2
OffLayoutArea={() => (
<Fab
size="small"
color="primary"
onClick={() => alert("Off layout are clicked")}
sx={{
position: "fixed",
bottom: "16px",
left: "16px",
}}
variant="extended"
>
Send us Feedback ๐Ÿ‘‹
</Fab>
)}
>
<Outlet />
</ThemedLayoutV2>
}
>
<Route path="samples">
<Route index element={<MuiInferencer />} />
</Route>
</Route>
</Routes>
</Refine>
</ThemeProvider>
</BrowserRouter>
);
};
import { Refine } from "@refinedev/core";
import { ThemedLayoutV2 } from "@refinedev/mui";
import { Fab } from "@mui/material";

const App: React.FC = () => {
return (
<Refine
// ...
>
<ThemedLayoutV2
OffLayoutArea={() => (
<Fab
size="small"
color="primary"
sx={{
position: "fixed",
bottom: "16px",
left: "16px",
}}
onClick={() => alert("Off layout are clicked")}
variant="extended"
>
Send us Feedback ๐Ÿ‘‹
</Fab>
)}
>
{/* ... */}
</ThemedLayoutV2>
</Refine>
);
};

Customizing with swizzleโ€‹

NOTE

๐Ÿšจ This feature is available with @refine/cli. Please refer to CLI documentation for more information.

<ThemedLayoutV2> component source code can be ejecting using the swizzle command. This will create a copy of the component in your project's src directory, allowing you to customize as your needs.

Usageโ€‹

Let's create a new component by swizzling the <ThemedLayoutV2> components.

> npm run refine swizzle

? Which package do you want to swizzle? (Use arrow keys or type to search)

Data Provider
โ—ฏ @refinedev/simple-rest
UI Framework
โ—‰ @refinedev/mui

First, you need to select the package you want to swizzle. In this example, we will swizzle the @refinedev/mui package.

INFORMATION

refine CLI will only show the packages that are installed in your project.

? Which component do you want to swizzle?

โ—ฏ TagField
โ—ฏ TextField
โ—ฏ UrlField
Other
โ—ฏ Breadcrumb
โฏโ—‰ ThemedLayoutV2
Pages
โ—ฏ ErrorPage
โ—ฏ AuthPage
(Move up and down to reveal more choices)

Then, you need to select the component you want to swizzle. In this example, we will swizzle the ThemedLayoutV2 component.

Successfully swizzled Themed Layout
Files created:
- src/components/themedLayout/sider.tsx
- src/components/themedLayout/header.tsx
- src/components/themedLayout/title.tsx
- src/components/themedLayout/index.tsx

Warning:
If you want to change the default layout;
You should pass layout related components to the <ThemedLayoutV2/> component's props.

โ•ญ App.tsx โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ โ”‚
โ”‚ import { ThemedLayoutV2 } from "components/themedLayout"; โ”‚
โ”‚ import { ThemedHeaderV2 } from "components/themedLayout/header"; โ”‚
โ”‚ import { ThemedSiderV2 } from "components/themedLayout/sider"; โ”‚
โ”‚ import { ThemedTitleV2 } from "components/themedLayout/title"; โ”‚
โ”‚ โ”‚
โ”‚ const App = () => { โ”‚
โ”‚ return ( โ”‚
โ”‚ <Refine โ”‚
โ”‚ /* ... */ โ”‚
โ”‚ > โ”‚
โ”‚ <ThemedLayoutV2 โ”‚
โ”‚ Header={ThemedHeaderV2} โ”‚
โ”‚ Sider={ThemedSiderV2} โ”‚
โ”‚ Title={ThemedTitleV2} โ”‚
โ”‚ /> โ”‚
โ”‚ /* ... */ โ”‚
โ”‚ </ThemedLayoutV2> โ”‚
โ”‚ </Refine> โ”‚
โ”‚ ); โ”‚
โ”‚ } โ”‚
โ”‚ โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Finally, the swizzle command will create a new folder in the src/components/layout directory and generate the layout components of the @refinedev/mui package in it.

You can use these components in your project as you wish.

import { Refine } from "@refinedev/core";
import { ThemedLayoutV2 } from "components/themedLayout";
import { ThemedHeaderV2 } from "components/themedLayout/header";
import { ThemedSiderV2 } from "components/themedLayout/sider";
import { ThemedTitleV2 } from "components/themedLayout/title";

const App = () => {
return (
<Refine
/* ... */
>
<ThemedLayoutV2 Header={ThemedHeaderV2} Sider={ThemedSiderV2} Title={ThemedTitleV2}>
/* ... */
</ThemedLayoutV2>
</Refine>
);
};
INFORMATION

refine CLI determines the path to create a new folder according to the framework you are using. For example, if you are using the remix, the path will be app/components/layout.

CAUTION

If there is already a file with the same name in the directory, the swizzle command will not overwrite it.

Migrate ThemedLayout to ThemedLayoutV2โ€‹

Fixed some UI problems with ThemedLayoutV2. If you are still using ThemedLayout you can update it by following these steps.

Only if you are using ThemedLayout. If you are not customizing the Header component, an update like the one below will suffice.

src/App.tsx
-import { ThemedLayout } from "@refinedev/mui";
+import { ThemedLayoutV2 } from "@refinedev/mui";

...
-<ThemedLayout>
+<ThemedLayoutV2>
<Outlet />
-</ThemedLayout>
+</ThemedLayoutV2>
...

But mostly we customize the Header component. For this, an update like the one below will suffice. Here, a HamburgerMenu should be added to the Header component that we have customized for the collapse/uncollapse of the Sider component.

src/components/header/index.tsx
-import { RefineThemedLayoutHeaderProps } from "@refinedev/mui";
+import { RefineThemedLayoutV2HeaderProps, HamburgerMenu } from "@refinedev/mui";

-export const Header: React.FC<RefineThemedLayoutHeaderProps> = ({
- isSiderOpen,
- onToggleSiderClick,
- toggleSiderIcon: toggleSiderIconFromProps,
-}) => {
+export const Header: React.FC<RefineThemedLayoutV2HeaderProps> = () => {
return (
<AppBar position="sticky">
<Toolbar>
<Stack direction="row" width="100%" alignItems="center">
- {hasSidebarToggle && (
- <IconButton
- color="inherit"
- aria-label="open drawer"
- onClick={() => onToggleSiderClick?.()}
- edge="start"
- sx={{
- mr: 2,
- display: { xs: "none", md: "flex" },
- ...(isSiderOpen && { display: "none" }),
- }}
- >
- {toggleSiderIconFromProps?.(
- Boolean(isSiderOpen),
- ) ?? <Menu />}
- </IconButton>
- )}
+ <HamburgerMenu />
<Stack
direction="row"
width="100%"
justifyContent="flex-end"
alignItems="center"
gap="16px"
>
{(user?.avatar || user?.name) && (
<Stack
direction="row"
gap="16px"
alignItems="center"
justifyContent="center"
>
{user?.name && (
<Typography variant="subtitle2">
{user?.name}
</Typography>
)}
<Avatar src={user?.avatar} alt={user?.name} />
</Stack>
)}
</Stack>
</Stack>
</Toolbar>
</AppBar>
);
};

Hamburger Menuโ€‹

The HamburgerMenu component is a component that is used to collapse/uncollapse the Sider component. It is used by default in the Header component. However, you can do this anywhere you want using the <HamburgerMenu /> component. Below you can see an example put on the dashboard page.

localhost:3000/samples
import { Refine } from "@refinedev/core";

import {
ThemedLayoutV2,
RefineThemes,
HamburgerMenu,
} from "@refinedev/mui";
import { CssBaseline, GlobalStyles, Box } from "@mui/material";
import { ThemeProvider } from "@mui/material/styles";
import { MuiInferencer } from "@refinedev/inferencer/mui";

import routerProvider from "@refinedev/react-router-v6";
import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";

import dataProvider from "@refinedev/simple-rest";

import { authProvider } from "./authProvider";

const API_URL = "https://api.fake-rest.refine.dev";

const DashboardPage = () => {
return (
<Box
sx={{
paddingLeft: "10px",
}}
>
<HamburgerMenu />
</Box>
);
};

const App: React.FC = () => {
return (
<BrowserRouter>
<ThemeProvider theme={RefineThemes.Blue}>
<CssBaseline />
<GlobalStyles styles={{ html: { WebkitFontSmoothing: "auto" } }} />
<Refine
routerProvider={routerProvider}
dataProvider={dataProvider(API_URL)}
authProvider={authProvider}
resources={[
{
name: "Dashboard",
list: "/",
},
{
name: "samples",
list: "/samples",
},
]}
>
<Routes>
<Route
element={
<ThemedLayoutV2 Header={() => null}>
<Outlet />
</ThemedLayoutV2>
}
>
<Route path="/" element={<DashboardPage />} />
<Route path="samples">
<Route index element={<MuiInferencer />} />
</Route>
</Route>
</Routes>
</Refine>
</ThemeProvider>
</BrowserRouter>
);
};

FAQโ€‹

How can I persist the collapsed state of the <ThemedSiderV2> component?โ€‹

You can use the initialSiderCollapsed prop to persist the collapsed state of the <ThemedSiderV2> component.

For example, you can get initialSiderCollapsed's value from localStorage or cookie for persistence between sessions.

src/App.tsx
import { useState } from "react";
import { Refine } from "@refinedev/core";
import { BrowserRouter, Routes, Route, Outlet } from "react-router-dom";
import { ThemedLayoutV2 } from "@refinedev/mui";

const App: React.FC = () => {
// you can get this value from `localStorage` or `cookie`
// for persistence between sessions
const [initialSiderCollapsed, setInitialSiderCollapsed] = useState(true);

return (
<BrowserRouter>
<Refine
// ...
>
{/* ... */}
<Routes>
<Route
element={
<ThemedLayoutV2 initialSiderCollapsed={initialSiderCollapsed}>
<Outlet />
</ThemedLayoutV2>
}
>
{/* ... */}
</Route>
</Routes>
</Refine>
</BrowserRouter>
);
};

export default App;
```