Top Related Projects
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
👩🎤 CSS-in-JS library designed for high performance style composition
Documentation about css-modules
Zero-runtime Stylesheets-in-TypeScript
JSS is an authoring tool for CSS which uses JavaScript as a host language.
Quick Overview
Linaria is a zero-runtime CSS-in-JS library for React applications. It allows developers to write CSS directly in their JavaScript files, but extracts the styles at build time, resulting in separate CSS files without any runtime overhead.
Pros
- Zero runtime overhead, as styles are extracted at build time
- Type-safe CSS with full TypeScript support
- Supports dynamic styles through CSS variables
- Works with server-side rendering out of the box
Cons
- Requires a build step, which may increase complexity in some projects
- Limited support for dynamic styles compared to runtime CSS-in-JS solutions
- Learning curve for developers used to traditional CSS or other CSS-in-JS libraries
- May require additional configuration for certain build tools or frameworks
Code Examples
- Basic styled component:
import { styled } from '@linaria/react';
const Button = styled.button`
background-color: #3498db;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
`;
- Using dynamic styles with CSS variables:
import { styled } from '@linaria/react';
const Box = styled.div`
width: var(--box-width);
height: var(--box-height);
background-color: var(--box-color);
`;
function MyComponent() {
return (
<Box
style={{
'--box-width': '100px',
'--box-height': '100px',
'--box-color': 'red',
}}
/>
);
}
- Using the css tag for global styles:
import { css } from '@linaria/core';
const globalStyles = css`
:global() {
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
}
`;
Getting Started
- Install Linaria and its React bindings:
npm install @linaria/core @linaria/react
- Configure your build tool (e.g., webpack) with Linaria. Here's a basic webpack configuration:
module.exports = {
module: {
rules: [
{
test: /\.js$/,
use: [
{ loader: 'babel-loader' },
{
loader: '@linaria/webpack-loader',
options: { sourceMap: process.env.NODE_ENV !== 'production' },
},
],
},
],
},
};
- Start using Linaria in your React components:
import React from 'react';
import { styled } from '@linaria/react';
const Title = styled.h1`
font-size: 24px;
color: #333;
`;
function App() {
return <Title>Hello, Linaria!</Title>;
}
export default App;
Competitor Comparisons
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
Pros of styled-components
- Dynamic styling with props and theming
- Automatic critical CSS extraction
- Seamless integration with React components
Cons of styled-components
- Runtime overhead due to CSS-in-JS approach
- Larger bundle size compared to static CSS solutions
- Potential for style duplication in server-side rendering
Code Comparison
styled-components:
const Button = styled.button`
background-color: ${props => props.primary ? 'blue' : 'white'};
color: ${props => props.primary ? 'white' : 'blue'};
padding: 10px 20px;
`;
Linaria:
import { css } from '@linaria/core';
const button = css`
background-color: ${props => props.primary ? 'blue' : 'white'};
color: ${props => props.primary ? 'white' : 'blue'};
padding: 10px 20px;
`;
Key Differences
- Linaria generates static CSS at build time, reducing runtime overhead
- styled-components offers more dynamic styling capabilities
- Linaria provides better performance and smaller bundle sizes
- styled-components has a larger ecosystem and community support
Both libraries offer CSS-in-JS solutions, but Linaria focuses on zero-runtime CSS generation, while styled-components provides more flexibility at the cost of runtime performance.
👩🎤 CSS-in-JS library designed for high performance style composition
Pros of Emotion
- Runtime styling allows for dynamic, prop-based styles
- Larger ecosystem with more integrations and plugins
- Better support for server-side rendering
Cons of Emotion
- Slightly larger bundle size due to runtime CSS generation
- Potential performance overhead in complex applications
- Less type-safe compared to Linaria's static extraction
Code Comparison
Emotion:
import { css } from '@emotion/react'
const style = css`
color: ${props => props.color};
`
Linaria:
import { styled } from '@linaria/react'
const StyledComponent = styled.div`
color: ${props => props.color};
`
Both Emotion and Linaria are popular CSS-in-JS solutions, but they take different approaches. Emotion focuses on runtime styling, offering more flexibility and a rich ecosystem. Linaria, on the other hand, emphasizes static extraction and zero-runtime CSS, resulting in smaller bundle sizes and potentially better performance.
Emotion shines in dynamic styling scenarios and complex applications with frequently changing styles. It also has better server-side rendering support and a wider range of integrations.
Linaria excels in projects prioritizing performance and bundle size. Its static extraction approach leads to smaller bundles and eliminates runtime overhead. It also offers better type safety due to its compile-time nature.
The code comparison shows that both libraries allow for dynamic, prop-based styling. However, Emotion's runtime approach may provide more flexibility in certain scenarios, while Linaria's static extraction ensures that styles are resolved at build time.
Documentation about css-modules
Pros of CSS Modules
- Widely adopted and well-established in the React ecosystem
- Works with existing CSS workflows and tooling
- Supports server-side rendering out of the box
Cons of CSS Modules
- Requires additional build configuration and tooling
- Limited runtime flexibility for dynamic styles
- Potential for larger bundle sizes due to generated class names
Code Comparison
CSS Modules:
/* styles.module.css */
.button {
background-color: blue;
color: white;
}
import styles from './styles.module.css';
const Button = () => <button className={styles.button}>Click me</button>;
Linaria:
import { css } from '@linaria/core';
const buttonStyle = css`
background-color: blue;
color: white;
`;
const Button = () => <button className={buttonStyle}>Click me</button>;
Key Differences
- CSS Modules uses separate CSS files, while Linaria allows for CSS-in-JS
- Linaria provides better TypeScript integration and type safety for styles
- CSS Modules relies on build-time processing, whereas Linaria supports both build-time and runtime styling
- Linaria offers more advanced features like theming and dynamic styles
- CSS Modules has a larger ecosystem and community support
Both solutions aim to solve CSS scoping issues in component-based architectures, but they take different approaches. The choice between them depends on project requirements, team preferences, and existing workflows.
Zero-runtime Stylesheets-in-TypeScript
Pros of vanilla-extract
- Type-safe CSS-in-TS with static extraction, offering better performance
- Zero-runtime styling solution, reducing bundle size
- Supports CSS variables and themes out of the box
Cons of vanilla-extract
- Requires a build step, which may increase complexity
- Less flexible for dynamic styles compared to runtime solutions
- Steeper learning curve for developers used to traditional CSS
Code Comparison
vanilla-extract:
import { style } from '@vanilla-extract/css';
export const button = style({
backgroundColor: 'blue',
color: 'white',
padding: '10px 20px'
});
Linaria:
import { css } from '@linaria/core';
export const button = css`
background-color: blue;
color: white;
padding: 10px 20px;
`;
Key Differences
- vanilla-extract uses object syntax for styles, while Linaria uses template literals
- vanilla-extract generates static CSS at build time, Linaria can work at runtime
- vanilla-extract offers stronger type safety and IDE support for styles
Both libraries aim to provide CSS-in-JS solutions with zero runtime overhead, but they differ in their approach to syntax and type safety. The choice between them often depends on project requirements and developer preferences.
JSS is an authoring tool for CSS which uses JavaScript as a host language.
Pros of JSS
- More mature and widely adopted, with a larger ecosystem
- Supports dynamic styling with JavaScript
- Offers better performance for large-scale applications
Cons of JSS
- Steeper learning curve due to its API complexity
- Requires additional setup and configuration
- Larger bundle size compared to Linaria
Code Comparison
JSS:
const styles = {
button: {
backgroundColor: 'blue',
color: 'white',
'&:hover': {
backgroundColor: 'darkblue',
},
},
};
Linaria:
import { css } from '@linaria/core';
const button = css`
background-color: blue;
color: white;
&:hover {
background-color: darkblue;
}
`;
Both JSS and Linaria are CSS-in-JS solutions, but they differ in their approach. JSS uses a JavaScript object syntax for defining styles, while Linaria uses tagged template literals that look more like traditional CSS. JSS offers more flexibility and dynamic styling capabilities, making it suitable for complex applications. Linaria, on the other hand, provides a simpler API and generates static CSS at build time, resulting in smaller bundle sizes and better performance for simpler use cases. The choice between the two depends on the specific needs of your project and your team's preferences.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Zero-runtime CSS in JS library.
Features
- Write CSS in JS, but with zero runtime, CSS is extracted to CSS files during build
- Familiar CSS syntax with Sass like nesting
- Use dynamic prop based styles with the React bindings, uses CSS variables behind the scenes
- Easily find where the style was defined with CSS sourcemaps
- Lint your CSS in JS with stylelint
- Use JavaScript for logic, no CSS preprocessor needed
- Optionally use any CSS preprocessor such as Sass or PostCSS
- Supports atomic styles with
@linaria/atomic
Learn how Airbnb improved both developer experience and web performance with Linaria
Installation
npm install @linaria/core @linaria/react @wyw-in-js/babel-preset
or
yarn add @linaria/core @linaria/react @wyw-in-js/babel-preset
Setup
Linaria is now built on top of wyw-in-js.dev. It supports various bundlers to extract the CSS at build time. To configure your bundler, check the following guides on the wyw-in-js.dev site:
See Configuration to customize how Linaria processes your files.
Syntax
Linaria can be used with any framework, with additional helpers for React. The basic syntax looks like this:
import { css } from '@linaria/core';
import { modularScale, hiDPI } from 'polished';
import fonts from './fonts';
// Write your styles in `css` tag
const header = css`
text-transform: uppercase;
font-family: ${fonts.heading};
font-size: ${modularScale(2)};
${hiDPI(1.5)} {
font-size: ${modularScale(2.5)};
}
`;
// Then use it as a class name
<h1 className={header}>Hello world</h1>;
You can use imported variables and functions for logic inside the CSS code. They will be evaluated at build time.
If you're using React, you can use the styled
helper, which makes it easy to write React components with dynamic styles with a styled-component like syntax:
import { styled } from '@linaria/react';
import { families, sizes } from './fonts';
// Write your styles in `styled` tag
const Title = styled.h1`
font-family: ${families.serif};
`;
const Container = styled.div`
font-size: ${sizes.medium}px;
color: ${props => props.color};
border: 1px solid red;
&:hover {
border-color: blue;
}
${Title} {
margin-bottom: 24px;
}
`;
// Then use the resulting component
<Container color="#333">
<Title>Hello world</Title>
</Container>;
Dynamic styles will be applied using CSS custom properties (aka CSS variables) and don't require any runtime.
See Basics for a detailed information about the syntax.
Demo
Documentation
- Basics
- API and usage
- Configuration
- Dynamic styles with
css
tag - Theming
- Critical CSS extraction
- Bundlers integration
- CLI
- Linting
- How it works
- Example
Contributing
We appreciate any support in library development!
Take a look on Contributing docs to check how you can run Linaria in development mode.
Trade-offs
-
No IE11 support when using dynamic styles in components with
styled
, since it uses CSS custom properties -
Dynamic styles are not supported with
css
tag. See Dynamic styles withcss
tag for alternative approaches. -
Modules used in the CSS rules cannot have side-effects. For example:
import { css } from '@linaria/core'; import colors from './colors'; const title = css` color: ${colors.text}; `;
Here, there should be no side-effects in the
colors.js
file, or any file it imports. We recommend to move helpers and shared configuration to files without any side-effects.
Interoperability with other CSS-in-JS libraries
Linaria can work together with other CSS-in-JS libraries out-of-the-box. However, if you want to use styled components from Linaria as selectors in styled-components
/emotion
, you need to use @linaria/interop
Editor Plugins
VSCode
- Syntax Highlighting - language-babel
- Autocompletion - vscode-styled-components
- Linting - stylelint
Atom
- Syntax Highlighting and Autocompletion - language-babel
Webstorm
- Syntax Highlighting & Autocompletion - webstorm-styled-components
Sublime Text
- Syntax Highlighting & Autocompletion - Naomi, JSCustom (refer to document on how to turn on Styled Component syntax)
- Linting - SublimeLinter-stylelint, LSP Stylelint
Recommended Libraries
- gatsby-plugin-linaria â Gatsby plugin that sets up Babel and webpack configuration for Linaria.
- polished.js - A lightweight toolset for writing styles in JavaScript.
- craco-linaria - A Craco plugin that allows you to use Linaria without ejecting from a CRA.
Inspiration
Acknowledgements
This project wouldn't have been possible without the following libraries or the people behind them.
Special thanks to @kentcdodds for his babel plugin and @threepointone for his suggestions and encouragement.
Made with â¤ï¸ at Callstack
Linaria is an open source project and will always remain free to use. If you think it's cool, please star it ð. Callstack is a group of React and React Native geeks, contact us at hello@callstack.com if you need any help with these or just want to say hi!
Like the project? âï¸ Join the team who does amazing stuff for clients and drives React Native Open Source! ð¥
Sponsors
Contributors
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Top Related Projects
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
👩🎤 CSS-in-JS library designed for high performance style composition
Documentation about css-modules
Zero-runtime Stylesheets-in-TypeScript
JSS is an authoring tool for CSS which uses JavaScript as a host language.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot