타입스크립트를 사용하는 프로젝트를 진행중인데,
UI작업이 끝난 기념으로 타입스크립트로 styled-components를 어떻게 써야하는지에 대한 포스팅을 할 것 이다 :)
GlobalStyles.tsx
파일을 만든다.createGlobalStyle
을 임포트한다.
import { createGlobalStyle } from 'styled-components';
const GlobalStyle = createGlobalStyle`
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;
}
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
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;
}
* {
box-sizing: border-box ;
}
html, body {
height: 100%;
font-size: 62.5%;
}
body {
background-color: ${props => props.theme.backgroundGrey};
max-width: 76.8rem;
margin: 0 auto;
margin-top:50px;
}
a {
text-decoration: none;
color:inherit;
}
`;
export default GlobalStyle;
styled.d.ts
파일을 만든다.import 'styled-components';
declare module 'styled-components' {
export interface DefaultTheme {
mainRed: string;
mainPink: string;
mainOrange: string;
mainYellow: string;
mainPurple: string;
backgroundGrey: string;
mainWhite: string;
mainBlack: string;
mainGrey: string;
}
}
theme.ts
파일을 만들어준다.DefaultTheme
을 임포트하고 변수를 정의한다.import { DefaultTheme } from 'styled-components';
export const defaultTheme: DefaultTheme = {
mainRed: '#FF4949',
mainPink: '#F7A9A9',
mainOrange: '#FF8D29',
mainYellow: '#FFCD38',
mainPurple: '#541690',
backgroundGrey: '#f2f2f2',
mainWhite: '#FFFDFD',
mainBlack: '#333333',
mainGrey: '#bdc3c7',
};
export default defaultTheme;
color: ${props=>props.theme.mainBlack};
const ComponentsName = styled('div')<{isActive : boolean}>
와 같은 형식으로 사용한다.export const LoginSubmitButton = styled('button')<{ isActive: boolean }>`
width: 100%;
height: 48px;
background-color: ${props =>
props.isActive ? props.theme.mainRed : props.theme.mainGrey};
border: 0;
border-radius: 12px;
margin-top: 20px;
color: ${props =>
props.isActive ? props.theme.mainWhite : props.theme.mainBlack};
`;
<LoginSubmitButton
type="submit"
isActive={isActive}/>
interface SubmitStyled {
isActive: boolean;
isError:boolean;
}
export const LoginSubmitButton = styled('button')<SubmitStyled>`
width: 100%;
height: 48px;
background-color: ${props =>
props.isActive ? props.theme.mainRed : props.theme.mainGrey};
border: 0;
border-radius: 12px;
margin-top: 20px;
color: ${props =>
props.isError ? props.theme.mainRed : props.theme.mainBlack};
`;
<LoginSubmitButton
type="submit"
isActive={isActive}
isError = {isError}
/>
import styled from 'styled-components';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
const ComponentsName = styled(FontAwesomeIcon) `
font-size:24px;
color:${props=>props.theme.mainRed};
import { faPencil } from '@fortawesome/free-solid-svg-icons';
{....}
<ComponentName icon={faPencil} />