728x90
์ด๋ฒ ์บก์คํค์ ํ๋ฉด์ ์ผ๊ด์ฑ ์๋ ์คํ์ผ ๊ด๋ฆฌ์ ํ์์ฑ์ ๊นจ๋ฌ์๋ค. ์ด๋, ์์ ์ ๋ฐฐ์ ๋ Themes๋ฅผ ์ ์ฉํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค๊ณ ์๊ฐํ์ฌ ๊ฐ๋จํ๊ฒ ๋ณต์ตํ ๊ฒธ ์ ๋ฆฌํด๋ณด์๋ค. โจ
๐Themes๋ฅผ React ํ๋ก์ ํธ์ ์ ์ฉํ๋ ๋ฐฉ๋ฒ
styled-components๋ฅผ ์ด์ฉํ๋ฉด ๋๋ค. ์ค์นํ๊ณ ๋ค์๊ณผ ๊ฐ์ด ํ์ผ์ ์์ฑํ๋ค.
1. theme ํ์ผ ์์ฑํ๊ธฐ
theme ํ์ผ์ ์ฌ์ฉํ ์คํ์ผ์ ์์ฑํ๋ค.
import { DefaultTheme } from "styled-components";
export const theme: DefaultTheme={
bgColor: "#EFEFEF",
subBgColor: "#FFFFFF",
textColor: "#2f3542",
primaryColor: "#22a6b3",
secondaryColor: "#f0932b",
successColor: "#192a56",
infoColor: "#0097e6",
dangerColor: "#c23616",
};
์ด๋, typescript๋ก ํ๋ก์ ํธ๋ฅผ ์งํํ๋ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ ์๋ฌ๊ฐ ๋ฐ์ํ ์ ์๋ค.
styled.d.ts ํ์ผ์ ์์ฑํ๊ณ type์ ์ง์ ํด์ค์ผ ํ๋ค.
import "styled-components";
declare module 'styled-components'{
export interface DefaultTheme{
bgColor: string,
subBgColor: string,
textColor: string,
primaryColor: string,
secondaryColor: string,
successColor: string,
infoColor: string,
dangerColor: string,
}
}
2. ThemeProvider๋ก theme ์ ์ฉํ๊ธฐ
import React from 'react';
import ReactDOM from 'react-dom/client';
import router from "./Router";
import { RouterProvider } from 'react-router-dom';
import { createGlobalStyle, ThemeProvider } from 'styled-components';
import { theme } from './theme';
const GlobalStyle = createGlobalStyle`
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
*{
box-sizing: border-box;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
`;
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
<ThemeProvider theme={theme}>
<GlobalStyle />
<RouterProvider router={router} />
</ThemeProvider>
</React.StrictMode>
);
3. theme ์ฌ์ฉํ๊ธฐ
๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค.
color: ${(props) => props.theme.bgColor}
*์กฐ๊ฑด์์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
const StarParent = styled.p<StarParentProps>`
width: 200%;
height: 100%;
position: absolute;
top: 0;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
font-size: ${({ fontSize }) => `${fontSize}rem`};
color: ${({ active, theme }: StarParentProps & { theme: DefaultTheme }) =>
active ? theme.secondaryColor : 'lightgrey'};
`;
728x90
'๐ป์น(Web) > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React]Netlify๋ก React ๋ฐฐํฌํ๊ธฐ: ์๋ก๊ณ ์นจ, API ์ฐ๊ฒฐ ์๋ฌ ํด๊ฒฐ๋ฐฉ๋ฒ (0) | 2023.09.01 |
---|---|
[React]์น ํ์ค์ ์ค์ํ๋ฉฐ ๊ฐ๋ฐํ๋ ๋ฐฉ๋ฒ (0) | 2023.08.28 |
[React]React ํ๋ก์ ํธ์ CSS ์ ์ฉํ๋ ๋ฐฉ๋ฒ(Nomad Coders ๊ฐ์ ์ ๋ฆฌ) (0) | 2023.08.15 |
[React]CORS ์๋ฌ (0) | 2023.06.16 |
[React]์นด์นด์คํก ์์ ๋ก๊ทธ์ธ, ๊ตฌ๊ธ ์์ ๋ก๊ทธ์ธ ๊ตฌํ (4) | 2023.06.15 |