-
-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Full documentation for React Router integration (#4185)
- Loading branch information
1 parent
8beaef2
commit b0132c4
Showing
43 changed files
with
1,005 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ pnpm-lock.yaml | |
/packages/toolpad-studio/public | ||
|
||
build | ||
dist | ||
|
||
/examples/*/toolpad.yml | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { createTheme } from '@mui/material/styles'; | ||
import Typography from '@mui/material/Typography'; | ||
import DashboardIcon from '@mui/icons-material/Dashboard'; | ||
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart'; | ||
import { createMemoryRouter, RouterProvider, Outlet } from 'react-router-dom'; | ||
import { AppProvider } from '@toolpad/core/react-router-dom'; | ||
import { DashboardLayout } from '@toolpad/core/DashboardLayout'; | ||
import { PageContainer } from '@toolpad/core/PageContainer'; | ||
|
||
function Layout() { | ||
return ( | ||
<DashboardLayout> | ||
<PageContainer> | ||
<Outlet /> | ||
</PageContainer> | ||
</DashboardLayout> | ||
); | ||
} | ||
|
||
function DashboardPage() { | ||
return <Typography>Welcome to Toolpad!</Typography>; | ||
} | ||
|
||
function OrdersPage() { | ||
return <Typography>Welcome to the orders page!</Typography>; | ||
} | ||
|
||
const NAVIGATION = [ | ||
{ | ||
kind: 'header', | ||
title: 'Main items', | ||
}, | ||
{ | ||
title: 'Dashboard', | ||
icon: <DashboardIcon />, | ||
}, | ||
{ | ||
segment: 'orders', | ||
title: 'Orders', | ||
icon: <ShoppingCartIcon />, | ||
}, | ||
]; | ||
|
||
const BRANDING = { | ||
title: 'My Toolpad Core App', | ||
}; | ||
|
||
const demoTheme = createTheme({ | ||
cssVariables: { | ||
colorSchemeSelector: 'data-toolpad-color-scheme', | ||
}, | ||
colorSchemes: { light: true, dark: true }, | ||
breakpoints: { | ||
values: { | ||
xs: 0, | ||
sm: 600, | ||
md: 600, | ||
lg: 1200, | ||
xl: 1536, | ||
}, | ||
}, | ||
}); | ||
|
||
function App(props) { | ||
const { window } = props; | ||
|
||
return ( | ||
<AppProvider | ||
navigation={NAVIGATION} | ||
branding={BRANDING} | ||
theme={demoTheme} | ||
window={window} | ||
> | ||
<Outlet /> | ||
</AppProvider> | ||
); | ||
} | ||
|
||
App.propTypes = { | ||
window: PropTypes.object, | ||
}; | ||
|
||
function ReactRouter(props) { | ||
const { window } = props; | ||
|
||
// Remove this const when copying and pasting into your project. | ||
const demoWindow = window !== undefined ? window() : undefined; | ||
|
||
// preview-start | ||
const router = React.useMemo( | ||
() => | ||
createMemoryRouter([ | ||
{ | ||
element: <App window={demoWindow} />, // root layout route | ||
children: [ | ||
{ | ||
path: '/', | ||
Component: Layout, | ||
children: [ | ||
{ | ||
path: '/', | ||
Component: DashboardPage, | ||
}, | ||
{ | ||
path: '/orders', | ||
Component: OrdersPage, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]), | ||
[demoWindow], | ||
); | ||
|
||
return <RouterProvider router={router} />; | ||
// preview-end | ||
} | ||
|
||
ReactRouter.propTypes = { | ||
/** | ||
* Injected by the documentation to work in an iframe. | ||
* Remove this when copying and pasting into your project. | ||
*/ | ||
window: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default ReactRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import * as React from 'react'; | ||
import { createTheme } from '@mui/material/styles'; | ||
import Typography from '@mui/material/Typography'; | ||
import DashboardIcon from '@mui/icons-material/Dashboard'; | ||
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart'; | ||
import { createMemoryRouter, RouterProvider, Outlet } from 'react-router-dom'; | ||
import { AppProvider } from '@toolpad/core/react-router-dom'; | ||
import { DashboardLayout } from '@toolpad/core/DashboardLayout'; | ||
import { PageContainer } from '@toolpad/core/PageContainer'; | ||
import type { Navigation } from '@toolpad/core'; | ||
|
||
function Layout() { | ||
return ( | ||
<DashboardLayout> | ||
<PageContainer> | ||
<Outlet /> | ||
</PageContainer> | ||
</DashboardLayout> | ||
); | ||
} | ||
|
||
function DashboardPage() { | ||
return <Typography>Welcome to Toolpad!</Typography>; | ||
} | ||
|
||
function OrdersPage() { | ||
return <Typography>Welcome to the orders page!</Typography>; | ||
} | ||
|
||
const NAVIGATION: Navigation = [ | ||
{ | ||
kind: 'header', | ||
title: 'Main items', | ||
}, | ||
{ | ||
title: 'Dashboard', | ||
icon: <DashboardIcon />, | ||
}, | ||
{ | ||
segment: 'orders', | ||
title: 'Orders', | ||
icon: <ShoppingCartIcon />, | ||
}, | ||
]; | ||
|
||
const BRANDING = { | ||
title: 'My Toolpad Core App', | ||
}; | ||
|
||
const demoTheme = createTheme({ | ||
cssVariables: { | ||
colorSchemeSelector: 'data-toolpad-color-scheme', | ||
}, | ||
colorSchemes: { light: true, dark: true }, | ||
breakpoints: { | ||
values: { | ||
xs: 0, | ||
sm: 600, | ||
md: 600, | ||
lg: 1200, | ||
xl: 1536, | ||
}, | ||
}, | ||
}); | ||
|
||
function App(props: { window?: Window }) { | ||
const { window } = props; | ||
|
||
return ( | ||
<AppProvider | ||
navigation={NAVIGATION} | ||
branding={BRANDING} | ||
theme={demoTheme} | ||
window={window} | ||
> | ||
<Outlet /> | ||
</AppProvider> | ||
); | ||
} | ||
|
||
interface DemoProps { | ||
/** | ||
* Injected by the documentation to work in an iframe. | ||
* Remove this when copying and pasting into your project. | ||
*/ | ||
window: () => Window; | ||
} | ||
|
||
export default function ReactRouter(props: DemoProps) { | ||
const { window } = props; | ||
|
||
// Remove this const when copying and pasting into your project. | ||
const demoWindow = window !== undefined ? window() : undefined; | ||
|
||
// preview-start | ||
const router = React.useMemo( | ||
() => | ||
createMemoryRouter([ | ||
{ | ||
element: <App window={demoWindow} />, // root layout route | ||
children: [ | ||
{ | ||
path: '/', | ||
Component: Layout, | ||
children: [ | ||
{ | ||
path: '/', | ||
Component: DashboardPage, | ||
}, | ||
{ | ||
path: '/orders', | ||
Component: OrdersPage, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]), | ||
[demoWindow], | ||
); | ||
|
||
return <RouterProvider router={router} />; | ||
// preview-end | ||
} |
27 changes: 27 additions & 0 deletions
27
docs/data/toolpad/core/introduction/ReactRouter.tsx.preview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const router = React.useMemo( | ||
() => | ||
createMemoryRouter([ | ||
{ | ||
element: <App window={demoWindow} />, // root layout route | ||
children: [ | ||
{ | ||
path: '/', | ||
Component: Layout, | ||
children: [ | ||
{ | ||
path: '/', | ||
Component: DashboardPage, | ||
}, | ||
{ | ||
path: '/orders', | ||
Component: OrdersPage, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
]), | ||
[demoWindow], | ||
); | ||
|
||
return <RouterProvider router={router} />; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.