Skip to content

Commit

Permalink
[core] Add theme switcher to dashboard layout (#3674)
Browse files Browse the repository at this point in the history
  • Loading branch information
apedroferreira authored Jul 19, 2024
1 parent 9dad773 commit 98b87ad
Show file tree
Hide file tree
Showing 50 changed files with 1,802 additions and 908 deletions.
52 changes: 39 additions & 13 deletions docs/data/toolpad/core/components/app-provider/AppProviderBasic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import DashboardIcon from '@mui/icons-material/Dashboard';
Expand All @@ -16,15 +17,36 @@ const NAVIGATION = [
title: 'Page',
icon: <DashboardIcon />,
},
// Add the following new item:
{
slug: '/page-2',
title: 'Page 2',
icon: <TimelineIcon />,
},
];

export default function AppProviderBasic() {
function DemoPageContent({ pathname }) {
return (
<Box
sx={{
py: 4,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
textAlign: 'center',
}}
>
<Typography>Dashboard content for {pathname}</Typography>
</Box>
);
}

DemoPageContent.propTypes = {
pathname: PropTypes.string.isRequired,
};

function AppProviderBasic(props) {
const { window } = props;

const [pathname, setPathname] = React.useState('/page');

const router = React.useMemo(() => {
Expand All @@ -35,22 +57,26 @@ export default function AppProviderBasic() {
};
}, [pathname]);

// Remove this const when copying and pasting into your project.
const demoWindow = window !== undefined ? window() : undefined;

return (
// preview-start
<AppProvider navigation={NAVIGATION} router={router}>
<AppProvider navigation={NAVIGATION} router={router} window={demoWindow}>
<DashboardLayout>
<Box
sx={{
py: 4,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
}}
>
<Typography>Dashboard content for {pathname}</Typography>
</Box>
<DemoPageContent pathname={pathname} />
</DashboardLayout>
</AppProvider>
// preview-end
);
}

AppProviderBasic.propTypes = {
/**
* Injected by the documentation to work in an iframe.
* Remove this when copying and pasting into your project.
*/
window: PropTypes.func,
};

export default AppProviderBasic;
49 changes: 34 additions & 15 deletions docs/data/toolpad/core/components/app-provider/AppProviderBasic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import DashboardIcon from '@mui/icons-material/Dashboard';
import TimelineIcon from '@mui/icons-material/Timeline';
import { AppProvider, Router } from '@toolpad/core/AppProvider';
import { AppProvider } from '@toolpad/core/AppProvider';
import { DashboardLayout } from '@toolpad/core/DashboardLayout';
import type { Navigation } from '@toolpad/core';
import type { Navigation, Router } from '@toolpad/core';

const NAVIGATION: Navigation = [
{
Expand All @@ -17,15 +17,40 @@ const NAVIGATION: Navigation = [
title: 'Page',
icon: <DashboardIcon />,
},
// Add the following new item:
{
slug: '/page-2',
title: 'Page 2',
icon: <TimelineIcon />,
},
];

export default function AppProviderBasic() {
function DemoPageContent({ pathname }: { pathname: string }) {
return (
<Box
sx={{
py: 4,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
textAlign: 'center',
}}
>
<Typography>Dashboard content for {pathname}</Typography>
</Box>
);
}

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 AppProviderBasic(props: DemoProps) {
const { window } = props;

const [pathname, setPathname] = React.useState('/page');

const router = React.useMemo<Router>(() => {
Expand All @@ -36,20 +61,14 @@ export default function AppProviderBasic() {
};
}, [pathname]);

// Remove this const when copying and pasting into your project.
const demoWindow = window !== undefined ? window() : undefined;

return (
// preview-start
<AppProvider navigation={NAVIGATION} router={router}>
<AppProvider navigation={NAVIGATION} router={router} window={demoWindow}>
<DashboardLayout>
<Box
sx={{
py: 4,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
}}
>
<Typography>Dashboard content for {pathname}</Typography>
</Box>
<DemoPageContent pathname={pathname} />
</DashboardLayout>
</AppProvider>
// preview-end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<AppProvider navigation={NAVIGATION} router={router}>
<AppProvider navigation={NAVIGATION} router={router} window={demoWindow}>
<DashboardLayout>
<Box
sx={{
py: 4,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
}}
>
<Typography>Dashboard content for {pathname}</Typography>
</Box>
<DemoPageContent pathname={pathname} />
</DashboardLayout>
</AppProvider>
109 changes: 109 additions & 0 deletions docs/data/toolpad/core/components/app-provider/AppProviderTheme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import { extendTheme } from '@mui/material/styles';
import DashboardIcon from '@mui/icons-material/Dashboard';
import TimelineIcon from '@mui/icons-material/Timeline';
import { AppProvider } from '@toolpad/core/AppProvider';
import { DashboardLayout } from '@toolpad/core/DashboardLayout';

const NAVIGATION = [
{
kind: 'header',
title: 'Main items',
},
{
slug: '/page',
title: 'Page',
icon: <DashboardIcon />,
},
{
slug: '/page-2',
title: 'Page 2',
icon: <TimelineIcon />,
},
];

const customTheme = extendTheme({
colorSchemes: {
light: {
palette: {
background: {
default: '#E2FAFF',
paper: '#D9FAFF',
},
},
},
dark: {
palette: {
background: {
default: '#2A4364',
paper: '#112E4D',
},
},
},
},
});

function DemoPageContent({ pathname }) {
return (
<Box
sx={{
py: 4,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
textAlign: 'center',
}}
>
<Typography>Dashboard content for {pathname}</Typography>
</Box>
);
}

DemoPageContent.propTypes = {
pathname: PropTypes.string.isRequired,
};

function AppProviderTheme(props) {
const { window } = props;

const [pathname, setPathname] = React.useState('/page');

const router = React.useMemo(() => {
return {
pathname,
searchParams: new URLSearchParams(),
navigate: (path) => setPathname(String(path)),
};
}, [pathname]);

// Remove this const when copying and pasting into your project.
const demoWindow = window !== undefined ? window() : undefined;

return (
// preview-start
<AppProvider
navigation={NAVIGATION}
router={router}
theme={customTheme}
window={demoWindow}
>
<DashboardLayout>
<DemoPageContent pathname={pathname} />
</DashboardLayout>
</AppProvider>
// preview-end
);
}

AppProviderTheme.propTypes = {
/**
* Injected by the documentation to work in an iframe.
* Remove this when copying and pasting into your project.
*/
window: PropTypes.func,
};

export default AppProviderTheme;
103 changes: 103 additions & 0 deletions docs/data/toolpad/core/components/app-provider/AppProviderTheme.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import { extendTheme } from '@mui/material/styles';
import DashboardIcon from '@mui/icons-material/Dashboard';
import TimelineIcon from '@mui/icons-material/Timeline';
import { AppProvider } from '@toolpad/core/AppProvider';
import { DashboardLayout } from '@toolpad/core/DashboardLayout';
import type { Navigation, Router } from '@toolpad/core';

const NAVIGATION: Navigation = [
{
kind: 'header',
title: 'Main items',
},
{
slug: '/page',
title: 'Page',
icon: <DashboardIcon />,
},
{
slug: '/page-2',
title: 'Page 2',
icon: <TimelineIcon />,
},
];

const customTheme = extendTheme({
colorSchemes: {
light: {
palette: {
background: {
default: '#E2FAFF',
paper: '#D9FAFF',
},
},
},
dark: {
palette: {
background: {
default: '#2A4364',
paper: '#112E4D',
},
},
},
},
});

function DemoPageContent({ pathname }: { pathname: string }) {
return (
<Box
sx={{
py: 4,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
textAlign: 'center',
}}
>
<Typography>Dashboard content for {pathname}</Typography>
</Box>
);
}

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 AppProviderTheme(props: DemoProps) {
const { window } = props;

const [pathname, setPathname] = React.useState('/page');

const router = React.useMemo<Router>(() => {
return {
pathname,
searchParams: new URLSearchParams(),
navigate: (path) => setPathname(String(path)),
};
}, [pathname]);

// Remove this const when copying and pasting into your project.
const demoWindow = window !== undefined ? window() : undefined;

return (
// preview-start
<AppProvider
navigation={NAVIGATION}
router={router}
theme={customTheme}
window={demoWindow}
>
<DashboardLayout>
<DemoPageContent pathname={pathname} />
</DashboardLayout>
</AppProvider>
// preview-end
);
}
Loading

0 comments on commit 98b87ad

Please sign in to comment.