<script type="module">
import brandonkalLinaria from 'https://cdn.skypack.dev/@brandonkal/linaria';
</script>
README
Zero-runtime CSS in JS library.
Fork
This is a fork of Linaria. Reasons you will enjoy using the @brandonkal fork:
Much smaller styled runtime! No dependency on @emotion/is-prop-valid.
Simple and predictable prop filtering. Props that end in "
quot; are not forwarded to the DOM.
Modifiers! All style logic can be contained inside your CSS.
Improved babel support for arrow functions. This means shorter interpolations inside our styles.
Generated BEM-like modifiers. No need to name all those modifiers! Javascript logic is transformed into a CSS modifier name, all of this is stripped away after development:
Tiny class names in production with optional prefix and suffix support
Uses postCSS rather than stylis. This allows for a much deeper integration with your existing CSS pipeline.
High resolution source maps. Even nested selectors and modifiers point to the proper line rather than the start of the template literal.
Vastly improved Typescript types and support.
Faster builds with an improved module invalidation system plus webpack caching support.
Modifiers look like this (with optional TS typing and shorthand prop syntax):
This has been published as a package due to a lack of response to an RFC and an open Pull Request. This package will be maintained and is used in production.
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:
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 '@brandonkal/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 class={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 '@brandonkal/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.
Modules used in the CSS rules cannot have side-effects.
For example:
import { css } from '@brandonkal/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.
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):