styled() - MUI System
Utility for creating styled components.
mui.com
์ด์ ์ ์ ๋ฆฌํด๋์ ๊ธฐ๋ณธ ์ปดํฌ๋ํธ๋ฅผ ์ปค์คํ ํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
[Material Design] Material-UI(MUI)๋ก react & styled component ์ฌ์ฉํ๊ธฐ <๊ธฐ๋ณธ theme์์ ์ปดํฌ๋ํธ ์ปค์คํฐ๋ง์ด์ง
๊ธฐ๋ณธ ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์ค๋ช ์ ๋๋ค. ๐ฅ https://avoc-o-d.tistory.com/43 [Material Design] Material-UI(MUI)๋ก react & styled component ์ฌ์ฉํ๊ธฐ https://mui.com/material-ui/getting-started/overview/ ์ปดํฌ๋ํธ๋ค์
avoc-o-d.tistory.com
์ ๊ธ์์ ์์ฑํ ๋ฐฉ๋ฒ๋ ๊ฐ๋ฅํ์ง๋ง, theme๋ฅผ ์ฌ์ฉํ์ฌ ์ปค์คํ ํ๋ ๋ฐฉ๋ฒ๋ ๊ฐ๋ฅํฉ๋๋ค.
(์๋ ์ฝ๋ ์ถ์ฒ : ๋ฉํ ๋ฆฌ์ผ ๋์์ธ ์ฌ์ดํธ)
์ปค์คํ ํ theme๋ฅผ ์ ์ํด์ค๋๋ค.
import * as React from 'react';
import { styled, createTheme, ThemeProvider } from '@mui/system';
const customTheme = createTheme({
palette: {
primary: {
main: '#1976d2',
contrastText: 'white',
},
},
});
์ปค์คํ ํ theme๋ก ๋ณธ์ธ์ ์ปดํฌ๋ํธ๋ฅผ ๊ผญ! ๊ฐ์ธ์ค๋๋ค. (์ ๊ทธ๋ฌ๋ฉด ์ปดํฌ๋ํธ์ theme ์ ์ฉ์ด ์ ๋ฉ๋๋ค.)
export default function ThemeUsage() {
return (
<ThemeProvider theme={customTheme}>
... ์ปค์คํ
ํ ์ปดํฌ๋ํธ
</ThemeProvider>
);
}
์๋์ ๊ฐ์ด ์ฝ๋๋ฅผ ์์ฑํด์ฃผ๋ฉด div๋ฅผ sx ๋งค๊ฐ๋ณ์๋ก๋ ์ปค์คํ ํ ์ ์๊ฒ ๋ฉ๋๋ค. ๐๐
import * as React from 'react';
import { styled, createTheme, ThemeProvider } from '@mui/system';
const customTheme = createTheme({
palette: {
primary: {
main: '#1976d2',
contrastText: 'white',
},
},
});
const MyThemeComponent = styled('div')(({ theme }) => ({
color: theme.palette.primary.contrastText,
backgroundColor: theme.palette.primary.main,
padding: theme.spacing(1),
borderRadius: theme.shape.borderRadius,
}));
export default function ThemeUsage() {
return (
<ThemeProvider theme={customTheme}>
<MyThemeComponent>Styled div with theme</MyThemeComponent>
</ThemeProvider>
);
}

ํธ๋ฒ๋์ ๋ ๋์์ธ ๋ฐ๊พธ๋ ๋ฒ
const StyledAppBar = styled(AppBar)(({ theme }) => ({
backgroundColor: theme.palette.primary.main,
transition: theme.transitions.create(["background-color", "transform"], {
duration: theme.transitions.duration.standard,
}),
"&:hover": {
backgroundColor: theme.palette.secondary.main,
transform: "scale(1.3)",
},
}));