Emotion is a library that enables you to write CSS in JavaScript. It offers different ways to style components, such as css
prop, styled
function, and Global
component. Emotion also supports theming, which allows you to define a set of design tokens (like colors, fonts, etc.) and apply them across your application.
TypeScript is a superset of JavaScript that adds static typing. When used with Emotion, TypeScript helps catch errors early in the development process by providing type checking for your CSS styles and theme objects.
Theming in Emotion involves creating a theme object that contains all the design tokens. You can then use this theme object to style your components. Emotion provides a ThemeProvider
component that allows you to pass the theme object down the component tree.
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 defining a theme object in TypeScript:
// theme.ts
import { Theme } from '@emotion/react';
const theme: Theme = {
colors: {
primary: '#007bff',
secondary: '#6c757d'
},
fonts: {
body: 'Arial, sans-serif'
}
};
export default theme;
You can use the ThemeProvider
component to make the theme available to all components in the application:
// App.tsx
import React from 'react';
import { ThemeProvider } from '@emotion/react';
import theme from './theme';
import MyComponent from './MyComponent';
const App: React.FC = () => {
return (
<ThemeProvider theme={theme}>
<MyComponent />
</ThemeProvider>
);
};
export default App;
// MyComponent.tsx
import React from 'react';
import styled from '@emotion/styled';
import { useTheme } from '@emotion/react';
const StyledButton = styled.button`
background-color: ${(props) => props.theme.colors.primary};
color: white;
font-family: ${(props) => props.theme.fonts.body};
border: none;
padding: 10px 20px;
border-radius: 5px;
`;
const MyComponent: React.FC = () => {
const theme = useTheme();
return (
<div>
<StyledButton>Click me</StyledButton>
</div>
);
};
export default MyComponent;
Keep all your theme-related code in a single file or a dedicated directory. This makes it easier to manage and update the theme as your application grows.
Define an interface for your theme object to ensure type safety. For example:
// theme.ts
import { Theme } from '@emotion/react';
interface AppTheme extends Theme {
colors: {
primary: string;
secondary: string;
};
fonts: {
body: string;
};
}
const theme: AppTheme = {
colors: {
primary: '#007bff',
secondary: '#6c757d'
},
fonts: {
body: 'Arial, sans-serif'
}
};
export default theme;
You can use Emotion’s Global
component to apply global styles based on the theme:
// GlobalStyles.tsx
import React from 'react';
import { Global, css } from '@emotion/react';
import { useTheme } from '@emotion/react';
const GlobalStyles: React.FC = () => {
const theme = useTheme();
return (
<Global
styles={css`
body {
font-family: ${theme.fonts.body};
color: ${theme.colors.secondary};
}
`}
/>
);
};
export default GlobalStyles;
Break your theme into smaller modules if it becomes too large. For example, you can have separate files for colors, fonts, and spacing.
Implement a theme switching mechanism if your application supports multiple themes. You can use state management libraries like React Context or Redux to manage the current theme.
Write unit tests for your theme-related code. You can use testing libraries like Jest and React Testing Library to test the styling of your components with different themes.
Using Emotion theme with TypeScript provides a powerful and type - safe way to style your React applications. By understanding the fundamental concepts, following the usage methods, common practices, and best practices, you can create maintainable and scalable applications with consistent styling.