Author styles collocated in JS, pull them out into static CSS on build. No runtime dependency.

Related tags

CSS css-out-js
Overview



Author styles collocated in JS, pull them out into static CSS on build. No runtime dependency.

 

the big idea:

I like to author styles collocated with component code, that's how I find them maintainable.

But before shipping them to users, I'd like to pull styles out of components and ship them in static CSS files. No runtime dependency.

animation showing code being pulled into dist.css

prior art: This idea is not entirely new, there have been other similar interpretations of this idea before like callstack/linaria and atlassian-labs/compiled

 

input

App.js

import { css } from 'css-out-js';

const Title = (props) => {
  return <h1 className={css({ fontSize: '2em' })}>Hello!</h1>;
};

output

The plugin extracts the styles into a css file
and replaces the function call with generated className string.
If there are any dynamic styles, it creates a css variable for them.
Finally, it removes the import for css-out-js, no runtime.

dist.css

+.Title-1bdbzov {
+  font-size: 2em;
+}

App.js

- import { css } from 'css-out-js';

const Title = (props) => {
- return <h1 className={css({ fontSize: '2em' })}>Hello!</h1>;
+ return <h1 className="Title-1bdbzov">Hello!</h1>;
};

 

works with dynamic styles as well:

input

App.js

import { css } from 'css-out-js';

const Home = (props) => {
  const className = css({ color: props.color });

  return (
    <section className={className}>
      <p>Home sweet home</p>
    </section>
  );
};

output

Dynamic styles based on props are made possible by setting a css variable.

dist.css

+.Home-2b9glr {
+  color: var(--props-color-1trdzir);
+}

App.js

- import { css } from 'css-out-js';

const Home = (props) => {
- const className = css({ color: props.color });
+ const className = "Home-2b9glr"
+ document.documentElement.style.setProperty('--props-color-1trdzir', props.color),

  return (
    <section className={className}>
      <p>Home sweet home</p>
    </section>
  );
}

setup

  1. install

    npm install css-out-js --save-dev
    
    # or
    
    yarn add css-out-js --dev
    
  2. Add plugin to babelrc, you can specify the location of your generated css file (hint: see examples directory)

    // .babelrc
    module.exports = {
      plugins: [['css-out-js/babel', { path: 'public/dist.css' }]]
    };
  3. add css import

    this may differ based on the framework of your choice. (hint: see examples directory)

    // App.js
    import './dist.css';
You might also like...

Framework-agnostic CSS-in-JS with support for server-side rendering, browser prefixing, and minimum CSS generation

Aphrodite Framework-agnostic CSS-in-JS with support for server-side rendering, browser prefixing, and minimum CSS generation. Support for colocating y

Jan 1, 2023

CSS Boilerplate / Starter Kit: Collection of best-practice CSS selectors

Natural Selection Natural Selection is a CSS framework without any styling at all. It is just a collection of selectors that can be used to define glo

Dec 8, 2022

Source code for Chrome/Edge/Firefox/Opera extension Magic CSS (Live editor for CSS, Less & Sass)

Live editor for CSS, Less & Sass (Magic CSS) Extension Live editor for CSS, Less & Sass (Magic CSS) for Google Chrome, Microsoft Edge, Mozilla Firefox

Dec 13, 2022

Easily create css variables without the need for a css file!

Tailwind CSS Variables This plugin allows you to configure CSS variables in the tailwind.config.js Similar to the tailwindcss configurations you are u

Dec 22, 2022

Cooltipz.css - A highly customisable, minimal, pure CSS tooltip library

Cooltipz.css - A highly customisable, minimal, pure CSS tooltip library

Cooltipz.css - Cool tooltips Cool customisable tooltips made from pure CSS Lightweight • Accessible • Customisable • Simple Cooltipz.css is a pure CSS

Dec 24, 2022

micro-library for CSS Flexbox and CSS Grid

SpeedGrid micro-library for CSS Flexbox and CSS Grid Overview SpeedGrid dynamically generates inline CSS by specifying the class name. Easy maintenanc

Mar 26, 2022

Data-tip.css - Wow, such tooltip, with pure css!

Notice: hint.css has been much better since I complained about it months ago, so try out its new features instead of this one! data-tip.css Wow, such

May 26, 2021

Tiny CSS framework with almost no classes and some pure CSS effects

Tiny CSS framework with almost no classes and some pure CSS effects

no.css INTERACTIVE DEMO I am tired of adding classes to style my HTML. I just want to include a .css file and I expect it to style the HTML for me. no

Dec 10, 2022

A simple build tool for Figma plugins based on webpack

Figpack EXPERIMENTAL / WORK IN PROGRESS A simple build tool for Figma plugins based on webpack. It's optimized for plugins that could get complex, mea

Oct 9, 2022
Owner
Siddharth Kshetrapal
design engineering at @github; building @ui-devtools
Siddharth Kshetrapal
PostCSS plugin to render WordPress global styles from a theme.json file

postcss-wp-global-styles PostCSS plugin to render WordPress global styles from a theme.json file. As of now it only supports preset styles. Usage @wp-

Luehrsen // Heinrich 10 Aug 5, 2022
A Unique Food order landing page web application called 'HOMELY' where you can select available meals and add them to cart then order them.

End Result Click the link : https://foodapp-by-eniola.netlify.com Getting Started with Create React App This project was bootstrapped with Create Reac

Eniola Odunmbaku 26 Dec 31, 2022
CSSHell - Collection of common CSS mistakes, and how to fix them

CSS Hell - To Hell with bad CSS! Collection of common CSS mistakes, and how to f

Stefánia Péter 181 Nov 4, 2022
Reseter.css - A Futuristic CSS Reset / CSS Normalizer

Reseter.css A CSS Reset/Normalizer Reseter.css is an awesome CSS reset for a website. It is a great tool for any web designer. Reseter.css resets all

Krish Dev DB 1.1k Jan 2, 2023
A modern static resume template and theme. Powered by Jekyll and GitHub pages.

modern-resume-theme A modern simple static resume template and theme. Powered by Jekyll and GitHub pages. Host your own resume on GitHub for free! Vie

James Grant 1.5k Dec 31, 2022
📱 Free to use static generated website template for your mobile app

Mobile App Landing Page Template ?? Free to use static generated landing page template for your mobile app ?? Features Mobile App Landing Page Templat

Sandoche ADITTANE 467 Dec 30, 2022
⚙️ Static site boilerplate. Using Gulp, PugJS, and Sass.

?? Alaska | Static-site Boilerplate ⚡ Fastest way to build HTML and CSS static sites. You don't have to learn complicated tools to build simple websit

Alaska Labs 8 Sep 10, 2022
The fastest way to build beautiful Electron apps using simple HTML and CSS

Photon UI toolkit for building desktop apps with Electron. Getting started Clone the repo with git clone https://github.com/connors/photon.git Read th

Connor Sears 9.9k Dec 29, 2022
Spectre.css - A Lightweight, Responsive and Modern CSS Framework

Spectre.css Spectre.css is a lightweight, responsive and modern CSS framework. Lightweight (~10KB gzipped) starting point for your projects Flexbox-ba

Yan Zhu 11.1k Jan 8, 2023
Low-level CSS Toolkit – the original Functional/Utility/Atomic CSS library

Basscss Low-level CSS toolkit – the original Functional CSS library https://basscss.com Lightning-Fast Modular CSS with No Side Effects Basscss is a l

Basscss 5.8k Dec 31, 2022