프로젝트에 일관되게 적용시켜줄 색상, 투명도, 그림자 상태 등을 theme로 정의
Mui는 light theme type이 기본 값.
theme 생성
import { createTheme } from "@mui/material";
export const theme = createTheme({
palette: {
primary: {
main: "#1750a5",
light: "skyblue"
},
secondary: {
main: "#15c630",
},
otherColor: {
main: "#999"
}
},
...
})
themeProvider
import { ThemeProvider } from '@mui/material';
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { theme } from './theme';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>
</React.StrictMode>
);
theme 활용하기
useTheme()
import { useTheme } from '@mui/material/styles';
function DeepChild() {
const theme = useTheme();
return <span>{`spacing ${theme.spacing}`}</span>;
}
useStyles()
import { makeStyles } from '@material-ui/core/styles';
const useStyles = makeStyles(theme =>
({
root: {
height: '100%',
background: theme.palette.primary.main
},
menuIconButton: {
marginRight: theme.spacing(2),
},
menuTitle: {
flexGrow: 5,
},
menuButton: {
marginRight: theme.spacing(2),
flexGrow: 1,
display: 'flex'
},
});