# molcss

> A simple, lightweight, and powerful CSS-in-JS library.

Latest version **0.2.5** (published 2024-11-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install molcss
pnpm add molcss
yarn add molcss
bun add molcss
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.5 |
| Published | 2024-11-23 |
| First published | 2023-08-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 6 |
| Unpacked size | 127.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | iMasanari |
| Maintainers | imasanari |

## Links

- npm: https://www.npmjs.com/package/molcss
- npm.io page: https://npm.io/package/molcss

## Dependencies (6)

- [stylis](https://npm.io/package/stylis.md) ^4.3.2
- [postcss](https://npm.io/package/postcss.md) ^8.4.38
- [fast-glob](https://npm.io/package/fast-glob.md) ^3.3.2
- [@babel/core](https://npm.io/package/@babel/core.md) ^7.24.7
- [@rollup/pluginutils](https://npm.io/package/@rollup/pluginutils.md) ^5.1.0
- [css-selector-parser](https://npm.io/package/css-selector-parser.md) ^3.0.5

## Recent versions

- 0.2.5 (latest) — 2024-11-23
- 0.2.4 — 2024-11-23
- 0.2.3 — 2024-10-24
- 0.2.2 — 2024-07-07
- 0.2.1 — 2024-06-29
- 0.2.0 — 2024-06-29
- 0.1.1 — 2024-04-21
- 0.1.0 — 2024-01-10
- 0.0.8 — 2024-01-02
- 0.0.7 — 2024-01-01
- 0.0.6 — 2023-09-04
- 0.0.5 — 2023-08-24
- 0.0.4 — 2023-08-21
- 0.0.3 — 2023-08-20
- 0.0.2 — 2023-08-04
- … 1 more at https://npm.io/package/molcss/versions

## README

# Molcss

A simple, lightweight, and powerful CSS-in-JS library.

## Usage

```jsx
import 'molcss/style.css'
import { css } from 'molcss'

const className = css`
  color: red;
  width: 500px;
  border: 1px solid black;
  &:hover {
    color: blue;
  }
`

export default () => <div className={className} />
```

## Features

### Atomic CSS-in-JS

The described style definitions are atomically decomposed and reused at build time.

#### Before build

```js
import { css, mergeStyle } from 'molcss'

const a = css`
  color: black;
  padding: 1px;
  margin-top: 4px;
`

const b = css`
  color: black;
  padding: 2px;
  margin: 3px;
`

const c1 = mergeStyle(a, b)
const c2 = mergeStyle(b, a)
```

#### After build

```js
import { mergeStyle } from 'molcss'

const a = 'c0 a0 l0'
const b = 'c0 a1 d0'
const c1 = mergeStyle(a, b) // 'c0 a1 d0'
const c2 = mergeStyle(b, a) // 'c0 a0 d0 l0'
```

```css
/* generated css */
.c0 { color: black; }
.a0 { padding: 1px; }
.a1 { padding: 2px; }
.d0 { margin: 3px; }
.l0 { margin-top: 4px; }
```

### Runtime Style

Supports runtime styles. However, unlike static styles, the css return value is a dedicated object.
For vanilla JS, use generateRuntime. For React, you can use CSS props to support SSR.

```jsx
/** @jsxImportSource molcss/react */
import { css, generateRuntime } from 'molcss'

const staticStyle = css`
  color: blue;
`

const dynamicStyle = (color) => css`
  color: ${color};
`

console.log(typeof staticStyle)         // string
console.log(typeof dynamicStyle('red')) // object

const ForVanillaJS = () => `
  <div class="${staticStyle}"></div>
  <div class="${generateRuntime(dynamicStyle('red'))}"></div>
`

const ForReact = () =>
  <>
    <div className={staticStyle} />
    <div css={dynamicStyle('red')} />
  </>
```

#### Zero-Runtime Runtime Style (experimental)

```jsx
import { css, toInlineProps } from 'molcss'

const dynamicStyle = (color) => css`
  color: ${color};
`

const props = toInlineProps({ css: dynamicStyle('red') })

const ZeroRuntimeForVanillaJS = () =>
  `<div
    class="${props.className}"
    style="${Object.entries(props.style).map(([key, value]) => `${key}:${value};`).join('')}"
  ></div>`

const ZeroRuntimeForReact = () =>
  <>
    <div className={props.className} style={props.style} />
    {/* or */}
    <div {...toInlineProps({ css: dynamicStyle('green') })} />
  </>
```

## Setup

Plugins are available for Vite, Next.js, and Webpack.  
See [examples](https://github.com/iMasanari/molcss/tree/main/examples).

### Vite + React

```js
// vite.config.mjs
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import molcss from 'molcss/vite-plugin'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react({
      jsxImportSource: 'molcss/react',
    }),
    molcss({
      content: 'src/**/*.{js,jsx,ts,tsx}',
    })
  ],
})
```

```js
// tsconfig.json (When using TypeScript)
{
  "compilerOptions": {
    "jsxImportSource": "molcss/react",
  }
}
```

```js
// entry point
import 'molcss/style.css'
```

### Next.js

```js
// next.config.mjs
import molcss from 'molcss/nextjs-plugin'

const withMolcss = molcss({
  content: [
    'src/**/*.{js,jsx,ts,tsx}',
    'app/**/*.{js,jsx,ts,tsx}',
    'pages/**/*.{js,jsx,ts,tsx}',
  ],
})

export default withMolcss({
  // ...
})
```

```js
// package.json
{
  // Change the browserslist settings so that only Molcss converts Tagged templates.
  // - safari 12 -> 13
  // https://nextjs.org/docs/architecture/supported-browsers
  "browserslist": [
    "chrome 64",
    "edge 79",
    "firefox 67",
    "opera 51",
    "safari 13"
  ]
}
```

```js
// tsconfig.json (or jsconfig.json)
{
  "compilerOptions": {
    "jsxImportSource": "molcss/react",
  }
}
```

```js
// entry point
import 'molcss/style.css'
```

### Babel & PostCSS Plugin (with Vite + React)

```js
// molcss.context.cjs
const { createContext } = require('molcss/context')

module.exports = createContext()
```

```js
// vite.config.mjs
import { createRequire } from 'node:module'
import react from '@vitejs/plugin-react'

const require = createRequire(import.meta.url)
const molcssContext = require('./molcss.context.cjs')

export default defineConfig({
  // ...
  plugins: [
    react({
      jsxImportSource: 'molcss/react',
      babel: {
        plugins: [
          ['molcss/babel-plugin', {
            context: molcssContext,
          }]
        ]
      }
    })
  ]
})
```

```js
// postcss.config.cjs
const molcssContext = require('./molcss.context.cjs')

module.exports = {
  plugins: [
    require('molcss/postcss-plugin')({
      content: 'src/**/*.{js,jsx,ts,tsx}',
      context: molcssContext,
    }),
  ],
}
```

```js
// tsconfig.json (When using TypeScript)
{
  "compilerOptions": {
    "jsxImportSource": "molcss/react",
  }
}
```

```js
// entry point
import 'molcss/style.css'
```

### Use React 19 `style` (experimental)

Change `jsxImportSource` from `molcss/react` to `molcss/react-19`.

---
_Source: https://npm.io/package/molcss · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
