Skip to content

Commit 3204104

Browse files
lukePeaveyoliviertassinari
authored andcommitted
[docs] Add Emotion to style interoperability guide (#12966)
* [docs] Add Emotion to style interoperability guide * Add note on controlling priority to React Emotion section * Remove extra spaces * let's merge
1 parent 72a7d2f commit 3204104

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

docs/src/pages/guides/interoperability/interoperability.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ We have provided examples for the following styling solutions:
99

1010
- [Raw CSS](#raw-css)
1111
- [Styled Components](#styled-components)
12+
- [Emotion](#emotion)
13+
- [React Emotion](#react-emotion)
1214
- [CSS Modules](#css-modules)
1315
- [Global CSS](#global-css)
1416
- [React JSS](#react-jss)
@@ -141,6 +143,8 @@ export default StyledComponentsButton;
141143

142144
[![Edit Button](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/xpp5oj9o0z)
143145

146+
### Deeper elements
147+
144148
In some cases, the approaches above will not work.
145149
For example, if you attempt to style a [Drawer](/demos/drawers) with variant `permanent`,
146150
you will likely need to affect the Drawer's underlying `paper` style.
@@ -184,6 +188,117 @@ export default StyledComponentsButton;
184188

185189
[![Edit Button](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/j4n13yl1r9)
186190

191+
192+
## Emotion
193+
194+
Emotion's **css()** method works seamlessly with Material-UI. The class names returned by **css()** can be directly passed to a component's `className` prop to override the root styles.
195+
196+
```jsx
197+
import React from 'react';
198+
import { css } from 'emotion';
199+
import Button from '@material-ui/core/Button';
200+
201+
const buttonStyles = css`
202+
background: linear-gradient(45deg, #fe6b8b 30%, #ff8e53 90%);
203+
border-radius: 3;
204+
border: 0;
205+
color: white;
206+
height: 48px;
207+
padding: 0 30px;
208+
box-shadow: 0 3px 5px 2px rgba(255, 105, 135, 0.3);
209+
`;
210+
211+
// We just assign them the Button's className attribute
212+
function EmotionButton() {
213+
return (
214+
<div>
215+
<Button>Material-UI</Button>
216+
<Button className={buttonStyles}>Emotion</Button>
217+
</div>
218+
);
219+
}
220+
221+
export default EmotionButton
222+
```
223+
224+
[![Edit Button](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/yw93kl7y0j)
225+
226+
### Deeper elements
227+
228+
The styles created with **css()** can also be mapped to class names using the `classes` prop. This is useful when you want to customize the styles of deeper elements within a component.
229+
230+
```jsx
231+
import React from 'react';
232+
import { css } from 'emotion';
233+
import Button from '@material-ui/core/Button';
234+
235+
const styles = {
236+
button: css({
237+
background: 'linear-gradient(45deg, #fe6b8b 30%, #ff8e53 90%)',
238+
borderRadius: 3,
239+
border: 0,
240+
height: 48,
241+
padding: '0 30px',
242+
boxShadow: '0 3px 5px 2px rgba(255, 105, 135, 0.3)',
243+
}),
244+
label: css({
245+
color: 'papayawhip',
246+
}),
247+
};
248+
249+
function EmotionButton() {
250+
return (
251+
<div>
252+
<Button>Material-UI</Button>
253+
<Button className={styles.button} classes={{ label: styles.label }}>
254+
Emotion
255+
</Button>
256+
</div>
257+
);
258+
}
259+
260+
export default EmotionButton
261+
```
262+
263+
[![Edit Button](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/4q8o1y975w)
264+
265+
**Note:** By default Emotion and JSS both inject their styles at the bottom of the `<head>`. If you don't want to mark style attributes with **!important**, you need to change [the CSS injection order](/customization/css-in-js#css-injection-order), as in the examples.
266+
267+
## React Emotion
268+
269+
The **styled()** function can be used to customize the root styles of any Material-UI component.
270+
271+
```jsx
272+
import React from 'react';
273+
import styled from 'react-emotion';
274+
import Button from '@material-ui/core/Button';
275+
276+
const StyledButton = styled(Button)`
277+
background: linear-gradient(45deg, #fe6b8b 30%, #ff8e53 90%);
278+
border-radius: 3;
279+
border: 0;
280+
color: white;
281+
height: 48px;
282+
padding: 0 30px;
283+
box-shadow: 0 3px 5px 2px rgba(255, 105, 135, 0.3);
284+
`;
285+
286+
function ReactEmotionButton() {
287+
return (
288+
<div>
289+
<Button>Material-UI</Button>
290+
<StyledButton>React Emotion</StyledButton>
291+
</div>
292+
);
293+
}
294+
295+
export default ReactEmotionButton;
296+
```
297+
298+
[![Edit Button](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/xj81yqx504)
299+
300+
**Note:** By default Emotion and JSS both inject their styles at the bottom of the `<head>`. If you don't want to mark style attributes with **!important**, you need to change [the CSS injection order](/customization/css-in-js#css-injection-order), as in the examples.
301+
187302
## CSS Modules
188303

189304
![stars](https://img.shields.io/github/stars/css-modules/css-modules.svg?style=social&label=Star)

0 commit comments

Comments
 (0)