
스토리북을 이용해 재사용하기 좋은 컴포넌트를 만드는 연습을 시작했다.
이를 위한 프로젝트 생성 과정을 기록했다.
React, TS, Emotion, Storybook
Yarn을 이용해서 typescript를 사용하는 Create React App을 생성한다
yarn create react-app [project-name] --template typescript
Emotion은 styled Component와 더불어 가장 유명한 css-in-js 스타일링 라이브러리다.
yarn add @emotion/react @emotion/styled
yarn add --dev @emotion/babel-plugin
그런데 CRA에서는 babel 설정을 커스텀할 수 없는 환경에서는 사용할 수 없고 JSX Pragma를 사용하라는 에러가 발생한다❗
/** @jsxImportSource @emotion/react */
yarn add @craco/craco
{
...
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test"
},
}
yarn add --dev @emotion/babel-preset-css-prop
const config = {
babel: {
presets: [
[
'@babel/preset-react',
{ runtime: 'automatic', importSource: '@emotion/react' },
],
],
plugins: ['@emotion/babel-plugin'],
},
};
export default config;
///<reference types="@emotion/react/types/css-prop" />
"jsx": "react-jsx",
"jsxImportSource": "@emotion/react",
import { css } from '@emotion/react';
const color = 'white';
function App() {
return (
<div
css={css`
padding: 32px;
background-color: hotpink;
font-size: 24px;
border-radius: 4px;
&:hover {
color: ${color};
}
`}>
Hover to change color.
</div>
);
}
export default App;
정상적으로 작동한다!
다음으로 스토리북을 설치할 차례다
npx storybook@latest init
One of your dependencies, babel-preset-react-app, is importing the
"@babel/plugin-proposal-private-property-in-object" package without
declaring it in its dependencies. This is currently working because
"@babel/plugin-proposal-private-property-in-object" is already in your
node_modules folder for unrelated reasons, but it may break at any time.
babel-preset-react-app is part of the create-react-app project, which
is not maintianed anymore. It is thus unlikely that this bug will
ever be fixed. Add "@babel/plugin-proposal-private-property-in-object" to
your devDependencies to work around this error. This will make this message
go away.
yarn add -D @babel/plugin-proposal-private-property-in-object
yarn storybook
정상적으로 작동한다!
감사합니다. 이런 정보를 나눠주셔서 좋아요.