Open1

Emotionメモ

defuntydefunty

メモ

参考:
https://changeset-release--emotion.netlify.app/docs/css-prop
https://changeset-release--emotion.netlify.app/docs/typescript

Style

Object Style

import { jsx } from '@emotion/core'
render(
  <div
    css={{
      backgroundColor: 'hotpink',
      '&:hover': {
        color: 'lightgreen'
      }
    }}
  >
    This has a hotpink background.
  </div>
)

String Styles

// this comment tells babel to convert jsx to calls to a function called jsx instead of React.createElement
/** @jsx jsx */
import { css, jsx } from '@emotion/core'
const color = 'darkgreen'
render(
  <div
    css={css`
      background-color: hotpink;
      &:hover {
        color: ${color};
      }
    `}
  >
    This has a hotpink background.
  </div>
)

Package

@emotion/core

@emotion/react の後釜?

import { css } from '@emotion/core'
const titleStyle = css({
  height: 200
})
const subtitleStyle = css`
  height: 60px;
`

@emotion/styled

import styled from '@emotion/styled'
const Link = styled('a')`
  color: red;
`
  • 本来のCSSの書き方と同じなのは良いが、補完が効かない(copilotがあればそこまで気にならないけど)
  • styledタグがcomponentと同格に見えるのが苦手。
  • rootタグが外に出されるのも見づらい(どういうドキュメント構造なのか俯瞰しづらい)。
  • TSの恩恵受けられないので、本当の意味で「スタイルをコンポーネントで閉じる」という旨味しかない

@emotion/css

ClassName Propを使うパッケージ。
ドキュメントから消えているのでなくなった模様。

#SSR

Server side rendering works out of the box in Emotion 10 and above if you’re only using @emotion/react and @emotion/styled. This means you can call React’s renderToString or renderToNodeStream methods directly without any extra configuration.

古いバージョンのドキュメントに掲載。
https://emotion.sh/docs/ssr

現況は不明なので必要に応じて調査する。