What is @linaria/core?
@linaria/core is a zero-runtime CSS-in-JS library for React that allows you to write CSS styles with JavaScript and extract them to CSS files at build time. This approach ensures that your styles are not included in the JavaScript bundle, leading to better performance and smaller bundle sizes.
What are @linaria/core's main functionalities?
Styled Components
Allows you to create styled components using tagged template literals. The styles are written in a CSS-like syntax and are scoped to the component.
const Button = styled.button`\n background: palevioletred;\n border-radius: 3px;\n border: none;\n color: white;\n padding: 0.5em 1em;\n font-size: 1em;\n`;
CSS Variables
Supports the use of JavaScript variables within your CSS, enabling dynamic styling based on your application's state or theme.
const theme = {\n primaryColor: 'palevioletred',\n};\nconst Title = styled.h1`\n color: ${theme.primaryColor};\n`;
Keyframes
Allows you to define CSS animations using the keyframes helper, which can then be applied to styled components.
import { keyframes } from '@linaria/core';\nconst fadeIn = keyframes`\n from { opacity: 0; }\n to { opacity: 1; }\n`;
Global Styles
Enables you to define global styles that apply to the entire application, not just individual components.
import { css } from '@linaria/core';\ncss`\n :global() {\n body {\n margin: 0;\n padding: 0;\n font-family: sans-serif;\n }\n }\n`;
Other packages similar to @linaria/core
styled-components
styled-components is a popular CSS-in-JS library that allows you to write actual CSS to style your components. It provides a similar API to @linaria/core but includes runtime overhead, which can impact performance.
emotion
Emotion is a performant and flexible CSS-in-JS library. It offers both a styled-components-like API and a lower-level API for more control. Unlike @linaria/core, Emotion includes runtime styling, which can be less performant.
Zero-runtime CSS in JS library.
📖 Please refer to the GitHub for full documentation.
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/core @linaria/react @linaria/babel-preset
or
yarn add @linaria/core @linaria/react @linaria/babel-preset