기록을 불러오는 중입니다...
css 프로퍼티를 지원한다css 프로퍼티를 활용하는 것이다.className 을 받을 수 있는 모든 요소와 컴포넌트는 css 프로퍼티를 사용할 수 있다. 이 프로퍼티는 (1) css 함수를 이용한 스타일 객체와 (2)리터럴 스타일 객체 둘 다 받을 수 있다.css 함수는 tagged templates 와 유사하게 생겼다.<div
css={css`
background-color: hotpink;
&:hover {
color: ${color};
}
`}
>
<div
css={css({
backgroundColor: "hotpink",
"&:hover": {
color: color,
},
})}
>
css 함수의 결과물은 name 과 평탄화된 스타일 문자열인 styles 속성을 가지는 객체다.{
// computed name
"name": "v5tptf",
"styles": "\\n background-color: antiquewhite;\\n color: red;\\n "
}
styled API는 스타일이 적용된 컴포넌트를 만드는데 사용할 수 있다.css 와 마찬가지로 className 속성을 받을 수 있는 컴포넌트라면 모두 적용이 가능하다.css 함수 호출 없이 스타일링을 할 수 있다. 단, 이 경우에는 카멜 케이스로 작성해야 한다.<div
css={{
backgroundColor: 'hotpink',
'&:hover': {
color: 'lightgreen'
}
}}
/>
<div
css={{
background: ['red', 'linear-gradient(#e66465, #9198e5)'],
height: 100
}}
>
Global 컴포넌트를 이용해 전역 스타일을 적용할 수 있다.<Global
styles={css`
* {
color: hotpink;
}
`}
/>
const myCss = css({
color: 'blue',
grid: 1 // Error: Type 'number' is not assignable to type 'Grid | Grid[] | undefined'
})
export const errorCss = css({
color: 'red',
fontWeight: 'bold'
})
// Use arrays to compose styles
export const largeErrorCss = css([errorCss, { fontSize: '1.5rem' }])
export function ErrorMessage({ className, children }) {
return (
<p css={{ color: 'red', fontWeight: 'bold' }} className={className}>
{children}
</p>
)
}
// `fontSize: '1.5rem'` is passed down to the ErrorMessage component via the
// className prop, so ErrorMessage must accept a className prop for this to
// work!
export function LargeErrorMessage({ className, children }) {
return (
<ErrorMessage css={{ fontSize: '1.5rem' }} className={className}>
{children}
</ErrorMessage>
)
}
css 프로퍼티는 내부적으로 className 을 이용해 스타일이 적용되기 때문에, 스타일을 상속받을 컴포넌트는 className 을 구현해야 한다.style prop 사용하기css 프로퍼티와 styled API는 정적 스타일에 사용되어야 한다. 동적 스타일링에는 style 프로퍼티를 사용해야 한다.css 프로퍼티는 입력값에 따라 고유한 스타일을 만들어낸다. 예를 들어, 프사 이미지를 생각해보자. 모든 프사는 width와 height 가 40px 이며 border-radius 가 50%이다. 그런데 각 이미지는 사람마다 다를 것이다. .css-1udhswa {
border-radius: 50%;
width: 40px;
height: 40px;
background-image: url(<https://i.pravatar.cc/150?u=0>);
}
.css-1cpwmbr {
border-radius: 50%;
width: 40px;
height: 40px;
background-image: url(<https://i.pravatar.cc/150?u=1>);
}
.css-am987o {
border-radius: 50%;
width: 40px;
height: 40px;
background-image: url(<https://i.pravatar.cc/150?u=2>);
}
css 프로퍼티 대신 style 프로퍼티를 사용하자.<div style={{
borderRadius: "50%",
width: "40px",
height: "40px",
backgroundImage: "url(...)",
}}/>
<div
css={{
backgroundColor: 'var(--background-image)',
}}
style={{ ['--background-image' as any]: imgUrl }}
/>
@emotion/styled 둘 중 하나만 쓰자css 프로퍼티에 동적 스타일을 할당하는 것을 방지할 수 있다.export const colors = {
primary: '#0d6efd',
success: '#198754',
danger: '#dc3545'
}
keyframes API를 활용해 구현할 수 있다.const bounce = keyframes`
from, 20%, 53%, 80%, to {
transform: translate3d(0,0,0);
}
40%, 43% {
transform: translate3d(0, -30px, 0);
}
70% {
transform: translate3d(0, -15px, 0);
}
90% {
transform: translate3d(0,-4px,0);
}
`;
render(
<div
css={css`
animation: ${bounce} 1s ease infinite;
`}
>
some bouncing text!
</div>
)
renderToString 혹은 renderToNodeStream 같은 함수를 직접 호출해도 된다는 뜻이다.// App.ts
import { renderToString } from 'react-dom/server'
import App from './App'
let html = renderToString(<App />)
// MyDiv.js
const MyDiv = styled('div')({ fontSize: 12 })
<MyDiv>Text</MyDiv>
<style/> 태그로 삽입이된다.<!-- MyDiv.js 가 렌더링 된 후 -->
<style data-emotion-css="21cs4">.css-21cs4 { font-size: 12 }</style>
<div class="css-21cs4">Text</div>
<head> 요소 안에 CSS 코드를 넣고 body > div#root 요소에다가 HTML 코드를 삽입한 문서를 내려줘야 한다.const PasswordInput = props => (
<input
type="password"
css={css`...`}
{...props}
/>
);
render(
<PasswordInput placeholder="pink"/>
);
@emotion/react 에 포함되어있다.ThemeProvider 를 앱 최상단에 배치하여 스타일을 전역적으로 공유하는 것이다.import { ThemeProvider } from '@emotion/react'
const theme = {
colors: {
primary: 'hotpink'
}
}
render(
<ThemeProvider theme={theme}>
<div css={theme => ({ color: theme.colors.primary })}>some other text</div>
</ThemeProvider>
)
css 프로퍼티에 함수를 넣어서 theme 에 접근할 수 있고, useTheme 라는 커스텀 훅도 제공한다.import * as PropTypes from 'prop-types'
import * as React from 'react'
import { withTheme } from '@emotion/react'
class TellMeTheColor extends React.Component {
render() {
return <div>The color is {this.props.theme.color}.</div>
}
}
TellMeTheColor.propTypes = {
theme: PropTypes.shape({
color: PropTypes.string
})
}
const TellMeTheColorWithTheme = withTheme(TellMeTheColor)
withTheme 를 이용해서 테마를 주입해줄 수 있다.css 프로퍼티로 받은 스타일 값을 이리저리 잘 조물조물해서 label 이라고 부르는 고유한 class name을 만들어 className 에 붙여주는 것이다.