linaria README

The project's README from github.com/callstack/linaria. As one markdown file: README.md.

Linaria

Zero-runtime CSS in JS library.


[![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![Version][version-badge]][package] [![MIT License][license-badge]][license]

![All Contributors][all-contributors-badge] [![PRs Welcome][prs-welcome-badge]][prs-welcome] [![Chat][chat-badge]][chat] [![Code of Conduct][coc-badge]][coc] [![Greenkeeper][greenkeeper-badge]][greenkeeper] [![Sponsored by Callstack][callstack-badge]][callstack]

[![tweet][tweet-badge]][tweet]

Features

Why use Linaria

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.

Stability

Linaria relies on WyW (@wyw-in-js/*) to evaluate your modules at build time and extract CSS. If you hit issues like slow builds, invalidation storms, or unexpected code being executed during the build, it’s usually related to the WyW evaluation model and how your modules are structured.

Linaria 8 requires Node.js >=22.12.0 (aligned with the WyW 2 / Oxc dependency graph). WyW 2 defaults to eval.strategy: "hybrid", so statically provable values are resolved before falling back to the evaluator for dynamic values.

If your build depends on evaluator-only side effects or exact CSS rule order ties, review the Linaria 8 migration notes in docs/MIGRATION_GUIDE.md.

See https://wyw-in-js.dev/stability for practical guidance and common pitfalls.

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

Edit Linaria Demo

Documentation

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

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

Atom

Webstorm

Sublime Text

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 [email protected] 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

{callstack}

Servers.com

Contributors

Thanks goes to these wonderful people (emoji key):

Paweł Trysła
Paweł Trysła

💻 📖 🤔
Satyajit Sahoo
Satyajit Sahoo

💻 📖 🤔
Michał Pierzchała
Michał Pierzchała

💻 📖 🤔
Lucas
Lucas

📖
Alexey Pronevich
Alexey Pronevich

📖
Wojtek Szafraniec
Wojtek Szafraniec

💻
Anton Evzhakov
Anton Evzhakov

💻 🤔 📖
Tushar Sonawane
Tushar Sonawane

📖 💡
Ferran Negre
Ferran Negre

📖
Jakub Beneš
Jakub Beneš

💻 📖
Oscar Busk
Oscar Busk

🐛 💻
Dawid
Dawid

💻 📖
Kacper Wiszczuk
Kacper Wiszczuk

💻 📖
Denis Rul
Denis Rul

💻
Johan Holmerin
Johan Holmerin

💻 📖
Gilad Peleg
Gilad Peleg

📖
Giuseppe
Giuseppe

💻
Matija Marohnić
Matija Marohnić

💻 📖
Stefan Schult
Stefan Schult

💻
Ward Peeters
Ward Peeters

💻
radoslaw-medryk
radoslaw-medryk

💻
杨兴洲
杨兴洲

💻
Dawid Karabin
Dawid Karabin

📖
Chris Abrams
Chris Abrams

💻 📖 🤔
Jayphen
Jayphen

💻
c4605
c4605

💻
Toru Kobayashi
Toru Kobayashi

💻
Jakub Mazurek
Jakub Mazurek

linaria (callstack/linaria)

Searching Database…

12.4K ·GitHub
这是什么?Linaria 是一个零运行时的 CSS-in-JS 库:你可以用 JavaScript 或 TypeScript 编写样式,构建时它们会被提取成普通的 CSS 文件。
'零运行时'对我的应用来说到底是什么意思?不会有任何样式库被打包发到浏览器。打包工具的插件会在构建阶段把你的样式提取成普通的 CSS 文件,而不是像传统 CSS-in-JS 那样在运行时把样式注入到页面里。
我怎么在自己的项目里把它跑起来?安装这个包,并配置你的打包工具在构建时提取 CSS:Linaria 基于 wyw-in-js.dev 构建,提供了 webpack、esbuild、Rollup、Vite 和 Svelte 的配置指南。
有没有我需要了解的版本要求?有:Linaria 8 要求 Node.js 22.12.0 或更新版本,与其 WyW 2 / Oxc 依赖体系保持一致,另外项目里还需要使用一种受支持的打包工具。
在采用它之前,主要的取舍是什么?有三个坑:通过 styled 实现的动态样式不支持 IE11;css 标签完全不支持动态样式;CSS 规则中引用的模块必须没有副作用。
它只能配合 React 使用,还是任何框架都行?任何框架都可以:核心语法就是用 JS 写的普通 CSS,React 用户还可以额外使用 styled 辅助函数,基于 props 构建动态样式的组件。
这只是纯 Web 的东西,还是也能给原生移动应用做样式?它只面向 Web:产出的是浏览器使用的标准 CSS,通过 webpack、Vite、Rollup 这类 Web 打包工具提取。
浏览器里什么都不运行,那动态样式是怎么实现的?基于 props 的样式会在构建时编译成 CSS 自定义属性(CSS 变量):改变一个 prop 只是切换变量的值,因此完全不需要运行时样式引擎。
我能把 Linaria 和 styled-components 或 Emotion 混着用吗?可以,它开箱即用就能和其他 CSS-in-JS 库一起工作。如果想在 styled-components 或 Emotion 里把 Linaria 的 styled 组件用作选择器,需要添加 @linaria/interop 包。
我怎么查到某条生成的样式是从哪来的?Linaria 会生成 CSS sourcemap,所以你可以把提取出的 CSS 中的某条规则追溯到定义它的文件和组件,还可以用 stylelint 对 JS 里的样式做检查。
它支持原子化 CSS 吗?支持,@linaria/atomic 包增加了原子化样式的支持,适合用细粒度的工具类式规则来代替按组件划分的样式块。
Creator
callstack
Size
32.8 MB
Stars
12.4K
Age
9y
Commits · 90 Days