1 | # @emotion/babel-preset-css-prop
|
2 |
|
3 | > A Babel preset to automatically enable Emotion's css prop when using the classic JSX runtime. If you want to use [the new JSX runtimes](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html) please do not use this preset but rather just include our [`@emotion/babel-plugin`](/packages/babel-plugin) directly and follow instructions for configuring the new JSX runtimes [here](/docs/css-prop.mdx#babel-preset).
|
4 |
|
5 | - [@emotion/babel-preset-css-prop](#emotionbabel-preset-css-prop)
|
6 | - [Install](#install)
|
7 | - [Usage](#usage)
|
8 | - [Via CLI](#via-cli)
|
9 | - [Via Node API](#via-node-api)
|
10 | - [Features](#features)
|
11 | - [Example](#example)
|
12 | - [Options](#options)
|
13 | - [Examples](#examples)
|
14 |
|
15 | ## Install
|
16 |
|
17 | ```bash
|
18 | yarn add @emotion/babel-preset-css-prop
|
19 | ```
|
20 |
|
21 | ## Usage
|
22 |
|
23 | > Note:
|
24 | >
|
25 | > This plugin is not compatible with `@babel/plugin-transform-react-inline-elements`. If you use both then your `css` prop styles won't be applied correctly.
|
26 |
|
27 | **.babelrc**
|
28 |
|
29 | ```json
|
30 | {
|
31 | "presets": ["@emotion/babel-preset-css-prop"]
|
32 | }
|
33 | ```
|
34 |
|
35 | `@emotion/babel-preset-css-prop` includes the emotion plugin. The `@emotion/babel-plugin` entry should be removed from your config and any options moved to the preset. If you use `@babel/preset-react` or `@babel/preset-typescript` ensure that `@emotion/babel-preset-css-prop` is inserted after them in your babel config.
|
36 |
|
37 | ```diff
|
38 | {
|
39 | + "presets": [
|
40 | + [
|
41 | + "@emotion/babel-preset-css-prop",
|
42 | + {
|
43 | + "autoLabel": "dev-only",
|
44 | + "labelFormat": "[local]"
|
45 | + }
|
46 | + ]
|
47 | + ],
|
48 | - "plugins": [
|
49 | - [
|
50 | - "@emotion",
|
51 | - {
|
52 | - "autoLabel": "dev-only",
|
53 | - "labelFormat": "[local]"
|
54 | - }
|
55 | - ]
|
56 | - ]
|
57 | }
|
58 | ```
|
59 |
|
60 | See [the options documentation](#options) for more information.
|
61 |
|
62 | ### Via CLI
|
63 |
|
64 | ```bash
|
65 | babel --presets @emotion/babel-preset-css-prop script.js
|
66 | ```
|
67 |
|
68 | ### Via Node API
|
69 |
|
70 | ```javascript
|
71 | require('@babel/core').transform(code, {
|
72 | presets: ['@emotion/babel-preset-css-prop']
|
73 | })
|
74 | ```
|
75 |
|
76 | ## Features
|
77 |
|
78 | This preset enables the `css` prop for an entire project via a single entry to the babel configuration. After adding the preset, compiled JSX code will use Emotion's `jsx` function instead of `React.createElement`.
|
79 |
|
80 | | | Input | Output |
|
81 | | ------ | -------------------------- | --------------------------------------------------- |
|
82 | | Before | `<img src="avatar.png" />` | `React.createElement('img', { src: 'avatar.png' })` |
|
83 | | After | `<img src="avatar.png" />` | `jsx('img', { src: 'avatar.png' })` |
|
84 |
|
85 | `import { jsx } from '@emotion/react'` is automatically added to the top of files where required.
|
86 |
|
87 | ## Example
|
88 |
|
89 | **In**
|
90 |
|
91 | ```javascript
|
92 | const Link = props => (
|
93 | <a
|
94 | css={{
|
95 | color: 'hotpink',
|
96 | '&:hover': {
|
97 | color: 'darkorchid'
|
98 | }
|
99 | }}
|
100 | {...props}
|
101 | />
|
102 | )
|
103 | ```
|
104 |
|
105 | **Out**
|
106 |
|
107 | ```javascript
|
108 | import { jsx as ___EmotionJSX } from '@emotion/react'
|
109 |
|
110 | function _extends() {
|
111 | /* babel Object.assign polyfill */
|
112 | }
|
113 |
|
114 | var _ref =
|
115 | process.env.NODE_ENV === 'production'
|
116 | ? {
|
117 | name: '1fpk7dx-Link',
|
118 | styles: 'color:hotpink;&:hover{color:darkorchid;}label:Link;'
|
119 | }
|
120 | : {
|
121 | name: '1fpk7dx-Link',
|
122 | styles: 'color:hotpink;&:hover{color:darkorchid;}label:Link;',
|
123 | map: '/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImF1dG9tYXRpYy1pbXBvcnQuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRUkiLCJmaWxlIjoiYXV0b21hdGljLWltcG9ydC5qcyIsInNvdXJjZXNDb250ZW50IjpbImNvbnN0IExpbmsgPSBwcm9wcyA9PiAoXG4gIDxhXG4gICAgY3NzPXt7XG4gICAgICBjb2xvcjogJ2hvdHBpbmsnLFxuICAgICAgJyY6aG92ZXInOiB7XG4gICAgICAgIGNvbG9yOiAnZGFya29yY2hpZCdcbiAgICAgIH1cbiAgICB9fVxuICAgIHsuLi5wcm9wc31cbiAgLz5cbilcbiJdfQ== */'
|
124 | }
|
125 |
|
126 | const Link = props =>
|
127 | ___EmotionJSX(
|
128 | 'a',
|
129 | _extends(
|
130 | {
|
131 | css: _ref
|
132 | },
|
133 | props
|
134 | )
|
135 | )
|
136 | ```
|
137 |
|
138 | _In addition to the custom JSX factory, this example includes `@emotion/babel-plugin` transforms that are enabled by default._
|
139 |
|
140 | ## Options
|
141 |
|
142 | Options for both `@emotion/babel-plugin` and `@babel/plugin-transform-react-jsx` are supported and will be forwarded to their respective plugin.
|
143 |
|
144 | > Refer to the plugin's documentation for full option documentation.
|
145 | >
|
146 | > - [`@emotion/babel-plugin`](https://emotion.sh/docs/babel)
|
147 | >
|
148 | > - [`@babel/plugin-transform-react-jsx`](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx)
|
149 |
|
150 | ### Examples
|
151 |
|
152 | ```json
|
153 | {
|
154 | "presets": [
|
155 | [
|
156 | "@emotion/babel-preset-css-prop",
|
157 | {
|
158 | "autoLabel": "dev-only",
|
159 | "labelFormat": "[local]",
|
160 | "useBuiltIns": false,
|
161 | "throwIfNamespace": true
|
162 | }
|
163 | ]
|
164 | ]
|
165 | }
|
166 | ```
|
167 |
|
168 | _Options set to default values for demonstration purposes._
|