In my project, I have an index.css file that includes the following @font-face declaration:
//Index.css
...
@font-face {
font-family: "My Font";
font-style: normal;
font-display: swap;
font-weight: 400;
src: url(/public/fonts/my-font.eot);
src: url(/public/fonts/my-font.eot#iefix) format("embedded-opentype"),
url(/public/fonts/my-font.otf) format("otf"),
url(/public/fonts/my-font.svg) format("svg"),
url(/public/fonts/my-font.woff) format("woff"),
url(/public/fonts/my-font.woff2) format("woff2");
}
...
Additionally, I have two files named GlobalStyles.jsx and CustomThemeProvider.jsx which are used to create my MUI theme:
// GlobalStyles.jsx
import GlobalStyles from "@mui/material/GlobalStyles";
const importGlobalStyles = (
<GlobalStyles
styles={{
body: {
backgroundImage: `url(${window.theme.backgroundLink})`,
backgroundColor: `${window.theme.colors.pageBackground}`,
color: `${window.theme.colors.font}`,
fontFamily: `-apple-system, ${window.theme.fontFamilyName}, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu",
"Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;`,
},
}}
/>
);
export default importGlobalStyles;
And
//CustomeThemeProvider.jsx
import React from "react";
import { createTheme, ThemeProvider } from "@mui/material/styles";
export const appTheme = createTheme({
palette: {
primary: {
main: window.theme.colors.primary,
},
error: {
main: window.theme.colors.error,
},
},
typography: {
allVariants: {
fontFamily: `-apple-system, ${window.theme.fonts.fontFamilyName}, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif`,
},
fontFamily: `-apple-system, ${window.theme.fonts.fontFamilyName}, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif`,
h1: {
fontWeight: 500,
fontSize: "30pt",
lineHeight: "40pt",
color: window.theme.colors.primary,
},
},
});
const CustomThemeProvider = (props) => {
const { children } = props;
return <ThemeProvider theme={appTheme}>{children}</ThemeProvider>;
};
export default CustomThemeProvider;
While this setup works well for customizing the theme using window.xxx variables from a public config.js file, I am facing an issue with integrating the @font-face definition from index.css into the theme configuration. My goal is to use variables to specify the font links in the theme, making it easier to white-label fonts when rebuilding the application.
I would appreciate any insights or suggestions on how to accomplish this task effectively without using CSSBaseline in our current implementation. Thank you!