์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- ํฌํธํด๋ฆฌ์ค
- ํด๋ก ์ฝ๋ฉ
- ํ์ ์คํฌ๋ฆฝํธ
- ๋ ธ๋ง๋์ฝ๋
- ๋ผ์ดํธ๋ชจ๋
- ๋ฐ์ํ์คํจ2
- sort
- ๋คํฌ๋ชจ๋
- reduce
- firebase
- styled-components
- ๊ฐ๋ฐ์๋ถํด๋ฝ
- onsnapshot
- Today
- Total
rigood
[styled-components] ํ์ ์คํฌ๋ฆฝํธ์์ styled-components ๋คํฌ๋ชจ๋/๋ผ์ดํธ๋ชจ๋ ์ค์ ํ๊ธฐ ๋ณธ๋ฌธ
[styled-components] ํ์ ์คํฌ๋ฆฝํธ์์ styled-components ๋คํฌ๋ชจ๋/๋ผ์ดํธ๋ชจ๋ ์ค์ ํ๊ธฐ
rigood 2023. 5. 18. 13:050. ์ค์น
// styled-components ์ค์น
npm i styled-components
npm i -D @types/styled-components
// ์ํ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ recoil ์ค์น
npm i recoil
1. ํ์ ์ ์ธ
1) src ํด๋ ๋ด์ styled.d.ts ํ์ผ ์์ฑํ๋ค.
2) ์๋จ์ styled-components๋ฅผ importํ๋ค.
3) styled-components ๋ชจ๋์ ์ ์ธํ๊ณ , DefaultTheme ์ธํฐํ์ด์ค๋ฅผ ์ค์ ํด์ค๋ค.
// styled.d.ts
import "styled-components";
declare module "styled-components" {
export interface DefaultTheme {
bgColor: string;
boardColor: string;
titleColor: string;
cardColor: string;
textColor: string;
}
}
2. ํ ๋ง ์์ฑ
1) src ํด๋ ๋ด์ theme.ts ํ์ผ ์์ฑํ๋ค.
2) ์๋จ์ styled-components๋ก๋ถํฐ DefaultTheme์ importํ๋ค.
3) ๋คํฌํ ๋ง, ๋ผ์ดํธํ ๋ง๋ฅผ ์ ์ธํ๊ณ , importํ DefaultTheme์ ํ์ ์ผ๋ก ์ง์ ํด์ค๋ค.
4) ๋คํฌ๋ชจ๋, ๋ผ์ดํธ๋ชจ๋์ ์ฌ์ฉ๋ ์์์ ์ง์ ํด์ค๋ค.
// theme.ts
import { DefaultTheme } from "styled-components";
export const darkTheme: DefaultTheme = {
bgColor: "#F2F1ED",
boardColor: "#FFFDF6",
titleColor: "#4C4A4B",
cardColor: "#ffffff",
textColor: "#000000",
};
export const lightTheme: DefaultTheme = {
bgColor: "#F2F1ED",
boardColor: "#FFFDF6",
titleColor: "#4C4A4B",
cardColor: "#ffffff",
textColor: "#000000",
};
3. recoil๋ก ๋คํฌ๋ชจ๋ state ๊ด๋ฆฌ
1) index.tsx ํ์ผ์ RecoilRoot๋ฅผ ์ ํ ํ๋ค.
// index.tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import { RecoilRoot } from 'recoil';
import App from './App';
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
<RecoilRoot>
<App />
</RecoilRoot>
</React.StrictMode>
);
2) src ํด๋ ๋ด์ recoil.ts ํ์ผ์ ์์ฑํ๊ณ , ๋คํฌ๋ชจ๋ ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ atom์ ์์ฑํ๋ค.
// recoil.ts
import { atom } from "recoil";
export const isDarkAtom = atom({
key: "isDarkAtom",
default: false,
})
4. ThemeProvider ์ ํ
1) App.tsx ํ์ผ์ styled-components์ ThemeProvider๋ฅผ ์ ํ ํด์ค๋ค.
2) recoil์ useRecoilValue ํ ์ ํตํด isDarkAtom์ ๋ถ๋ฌ์จ๋ค.
3) isDark๊ฐ true์ผ ๋๋ darkTheme์, false์ผ ๋๋ lightTheme์ ์ ์ฉํด์ค๋ค.
// App.tsx
import { ThemeProvider, createGlobalStyle } from 'styled-components';
import reset from "styled-reset";
import { useRecoilValue } from "recoil";
import { isDarkAtom } from './recoil';
import { darkTheme, lightTheme } from "./theme";
import Kanban from './Kanban';
const GlobalStyle = createGlobalStyle`
${reset};
*{
box-sizing: border-box;
}
`;
function App() {
const isDark = useRecoilValue(isDarkAtom);
return (
<ThemeProvider theme={isDark ? darkTheme : lightTheme}>
<GlobalStyle />
<Kanban />
</ThemeProvider>
)
}
export default App;
5. ์ปดํฌ๋ํธ์์ CSS ์ค์ ํ๊ธฐ
styled-components์ props๋ฅผ ํตํด CSS๋ฅผ ์ค์ ํ ์ ์๋ค.
props.theme ์์ defaultTheme์์ ์ค์ ํ๋ ๋ณ์๋ค์ด ์๋์์ฑ๋์ด ๋์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
// Kanban.tsx
import styled from "styled-components";
function Kanban() {
return (
<Layout>Kanban</Layout>
)
}
export default Kanban
const Layout = styled.div`
color: ${props => props.theme.textColor}
`;