Style your React components with simplicity using css inside your comments.

Overview

babel-plugin-css-in-comments

Style your React components with simplicity using css inside your comments. Styles get transformed into css modules and are automatically imported. The className attribute is also transformed to add some utilities and prevent intensive use of styles. .

Installation

If you don't have a React app nearby with easy access to the babel configuration, consider using the quick start guide.

Install the plugin:

npm i -D babel-plugin-css-in-comments

And add it to the .babelrc file:

{
  "plugins": ["babel-plugin-css-in-comments"]
}

How to use

Comments

Comments can be used at the global scope of a file. Start your comments with css for scoped styles and css global for global styles. It's best to place scoped styles under their component and global styles at the end of the file.

Example:
const Logo = () => (
  <a href='/' className='logo .active'>
    Logo
  </a>
);
/*css
.logo {
  font-size: 2rem;
  font-weight: bold;
  font-family: 'Montserrat', sans-serif;
}
*/

export default Logo;
/*css global
a {
  text-decoration: none;
  color: inherit;
}
.active {
  opacity: 1;
}
*/

className attribute

Use scoped and global class names within the className attribute, along with conditionals to reduce the need to combine class names with " ".

Example:
const Logo = ({ active }) => (
  <a href='/' className='logo .dark active?'>
    Logo
  </a>
);

// transformed
const Logo = ({ active }) => (
  <a
    href='/'
    className={styles.logo + ' dark' + (active ? ' ' + styles.active : '')}
  >
    Logo
  </a>
);

What happened here?

  • logo get transformed into styles.logo because it's a scoped class name.
  • .dark is a global class name because it starts with ., the dot is removed and the class name isn't transformed.
  • active? is a conditional which depend on the active prop of the component.

You can also use template literals, the quasis aren't transformed but the string literals inside the expressions are.

Example:
const Logo = ({ active }) => (
  <a href='/' className={`dark ${'logo active?'}`}>
    Logo
  </a>
);

// transformed
const Logo = ({ active }) => (
  <a
    href='/'
    className={`dark ${styles.logo + (active ? ' ' + styles.active : '')}`}
  >
    Logo
  </a>
);

Conditionals

Simple conditionals can be used to write less code.

Example:

With test only:

<input className='hidden?' />
<input className='.hidden?' />

// transformed
<input className={hidden ? styles.hidden : ''} />
<input className={hidden ? 'hidden' : ''} />

With consequent:

<input className='active?dark' />
<input className='active?.dark' />

// transformed
<input className={active ? styles.dark : ''} />
<input className={active ? 'dark' : ''} />

With consequent and alternate:

<input className='active?primary:secondary' />
<input className='active?.active:.hidden' />

// transformed
<input className={active ? styles.primary : styles.secondary} />
<input className={active ? 'active' : 'hidden'} />

Logical expressions

Logical expressions can also be used. They are converted into conditionals.

Example:
<input className='dark|light' />
<input className='.active|.hidden' />

// transformed
<input className={dark ? styles.dark : styles.light} />
<input className={active ? 'active' : 'hidden' />

Quick Start

This example use create-next-app since the babel configuration is easier to access.

git clone https://github.com/Charly1620/css-in-comments-example.git
cd ./css-in-comments-example
npm install
npm run dev

As you can see, it looks just like the default template but with styles contained into their own components and less styles. .

If you want to use sass, you can install it and set the cssExt option to scss or sass.

Play around and see if this fit your needs. Also check out the how to use section to learn more.

Options

cssDir string
  • Directory where styles are created.
  • Default: ./styles
cssExt string
  • Extension of css files.
  • Default: css
useCSS boolean
  • Create css files.
  • Default: true
useImportDeclaration boolean
  • Add import declaration at top of files that use css comments.
  • Default: true
transpileWithoutCSS boolean
  • Transpile className attribute even if no css comments in the file.
  • Default: false

License

Licensed under the MIT License, Copyright Β© 2021-present Charly Albin.

See LICENSE.md for more information.

You might also like...
Tailwind CSS variants for email client CSS targeting.

tailwindcss-email-variants A plugin that provides variants for email client targeting hacks used in HTML emails. All variants are based on targeting h

A tool for creating ratio-based style systems built in React.
A tool for creating ratio-based style systems built in React.

Rational Design is a tool for generating type-scales for greater consistency in design systems or web design projects. It’s built with React Hooks, SCSS, Webpack, and npm scripts, and is fully responsive.

Autoprefixer for JavaScript style objects

inline-style-prefixer A small, simple and fast vendor prefixer from JavaScript style object. Support Us Support Robin Frischmann's work on Fela and it

Dynamic, theme-driven, style props for vanilla-extract.

Rainbow Sprinkles 🧁 Dynamic, theme-driven, style props for vanilla-extract Rainbow sprinkles works similarly to @vanilla-extract/sprinkles. Like spri

Encapsulated styling for your javascript components with all the power of javascript and CSS combined.

Stilr Encapsulated styling for your javascript components with all the power of javascript and CSS combined. Unique class names (Content Hash Based) U

Vite plugin that emulates Scoped CSS for React components

vite-react-css Vite plugin that emulates Scoped CSS for React components (using generated class names) Compatible with SSR Automatic code-splitting Ho

Incredible fast template toolkit for making new or re-styling existing components with Tailwind CSS.

Stail Incredible fast template toolkit for making new or re-styling existing components with Tailwind CSS. Why it's needed? First of all, I'm tired fr

Material-tailwind - An easy-to-use components library for Tailwind CSS and Material Design
Material-tailwind - An easy-to-use components library for Tailwind CSS and Material Design

Material-tailwind - An easy-to-use components library for Tailwind CSS and Material Design

🏠 Airbnb website using React, Next.js and Tailwind CSS
🏠 Airbnb website using React, Next.js and Tailwind CSS

This example shows how to use Tailwind CSS (v2.2) with Next.js. It follows the steps outlined in the official Tailwind docs.

Owner
Charly Albin
Charly Albin
Seamless mapping of class names to CSS modules inside of React components.

React CSS Modules React CSS Modules implement automatic mapping of CSS modules. Every CSS class is assigned a local-scoped identifier with a global un

Gajus Kuizinas 5.3k Dec 28, 2022
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress πŸ’…

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress ?? Looking for v5? The master branch is un

styled-components 38k Dec 27, 2022
Court.css - A naive CSS Utility framework based on JSX and css custom properties

court.css ?? A (work in progress) JSX-based, CSS utility framework with a predic

Tone Row 7 Aug 29, 2022
πŸ‘©β€πŸŽ€ CSS-in-JS library designed for high performance style composition

emotion The Next Generation of CSS-in-JS Emotion 11 has been released ?? See the blog post Emotion is a performant and flexible CSS-in-JS library. Bui

emotion 15.9k Jan 5, 2023
🎨 Aesthetic is an end-to-end multi-platform styling framework that offers a strict design system, robust atomic CSS-in-JS engine, a structural style sheet specification (SSS), a low-runtime solution, and much more!

Aesthetic Start using Aesthetic now! aestheticsuite.dev Aesthetic is an end-to-end, multi-platform styling and design suite, that offers the following

Aesthetic 198 Nov 11, 2022
CSS-in-JS solution with style-to-state bindings, SSR, and a next-level developer experience.

A set of modules is for CSS-in-JS solution that includes state-to-style bindings, SRR and next-level developer experience. It includes a framework-agnostic implementation

Numl.Design 20 Nov 24, 2022
CSS style loader for Webpack that is optimized for isomorphic (universal) web apps.

Isomorphic CSS style loader for Webpack CSS style loader for Webpack that works similarly to style-loader, but is optimized for critical path CSS rend

Kriasoft 1.3k Dec 19, 2022
A terminal style/styled portfolio website made with <3 using react.

A Terminal Styled Portfolio Website. ??‍??, a terminal style/styled portfolio website made with <3 using react.

ashish 67 Nov 22, 2022
Create tailwind css react components like styled components with classes name on multiple lines

Create Tailwind CSS React components like styled components with class names on multiple lines and conditional class rendering

Mathias Gilson 610 Dec 28, 2022
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

Khan Academy 5.3k Jan 1, 2023