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
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.
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 <rte-code>styled<rte-code> 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’s recommended to enable sourcemaps during development. The sourcemaps are inlined into the CSS files, so don’t enable them in production.
New <rte-code>styled<rte-code> 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.
Now, we have added a new <rte-code>styled<rte-code> tag à la styled-components. It makes it super easy to create a React component with some styles:
In addition to that, the <rte-code>styled<rte-code> 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:
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:
The <rte-code>px<rte-code> unit needs to be part of the CSS variable or should be applied with <rte-code>calc<rte-code>, e.g. — <rte-code>calc(var(--box-height) * 1px)<rte-code>. 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 <rte-code>styled-components<rte-code>. For example, the following code won’t be valid:
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 <rte-code>src<rte-code> to <rte-code>dist/styles<rte-code>, you can run the following command:
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
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: <rte-code>linaria.config.js<rte-code>
- 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 <rte-code>css<rte-code> and <rte-code>styled<rte-code> from Linaria before processing the file.
- Support using <rte-code>styled<rte-code> helper with Preact (with webpack alias).
- Support webpack aliases when evaluating modules.
- Fix issues with resolving <rte-code>url(..)<rte-code> expressions when using <rte-code>file-loader<rte-code> and <rte-code>url-loader<rte-code>.
- Fix cases where output CSS had missing class names.
- Fix issues with path on windows.
- Fail for invalid interpolations such <rte-code>null<rte-code>, <rte-code>NaN<rte-code> etc. at build time to avoid mistakes.
- Many more.