explaingit

callstack/linaria

12,301TypeScriptAudience · developerComplexity · 3/5Setup · moderate

TLDR

A library that lets you write CSS inside JavaScript files using styled-components-like syntax, but extracts all styles to plain CSS files at build time so users' browsers load zero JavaScript for styling.

Mindmap

mindmap
  root((Linaria))
    What it does
      Zero-runtime CSS-in-JS
      Build-time extraction
      Plain CSS output
    Syntax
      Tagged template literals
      styled helper for React
      Dynamic via CSS variables
    Build tool support
      webpack
      Vite
      esbuild and Rollup
    Limitations
      No IE11 dynamic styles
      Build config required
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Style React components with CSS-in-JS syntax while shipping zero runtime styling overhead to users.

USE CASE 2

Replace styled-components in a performance-critical app to eliminate JavaScript-driven style injection.

USE CASE 3

Change component styles dynamically based on props without any JavaScript running in the browser at runtime.

Tech stack

TypeScriptJavaScriptwebpackViteesbuildRollup

Getting it running

Difficulty · moderate Time to first run · 30min

Requires configuring your build tool (webpack, Vite, esbuild) with the Linaria plugin, unusual module structures can cause build-time issues.

In plain English

Linaria is a library that lets web developers write CSS styles directly inside JavaScript files, but without any performance penalty at runtime. The key idea is that Linaria processes your code during the build step and extracts all the styles into plain CSS files. By the time a user visits your site, the styles are already in a regular CSS file, so the browser loads them the normal way. There is no JavaScript running in the user's browser just to apply styles, which is the main drawback of many other "CSS-in-JS" approaches. The syntax feels similar to styled-components, a popular library in the React world. For basic use, you write CSS inside a tagged template literal (a JavaScript string prefixed with css) and then use the result as a class name on an HTML element. For React projects there is a styled helper that lets you create React components with styles baked in, and those components can accept props to change certain style values dynamically. Dynamic values use CSS custom properties under the hood, so they still work without any JavaScript overhead at runtime. Linaria works with the common JavaScript build tools: webpack, Vite, esbuild, Rollup, and Svelte. It is built on top of an underlying engine called wyw-in-js that handles the build-time code evaluation. The README notes that unusual module structures or side-effect-heavy imports can cause problems during the build, and points to stability guidance on the wyw-in-js documentation site. Node.js version 20 or higher is required. The library also supports linting CSS written inside JavaScript using stylelint, and editor plugins are available for VS Code, Atom, WebStorm, and Sublime Text to get syntax highlighting and autocompletion for the template literal styles. One known limitation is that dynamic styles in the styled helper do not work in Internet Explorer 11 because they depend on CSS custom properties, which that browser does not support. The full README is longer than what was shown.

Copy-paste prompts

Prompt 1
Using Linaria's styled helper, write a React Button component that accepts a `variant` prop and switches background color between primary blue and secondary grey.
Prompt 2
I'm migrating from styled-components to Linaria. Show me how to convert an existing styled div with dynamic props to Linaria's zero-runtime equivalent.
Prompt 3
Set up Linaria with Vite for a React project: show me the vite.config.js changes and an example component that uses the css tag to apply scoped styles.
Prompt 4
Explain how Linaria uses CSS custom properties to handle dynamic values and show an example where a component's color changes based on a JavaScript variable.
Open on GitHub → Explain another repo

← callstack on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.