Skip to content

chrisabrams/linaria

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

509 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Linaria

Zero-runtime CSS in JS library.


Build Status Code Coverage Version MIT License

All Contributors PRs Welcome Chat Code of Conduct Greenkeeper Sponsored by Callstack

tweet

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

Why use Linaria

Installation

npm install linaria

or

yarn add linaria

Setup

Linaria currently supports webpack and Rollup to extract the CSS at build time. To configure your bundler, check the following guides:

Optionally, add the linaria/babel preset to your Babel configuration at the end of the presets list to avoid errors when importing the components in your server code or tests:

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react",
    "linaria/babel"
  ]
}

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';
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

  • 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 with css tag for alternative approaches.

  • Modules used in the CSS rules cannot have side-effects. For example:

    import { css } from 'linaria';
    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.

Editor Plugins

VSCode

Atom

Webstorm

Recommended Libraries

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!

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

๐Ÿ’ป
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

๐Ÿ“–
Anton Evzhakov
Anton Evzhakov

๐Ÿ’ป
Chris Abrams
Chris Abrams

๐Ÿ’ป ๐Ÿ“– ๐Ÿค”
Jayphen
Jayphen

๐Ÿ’ป
c4605
c4605

๐Ÿ’ป
Toru Kobayashi
Toru Kobayashi

๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!

About

Zero-runtime CSS in JS library

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • TypeScript 90.7%
  • JavaScript 8.8%
  • Other 0.5%