What is emotion?
Emotion is a performant and flexible CSS-in-JS library that allows you to style applications quickly and efficiently with JavaScript. It provides powerful and flexible tools for writing CSS styles with JavaScript, including support for styled components, CSS prop, and keyframes for animations.
What are emotion's main functionalities?
Styled Components
Styled components allow you to create React components with styles attached to them. This makes it easy to manage styles in a modular and reusable way.
const Button = styled.button`
color: hotpink;
`;
<Button>Click me</Button>
CSS Prop
The CSS prop allows you to style elements using a prop directly in your JSX. This is useful for applying styles conditionally or dynamically.
<div css={{ color: 'hotpink' }}>Hello World</div>
Keyframes for Animations
Emotion provides support for keyframes, allowing you to define animations in your JavaScript code. This makes it easy to create complex animations and apply them to your components.
import { keyframes } from '@emotion/react';
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);
}
`;
const BouncingDiv = styled.div`
animation: ${bounce} 1s ease infinite;
`;
<BouncingDiv>Bounce!</BouncingDiv>
Other packages similar to emotion
styled-components
Styled-components is another popular CSS-in-JS library that allows you to write plain CSS in your JavaScript. It offers a similar API to Emotion's styled components and is widely used in the React community.
aphrodite
Aphrodite is a CSS-in-JS library developed by Khan Academy. It focuses on performance and provides a straightforward API for defining styles in JavaScript. It is less feature-rich compared to Emotion but is known for its simplicity and performance.
jss
JSS is a library for generating CSS from JavaScript. It provides a powerful and flexible API for defining styles and supports various plugins for additional functionality. JSS is highly customizable and can be used with various frameworks, not just React.
emotion
👩🎤 Glam + React
Install
npm install -S emotion glam
.babelrc
{
"plugins": [
"emotion/glam",
"glam/babel"
]
}
API
emotion
import { emotion } from 'emotion'
const H1 = emotion('h1')`
color: 'blue';
font-size: 48px;
transform: scale(${props => props.scale});
`
function Greeting ({ name }) {
return <H1 scale={2}>Hello {name}</H1>
}
const H2 = emotion(H1)`
font-size: ${fontSize * 2/3}px;
color: 'red';
`
function Greeting ({ name }) {
return <H2>Hello {name}</H2>
}
const H3 = emotion.h3`
font-size: ${fontSize * 1/3}px;
color: 'red';
`
function Greeting ({ name }) {
return <H3>Hello {name}</H3>
}
css prop
When using the emotion babel plugin, any css
prop is converted to a class name via glam and appended to any existing class names.
Similar to importing React when using jsx, import css from 'glam'
must be at the top of your source files.
const Name = ({ color, name }) => <h1 css={`color: ${color};`}>{name}</h1>
const Name = ({ color, name }) => <h1 className={css`color: ${color};`}>{name}</h1>