Announcing Linaria 1.0

Satyajit Sahoo
Callstack Engineers
5 min readFeb 8, 2019

The first major release of Linaria is here. It’s more robust, handles more edge cases and introduces a new API to make easier to use with React.

What is Linaria

Linaria is a zero-runtime CSS in JS library. What’s special about Linaria is that it extracts all of the styles you write in your JavaScript code to real CSS files, unlike other CSS in JS libraries which parse and apply the styles in the browser.

If you’re wondering how it is different from Emotion and Styled Components, you can read this short post.

For a list of features and benefits over other solutions, check our GitHub repo.

Highlights

Rewritten core to be more robust

We have changed the approach of how we evaluate and extract styles. This fixed some long-standing issues and made it easier for us to support features like sourcemaps, improved stylelint preprocessor, the new styled tag for components, etc.

Support for Babel 7

The Babel plugin is now written with the latest stable version of Babel, so you don’t have to stay on an older version of Babel anymore. It also brings TypeScript support!

We also support Babel’s caller option to make it easier to provide a different configuration for Linaria’s evaluation.

Support for CSS sourcemaps

It was always tricky to find the file where you wrote the styles. Now it’s much easier thanks to sourcemaps support. You can jump to the original JavaScript file from the line where the CSS was generated from the dev tools.

It’s recommended to enable sourcemaps during development. The sourcemaps are inlined into the CSS files, so don’t enable them in production.

New styled tag for React components

Since the first release of Linaria last year, we’ve been looking for new ways to improve the way we write our styles. One feedback we got was that people missed being able to dynamically apply styles based on a React component’s props. Many also preferred the abstraction styled-components provided as opposed to using class names.

We have now added a new styled tag à la styled-components. It makes it super easy to create a React component with some styles:

import { styled } from 'linaria/react';const Button = styled.button`
appearance: none;
background-color: ${colors.primary};
border: 2px solid ${colors.primary};
color: white;
&:hover {
background-color: transparent;
color: ${colors.primary};
}
`;

In addition to that, the styled tag also supports dynamic prop based styling leveraging CSS custom properties (aka CSS variables). You can write a function interpolation and we automatically convert it to a CSS variable:

import { styled } from 'linaria/react';const Box = styled.div`
height: ${props => props.size}px;
width: ${props => props.size}px;
background-color: ${props => props.color};
border: 2px solid white;
margin: 16px;
`;
<Box size={48} color='blue' />

The variables are scoped to the class name.

We optimize how many CSS variables we generate automatically, so you can write idiomatic code. For example, in the above code, even though there are 3 dynamic interpolations, we’ll only generate 2 CSS variables because 2 of the dynamic interpolations have the same expression.

We also transparently handle some edge cases like having interpolations followed by units. For example, the following code won’t work in vanilla CSS:

height: var(--box-height)px;

The px unit needs to be part of the CSS variable or should be applied with calc, e.g. — calc(var(--box-height) * 1px). But you don’t have to worry about doing that manually when writing interpolations. We detect interpolations followed by units and apply them correctly.

A limitation of using CSS variables for dynamic values is that they only work for property values, and you can’t place them arbitrarily like you can with styled-components. For example, the following code won’t be valid:

@media(min-width: ${props => props.minWidth}) {
font-size: 16px;
}

New CLI for libraries

The Babel plugin no longer writes the CSS files to the filesystem due to several issues with the approach, which means you can’t use Babel CLI to extract the CSS to files anymore like in the previous versions. So we added a new CLI to do just that.

For example, to extract CSS files from the JS files inside src to dist/styles, you can run the following command:

npx linaria --out-dir dist/styles src/**/*.js

There are also other features such as inserting require statements to built JS files for publishing libraries. Check the docs for the full list of features.

New Rollup plugin

Thanks to external contributor John Holmerin, we now have a plugin for Rollup.

Improved stylelint processor

The stylelint processor can now correctly report errors inside interpolations.

Due to our build time evaluation, it can provide much better error messages as if you’re linting plain CSS files.

Support for using preprocessors

By default, Linaria uses stylis to process the extracted CSS. You can now disable or override this behavior which lets you use any CSS preprocessor you like, such as Sass, Less or custom PostCSS syntaxes.

Many bugfixes and enhancements

We fixed several issues and added many nice improvements. Some of them are:

  • Support a config file to configure Linaria: linaria.config.js
  • Ship typescript declarations in the npm package.
  • Play nice when you’re using Linaria in the same codebase where you use other CSS in JS libraries with the same syntax. The Babel plugin will check if you’re importing css and styled from Linaria before processing the file.
  • Support using styled helper with Preact (with webpack alias).
  • Support webpack aliases when evaluating modules.
  • Fix issues with resolving url(..) expressions when using file-loader and url-loader.
  • Fix cases where output CSS had missing class names.
  • Fix issues with path on windows.
  • Fail for invalid interpolations such null, NaN etc. at build time to avoid mistakes.
  • And many more.

Check it out and give us a star. You can also join our discord to talk to us.

This article was originally published at callstack.com on February 8, 2019.

--

--

Satyajit Sahoo
Callstack Engineers

Front-end developer. React Native Core Contributor. Codes JavaScript at night. Crazy for Tacos. Comic book fanatic. DC fan. Introvert. Works at @Callstackio