Emotion is a library that allows you to write CSS styles in JavaScript. It provides different ways to style components, such as inline styles, tagged template literals, and object styles. Emotion also supports features like theming, keyframes, and global styles.
TypeScript is a superset of JavaScript that adds static typing. When used with Emotion, it helps catch errors early in the development process, provides autocompletion in IDEs, and makes the codebase more maintainable.
First, you need to install Emotion and its TypeScript types. You can use npm or yarn:
npm install @emotion/react @emotion/styled
npm install --save-dev @types/emotion__react @types/emotion__styled
Here is an example of creating a simple styled component using Emotion and TypeScript:
import React from 'react';
import styled from '@emotion/styled';
// Define a styled component
const Button = styled.button`
background-color: blue;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
&:hover {
background-color: darkblue;
}
`;
const App: React.FC = () => {
return (
<div>
<Button>Click me</Button>
</div>
);
};
export default App;
You can define a theme and use it in your components. Here is an example:
import React from 'react';
import { ThemeProvider, css } from '@emotion/react';
// Define the theme
const theme = {
colors: {
primary: 'blue',
secondary: 'green'
}
};
// Create a styled component that uses the theme
const StyledDiv = styled.div`
background-color: ${({ theme }) => theme.colors.primary};
color: white;
padding: 20px;
`;
const App: React.FC = () => {
return (
<ThemeProvider theme={theme}>
<StyledDiv>
This div uses the theme color.
</StyledDiv>
</ThemeProvider>
);
};
export default App;
You can define global styles using Emotion and TypeScript:
import React from 'react';
import { Global, css } from '@emotion/react';
const globalStyles = css`
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
`;
const App: React.FC = () => {
return (
<div>
<Global styles={globalStyles} />
<h1>Hello, World!</h1>
</div>
);
};
export default App;
You can create reusable style objects and use them in multiple components:
import React from 'react';
import styled from '@emotion/styled';
const baseButtonStyles = {
padding: '10px 20px',
border: 'none',
borderRadius: '5px',
cursor: 'pointer'
};
const PrimaryButton = styled.button`
${baseButtonStyles}
background-color: blue;
color: white;
&:hover {
background-color: darkblue;
}
`;
const SecondaryButton = styled.button`
${baseButtonStyles}
background-color: gray;
color: white;
&:hover {
background-color: darkgray;
}
`;
const App: React.FC = () => {
return (
<div>
<PrimaryButton>Primary</PrimaryButton>
<SecondaryButton>Secondary</SecondaryButton>
</div>
);
};
export default App;
You can apply conditional styles based on component props:
import React from 'react';
import styled from '@emotion/styled';
interface ButtonProps {
isPrimary: boolean;
}
const Button = styled.button<ButtonProps>`
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
background-color: ${({ isPrimary }) => (isPrimary ? 'blue' : 'gray')};
color: white;
&:hover {
background-color: ${({ isPrimary }) => (isPrimary ? 'darkblue' : 'darkgray')};
}
`;
const App: React.FC = () => {
return (
<div>
<Button isPrimary={true}>Primary</Button>
<Button isPrimary={false}>Secondary</Button>
</div>
);
};
export default App;
Break down your styles into smaller, reusable modules. This makes the code easier to maintain and test.
When creating styled components that accept props, define the prop types clearly. This helps catch errors and provides better documentation.
Even though you are writing styles in JavaScript, follow CSS best practices such as using meaningful class names, avoiding inline styles for complex styles, and using relative units.
Emotion with TypeScript is a powerful combination for styling React applications. It provides type safety, flexibility, and a better developer experience. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can efficiently use Emotion with TypeScript in your projects. Whether you are building small or large-scale applications, Emotion and TypeScript can help you create beautiful and maintainable styles.