Theme System Refactor #6
@@ -1,48 +0,0 @@
|
|||||||
import * as React from "react";
|
|
||||||
import { ThemeProvider, createTheme } from "@mui/material/styles";
|
|
||||||
import { getDesignTokens } from "./shared-theme/themePrimitives";
|
|
||||||
import { inputsCustomizations } from "./shared-theme/customizations/inputs";
|
|
||||||
import { dataDisplayCustomizations } from "./shared-theme/customizations/dataDisplay";
|
|
||||||
import { feedbackCustomizations } from "./shared-theme/customizations/feedback";
|
|
||||||
import { navigationCustomizations } from "./shared-theme/customizations/navigation";
|
|
||||||
import { surfacesCustomizations } from "./shared-theme/customizations/surfaces";
|
|
||||||
|
|
||||||
export const ColorModeContext = React.createContext({
|
|
||||||
toggleColorMode: () => {},
|
|
||||||
mode: "light" as "light" | "dark",
|
|
||||||
});
|
|
||||||
|
|
||||||
export default function AppTheme({ children }: { children: React.ReactNode }) {
|
|
||||||
const [mode, setMode] = React.useState<"light" | "dark">("light");
|
|
||||||
|
|
||||||
const colorMode = React.useMemo(
|
|
||||||
() => ({
|
|
||||||
toggleColorMode: () => {
|
|
||||||
setMode((prevMode) => (prevMode === "light" ? "dark" : "light"));
|
|
||||||
},
|
|
||||||
mode,
|
|
||||||
}),
|
|
||||||
[mode]
|
|
||||||
);
|
|
||||||
|
|
||||||
const theme = React.useMemo(
|
|
||||||
() =>
|
|
||||||
createTheme({
|
|
||||||
...getDesignTokens(mode),
|
|
||||||
components: {
|
|
||||||
...inputsCustomizations,
|
|
||||||
...dataDisplayCustomizations,
|
|
||||||
...feedbackCustomizations,
|
|
||||||
...navigationCustomizations,
|
|
||||||
...surfacesCustomizations,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
[mode]
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<ColorModeContext.Provider value={colorMode}>
|
|
||||||
<ThemeProvider theme={theme}>{children}</ThemeProvider>
|
|
||||||
</ColorModeContext.Provider>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -20,7 +20,7 @@ import DarkModeIcon from "@mui/icons-material/DarkMode";
|
|||||||
import LightModeIcon from "@mui/icons-material/LightMode";
|
import LightModeIcon from "@mui/icons-material/LightMode";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useAuth } from "../react-auth";
|
import { useAuth } from "../react-auth";
|
||||||
import { ColorModeContext } from "./AppTheme";
|
import { ColorModeContext } from "./shared-theme/AppTheme";
|
||||||
|
|
||||||
interface HeaderProps {
|
interface HeaderProps {
|
||||||
routerMapping: {
|
routerMapping: {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import process from 'process';
|
|||||||
import { AuthProvider } from "../react-auth";
|
import { AuthProvider } from "../react-auth";
|
||||||
import Header from './Header';
|
import Header from './Header';
|
||||||
import Footer from './Footer';
|
import Footer from './Footer';
|
||||||
import AppTheme from './AppTheme';
|
import AppTheme from './shared-theme/AppTheme';
|
||||||
|
|
||||||
window.Buffer = Buffer;
|
window.Buffer = Buffer;
|
||||||
window.process = process;
|
window.process = process;
|
||||||
|
|||||||
@@ -1,53 +1,82 @@
|
|||||||
import * as React from 'react';
|
import * as React from "react";
|
||||||
import { ThemeProvider, createTheme } from '@mui/material/styles';
|
import {
|
||||||
import type { ThemeOptions } from '@mui/material/styles';
|
ThemeProvider,
|
||||||
import { inputsCustomizations } from './customizations/inputs';
|
createTheme,
|
||||||
import { dataDisplayCustomizations } from './customizations/dataDisplay';
|
CssBaseline,
|
||||||
import { feedbackCustomizations } from './customizations/feedback';
|
} from "@mui/material";
|
||||||
import { navigationCustomizations } from './customizations/navigation';
|
|
||||||
import { surfacesCustomizations } from './customizations/surfaces';
|
|
||||||
import { colorSchemes, typography, shadows, shape } from './themePrimitives';
|
|
||||||
|
|
||||||
interface AppThemeProps {
|
import { getDesignTokens } from "./themePrimitives";
|
||||||
|
|
||||||
|
import { inputsCustomizations } from "./customizations/inputs";
|
||||||
|
import { dataDisplayCustomizations } from "./customizations/dataDisplay";
|
||||||
|
import { feedbackCustomizations } from "./customizations/feedback";
|
||||||
|
import { navigationCustomizations } from "./customizations/navigation";
|
||||||
|
import { surfacesCustomizations } from "./customizations/surfaces";
|
||||||
|
|
||||||
|
export type ColorMode = "light" | "dark";
|
||||||
|
|
||||||
|
type ColorModeContextValue = {
|
||||||
|
mode: ColorMode;
|
||||||
|
setMode: (mode: ColorMode) => void;
|
||||||
|
toggleColorMode: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ColorModeContext =
|
||||||
|
React.createContext<ColorModeContextValue>({
|
||||||
|
mode: "light",
|
||||||
|
setMode: () => {},
|
||||||
|
toggleColorMode: () => {},
|
||||||
|
});
|
||||||
|
|
||||||
|
type AppThemeProps = {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
/**
|
defaultMode?: ColorMode;
|
||||||
* This is for the docs site. You can ignore it or remove it.
|
};
|
||||||
*/
|
|
||||||
disableCustomTheme?: boolean;
|
export default function AppTheme({
|
||||||
themeComponents?: ThemeOptions['components'];
|
children,
|
||||||
}
|
defaultMode = "dark",
|
||||||
|
}: AppThemeProps) {
|
||||||
|
const [mode, setMode] =
|
||||||
|
React.useState<ColorMode>(defaultMode);
|
||||||
|
|
||||||
|
const toggleColorMode = React.useCallback(() => {
|
||||||
|
setMode((prev) =>
|
||||||
|
prev === "light" ? "dark" : "light"
|
||||||
|
);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const contextValue = React.useMemo(
|
||||||
|
() => ({
|
||||||
|
mode,
|
||||||
|
setMode,
|
||||||
|
toggleColorMode,
|
||||||
|
}),
|
||||||
|
[mode, toggleColorMode]
|
||||||
|
);
|
||||||
|
|
||||||
|
const theme = React.useMemo(
|
||||||
|
() =>
|
||||||
|
createTheme({
|
||||||
|
...getDesignTokens(mode),
|
||||||
|
|
||||||
|
components: {
|
||||||
|
...inputsCustomizations,
|
||||||
|
...dataDisplayCustomizations,
|
||||||
|
...feedbackCustomizations,
|
||||||
|
...navigationCustomizations,
|
||||||
|
...surfacesCustomizations,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
[mode]
|
||||||
|
);
|
||||||
|
|
||||||
export default function AppTheme(props: AppThemeProps) {
|
|
||||||
const { children, disableCustomTheme, themeComponents } = props;
|
|
||||||
const theme = React.useMemo(() => {
|
|
||||||
return disableCustomTheme
|
|
||||||
? {}
|
|
||||||
: createTheme({
|
|
||||||
// For more details about CSS variables configuration, see https://mui.com/material-ui/customization/css-theme-variables/configuration/
|
|
||||||
cssVariables: {
|
|
||||||
colorSchemeSelector: 'data-mui-color-scheme',
|
|
||||||
cssVarPrefix: 'template',
|
|
||||||
},
|
|
||||||
colorSchemes, // Recently added in v6 for building light & dark mode app, see https://mui.com/material-ui/customization/palette/#color-schemes
|
|
||||||
typography,
|
|
||||||
shadows,
|
|
||||||
shape,
|
|
||||||
components: {
|
|
||||||
...inputsCustomizations,
|
|
||||||
...dataDisplayCustomizations,
|
|
||||||
...feedbackCustomizations,
|
|
||||||
...navigationCustomizations,
|
|
||||||
...surfacesCustomizations,
|
|
||||||
...themeComponents,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}, [disableCustomTheme, themeComponents]);
|
|
||||||
if (disableCustomTheme) {
|
|
||||||
return <React.Fragment>{children}</React.Fragment>;
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<ThemeProvider theme={theme} disableTransitionOnChange>
|
<ColorModeContext.Provider value={contextValue}>
|
||||||
{children}
|
<ThemeProvider theme={theme}>
|
||||||
</ThemeProvider>
|
<CssBaseline />
|
||||||
|
{children}
|
||||||
|
</ThemeProvider>
|
||||||
|
</ColorModeContext.Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user